seletion_square.go 35 KB

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