seletion_square.go 28 KB

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