seletion_square.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. package youngee_sectask_service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gogf/gf/database/gdb"
  7. "reflect"
  8. "youngmini_server/app/dao"
  9. "youngmini_server/app/model"
  10. "youngmini_server/app/model/youngee_talent_model"
  11. "youngmini_server/app/utils"
  12. "github.com/gogf/gf/encoding/gjson"
  13. "github.com/gogf/gf/frame/g"
  14. "github.com/gogf/gf/net/ghttp"
  15. "github.com/gogf/gf/os/gtime"
  16. )
  17. const (
  18. selectionStatusCreating = iota + 1
  19. selectionStatusReviewing
  20. selectionStatusReviewed
  21. selectionStatusPaying
  22. selectionStatusPaid
  23. selectionStatusInProgress
  24. selectionStatusInvalid
  25. selectionStatusClosed
  26. )
  27. // 获取项目信息列表service
  28. func GetSelectionList(r *ghttp.Request) *TalentHttpResult {
  29. pageIndex := r.GetQueryInt("idx", -1)
  30. cntPerPage := r.GetQueryInt("cnt", -1)
  31. platform := r.Get("platform")
  32. secForm := r.Get("secform")
  33. taskForm := r.Get("taskform")
  34. searchValue := r.Get("searchvalue")
  35. if pageIndex == -1 || cntPerPage == -1 || cntPerPage == 0 {
  36. return &TalentHttpResult{Code: -1, Msg: "参数错误"}
  37. }
  38. // 如果有领样形式的过滤条件,则将过滤条件保存于secFormList
  39. var secFormList []interface{}
  40. if secForm != nil {
  41. if reflect.TypeOf(secForm).Kind() != reflect.Slice {
  42. return &TalentHttpResult{Code: -2, Msg: "搜索条件领样形式错误"}
  43. }
  44. secFormList = make([]interface{}, 0)
  45. secFormList = secForm.([]interface{})
  46. }
  47. // 如果有任务形式的过滤条件,则将过滤条件保存于taskFormList
  48. var taskFormList []interface{}
  49. if taskForm != nil {
  50. if reflect.TypeOf(taskForm).Kind() != reflect.Slice {
  51. return &TalentHttpResult{Code: -2, Msg: "搜索条件任务形式错误"}
  52. }
  53. taskFormList = make([]interface{}, 0)
  54. taskFormList = taskForm.([]interface{})
  55. }
  56. // 如果有平台的过滤条件,则将平台列表保存于platformList
  57. var platformList []interface{}
  58. if platform != nil {
  59. if reflect.TypeOf(platform).Kind() != reflect.Slice {
  60. return &TalentHttpResult{Code: -2, Msg: "搜索条件平台类型错误"}
  61. }
  62. platformList = make([]interface{}, 0)
  63. platformList = platform.([]interface{})
  64. }
  65. // 构造查询的条件
  66. startId := pageIndex * cntPerPage
  67. whereStr := fmt.Sprintf("(selection_status >= %d)", selectionStatusInProgress)
  68. if platformList != nil {
  69. whereStr = whereStr + " and platform in ("
  70. for _, v := range platformList {
  71. whereStr += v.(string) + ", "
  72. }
  73. whereStr = whereStr[0 : len(whereStr)-2]
  74. whereStr += ")"
  75. }
  76. if taskFormList != nil {
  77. whereStr += " and task_mode in ("
  78. for _, v := range taskFormList {
  79. whereStr += v.(string) + ", "
  80. }
  81. whereStr = whereStr[0 : len(whereStr)-2]
  82. whereStr += ")"
  83. }
  84. if secFormList != nil {
  85. whereStr += " and sample_mode in ("
  86. for _, v := range secFormList {
  87. whereStr += v.(string) + ", "
  88. }
  89. whereStr = whereStr[0 : len(whereStr)-2]
  90. whereStr += ")"
  91. }
  92. if searchValue != nil {
  93. whereStr += " and selection_name like '%" + searchValue.(string) + "%'"
  94. }
  95. // 查询所有selection
  96. var selectionList = []model.YounggeeSelectionInfo{}
  97. err := g.Model(dao.YounggeeSelectionInfo.Table).Where(whereStr).Scan(&selectionList)
  98. if err != nil {
  99. return &TalentHttpResult{Code: -3, Msg: "查询数据库失败"}
  100. }
  101. fmt.Println("searchValue:", searchValue)
  102. fmt.Println("secFormList:", secFormList)
  103. fmt.Println("taskFormList:", taskFormList)
  104. fmt.Println("whereStr: ", whereStr)
  105. // 判断请求页面是否超过最大页面
  106. c, err := g.DB().Model(dao.YounggeeSelectionInfo.Table).Where(whereStr).Count()
  107. if err != nil {
  108. return &TalentHttpResult{Code: -4, Msg: err.Error(), Data: nil}
  109. }
  110. maxPage := c / cntPerPage
  111. if c%cntPerPage > 0 {
  112. maxPage += 1
  113. }
  114. if pageIndex+1 > maxPage {
  115. return &TalentHttpResult{Code: -5, Msg: "over max page"}
  116. }
  117. var selectionInfoList = youngee_talent_model.SelectionInfoList{
  118. Count: c,
  119. }
  120. err = g.DB().Model(dao.YounggeeSelectionInfo.Table).WithAll().Where(whereStr).
  121. Order("selection_status ASC,task_ddl DESC, selection_id").Limit(startId, cntPerPage).Scan(&selectionInfoList.SeletionInfos)
  122. if err != nil {
  123. return &TalentHttpResult{Code: -6, Msg: "查询数据库失败"}
  124. }
  125. selectionInfoList.MaxPage = maxPage
  126. return &TalentHttpResult{Code: 0, Msg: "success", Data: selectionInfoList}
  127. }
  128. // 获取单个选品详情service
  129. func GetSelectionDetail(r *ghttp.Request) *TalentHttpResult {
  130. sid := r.GetQueryString("selectionid", 0)
  131. if sid == "" {
  132. return &TalentHttpResult{Code: -2, Msg: "data query failed"}
  133. }
  134. var selectionDetail *youngee_talent_model.SelectionDetail
  135. err := g.DB().Model(youngee_talent_model.SelectionDetail{}).WithAll().Where("selection_id", sid).Scan(&selectionDetail)
  136. if err != nil {
  137. return &TalentHttpResult{Code: -3, Msg: err.Error()}
  138. }
  139. return &TalentHttpResult{Code: 0, Msg: "success", Data: selectionDetail}
  140. }
  141. // 判断是否已报名任务
  142. func IsSignUpSecTask(r *ghttp.Request) *TalentHttpResult {
  143. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  144. if err != nil {
  145. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  146. }
  147. selectionId := r.GetQueryInt("selection_id", -1)
  148. task := []model.YounggeeSecTaskInfo{}
  149. err = g.Model(dao.YounggeeSecTaskInfo.Table).Where("selection_id = ? and talent_id = ?", selectionId, tid).Scan(&task)
  150. if err != nil {
  151. return &TalentHttpResult{Code: -1, Msg: err.Error()}
  152. }
  153. isSign := youngee_talent_model.IsSignSecTask{}
  154. if len(task) != 0 {
  155. isSign.SecTaskInfo = &task[0]
  156. isSign.IsSign = 1
  157. } else {
  158. isSign.IsSign = 0
  159. }
  160. return &TalentHttpResult{Code: 0, Msg: "success", Data: isSign}
  161. }
  162. // 选品任务报名service
  163. func SignUpSecTask(r *ghttp.Request) *TalentHttpResult {
  164. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  165. if err != nil {
  166. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  167. }
  168. // 解析body/json参数
  169. var signSecTaskReq *youngee_talent_model.SignSecTaskReq
  170. err = r.ParseForm(&signSecTaskReq)
  171. if err != nil {
  172. return &TalentHttpResult{Code: -2, Msg: "parse param error"}
  173. }
  174. // 查询选品详情
  175. var selectionDetail *youngee_talent_model.SelectionDetail
  176. err = g.DB().Model(youngee_talent_model.SelectionDetail{}).WithAll().Where("selection_id", signSecTaskReq.SelectionId).Scan(&selectionDetail)
  177. if err != nil {
  178. return &TalentHttpResult{Code: -3, Msg: err.Error()}
  179. }
  180. var product model.YounggeeProduct
  181. err = json.Unmarshal([]byte(selectionDetail.ProductSnap), &product)
  182. if err != nil {
  183. return &TalentHttpResult{Code: -3, Msg: "json Unmarshal failed"}
  184. }
  185. // 查询达人详情
  186. var talentInfo *youngee_talent_model.TalentInfo
  187. err = g.DB().Model("youngee_talent_info").WithAll().Where("id", tid).Scan(&talentInfo)
  188. if err != nil {
  189. return &TalentHttpResult{Code: -4, Msg: "Get talent info failed"}
  190. }
  191. // 社媒账号详情
  192. var accountInfo *youngee_talent_model.PlatformAccountInfo
  193. err = g.DB().Model("youngee_platform_account_info").WithAll().Where("talent_id = ? and platform_id = ?", tid, selectionDetail.Platform).Scan(&accountInfo)
  194. if err != nil {
  195. return &TalentHttpResult{Code: -5, Msg: err.Error()}
  196. }
  197. // 收货地址详情
  198. address, err := g.DB().Model(dao.YoungeeTalentDeliveryAddress.Table).One("talent_id = ? and address_id = ?", tid, signSecTaskReq.AddressId)
  199. if err != nil {
  200. return &TalentHttpResult{Code: -6, Msg: err.Error()}
  201. }
  202. var newTaskId string
  203. // 首先生成任务id
  204. newTaskId = utils.GetUuid.GetTaskId(selectionDetail.SelectionId, selectionDetail.EnterpriseId, tid)
  205. // 生成达人平台账号信息快照
  206. accountSnap, err := gjson.Encode(accountInfo)
  207. if err != nil {
  208. return &TalentHttpResult{Code: -7, Msg: "encode platform snap failed"}
  209. }
  210. // 生成达人信息快照
  211. talentSnap, err := gjson.Encode(talentInfo)
  212. if err != nil {
  213. return &TalentHttpResult{Code: -8, Msg: "encode talent info snap failed"}
  214. }
  215. // 生成收货地址快照
  216. addrSnap, err := gjson.Encode(address)
  217. if err != nil {
  218. return &TalentHttpResult{Code: -9, Msg: "encode delivery address snap failed"}
  219. }
  220. // 新建选品任务
  221. secTaskInfo := model.YounggeeSecTaskInfo{
  222. TaskId: newTaskId,
  223. SelectionId: signSecTaskReq.SelectionId,
  224. TalentId: tid,
  225. AccountId: accountInfo.AccountId,
  226. TalentPlatformInfoSnap: string(accountSnap),
  227. TalentPersonalInfoSnap: string(talentSnap),
  228. TalentPostAddrSnap: string(addrSnap),
  229. TaskReward: selectionDetail.TaskReward,
  230. TalentPayment: product.ProductPrice,
  231. IsPayPayment: 0,
  232. IsPayReward: 0,
  233. TaskMode: selectionDetail.TaskMode,
  234. SampleMode: selectionDetail.SampleMode,
  235. TaskStage: 3,
  236. TaskStatus: 1,
  237. CreateDate: gtime.Now(),
  238. CompleteStatus: 1,
  239. LogisticsStatus: 1,
  240. AssignmentStatus: 1,
  241. UpdateAt: gtime.Now(),
  242. WithdrawStatus: 1,
  243. LeadTeamId: signSecTaskReq.LeadTeamId,
  244. TeamId: signSecTaskReq.TeamId,
  245. TeamIncome: 0,
  246. TeamPoint: 0,
  247. }
  248. err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  249. // young之团收益计算
  250. var rewardConfig *model.YounggeeTeamRewardConfig
  251. whereCondition := g.Map{
  252. dao.YounggeeTeamRewardConfig.Columns.ProjectType: 2,
  253. dao.YounggeeTeamRewardConfig.Columns.TaskForm: selectionDetail.TaskMode + 3,
  254. dao.YounggeeTeamRewardConfig.Columns.ContentForm: selectionDetail.ContentType,
  255. dao.YounggeeTeamRewardConfig.Columns.RewardReason: talentInfo.UserType,
  256. }
  257. err = tx.Model(dao.YounggeeTeamRewardConfig.Table).Where(whereCondition).Scan(&rewardConfig)
  258. if err != nil {
  259. fmt.Println(err)
  260. return err
  261. }
  262. if rewardConfig != nil {
  263. secTaskInfo.TeamPoint = rewardConfig.Point
  264. secTaskInfo.TeamIncome = rewardConfig.Money
  265. whereCondition = g.Map{
  266. dao.YounggeeTalentTeam.Columns.TeamId: signSecTaskReq.LeadTeamId,
  267. }
  268. whereCondition1 := g.Map{
  269. dao.YounggeeTalentTeam.Columns.TeamId: signSecTaskReq.TeamId,
  270. }
  271. updateData := g.Map{
  272. dao.YounggeeTalentTeam.Columns.PointIncome: gdb.Raw(fmt.Sprintf("%s + %d", dao.YounggeeTalentTeam.Columns.PointIncome, rewardConfig.Point)),
  273. dao.YounggeeTalentTeam.Columns.MoneyIncome: gdb.Raw(fmt.Sprintf("%s + %f", dao.YounggeeTalentTeam.Columns.MoneyIncome, float64(rewardConfig.Money)*secTaskInfo.TaskReward/100)),
  274. }
  275. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Data(updateData).Where(whereCondition).Update()
  276. if err != nil {
  277. return err
  278. }
  279. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Data(updateData).Where(whereCondition1).Update()
  280. if err != nil {
  281. return err
  282. }
  283. // young之团状态变更
  284. if signSecTaskReq.LeadTeamId != "" {
  285. // 更新young之团状态
  286. whereCondition2 := g.Map{
  287. dao.YounggeeTalentTeam.Columns.TeamStatus: 1,
  288. }
  289. updateData = g.Map{
  290. dao.YounggeeTalentTeam.Columns.TeamStatus: 2,
  291. }
  292. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Where(whereCondition).Where(whereCondition2).Data(updateData).Update()
  293. if err != nil {
  294. return err
  295. }
  296. }
  297. if signSecTaskReq.TeamId != "" {
  298. // 更新young之团状态
  299. whereCondition2 := g.Map{
  300. dao.YounggeeTalentTeam.Columns.TeamStatus: 1,
  301. }
  302. updateData = g.Map{
  303. dao.YounggeeTalentTeam.Columns.TeamStatus: 2,
  304. }
  305. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Where(whereCondition1).Where(whereCondition2).Data(updateData).Update()
  306. if err != nil {
  307. return err
  308. }
  309. }
  310. }
  311. if selectionDetail.SampleMode == 2 {
  312. // 减少选品库存
  313. whereCondition1 := g.Map{
  314. dao.YounggeeSelectionInfo.Columns.SelectionId: selectionDetail.SelectionId,
  315. }
  316. updateData := g.Map{
  317. dao.YounggeeSelectionInfo.Columns.RemainNum: gdb.Raw(fmt.Sprintf("%s - 1", dao.YounggeeSelectionInfo.Columns.RemainNum)),
  318. }
  319. _, err = tx.Ctx(ctx).Model(dao.YounggeeSelectionInfo.Table).Where(whereCondition1).Data(updateData).Update()
  320. if err != nil {
  321. return err
  322. }
  323. // 新建任务,初始化状态为待发货
  324. secTaskInfo.TaskStage = 6
  325. _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo).Insert()
  326. if err != nil {
  327. return err
  328. }
  329. } else {
  330. // 新建任务,初始化状态为待确认
  331. _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo).Insert()
  332. if err != nil {
  333. return err
  334. }
  335. }
  336. return nil
  337. })
  338. if err != nil {
  339. return &TalentHttpResult{Code: -18, Msg: "add Task data failed"}
  340. }
  341. signSecTaskResp := youngee_talent_model.SignSecTaskResp{
  342. TaskId: newTaskId,
  343. }
  344. return &TalentHttpResult{Code: 0, Msg: "success", Data: signSecTaskResp}
  345. }