seletion_square.go 35 KB

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