h5.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package newcomer
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/creative/model/newcomer"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. // H5TaskList for H5 detail task list
  10. func (s *Service) H5TaskList(c context.Context, mid int64, from string) (res *newcomer.H5TaskRewardList, err error) {
  11. var (
  12. u *UserTaskInfo
  13. tasks []*newcomer.Task
  14. taskTypeMap = make(map[int8][]*newcomer.Task) // taskType-task
  15. taskGroupMap = make(map[int64][]*newcomer.Task) // groupID-task
  16. )
  17. // get user tasks
  18. userTasks, err := s.newc.UserTasks(c, mid)
  19. if err != nil {
  20. log.Error("TaskList s.newc.UserTasks mid(%d)|error(%v)", mid, err)
  21. return
  22. }
  23. if len(userTasks) == 0 {
  24. // return:User did not receive the task
  25. res = &newcomer.H5TaskRewardList{
  26. TaskReceived: newcomer.NoBindTask,
  27. }
  28. return
  29. }
  30. // get user info
  31. u, err = s.getUserTaskInfo(c, mid, userTasks)
  32. if err != nil {
  33. return
  34. }
  35. // get tasks
  36. tasks = s.getTasksInfoByType(userTasks, newcomer.DefualtTaskType)
  37. if len(tasks) == 0 {
  38. err = ecode.CreativeNewcomerNoTask
  39. log.Error("TaskList s.GetTaskByType len(tasks) == 0")
  40. return
  41. }
  42. // group by groupID & taskType
  43. taskGroupMap, taskTypeMap = s.groupByTasks(tasks)
  44. // add task label & redirect
  45. s.addLabelRedirect(tasks, from)
  46. // task_gift
  47. taskGift, err := s.getTaskGiftData(c, mid, taskTypeMap, newcomer.FromH5)
  48. if err != nil {
  49. return
  50. }
  51. // task_groups
  52. tgs, err := s.getTaskGroupData(c, mid, taskGroupMap)
  53. if err != nil {
  54. return
  55. }
  56. // if userLevel == UserTaskLevel01 , set unlock state
  57. if u.UserTaskLevel == newcomer.UserTaskLevel01 {
  58. for _, v := range tgs {
  59. if v.TaskType == newcomer.AdvancedTaskType {
  60. v.RewardState = newcomer.RewardUnlock
  61. }
  62. }
  63. }
  64. // add tips
  65. s.addTaskGroupTip(tgs)
  66. s.addGiftTip(taskGift, taskTypeMap)
  67. res = &newcomer.H5TaskRewardList{
  68. TaskReceived: newcomer.BindTask,
  69. TaskGroups: tgs,
  70. TaskGift: taskGift,
  71. }
  72. return
  73. }
  74. // addLabelRedirect add label & redirect
  75. func (s *Service) addLabelRedirect(tasks []*newcomer.Task, from string) {
  76. if len(tasks) == 0 {
  77. return
  78. }
  79. for _, v := range tasks {
  80. if v == nil {
  81. continue
  82. }
  83. t, ok := s.TaskMapCache[v.ID]
  84. if !ok {
  85. continue
  86. }
  87. m, ook := newcomer.H5RedirectMap[from][t.TargetType]
  88. if !ook || len(m) == 0 {
  89. continue
  90. }
  91. v.Label = m[0]
  92. v.Redirect = m[1]
  93. }
  94. }
  95. // addGiftTip get gift tip
  96. func (s *Service) addGiftTip(tg []*newcomer.TaskGift, kindMap map[int8][]*newcomer.Task) {
  97. for _, v := range tg {
  98. if v == nil {
  99. continue
  100. }
  101. if v.State != newcomer.RewardUnlock {
  102. if tip, ok := newcomer.GiftTipMap[v.State][v.Type]; ok {
  103. v.Tip = tip
  104. } else {
  105. v.Tip = ""
  106. }
  107. continue
  108. }
  109. // 判断还需要完成奖几个任务
  110. if len(kindMap[v.Type]) == 0 {
  111. v.Tip = ""
  112. continue
  113. }
  114. unfinished := 0
  115. for _, v := range kindMap[v.Type] {
  116. if v == nil {
  117. continue
  118. }
  119. if v.CompleteSate == newcomer.TaskIncomplete {
  120. unfinished++
  121. }
  122. }
  123. v.Tip = fmt.Sprintf("再完成%d个任务就能领取了呢", unfinished)
  124. }
  125. }
  126. // addTaskGroupTip get taskGroup tip
  127. func (s *Service) addTaskGroupTip(tr []*newcomer.TaskRewardGroup) {
  128. for _, v := range tr {
  129. if v == nil {
  130. continue
  131. }
  132. if tip, ok := newcomer.TaskGroupTipMap[v.RewardState][v.GroupID]; ok {
  133. v.Tip = tip
  134. } else {
  135. v.Tip = ""
  136. }
  137. }
  138. }