seletion_square.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package youngee_sectask_service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gogf/gf/database/gdb"
  7. "reflect"
  8. "youngmini_server/app/dao"
  9. "youngmini_server/app/model"
  10. "youngmini_server/app/model/youngee_talent_model"
  11. "youngmini_server/app/utils"
  12. "github.com/gogf/gf/encoding/gjson"
  13. "github.com/gogf/gf/frame/g"
  14. "github.com/gogf/gf/net/ghttp"
  15. "github.com/gogf/gf/os/gtime"
  16. )
  17. const (
  18. selectionStatusCreating = iota + 1
  19. selectionStatusReviewing
  20. selectionStatusReviewed
  21. selectionStatusPaying
  22. selectionStatusPaid
  23. selectionStatusInProgress
  24. selectionStatusInvalid
  25. selectionStatusClosed
  26. )
  27. // 获取项目信息列表service
  28. func GetSelectionList(r *ghttp.Request) *TalentHttpResult {
  29. pageIndex := r.GetQueryInt("idx", -1)
  30. cntPerPage := r.GetQueryInt("cnt", -1)
  31. //platform := r.Get("platform")
  32. secForm := r.Get("secform")
  33. taskForm := r.Get("taskform")
  34. categoryForm := r.Get("categoryform")
  35. searchValue := r.Get("searchvalue")
  36. if pageIndex == -1 || cntPerPage == -1 || cntPerPage == 0 {
  37. return &TalentHttpResult{Code: -1, Msg: "参数错误"}
  38. }
  39. // 如果有领样形式的过滤条件,则将过滤条件保存于secFormList
  40. var secFormList []interface{}
  41. if secForm != nil {
  42. if reflect.TypeOf(secForm).Kind() != reflect.Slice {
  43. return &TalentHttpResult{Code: -2, Msg: "搜索条件领样形式错误"}
  44. }
  45. secFormList = make([]interface{}, 0)
  46. secFormList = secForm.([]interface{})
  47. }
  48. // 如果有任务形式的过滤条件,则将过滤条件保存于taskFormList
  49. var taskFormList []interface{}
  50. if taskForm != nil {
  51. if reflect.TypeOf(taskForm).Kind() != reflect.Slice {
  52. return &TalentHttpResult{Code: -2, Msg: "搜索条件任务形式错误"}
  53. }
  54. taskFormList = make([]interface{}, 0)
  55. taskFormList = taskForm.([]interface{})
  56. }
  57. // 如果有商品类目的过滤条件,则将过滤条件保存于categoryFormList
  58. var categoryFormList []interface{}
  59. if categoryForm != nil {
  60. if reflect.TypeOf(categoryForm).Kind() != reflect.Slice {
  61. return &TalentHttpResult{Code: -2, Msg: "搜索条件任务形式错误"}
  62. }
  63. categoryFormList = make([]interface{}, 0)
  64. categoryFormList = categoryForm.([]interface{})
  65. }
  66. // 如果有平台的过滤条件,则将平台列表保存于platformList 弃用
  67. /* var platformList []interface{}
  68. if platform != nil {
  69. if reflect.TypeOf(platform).Kind() != reflect.Slice {
  70. return &TalentHttpResult{Code: -2, Msg: "搜索条件平台类型错误"}
  71. }
  72. platformList = make([]interface{}, 0)
  73. platformList = platform.([]interface{})
  74. }*/
  75. // 构造查询的条件
  76. startId := pageIndex * cntPerPage
  77. whereStr := fmt.Sprintf("(selection_status >= %d)", selectionStatusInProgress)
  78. /* if platformList != nil {
  79. whereStr = whereStr + " and platform in ("
  80. for _, v := range platformList {
  81. whereStr += v.(string) + ", "
  82. }
  83. whereStr = whereStr[0 : len(whereStr)-2]
  84. whereStr += ")"
  85. }*/
  86. if taskFormList != nil {
  87. whereStr += " and task_mode in ("
  88. for _, v := range taskFormList {
  89. whereStr += v.(string) + ", "
  90. }
  91. whereStr = whereStr[0 : len(whereStr)-2]
  92. whereStr += ")"
  93. }
  94. if secFormList != nil {
  95. whereStr += " and sample_mode in ("
  96. for _, v := range secFormList {
  97. whereStr += v.(string) + ", "
  98. }
  99. whereStr = whereStr[0 : len(whereStr)-2]
  100. whereStr += ")"
  101. }
  102. if categoryFormList != nil {
  103. whereStr += " and product_category in ("
  104. for _, v := range categoryFormList {
  105. whereStr += v.(string) + ", "
  106. }
  107. whereStr = whereStr[0 : len(whereStr)-2]
  108. whereStr += ")"
  109. }
  110. //搜索栏
  111. if searchValue != nil {
  112. whereStr += " and selection_name like '%" + searchValue.(string) + "%'"
  113. }
  114. // 查询所有selection
  115. //YounggeeSelectionInfo含有表中的所有属性
  116. var selectionList = []model.YounggeeSelectionInfo{}
  117. //err := g.Model(dao.YounggeeSelectionInfo.Table).Where(whereStr).Scan(&selectionList)
  118. //展示带货商品的排序规则 预估赚、ddl未处理
  119. err := g.Model(dao.YounggeeSelectionInfo.Table).Where(whereStr).Order("commission_rate DESC , task_reward DESC ").Scan(&selectionList)
  120. if err != nil {
  121. return &TalentHttpResult{Code: -3, Msg: "查询数据库失败"}
  122. }
  123. fmt.Println("****searchValue:", searchValue)
  124. fmt.Println("****secFormList:", secFormList)
  125. fmt.Println("****taskFormList:", taskFormList)
  126. fmt.Println("****whereStr: ", whereStr)
  127. // 判断请求页面是否超过最大页面
  128. c, err := g.DB().Model(dao.YounggeeSelectionInfo.Table).Where(whereStr).Count()
  129. if err != nil {
  130. return &TalentHttpResult{Code: -4, Msg: err.Error(), Data: nil}
  131. }
  132. maxPage := c / cntPerPage
  133. if c%cntPerPage > 0 {
  134. maxPage += 1
  135. }
  136. if pageIndex+1 > maxPage {
  137. return &TalentHttpResult{Code: -5, Msg: "over max page"}
  138. }
  139. var selectionInfoList = youngee_talent_model.SelectionInfoList{
  140. Count: c,
  141. }
  142. err = g.DB().Model(dao.YounggeeSelectionInfo.Table).WithAll().Where(whereStr).
  143. Order("selection_status ASC , task_ddl DESC , commission_rate DESC , task_reward DESC, selection_id").Limit(startId, cntPerPage).Scan(&selectionInfoList.SeletionInfos)
  144. if err != nil {
  145. return &TalentHttpResult{Code: -6, Msg: "查询数据库失败"}
  146. }
  147. selectionInfoList.MaxPage = maxPage
  148. return &TalentHttpResult{Code: 0, Msg: "success", Data: selectionInfoList}
  149. }
  150. // 获取单个选品详情service
  151. func GetSelectionDetail(r *ghttp.Request) *TalentHttpResult {
  152. sid := r.GetQueryString("selectionid", 0)
  153. if sid == "" {
  154. return &TalentHttpResult{Code: -2, Msg: "data query failed"}
  155. }
  156. var selectionDetail *youngee_talent_model.SelectionDetail
  157. err := g.DB().Model(youngee_talent_model.SelectionDetail{}).WithAll().Where("selection_id", sid).Scan(&selectionDetail)
  158. if err != nil {
  159. return &TalentHttpResult{Code: -3, Msg: err.Error()}
  160. }
  161. return &TalentHttpResult{Code: 0, Msg: "success", Data: selectionDetail}
  162. }
  163. // 判断是否已报名任务
  164. func IsSignUpSecTask(r *ghttp.Request) *TalentHttpResult {
  165. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  166. if err != nil {
  167. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  168. }
  169. selectionId := r.GetQueryInt("selection_id", -1)
  170. //定义接口存该达人所有的报名任务
  171. task := []model.YounggeeSecTaskInfo{}
  172. err = g.Model(dao.YounggeeSecTaskInfo.Table).Where("selection_id = ? and talent_id = ?", selectionId, tid).Scan(&task)
  173. if err != nil {
  174. return &TalentHttpResult{Code: -1, Msg: err.Error()}
  175. }
  176. isSign := youngee_talent_model.IsSignSecTask{}
  177. if len(task) != 0 {
  178. isSign.SecTaskInfo = &task[0]
  179. isSign.IsSign = 1
  180. } else {
  181. isSign.IsSign = 0
  182. }
  183. fmt.Println("判断报名*********", isSign)
  184. return &TalentHttpResult{Code: 0, Msg: "success", Data: isSign}
  185. }
  186. // 选品任务报名service
  187. func SignUpSecTask(r *ghttp.Request) *TalentHttpResult {
  188. tid, err := utils.SessionTalentInfo.GetTalentIdFromSession(r)
  189. if err != nil {
  190. return &TalentHttpResult{Code: -1, Msg: "Get talent info failed"}
  191. }
  192. // 解析body/json参数
  193. var signSecTaskReq *youngee_talent_model.SignSecTaskReq
  194. err = r.ParseForm(&signSecTaskReq)
  195. if err != nil {
  196. return &TalentHttpResult{Code: -2, Msg: "parse param error"}
  197. }
  198. // 查询选品详情
  199. var selectionDetail *youngee_talent_model.SelectionDetail
  200. err = g.DB().Model(youngee_talent_model.SelectionDetail{}).WithAll().Where("selection_id", signSecTaskReq.SelectionId).Scan(&selectionDetail)
  201. if err != nil {
  202. return &TalentHttpResult{Code: -3, Msg: err.Error()}
  203. }
  204. var product model.YounggeeProduct
  205. err = json.Unmarshal([]byte(selectionDetail.ProductSnap), &product)
  206. if err != nil {
  207. return &TalentHttpResult{Code: -3, Msg: "json Unmarshal failed"}
  208. }
  209. // 查询达人详情
  210. var talentInfo *youngee_talent_model.TalentInfo
  211. err = g.DB().Model("youngee_talent_info").WithAll().Where("id", tid).Scan(&talentInfo)
  212. if err != nil {
  213. return &TalentHttpResult{Code: -4, Msg: "Get talent info failed"}
  214. }
  215. // 社媒账号详情
  216. var accountInfo *youngee_talent_model.PlatformAccountInfo
  217. err = g.DB().Model("youngee_platform_account_info").WithAll().Where("talent_id = ? and platform_id = ?", tid, selectionDetail.Platform).Scan(&accountInfo)
  218. if err != nil {
  219. return &TalentHttpResult{Code: -5, Msg: err.Error()}
  220. }
  221. // 收货地址详情
  222. address, err := g.DB().Model(dao.YoungeeTalentDeliveryAddress.Table).One("talent_id = ? and address_id = ?", tid, signSecTaskReq.AddressId)
  223. if err != nil {
  224. return &TalentHttpResult{Code: -6, Msg: err.Error()}
  225. }
  226. var newTaskId string
  227. // 首先生成任务id
  228. newTaskId = utils.GetUuid.GetTaskId(selectionDetail.SelectionId, selectionDetail.EnterpriseId, tid)
  229. // 生成达人平台账号信息快照
  230. accountSnap, err := gjson.Encode(accountInfo)
  231. if err != nil {
  232. return &TalentHttpResult{Code: -7, Msg: "encode platform snap failed"}
  233. }
  234. // 生成达人信息快照
  235. talentSnap, err := gjson.Encode(talentInfo)
  236. if err != nil {
  237. return &TalentHttpResult{Code: -8, Msg: "encode talent info snap failed"}
  238. }
  239. // 生成收货地址快照
  240. addrSnap, err := gjson.Encode(address)
  241. if err != nil {
  242. return &TalentHttpResult{Code: -9, Msg: "encode delivery address snap failed"}
  243. }
  244. // 新建选品任务
  245. secTaskInfo := model.YounggeeSecTaskInfo{
  246. TaskId: newTaskId,
  247. SelectionId: signSecTaskReq.SelectionId,
  248. TalentId: tid,
  249. AccountId: accountInfo.AccountId,
  250. TalentPlatformInfoSnap: string(accountSnap),
  251. TalentPersonalInfoSnap: string(talentSnap),
  252. TalentPostAddrSnap: string(addrSnap),
  253. TaskReward: selectionDetail.TaskReward,
  254. TalentPayment: product.ProductPrice,
  255. IsPayPayment: 0,
  256. IsPayReward: 0,
  257. TaskMode: selectionDetail.TaskMode,
  258. SampleMode: selectionDetail.SampleMode,
  259. TaskStage: 3,
  260. TaskStatus: 1,
  261. CreateDate: gtime.Now(),
  262. CompleteStatus: 1,
  263. LogisticsStatus: 1,
  264. AssignmentStatus: 1,
  265. UpdateAt: gtime.Now(),
  266. WithdrawStatus: 1,
  267. LeadTeamId: signSecTaskReq.LeadTeamId,
  268. TeamId: signSecTaskReq.TeamId,
  269. TeamIncome: 0,
  270. TeamPoint: 0,
  271. }
  272. err = g.DB().Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  273. // young之团收益计算
  274. var rewardConfig *model.YounggeeTeamRewardConfig
  275. whereCondition := g.Map{
  276. dao.YounggeeTeamRewardConfig.Columns.ProjectType: 2,
  277. dao.YounggeeTeamRewardConfig.Columns.TaskForm: selectionDetail.TaskMode + 3,
  278. dao.YounggeeTeamRewardConfig.Columns.ContentForm: selectionDetail.ContentType,
  279. dao.YounggeeTeamRewardConfig.Columns.RewardReason: talentInfo.UserType,
  280. }
  281. err = tx.Model(dao.YounggeeTeamRewardConfig.Table).Where(whereCondition).Scan(&rewardConfig)
  282. if err != nil {
  283. fmt.Println(err)
  284. return err
  285. }
  286. if rewardConfig != nil {
  287. secTaskInfo.TeamPoint = rewardConfig.Point
  288. secTaskInfo.TeamIncome = rewardConfig.Money
  289. whereCondition = g.Map{
  290. dao.YounggeeTalentTeam.Columns.TeamId: signSecTaskReq.LeadTeamId,
  291. }
  292. whereCondition1 := g.Map{
  293. dao.YounggeeTalentTeam.Columns.TeamId: signSecTaskReq.TeamId,
  294. }
  295. updateData := g.Map{
  296. dao.YounggeeTalentTeam.Columns.PointIncome: gdb.Raw(fmt.Sprintf("%s + %d", dao.YounggeeTalentTeam.Columns.PointIncome, rewardConfig.Point)),
  297. dao.YounggeeTalentTeam.Columns.MoneyIncome: gdb.Raw(fmt.Sprintf("%s + %f", dao.YounggeeTalentTeam.Columns.MoneyIncome, float64(rewardConfig.Money)*secTaskInfo.TaskReward/100)),
  298. }
  299. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Data(updateData).Where(whereCondition).Update()
  300. if err != nil {
  301. return err
  302. }
  303. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Data(updateData).Where(whereCondition1).Update()
  304. if err != nil {
  305. return err
  306. }
  307. // young之团状态变更
  308. if signSecTaskReq.LeadTeamId != "" {
  309. // 更新young之团状态
  310. whereCondition2 := g.Map{
  311. dao.YounggeeTalentTeam.Columns.TeamStatus: 1,
  312. }
  313. updateData = g.Map{
  314. dao.YounggeeTalentTeam.Columns.TeamStatus: 2,
  315. }
  316. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Where(whereCondition).Where(whereCondition2).Data(updateData).Update()
  317. if err != nil {
  318. return err
  319. }
  320. }
  321. if signSecTaskReq.TeamId != "" {
  322. // 更新young之团状态
  323. whereCondition2 := g.Map{
  324. dao.YounggeeTalentTeam.Columns.TeamStatus: 1,
  325. }
  326. updateData = g.Map{
  327. dao.YounggeeTalentTeam.Columns.TeamStatus: 2,
  328. }
  329. _, err = tx.Ctx(ctx).Model(dao.YounggeeTalentTeam.Table).Where(whereCondition1).Where(whereCondition2).Data(updateData).Update()
  330. if err != nil {
  331. return err
  332. }
  333. }
  334. }
  335. if selectionDetail.SampleMode == 2 {
  336. // 减少选品库存
  337. whereCondition1 := g.Map{
  338. dao.YounggeeSelectionInfo.Columns.SelectionId: selectionDetail.SelectionId,
  339. }
  340. updateData := g.Map{
  341. dao.YounggeeSelectionInfo.Columns.RemainNum: gdb.Raw(fmt.Sprintf("%s - 1", dao.YounggeeSelectionInfo.Columns.RemainNum)),
  342. }
  343. _, err = tx.Ctx(ctx).Model(dao.YounggeeSelectionInfo.Table).Where(whereCondition1).Data(updateData).Update()
  344. if err != nil {
  345. return err
  346. }
  347. // 新建任务,初始化状态为待发货
  348. secTaskInfo.TaskStage = 6
  349. _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo).Insert()
  350. if err != nil {
  351. return err
  352. }
  353. } else {
  354. // 新建任务,初始化状态为待确认
  355. _, err = tx.Ctx(ctx).Model(dao.YounggeeSecTaskInfo.Table).Data(&secTaskInfo).Insert()
  356. if err != nil {
  357. return err
  358. }
  359. }
  360. return nil
  361. })
  362. if err != nil {
  363. return &TalentHttpResult{Code: -18, Msg: "add Task data failed"}
  364. }
  365. signSecTaskResp := youngee_talent_model.SignSecTaskResp{
  366. TaskId: newTaskId,
  367. }
  368. return &TalentHttpResult{Code: 0, Msg: "success", Data: signSecTaskResp}
  369. }