seletion_square.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  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)", 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. //定义接口存该达人所有的报名任务
  206. //1.查task表全部数据
  207. var task []youngee_talent_model.SecTaskInfoDetail
  208. err = g.DB().Model(youngee_talent_model.SecTaskInfoDetail{}).WithAll().
  209. Where("talent_id = ? AND selection_id = ? ", tid, selectionId).
  210. Scan(&task)
  211. if err != nil {
  212. return &TalentHttpResult{Code: 0, Msg: err.Error(), Data: nil}
  213. }
  214. isSign := youngee_talent_model.IsSignSecTask{}
  215. //有数据且sample_mode==1说明报名了,否则没报名 &&左边为false就不执行了 free_stage!=0
  216. if len(task) != 0 && task[0].FreeStage != 0 {
  217. isSign.IsSign = 1
  218. isSign.SecTaskInfo = &task[0]
  219. } else {
  220. isSign.IsSign = 0
  221. }
  222. return &TalentHttpResult{Code: 0, Msg: "success", Data: isSign}
  223. }
  224. func IsCreateSecTask(r *ghttp.Request) *TalentHttpResult {
  225. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  226. if err != nil {
  227. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  228. }
  229. selectionId := r.GetQueryInt("selection_id", -1)
  230. //定义接口存该达人所有的报名任务
  231. //1.查task表全部数据
  232. var task []youngee_talent_model.SecTaskInfoDetail
  233. err = g.DB().Model(youngee_talent_model.SecTaskInfoDetail{}).WithAll().
  234. Where("talent_id = ? AND selection_id = ? ", tid, selectionId).
  235. Scan(&task)
  236. if err != nil {
  237. return &TalentHttpResult{Code: 0, Msg: err.Error(), Data: nil}
  238. }
  239. isSign := youngee_talent_model.IsSignSecTask{}
  240. //添加橱窗时,有task了就不再创建
  241. if len(task) != 0 {
  242. isSign.IsSign = 1
  243. isSign.SecTaskInfo = &task[0]
  244. } else {
  245. isSign.IsSign = 0
  246. }
  247. return &TalentHttpResult{Code: 0, Msg: "success", Data: isSign}
  248. }
  249. // 选品任务报名service
  250. func SignUpSecTask(r *ghttp.Request) *TalentHttpResult {
  251. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  252. if err != nil {
  253. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  254. }
  255. // 解析前端传来的参数
  256. var signSecTaskReq *youngee_talent_model.SignSecTaskReq
  257. err = r.ParseForm(&signSecTaskReq)
  258. if err != nil {
  259. return &TalentHttpResult{Code: -2, Msg: "parse param error"}
  260. }
  261. // 查得的selectionDetail包含product、photo、策略表等综合数据
  262. var selectionDetail *youngee_talent_model.SelectionDetail
  263. err = g.DB().Model(youngee_talent_model.SelectionDetail{}).WithAll().Where("selection_id", signSecTaskReq.SelectionId).Scan(&selectionDetail)
  264. if err != nil {
  265. return &TalentHttpResult{Code: -3, Msg: err.Error()}
  266. }
  267. //获取选品表中的ddl
  268. var selectionInfo *model.YounggeeSelectionInfo
  269. err = g.DB().Model("younggee_selection_info").Where("selection_id", signSecTaskReq.SelectionId).Scan(&selectionInfo)
  270. var product model.YounggeeProduct
  271. err = json.Unmarshal([]byte(selectionDetail.ProductSnap), &product)
  272. if err != nil {
  273. return &TalentHttpResult{Code: -3, Msg: "json Unmarshal failed"}
  274. }
  275. // 查询达人详情
  276. var talentInfo *youngee_talent_model.TalentInfo
  277. err = g.DB().Model("youngee_talent_info").WithAll().Where("id", tid).Scan(&talentInfo)
  278. if err != nil {
  279. return &TalentHttpResult{Code: -4, Msg: "Get talent info failed"}
  280. }
  281. // 社媒账号详情
  282. var accountInfo *youngee_talent_model.PlatformAccountInfo
  283. err = g.DB().Model("youngee_platform_account_info").WithAll().Where("talent_id = ? and platform_id = ?", tid, selectionDetail.Platform).Scan(&accountInfo)
  284. if err != nil {
  285. return &TalentHttpResult{Code: -5, Msg: err.Error()}
  286. }
  287. // 收货地址详情
  288. address, err := g.DB().Model(dao.YoungeeTalentDeliveryAddress.Table).One("talent_id = ? and address_id = ?", tid, signSecTaskReq.AddressId)
  289. if err != nil {
  290. return &TalentHttpResult{Code: -6, Msg: err.Error()}
  291. }
  292. var newTaskId string
  293. // 首先生成任务id
  294. newTaskId = utils.GetUuid.GetTaskId(selectionDetail.SelectionId, selectionDetail.EnterpriseId, tid)
  295. // 生成达人平台账号信息快照
  296. accountSnap, err := gjson.Encode(accountInfo)
  297. if err != nil {
  298. return &TalentHttpResult{Code: -7, Msg: "encode platform snap failed"}
  299. }
  300. // 生成达人信息快照
  301. talentSnap, err := gjson.Encode(talentInfo)
  302. if err != nil {
  303. return &TalentHttpResult{Code: -8, Msg: "encode talent info snap failed"}
  304. }
  305. // 生成收货地址快照
  306. addrSnap, err := gjson.Encode(address)
  307. if err != nil {
  308. return &TalentHttpResult{Code: -9, Msg: "encode delivery address snap failed"}
  309. }
  310. secTaskInfo := youngee_talent_model.SecTaskInfoDetail{
  311. TaskId: newTaskId,
  312. SelectionId: signSecTaskReq.SelectionId,
  313. ProductId: signSecTaskReq.ProductId,
  314. SaleNum: signSecTaskReq.SaleNum,
  315. FansNum: signSecTaskReq.FansNum,
  316. TalentId: tid,
  317. AccountId: accountInfo.AccountId,
  318. TalentPlatformInfoSnap: string(accountSnap),
  319. TalentPersonalInfoSnap: string(talentSnap),
  320. TalentPostAddrSnap: string(addrSnap),
  321. TaskReward: selectionDetail.TaskReward,
  322. TalentPayment: product.ProductPrice,
  323. IsPayPayment: 0,
  324. IsPayReward: 0,
  325. TaskMode: selectionDetail.TaskMode,
  326. SampleMode: selectionDetail.SampleMode,
  327. FreeStage: 1, //领样状态:已申请 悬赏状态默认为0
  328. PlatformId: 4, //快手平台
  329. TaskStage: 3,
  330. TaskStatus: 1,
  331. CreateDate: gtime.Now(),
  332. CompleteStatus: 1,
  333. LogisticsStatus: 1,
  334. AssignmentStatus: 1,
  335. UpdateAt: gtime.Now(),
  336. WithdrawStatus: 1,
  337. LeadTeamId: signSecTaskReq.LeadTeamId,
  338. TeamId: signSecTaskReq.TeamId,
  339. TeamIncome: 0,
  340. TeamPoint: 0,
  341. TaskDdl: selectionInfo.TaskDdl,
  342. }
  343. //删除添加橱窗时创建的不完整的数据
  344. _, err = g.DB().Model("younggee_sec_task_info").
  345. Where("talent_id = ? AND selection_id = ? ", tid, signSecTaskReq.SelectionId).
  346. Delete()
  347. if err != nil {
  348. return &TalentHttpResult{Code: -17, Msg: "younggee_sec_task_info delete failed"}
  349. }
  350. err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  351. // young之团收益计算
  352. var rewardConfig *model.YounggeeTeamRewardConfig
  353. whereCondition := g.Map{
  354. dao.YounggeeTeamRewardConfig.Columns.ProjectType: 2,
  355. dao.YounggeeTeamRewardConfig.Columns.TaskForm: selectionDetail.TaskMode + 3,
  356. dao.YounggeeTeamRewardConfig.Columns.ContentForm: selectionDetail.ContentType,
  357. dao.YounggeeTeamRewardConfig.Columns.RewardReason: talentInfo.UserType,
  358. }
  359. err = tx.Model(dao.YounggeeTeamRewardConfig.Table).Where(whereCondition).Scan(&rewardConfig)
  360. if err != nil {
  361. fmt.Println(err)
  362. return err
  363. }
  364. if rewardConfig != nil {
  365. secTaskInfo.TeamPoint = rewardConfig.Point
  366. secTaskInfo.TeamIncome = rewardConfig.Money
  367. whereCondition = g.Map{
  368. dao.YounggeeTalentTeam.Columns.TeamId: signSecTaskReq.LeadTeamId,
  369. }
  370. whereCondition1 := g.Map{
  371. dao.YounggeeTalentTeam.Columns.TeamId: signSecTaskReq.TeamId,
  372. }
  373. updateData := g.Map{
  374. dao.YounggeeTalentTeam.Columns.PointIncome: gdb.Raw(fmt.Sprintf("%s + %d", dao.YounggeeTalentTeam.Columns.PointIncome, rewardConfig.Point)),
  375. dao.YounggeeTalentTeam.Columns.MoneyIncome: gdb.Raw(fmt.Sprintf("%s + %f", dao.YounggeeTalentTeam.Columns.MoneyIncome, float64(rewardConfig.Money)*secTaskInfo.TaskReward/100)),
  376. }
  377. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Data(updateData).Where(whereCondition).Update()
  378. if err != nil {
  379. return err
  380. }
  381. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Data(updateData).Where(whereCondition1).Update()
  382. if err != nil {
  383. return err
  384. }
  385. // young之团状态变更
  386. if signSecTaskReq.LeadTeamId != "" {
  387. // 更新young之团状态
  388. whereCondition2 := g.Map{
  389. dao.YounggeeTalentTeam.Columns.TeamStatus: 1,
  390. }
  391. updateData = g.Map{
  392. dao.YounggeeTalentTeam.Columns.TeamStatus: 2,
  393. }
  394. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Where(whereCondition).Where(whereCondition2).Data(updateData).Update()
  395. if err != nil {
  396. return err
  397. }
  398. }
  399. if signSecTaskReq.TeamId != "" {
  400. // 更新young之团状态
  401. whereCondition2 := g.Map{
  402. dao.YounggeeTalentTeam.Columns.TeamStatus: 1,
  403. }
  404. updateData = g.Map{
  405. dao.YounggeeTalentTeam.Columns.TeamStatus: 2,
  406. }
  407. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Where(whereCondition1).Where(whereCondition2).Data(updateData).Update()
  408. if err != nil {
  409. return err
  410. }
  411. }
  412. }
  413. if selectionDetail.SampleMode == 2 {
  414. // 减少选品库存
  415. whereCondition1 := g.Map{
  416. dao.YounggeeSelectionInfo.Columns.SelectionId: selectionDetail.SelectionId,
  417. }
  418. updateData := g.Map{
  419. dao.YounggeeSelectionInfo.Columns.RemainNum: gdb.Raw(fmt.Sprintf("%s - 1", dao.YounggeeSelectionInfo.Columns.RemainNum)),
  420. }
  421. _, err = tx.Ctx(ctx).Model(dao.YounggeeSelectionInfo.Table).Where(whereCondition1).Data(updateData).Update()
  422. if err != nil {
  423. return err
  424. }
  425. // 新建任务,初始化状态为待发货
  426. secTaskInfo.TaskStage = 6
  427. _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo).Insert()
  428. if err != nil {
  429. return err
  430. }
  431. } else {
  432. // 新建任务,初始化状态为待确认
  433. _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo).Insert()
  434. if err != nil {
  435. return err
  436. }
  437. }
  438. return nil
  439. })
  440. if err != nil {
  441. return &TalentHttpResult{Code: -18, Msg: "add Task data failed"}
  442. }
  443. signSecTaskResp := youngee_talent_model.SignSecTaskResp{
  444. TaskId: newTaskId,
  445. }
  446. return &TalentHttpResult{Code: 0, Msg: "success", Data: signSecTaskResp}
  447. //return &TalentHttpResult{Code: 0, Msg: "success", Data: selectionDetail}
  448. }
  449. func SignUpSecTaskFromWindow(r *ghttp.Request) *TalentHttpResult {
  450. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  451. if err != nil {
  452. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  453. }
  454. // 解析body/json参数
  455. var signSecTaskReq *youngee_talent_model.SignSecTaskReq
  456. err = r.ParseForm(&signSecTaskReq)
  457. if err != nil {
  458. return &TalentHttpResult{Code: -2, Msg: "parse param error"}
  459. }
  460. // 查询选品详情
  461. var selectionDetail *youngee_talent_model.SelectionDetail
  462. err = g.DB().Model(youngee_talent_model.SelectionDetail{}).WithAll().Where("selection_id", signSecTaskReq.SelectionId).Scan(&selectionDetail)
  463. if err != nil {
  464. return &TalentHttpResult{Code: -3, Msg: err.Error()}
  465. }
  466. //获取选品表中的ddl
  467. var selectionInfo *model.YounggeeSelectionInfo
  468. err = g.DB().Model("younggee_selection_info").Where("selection_id", signSecTaskReq.SelectionId).Scan(&selectionInfo)
  469. var product model.YounggeeProduct
  470. err = json.Unmarshal([]byte(selectionDetail.ProductSnap), &product)
  471. if err != nil {
  472. return &TalentHttpResult{Code: -3, Msg: "json Unmarshal failed"}
  473. }
  474. // 查询达人详情
  475. var talentInfo *youngee_talent_model.TalentInfo
  476. err = g.DB().Model("youngee_talent_info").WithAll().Where("id", tid).Scan(&talentInfo)
  477. if err != nil {
  478. return &TalentHttpResult{Code: -4, Msg: "Get talent info failed"}
  479. }
  480. // 社媒账号详情
  481. var accountInfo *youngee_talent_model.PlatformAccountInfo
  482. err = g.DB().Model("youngee_platform_account_info").WithAll().Where("talent_id = ? and platform_id = ?", tid, selectionDetail.Platform).Scan(&accountInfo)
  483. if err != nil {
  484. return &TalentHttpResult{Code: -5, Msg: err.Error()}
  485. }
  486. var newTaskId string
  487. // 首先生成任务id
  488. newTaskId = utils.GetUuid.GetTaskId(selectionDetail.SelectionId, selectionDetail.EnterpriseId, tid)
  489. // 生成达人平台账号信息快照
  490. accountSnap, err := gjson.Encode(accountInfo)
  491. if err != nil {
  492. return &TalentHttpResult{Code: -7, Msg: "encode platform snap failed"}
  493. }
  494. // 生成达人信息快照
  495. talentSnap, err := gjson.Encode(talentInfo)
  496. if err != nil {
  497. return &TalentHttpResult{Code: -8, Msg: "encode talent info snap failed"}
  498. }
  499. secTaskInfo := youngee_talent_model.SecTaskInfoWindowDetail{
  500. TaskId: newTaskId,
  501. SelectionId: signSecTaskReq.SelectionId,
  502. ProductId: signSecTaskReq.ProductId,
  503. SaleNum: signSecTaskReq.SaleNum,
  504. FansNum: signSecTaskReq.FansNum,
  505. TalentId: tid,
  506. AccountId: accountInfo.AccountId,
  507. TalentPlatformInfoSnap: string(accountSnap),
  508. TalentPersonalInfoSnap: string(talentSnap),
  509. //TalentPostAddrSnap: string(addrSnap),
  510. TaskReward: selectionInfo.TaskReward,
  511. TalentPayment: product.ProductPrice,
  512. IsPayPayment: 0,
  513. IsPayReward: 0,
  514. TaskMode: selectionInfo.TaskMode,
  515. SampleMode: 3, //添加橱窗,当作不提供领样处理
  516. PlatformId: 4, //快手平台
  517. TaskStage: 3,
  518. TaskStatus: 1,
  519. CreateDate: gtime.Now(),
  520. CompleteStatus: 1,
  521. LogisticsStatus: 1,
  522. AssignmentStatus: 1,
  523. UpdateAt: gtime.Now(),
  524. WithdrawStatus: 1,
  525. LeadTeamId: signSecTaskReq.LeadTeamId,
  526. TeamId: signSecTaskReq.TeamId,
  527. TeamIncome: 0,
  528. TeamPoint: 0,
  529. TaskDdl: selectionInfo.TaskDdl,
  530. }
  531. //如果已经有数据了,删除。模拟在快手侧里把橱窗商品删了,想再加回来
  532. //1.查task表全部数据
  533. var secTaskInfoList []youngee_talent_model.SecTaskInfoDetail
  534. err = g.DB().Model(youngee_talent_model.SecTaskInfoDetail{}).WithAll().
  535. Where("talent_id = ? AND selection_id = ? ", tid, signSecTaskReq.SelectionId).
  536. Scan(&secTaskInfoList)
  537. if err != nil {
  538. return &TalentHttpResult{Code: 0, Msg: err.Error(), Data: nil}
  539. }
  540. //2.如果task不为空。取出free_stage的值。插入的值含有删除数据的free_stage
  541. if len(secTaskInfoList) != 0 {
  542. free_stage := secTaskInfoList[0].FreeStage
  543. //删除旧的,
  544. _, err = g.DB().Model("younggee_sec_task_info").
  545. Where("talent_id = ? AND selection_id = ? ", tid, signSecTaskReq.SelectionId).
  546. Delete()
  547. if err != nil {
  548. return &TalentHttpResult{Code: -17, Msg: "younggee_sec_task_info delete failed"}
  549. }
  550. //插入新的(含free_stage)
  551. secTaskInfo_new := youngee_talent_model.SecTaskInfoWindowDetail{
  552. TaskId: newTaskId,
  553. SelectionId: signSecTaskReq.SelectionId,
  554. ProductId: signSecTaskReq.ProductId,
  555. SaleNum: signSecTaskReq.SaleNum,
  556. FansNum: signSecTaskReq.FansNum,
  557. TalentId: tid,
  558. AccountId: accountInfo.AccountId,
  559. TalentPlatformInfoSnap: string(accountSnap),
  560. TalentPersonalInfoSnap: string(talentSnap),
  561. //TalentPostAddrSnap: string(addrSnap),
  562. TaskReward: selectionInfo.TaskReward,
  563. TalentPayment: product.ProductPrice,
  564. IsPayPayment: 0,
  565. IsPayReward: 0,
  566. TaskMode: selectionInfo.TaskMode,
  567. SampleMode: 3, //添加橱窗,当作不提供领样处理
  568. PlatformId: 4, //快手平台
  569. TaskStage: 3,
  570. TaskStatus: 1,
  571. CreateDate: gtime.Now(),
  572. CompleteStatus: 1,
  573. LogisticsStatus: 1,
  574. AssignmentStatus: 1,
  575. UpdateAt: gtime.Now(),
  576. WithdrawStatus: 1,
  577. LeadTeamId: signSecTaskReq.LeadTeamId,
  578. TeamId: signSecTaskReq.TeamId,
  579. TeamIncome: 0,
  580. TeamPoint: 0,
  581. TaskDdl: selectionInfo.TaskDdl,
  582. FreeStage: free_stage,
  583. }
  584. err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  585. // 新建任务,初始化状态为待确认
  586. _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo_new).Insert()
  587. if err != nil {
  588. return err
  589. }
  590. return nil
  591. })
  592. if err != nil {
  593. return &TalentHttpResult{Code: -18, Msg: "add Task data failed"}
  594. }
  595. signSecTaskResp := youngee_talent_model.SignSecTaskResp{
  596. TaskId: newTaskId,
  597. }
  598. return &TalentHttpResult{Code: 0, Msg: "success", Data: signSecTaskResp}
  599. } else {
  600. //3.如果已经报过名了需要传递给新的数据。删除添加橱窗时创建的不完整的数据
  601. _, err = g.DB().Model("younggee_sec_task_info").
  602. Where("talent_id = ? AND selection_id = ? ", tid, signSecTaskReq.SelectionId).
  603. Delete()
  604. if err != nil {
  605. return &TalentHttpResult{Code: -17, Msg: "younggee_sec_task_info delete failed"}
  606. }
  607. err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  608. // 新建任务,初始化状态为待确认
  609. _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo).Insert()
  610. if err != nil {
  611. return err
  612. }
  613. return nil
  614. })
  615. if err != nil {
  616. return &TalentHttpResult{Code: -18, Msg: "add Task data failed"}
  617. }
  618. signSecTaskResp := youngee_talent_model.SignSecTaskResp{
  619. TaskId: newTaskId,
  620. }
  621. return &TalentHttpResult{Code: 0, Msg: "success", Data: signSecTaskResp}
  622. }
  623. }