seletion_square.go 27 KB

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