seletion_square.go 35 KB

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