seletion_square.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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. categoryForm := r.Get("categoryform")
  35. searchValue := r.Get("searchvalue")
  36. if pageIndex == -1 || cntPerPage == -1 || cntPerPage == 0 {
  37. return &TalentHttpResult{Code: -1, Msg: "参数错误"}
  38. }
  39. // 如果有领样形式的过滤条件,则将过滤条件保存于secFormList
  40. var secFormList []interface{}
  41. if secForm != nil {
  42. if reflect.TypeOf(secForm).Kind() != reflect.Slice {
  43. return &TalentHttpResult{Code: -2, Msg: "搜索条件领样形式错误"}
  44. }
  45. secFormList = make([]interface{}, 0)
  46. secFormList = secForm.([]interface{})
  47. }
  48. // 如果有任务形式的过滤条件,则将过滤条件保存于taskFormList
  49. var taskFormList []interface{}
  50. if taskForm != nil {
  51. if reflect.TypeOf(taskForm).Kind() != reflect.Slice {
  52. return &TalentHttpResult{Code: -2, Msg: "搜索条件任务形式错误"}
  53. }
  54. taskFormList = make([]interface{}, 0)
  55. taskFormList = taskForm.([]interface{})
  56. }
  57. // 如果有商品类目的过滤条件,则将过滤条件保存于categoryFormList
  58. var categoryFormList []interface{}
  59. if categoryForm != nil {
  60. if reflect.TypeOf(categoryForm).Kind() != reflect.Slice {
  61. return &TalentHttpResult{Code: -2, Msg: "搜索条件任务形式错误"}
  62. }
  63. categoryFormList = make([]interface{}, 0)
  64. categoryFormList = categoryForm.([]interface{})
  65. }
  66. // 如果有平台的过滤条件,则将平台列表保存于platformList 弃用
  67. /* var platformList []interface{}
  68. if platform != nil {
  69. if reflect.TypeOf(platform).Kind() != reflect.Slice {
  70. return &TalentHttpResult{Code: -2, Msg: "搜索条件平台类型错误"}
  71. }
  72. platformList = make([]interface{}, 0)
  73. platformList = platform.([]interface{})
  74. }*/
  75. // 构造查询的条件
  76. startId := pageIndex * cntPerPage
  77. whereStr := fmt.Sprintf("(selection_status >= %d) AND (status = 0) ", selectionStatusInProgress)
  78. /* if platformList != nil {
  79. whereStr = whereStr + " and platform in ("
  80. for _, v := range platformList {
  81. whereStr += v.(string) + ", "
  82. }
  83. whereStr = whereStr[0 : len(whereStr)-2]
  84. whereStr += ")"
  85. }*/
  86. if taskFormList != nil {
  87. whereStr += " and task_mode in ("
  88. for _, v := range taskFormList {
  89. whereStr += v.(string) + ", "
  90. }
  91. whereStr = whereStr[0 : len(whereStr)-2]
  92. whereStr += ")"
  93. }
  94. if secFormList != nil {
  95. whereStr += " and sample_mode in ("
  96. for _, v := range secFormList {
  97. whereStr += v.(string) + ", "
  98. }
  99. whereStr = whereStr[0 : len(whereStr)-2]
  100. whereStr += ")"
  101. }
  102. if categoryFormList != nil {
  103. whereStr += " and product_category in ("
  104. for _, v := range categoryFormList {
  105. whereStr += v.(string) + ", "
  106. }
  107. whereStr = whereStr[0 : len(whereStr)-2]
  108. whereStr += ")"
  109. }
  110. //搜索栏
  111. if searchValue != nil {
  112. whereStr += " and selection_name like '%" + searchValue.(string) + "%'"
  113. }
  114. // 查询所有selection
  115. //YounggeeSelectionInfo含有表中的所有属性
  116. var selectionList = []model.YounggeeSelectionInfo{}
  117. //err := g.Model(dao.YounggeeSelectionInfo.Table).Where(whereStr).Scan(&selectionList)
  118. //展示带货商品的排序规则 预估赚、ddl未处理
  119. err := g.Model(dao.YounggeeSelectionInfo.Table).Where(whereStr).Order("commission_rate DESC , task_reward DESC ").Scan(&selectionList)
  120. if err != nil {
  121. return &TalentHttpResult{Code: -3, Msg: "查询数据库失败"}
  122. }
  123. fmt.Println("****searchValue:", searchValue)
  124. fmt.Println("****secFormList:", secFormList)
  125. fmt.Println("****taskFormList:", taskFormList)
  126. fmt.Println("****whereStr: ", whereStr)
  127. // 判断请求页面是否超过最大页面
  128. c, err := g.DB().Model(dao.YounggeeSelectionInfo.Table).Where(whereStr).Count()
  129. if err != nil {
  130. return &TalentHttpResult{Code: -4, Msg: err.Error(), Data: nil}
  131. }
  132. maxPage := c / cntPerPage
  133. if c%cntPerPage > 0 {
  134. maxPage += 1
  135. }
  136. if pageIndex+1 > maxPage {
  137. return &TalentHttpResult{Code: -5, Msg: "over max page"}
  138. }
  139. var selectionInfoList = youngee_talent_model.SelectionInfoList{
  140. Count: c,
  141. }
  142. err = g.DB().Model(dao.YounggeeSelectionInfo.Table).WithAll().Where(whereStr).
  143. Order("selection_status ASC , task_ddl DESC , commission_rate DESC , task_reward DESC, selection_id").Limit(startId, cntPerPage).Scan(&selectionInfoList.SelectionDetail)
  144. if err != nil {
  145. return &TalentHttpResult{Code: -6, Msg: "查询数据库失败"}
  146. }
  147. selectionInfoList.MaxPage = maxPage
  148. return &TalentHttpResult{Code: 0, Msg: "success", Data: selectionInfoList}
  149. }
  150. // 获取单个选品详情service
  151. func GetSelectionDetail(r *ghttp.Request) *TalentHttpResult {
  152. sid := r.GetQueryString("selectionid", 0)
  153. pid := r.GetQueryString("productid", 0)
  154. if sid == "" {
  155. return &TalentHttpResult{Code: -2, Msg: "data query failed"}
  156. }
  157. var selectionDetail *youngee_talent_model.SelectionDetail
  158. err := g.DB().Model(youngee_talent_model.SelectionDetail{}).WithAll().Where("selection_id", sid).Scan(&selectionDetail)
  159. if err != nil {
  160. return &TalentHttpResult{Code: -3, Msg: err.Error()}
  161. }
  162. // 查询younggee_product表数据
  163. var younggeeProduct []*youngee_talent_model.YounggeeProduct
  164. err = g.DB().Model(youngee_talent_model.YounggeeProduct{}).WithAll().Where("product_id", pid).Scan(&younggeeProduct)
  165. if err != nil {
  166. return &TalentHttpResult{Code: -3, Msg: err.Error()}
  167. }
  168. //查询younggee_product_photo表数据
  169. var younggeeProductPhoto []*youngee_talent_model.YounggeeProductPhoto
  170. err = g.DB().Model(youngee_talent_model.YounggeeProductPhoto{}).WithAll().Where("product_id", pid).Scan(&younggeeProductPhoto)
  171. if err != nil {
  172. return &TalentHttpResult{Code: -3, Msg: err.Error()}
  173. }
  174. //selectionDetail.FreeStrategy = freeStrategy
  175. //selectionDetail.RewardStrategy = rewardStrategy
  176. selectionDetail.YounggeeProduct = younggeeProduct
  177. selectionDetail.YounggeeProductPhoto = younggeeProductPhoto
  178. //selectionDetail.SelectionBrief = selectionBrief
  179. //selectionDetail.SelectionExample = selectionExample
  180. return &TalentHttpResult{Code: 0, Msg: "success", Data: selectionDetail}
  181. }
  182. func GetProductDetail(r *ghttp.Request) *TalentHttpResult {
  183. pid := r.GetQueryString("productid", 0)
  184. if pid == "" {
  185. return &TalentHttpResult{Code: -2, Msg: "data query failed"}
  186. }
  187. var productDetail *youngee_talent_model.YounggeeProduct
  188. err := g.DB().Model(youngee_talent_model.YounggeeProduct{}).WithAll().Where("product_id", pid).Scan(&productDetail)
  189. if err != nil {
  190. return &TalentHttpResult{Code: -3, Msg: err.Error()}
  191. }
  192. return &TalentHttpResult{Code: 0, Msg: "success", Data: productDetail}
  193. }
  194. func GetProductPhoto(r *ghttp.Request) *TalentHttpResult {
  195. //访问表,获取图片
  196. return nil
  197. }
  198. // 判断是否已报名任务,如果是提供免费领样(按钮既然有肯定是免费领样),sectask中的sample_mode==3,或者没有数据。表示还没报名,只是添加橱窗了
  199. func IsSignUpSecTask(r *ghttp.Request) *TalentHttpResult {
  200. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  201. if err != nil {
  202. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  203. }
  204. selectionId := r.GetQueryInt("selection_id", -1)
  205. openId := r.GetQueryString("open_id", "")
  206. //定义接口存该达人所有的报名任务
  207. //1.查task表全部数据
  208. var task []youngee_talent_model.SecTaskInfoDetail
  209. err = g.DB().Model(youngee_talent_model.SecTaskInfoDetail{}).WithAll().
  210. Where("talent_id = ? AND selection_id = ? AND open_id = ?", tid, selectionId, openId).
  211. Scan(&task)
  212. if err != nil {
  213. return &TalentHttpResult{Code: 0, Msg: err.Error(), Data: nil}
  214. }
  215. isSign := youngee_talent_model.IsSignSecTask{}
  216. //有数据且sample_mode==1说明报名了,否则没报名 &&左边为false就不执行了 free_stage!=0
  217. if len(task) != 0 && task[0].FreeStage != 0 {
  218. isSign.IsSign = 1
  219. isSign.SecTaskInfo = &task[0]
  220. } else {
  221. isSign.IsSign = 0
  222. }
  223. return &TalentHttpResult{Code: 0, Msg: "success", Data: isSign}
  224. }
  225. func IsCreateSecTask(r *ghttp.Request) *TalentHttpResult {
  226. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  227. if err != nil {
  228. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  229. }
  230. selectionId := r.GetQueryInt("selection_id", -1)
  231. //定义接口存该达人所有的报名任务
  232. //1.查task表全部数据
  233. var task []youngee_talent_model.SecTaskInfoDetail
  234. err = g.DB().Model(youngee_talent_model.SecTaskInfoDetail{}).WithAll().
  235. Where("talent_id = ? AND selection_id = ? ", tid, selectionId).
  236. Scan(&task)
  237. if err != nil {
  238. return &TalentHttpResult{Code: 0, Msg: err.Error(), Data: nil}
  239. }
  240. isSign := youngee_talent_model.IsSignSecTask{}
  241. //添加橱窗时,有task了就不再创建
  242. if len(task) != 0 {
  243. isSign.IsSign = 1
  244. isSign.SecTaskInfo = &task[0]
  245. } else {
  246. isSign.IsSign = 0
  247. }
  248. return &TalentHttpResult{Code: 0, Msg: "success", Data: isSign}
  249. }
  250. // 选品任务报名service
  251. func SignUpSecTask(r *ghttp.Request) *TalentHttpResult {
  252. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  253. if err != nil {
  254. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  255. }
  256. // 解析前端传来的参数
  257. var signSecTaskReq *youngee_talent_model.SignSecTaskReq
  258. err = r.ParseForm(&signSecTaskReq)
  259. if err != nil {
  260. return &TalentHttpResult{Code: -2, Msg: "parse param error"}
  261. }
  262. // 查得的selectionDetail包含product、photo、策略表等综合数据
  263. var selectionDetail *youngee_talent_model.SelectionDetail
  264. err = g.DB().Model(youngee_talent_model.SelectionDetail{}).WithAll().Where("selection_id", signSecTaskReq.SelectionId).Scan(&selectionDetail)
  265. if err != nil {
  266. return &TalentHttpResult{Code: -3, Msg: err.Error()}
  267. }
  268. //获取选品表中的ddl
  269. var selectionInfo *model.YounggeeSelectionInfo
  270. err = g.DB().Model("younggee_selection_info").Where("selection_id", signSecTaskReq.SelectionId).Scan(&selectionInfo)
  271. var product model.YounggeeProduct
  272. err = json.Unmarshal([]byte(selectionDetail.ProductSnap), &product)
  273. if err != nil {
  274. return &TalentHttpResult{Code: -3, Msg: "json Unmarshal failed"}
  275. }
  276. // 查询达人详情
  277. var talentInfo *youngee_talent_model.TalentInfo
  278. err = g.DB().Model("youngee_talent_info").WithAll().Where("id", tid).Scan(&talentInfo)
  279. if err != nil {
  280. return &TalentHttpResult{Code: -4, Msg: "Get talent info failed"}
  281. }
  282. // 社媒账号详情
  283. var accountInfo *youngee_talent_model.PlatformAccountInfo
  284. err = g.DB().Model("youngee_platform_account_info").WithAll().Where("talent_id = ? and platform_id = ?", tid, selectionDetail.Platform).Scan(&accountInfo)
  285. if err != nil {
  286. return &TalentHttpResult{Code: -5, Msg: err.Error()}
  287. }
  288. // 收货地址详情 生成的结果变成snap存在task表中,供快递100使用
  289. address, err := g.DB().Model(dao.YoungeeTalentDeliveryAddress.Table).One("talent_id = ? and address_id = ?", tid, signSecTaskReq.AddressId)
  290. if err != nil {
  291. return &TalentHttpResult{Code: -6, Msg: err.Error()}
  292. }
  293. var newTaskId string
  294. // 首先生成任务id
  295. newTaskId = utils.GetUuid.GetTaskId(selectionDetail.SelectionId, selectionDetail.EnterpriseId, tid)
  296. // 生成达人平台账号信息快照
  297. accountSnap, err := gjson.Encode(accountInfo)
  298. if err != nil {
  299. return &TalentHttpResult{Code: -7, Msg: "encode platform snap failed"}
  300. }
  301. // 生成达人信息快照
  302. talentSnap, err := gjson.Encode(talentInfo)
  303. if err != nil {
  304. return &TalentHttpResult{Code: -8, Msg: "encode talent info snap failed"}
  305. }
  306. // 生成收货地址快照
  307. addrSnap, err := gjson.Encode(address)
  308. if err != nil {
  309. return &TalentHttpResult{Code: -9, Msg: "encode delivery address snap failed"}
  310. }
  311. secTaskInfo := youngee_talent_model.SecTaskInfoDetail{
  312. TaskId: newTaskId,
  313. SelectionId: signSecTaskReq.SelectionId,
  314. ProductId: signSecTaskReq.ProductId,
  315. SaleNum: signSecTaskReq.SaleNum,
  316. FansNum: signSecTaskReq.FansNum,
  317. OpenId: signSecTaskReq.OpenId,
  318. TalentId: tid,
  319. AccountId: accountInfo.AccountId,
  320. TalentPlatformInfoSnap: string(accountSnap),
  321. TalentPersonalInfoSnap: string(talentSnap),
  322. TalentPostAddrSnap: string(addrSnap),
  323. TaskReward: selectionDetail.TaskReward,
  324. TalentPayment: product.ProductPrice,
  325. IsPayPayment: 0,
  326. IsPayReward: 0,
  327. TaskMode: selectionDetail.TaskMode,
  328. SampleMode: selectionDetail.SampleMode,
  329. FreeStage: 1, //领样状态:已申请 悬赏状态默认为0
  330. PlatformId: 4, //快手平台
  331. TaskStage: 3,
  332. TaskStatus: 1,
  333. CreateDate: gtime.Now(),
  334. CompleteStatus: 1,
  335. LogisticsStatus: 1,
  336. AssignmentStatus: 1,
  337. UpdateAt: gtime.Now(),
  338. WithdrawStatus: 1,
  339. LeadTeamId: signSecTaskReq.LeadTeamId,
  340. TeamId: signSecTaskReq.TeamId,
  341. TeamIncome: 0,
  342. TeamPoint: 0,
  343. TaskDdl: selectionInfo.TaskDdl,
  344. }
  345. //删除添加橱窗时创建的不完整的数据
  346. _, err = g.DB().Model("younggee_sec_task_info").
  347. Where("talent_id = ? AND selection_id = ? ", tid, signSecTaskReq.SelectionId).
  348. Delete()
  349. if err != nil {
  350. return &TalentHttpResult{Code: -17, Msg: "younggee_sec_task_info delete failed"}
  351. }
  352. err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  353. // young之团收益计算
  354. var rewardConfig *model.YounggeeTeamRewardConfig
  355. whereCondition := g.Map{
  356. dao.YounggeeTeamRewardConfig.Columns.ProjectType: 2,
  357. dao.YounggeeTeamRewardConfig.Columns.TaskForm: selectionDetail.TaskMode + 3,
  358. dao.YounggeeTeamRewardConfig.Columns.ContentForm: selectionDetail.ContentType,
  359. dao.YounggeeTeamRewardConfig.Columns.RewardReason: talentInfo.UserType,
  360. }
  361. err = tx.Model(dao.YounggeeTeamRewardConfig.Table).Where(whereCondition).Scan(&rewardConfig)
  362. if err != nil {
  363. fmt.Println(err)
  364. return err
  365. }
  366. if rewardConfig != nil {
  367. secTaskInfo.TeamPoint = rewardConfig.Point
  368. secTaskInfo.TeamIncome = rewardConfig.Money
  369. whereCondition = g.Map{
  370. dao.YounggeeTalentTeam.Columns.TeamId: signSecTaskReq.LeadTeamId,
  371. }
  372. whereCondition1 := g.Map{
  373. dao.YounggeeTalentTeam.Columns.TeamId: signSecTaskReq.TeamId,
  374. }
  375. updateData := g.Map{
  376. dao.YounggeeTalentTeam.Columns.PointIncome: gdb.Raw(fmt.Sprintf("%s + %d", dao.YounggeeTalentTeam.Columns.PointIncome, rewardConfig.Point)),
  377. dao.YounggeeTalentTeam.Columns.MoneyIncome: gdb.Raw(fmt.Sprintf("%s + %f", dao.YounggeeTalentTeam.Columns.MoneyIncome, float64(rewardConfig.Money)*secTaskInfo.TaskReward/100)),
  378. }
  379. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Data(updateData).Where(whereCondition).Update()
  380. if err != nil {
  381. return err
  382. }
  383. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Data(updateData).Where(whereCondition1).Update()
  384. if err != nil {
  385. return err
  386. }
  387. // young之团状态变更
  388. if signSecTaskReq.LeadTeamId != "" {
  389. // 更新young之团状态
  390. whereCondition2 := g.Map{
  391. dao.YounggeeTalentTeam.Columns.TeamStatus: 1,
  392. }
  393. updateData = g.Map{
  394. dao.YounggeeTalentTeam.Columns.TeamStatus: 2,
  395. }
  396. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Where(whereCondition).Where(whereCondition2).Data(updateData).Update()
  397. if err != nil {
  398. return err
  399. }
  400. }
  401. if signSecTaskReq.TeamId != "" {
  402. // 更新young之团状态
  403. whereCondition2 := g.Map{
  404. dao.YounggeeTalentTeam.Columns.TeamStatus: 1,
  405. }
  406. updateData = g.Map{
  407. dao.YounggeeTalentTeam.Columns.TeamStatus: 2,
  408. }
  409. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Where(whereCondition1).Where(whereCondition2).Data(updateData).Update()
  410. if err != nil {
  411. return err
  412. }
  413. }
  414. }
  415. if selectionDetail.SampleMode == 2 {
  416. // 减少选品库存
  417. whereCondition1 := g.Map{
  418. dao.YounggeeSelectionInfo.Columns.SelectionId: selectionDetail.SelectionId,
  419. }
  420. updateData := g.Map{
  421. dao.YounggeeSelectionInfo.Columns.RemainNum: gdb.Raw(fmt.Sprintf("%s - 1", dao.YounggeeSelectionInfo.Columns.RemainNum)),
  422. }
  423. _, err = tx.Ctx(ctx).Model(dao.YounggeeSelectionInfo.Table).Where(whereCondition1).Data(updateData).Update()
  424. if err != nil {
  425. return err
  426. }
  427. // 新建任务,初始化状态为待发货
  428. secTaskInfo.TaskStage = 6
  429. _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo).Insert()
  430. if err != nil {
  431. return err
  432. }
  433. } else {
  434. // 新建任务,初始化状态为待确认
  435. _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo).Insert()
  436. if err != nil {
  437. return err
  438. }
  439. }
  440. return nil
  441. })
  442. if err != nil {
  443. return &TalentHttpResult{Code: -18, Msg: "add Task data failed"}
  444. }
  445. signSecTaskResp := youngee_talent_model.SignSecTaskResp{
  446. TaskId: newTaskId,
  447. }
  448. return &TalentHttpResult{Code: 0, Msg: "success", Data: signSecTaskResp}
  449. //return &TalentHttpResult{Code: 0, Msg: "success", Data: selectionDetail}
  450. }
  451. func SignUpSecTaskFromWindow(r *ghttp.Request) *TalentHttpResult {
  452. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  453. if err != nil {
  454. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  455. }
  456. // 解析body/json参数
  457. var signSecTaskReq *youngee_talent_model.SignSecTaskReq
  458. err = r.ParseForm(&signSecTaskReq)
  459. if err != nil {
  460. return &TalentHttpResult{Code: -2, Msg: "parse param error"}
  461. }
  462. // 查询选品详情
  463. var selectionDetail *youngee_talent_model.SelectionDetail
  464. err = g.DB().Model(youngee_talent_model.SelectionDetail{}).WithAll().Where("selection_id", signSecTaskReq.SelectionId).Scan(&selectionDetail)
  465. if err != nil {
  466. return &TalentHttpResult{Code: -3, Msg: err.Error()}
  467. }
  468. //获取选品表中的ddl
  469. var selectionInfo *model.YounggeeSelectionInfo
  470. err = g.DB().Model("younggee_selection_info").Where("selection_id", signSecTaskReq.SelectionId).Scan(&selectionInfo)
  471. var product model.YounggeeProduct
  472. err = json.Unmarshal([]byte(selectionDetail.ProductSnap), &product)
  473. if err != nil {
  474. return &TalentHttpResult{Code: -3, Msg: "json Unmarshal failed"}
  475. }
  476. // 查询达人详情
  477. var talentInfo *youngee_talent_model.TalentInfo
  478. err = g.DB().Model("youngee_talent_info").WithAll().Where("id", tid).Scan(&talentInfo)
  479. if err != nil {
  480. return &TalentHttpResult{Code: -4, Msg: "Get talent info failed"}
  481. }
  482. // 社媒账号详情
  483. var accountInfo *youngee_talent_model.PlatformAccountInfo
  484. err = g.DB().Model("youngee_platform_account_info").WithAll().Where("talent_id = ? and platform_id = ?", tid, selectionDetail.Platform).Scan(&accountInfo)
  485. if err != nil {
  486. return &TalentHttpResult{Code: -5, Msg: err.Error()}
  487. }
  488. var newTaskId string
  489. // 首先生成任务id
  490. newTaskId = utils.GetUuid.GetTaskId(selectionDetail.SelectionId, selectionDetail.EnterpriseId, tid)
  491. // 生成达人平台账号信息快照
  492. accountSnap, err := gjson.Encode(accountInfo)
  493. if err != nil {
  494. return &TalentHttpResult{Code: -7, Msg: "encode platform snap failed"}
  495. }
  496. // 生成达人信息快照
  497. talentSnap, err := gjson.Encode(talentInfo)
  498. if err != nil {
  499. return &TalentHttpResult{Code: -8, Msg: "encode talent info snap failed"}
  500. }
  501. secTaskInfo := youngee_talent_model.SecTaskInfoWindowDetail{
  502. TaskId: newTaskId,
  503. SelectionId: signSecTaskReq.SelectionId,
  504. ProductId: signSecTaskReq.ProductId,
  505. SaleNum: signSecTaskReq.SaleNum,
  506. FansNum: signSecTaskReq.FansNum,
  507. TalentId: tid,
  508. AccountId: accountInfo.AccountId,
  509. TalentPlatformInfoSnap: string(accountSnap),
  510. TalentPersonalInfoSnap: string(talentSnap),
  511. //TalentPostAddrSnap: string(addrSnap),
  512. TaskReward: selectionInfo.TaskReward,
  513. TalentPayment: product.ProductPrice,
  514. IsPayPayment: 0,
  515. IsPayReward: 0,
  516. TaskMode: selectionInfo.TaskMode,
  517. SampleMode: 3, //添加橱窗,当作不提供领样处理
  518. PlatformId: 4, //快手平台
  519. TaskStage: 3,
  520. TaskStatus: 1,
  521. CreateDate: gtime.Now(),
  522. CompleteStatus: 1,
  523. LogisticsStatus: 1,
  524. AssignmentStatus: 1,
  525. UpdateAt: gtime.Now(),
  526. WithdrawStatus: 1,
  527. LeadTeamId: signSecTaskReq.LeadTeamId,
  528. TeamId: signSecTaskReq.TeamId,
  529. TeamIncome: 0,
  530. TeamPoint: 0,
  531. TaskDdl: selectionInfo.TaskDdl,
  532. }
  533. //如果已经有数据了,删除。模拟在快手侧里把橱窗商品删了,想再加回来
  534. //1.查task表全部数据
  535. var secTaskInfoList []youngee_talent_model.SecTaskInfoDetail
  536. err = g.DB().Model(youngee_talent_model.SecTaskInfoDetail{}).WithAll().
  537. Where("talent_id = ? AND selection_id = ? ", tid, signSecTaskReq.SelectionId).
  538. Scan(&secTaskInfoList)
  539. if err != nil {
  540. return &TalentHttpResult{Code: 0, Msg: err.Error(), Data: nil}
  541. }
  542. //2.如果task不为空。取出free_stage的值。插入的值含有删除数据的free_stage
  543. if len(secTaskInfoList) != 0 {
  544. free_stage := secTaskInfoList[0].FreeStage
  545. //删除旧的,
  546. _, err = g.DB().Model("younggee_sec_task_info").
  547. Where("talent_id = ? AND selection_id = ? ", tid, signSecTaskReq.SelectionId).
  548. Delete()
  549. if err != nil {
  550. return &TalentHttpResult{Code: -17, Msg: "younggee_sec_task_info delete failed"}
  551. }
  552. //插入新的(含free_stage)
  553. secTaskInfo_new := youngee_talent_model.SecTaskInfoWindowDetail{
  554. TaskId: newTaskId,
  555. SelectionId: signSecTaskReq.SelectionId,
  556. ProductId: signSecTaskReq.ProductId,
  557. SaleNum: signSecTaskReq.SaleNum,
  558. FansNum: signSecTaskReq.FansNum,
  559. TalentId: tid,
  560. AccountId: accountInfo.AccountId,
  561. TalentPlatformInfoSnap: string(accountSnap),
  562. TalentPersonalInfoSnap: string(talentSnap),
  563. //TalentPostAddrSnap: string(addrSnap),
  564. TaskReward: selectionInfo.TaskReward,
  565. TalentPayment: product.ProductPrice,
  566. IsPayPayment: 0,
  567. IsPayReward: 0,
  568. TaskMode: selectionInfo.TaskMode,
  569. SampleMode: 3, //添加橱窗,当作不提供领样处理
  570. PlatformId: 4, //快手平台
  571. TaskStage: 3,
  572. TaskStatus: 1,
  573. CreateDate: gtime.Now(),
  574. CompleteStatus: 1,
  575. LogisticsStatus: 1,
  576. AssignmentStatus: 1,
  577. UpdateAt: gtime.Now(),
  578. WithdrawStatus: 1,
  579. LeadTeamId: signSecTaskReq.LeadTeamId,
  580. TeamId: signSecTaskReq.TeamId,
  581. TeamIncome: 0,
  582. TeamPoint: 0,
  583. TaskDdl: selectionInfo.TaskDdl,
  584. FreeStage: free_stage,
  585. }
  586. err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  587. // 新建任务,初始化状态为待确认
  588. _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo_new).Insert()
  589. if err != nil {
  590. return err
  591. }
  592. return nil
  593. })
  594. if err != nil {
  595. return &TalentHttpResult{Code: -18, Msg: "add Task data failed"}
  596. }
  597. signSecTaskResp := youngee_talent_model.SignSecTaskResp{
  598. TaskId: newTaskId,
  599. }
  600. return &TalentHttpResult{Code: 0, Msg: "success", Data: signSecTaskResp}
  601. } else {
  602. //3.如果已经报过名了需要传递给新的数据。删除添加橱窗时创建的不完整的数据
  603. _, err = g.DB().Model("younggee_sec_task_info").
  604. Where("talent_id = ? AND selection_id = ? ", tid, signSecTaskReq.SelectionId).
  605. Delete()
  606. if err != nil {
  607. return &TalentHttpResult{Code: -17, Msg: "younggee_sec_task_info delete failed"}
  608. }
  609. err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  610. // 新建任务,初始化状态为待确认
  611. _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo).Insert()
  612. if err != nil {
  613. return err
  614. }
  615. return nil
  616. })
  617. if err != nil {
  618. return &TalentHttpResult{Code: -18, Msg: "add Task data failed"}
  619. }
  620. signSecTaskResp := youngee_talent_model.SignSecTaskResp{
  621. TaskId: newTaskId,
  622. }
  623. return &TalentHttpResult{Code: 0, Msg: "success", Data: signSecTaskResp}
  624. }
  625. }