db.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package newcomer
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/creative/model"
  7. "go-common/library/log"
  8. )
  9. //UserTasks get user unfinish task.
  10. func (d *Dao) UserTasks(c context.Context, index string, id int64, limit int) (res []*model.UserTask, err error) {
  11. _getUserTaskSQL := "SELECT id, mid, task_id, task_group_id, task_type, state, task_bind_time, ctime, mtime FROM newcomers_task_user_%s WHERE state=-1 AND id > ? order by id asc limit ?"
  12. rows, err := d.db.Query(c, fmt.Sprintf(_getUserTaskSQL, index), id, limit)
  13. if err != nil {
  14. log.Error("UserTasks d.db.Query error(%v)", err)
  15. return
  16. }
  17. defer rows.Close()
  18. res = make([]*model.UserTask, 0)
  19. for rows.Next() {
  20. r := &model.UserTask{}
  21. if err = rows.Scan(&r.ID, &r.MID, &r.TaskID, &r.TaskGroupID, &r.TaskType, &r.State, &r.TaskBindTime, &r.CTime, &r.MTime); err != nil {
  22. log.Error("UserTasks rows.Scan error(%v)", err)
  23. return
  24. }
  25. res = append(res, r)
  26. }
  27. return
  28. }
  29. // getTableName by mid%100
  30. func getTableName(mid int64) string {
  31. return fmt.Sprintf("%02d", mid%100)
  32. }
  33. // UpUserTask update user task finish state
  34. func (d *Dao) UpUserTask(c context.Context, mid, tid int64) (int64, error) {
  35. _upUserTaskSQL := "UPDATE newcomers_task_user_%s SET state=? WHERE mid=? AND task_id=?"
  36. res, err := d.db.Exec(c, fmt.Sprintf(_upUserTaskSQL, getTableName(mid)), 0, mid, tid)
  37. if err != nil {
  38. log.Error("RewardActivate d.db.Exec mid(%d) id(%d) error(%v)", mid, tid, err)
  39. return 0, err
  40. }
  41. return res.RowsAffected()
  42. }
  43. //UserTasksByMIDAndState get user unfinish task by mid & state.
  44. func (d *Dao) UserTasksByMIDAndState(c context.Context, mid int64, state int) (res []*model.UserTask, err error) {
  45. _getUserTaskByMIDSQL := "SELECT id, mid, task_id, task_group_id, task_type, state, ctime, mtime FROM newcomers_task_user_%s WHERE mid=? AND state=?"
  46. rows, err := d.db.Query(c, fmt.Sprintf(_getUserTaskByMIDSQL, getTableName(mid)), mid, state)
  47. if err != nil {
  48. log.Error("UserTasksByMIDAndState d.db.Query error(%v)", err)
  49. return
  50. }
  51. defer rows.Close()
  52. res = make([]*model.UserTask, 0)
  53. for rows.Next() {
  54. r := &model.UserTask{}
  55. if err = rows.Scan(&r.ID, &r.MID, &r.TaskID, &r.TaskGroupID, &r.TaskType, &r.State, &r.CTime, &r.MTime); err != nil {
  56. log.Error("UserTasksByMIDAndState rows.Scan error(%v)", err)
  57. return
  58. }
  59. res = append(res, r)
  60. }
  61. return
  62. }
  63. //Tasks get all task.
  64. func (d *Dao) Tasks(c context.Context) (res []*model.Task, err error) {
  65. _getTaskSQL := "SELECT id, group_id, type, state, target_type, target_value, title, `desc`, comment, ctime, mtime FROM newcomers_task WHERE state=0"
  66. rows, err := d.db.Query(c, _getTaskSQL)
  67. if err != nil {
  68. log.Error("Tasks d.db.Query error(%v)", err)
  69. return
  70. }
  71. defer rows.Close()
  72. res = make([]*model.Task, 0)
  73. for rows.Next() {
  74. r := &model.Task{}
  75. if err = rows.Scan(&r.ID, &r.GroupID, &r.Type, &r.State, &r.TargetType, &r.TargetValue, &r.Title, &r.Desc, &r.Comment, &r.CTime, &r.MTime); err != nil {
  76. log.Error("Tasks rows.Scan error(%v)", err)
  77. return
  78. }
  79. res = append(res, r)
  80. }
  81. return
  82. }
  83. // TaskByTID get task by task_id
  84. func (d *Dao) TaskByTID(c context.Context, mid int64, tid int64) (res *model.Task, err error) {
  85. _getOneTaskSQL := fmt.Sprintf("SELECT task_id,task_group_id,task_type,state FROM newcomers_task_user_%s WHERE mid=? AND task_id=?", getTableName(mid))
  86. row := d.db.QueryRow(c, _getOneTaskSQL, mid, tid)
  87. res = &model.Task{}
  88. if err = row.Scan(&res.ID, &res.GroupID, &res.Type, &res.State); err != nil {
  89. log.Error("TaskByTID ow.Scan error(%v)", err)
  90. return
  91. }
  92. return
  93. }
  94. // CheckTaskComplete check task complete state
  95. func (d *Dao) CheckTaskComplete(c context.Context, mid int64, tid int64) bool {
  96. task, err := d.TaskByTID(c, mid, tid)
  97. if err != nil || task == nil {
  98. return false
  99. }
  100. if task.State == 0 {
  101. return true
  102. }
  103. return false
  104. }
  105. //UserTasksNotify get unfinish task send notify to user.
  106. func (d *Dao) UserTasksNotify(c context.Context, index string, id int64, start, end string, limit int) (res []*model.UserTask, err error) {
  107. _getUserTaskSQL := "SELECT id, mid, task_id, task_group_id, task_type, state, ctime, mtime FROM newcomers_task_user_%s WHERE state=-1 AND task_type=1 AND id>? AND ctime >= ? AND ctime <= ? order by id asc limit ?"
  108. rows, err := d.db.Query(c, fmt.Sprintf(_getUserTaskSQL, index), id, start, end, limit)
  109. if err != nil {
  110. log.Error("UserTasks d.db.Query error(%v)", err)
  111. return
  112. }
  113. defer rows.Close()
  114. res = make([]*model.UserTask, 0)
  115. for rows.Next() {
  116. r := &model.UserTask{}
  117. if err = rows.Scan(&r.ID, &r.MID, &r.TaskID, &r.TaskGroupID, &r.TaskType, &r.State, &r.CTime, &r.MTime); err != nil {
  118. log.Error("UserTasks rows.Scan error(%v)", err)
  119. return
  120. }
  121. res = append(res, r)
  122. }
  123. return
  124. }
  125. //UserTasksByMID get user unfinish task by mid.
  126. func (d *Dao) UserTasksByMID(c context.Context, mid int64) (res []*model.UserTask, err error) {
  127. _getUserTaskByMIDSQL := "SELECT id, mid, task_id, task_group_id, task_type, state, ctime, mtime FROM newcomers_task_user_%s WHERE mid=?"
  128. rows, err := d.db.Query(c, fmt.Sprintf(_getUserTaskByMIDSQL, getTableName(mid)), mid)
  129. if err != nil {
  130. log.Error("UserTasksByMID d.db.Query error(%v)", err)
  131. return
  132. }
  133. defer rows.Close()
  134. res = make([]*model.UserTask, 0)
  135. for rows.Next() {
  136. r := &model.UserTask{}
  137. if err = rows.Scan(&r.ID, &r.MID, &r.TaskID, &r.TaskGroupID, &r.TaskType, &r.State, &r.CTime, &r.MTime); err != nil {
  138. log.Error("UserTasksByMID rows.Scan error(%v)", err)
  139. return
  140. }
  141. res = append(res, r)
  142. }
  143. return
  144. }
  145. // AllGiftRewards get all GiftRewards for cache
  146. func (d *Dao) AllGiftRewards(c context.Context) (res map[int8][]*model.GiftReward, err error) {
  147. _getAllGiftRewardSQL := "SELECT task_type,reward_id,state,comment,ctime,mtime FROM newcomers_gift_reward WHERE state=0"
  148. rows, err := d.db.Query(c, _getAllGiftRewardSQL)
  149. if err != nil {
  150. log.Error("AllGiftRewards d.db.Query error(%v)", err)
  151. return
  152. }
  153. defer rows.Close()
  154. res = make(map[int8][]*model.GiftReward)
  155. for rows.Next() {
  156. t := &model.GiftReward{}
  157. if err = rows.Scan(&t.TaskType, &t.RewardID, &t.State, &t.Comment, &t.CTime, &t.MTime); err != nil {
  158. log.Error("AllGiftRewards rows.Scan error(%v)", err)
  159. return
  160. }
  161. res[t.TaskType] = append(res[t.TaskType], t)
  162. }
  163. return
  164. }
  165. // GiftRewardCount get received gift reward count
  166. func (d *Dao) GiftRewardCount(c context.Context, mid int64) (res int, err error) {
  167. sqlStr := "SELECT count(DISTINCT task_gift_id) FROM newcomers_reward_receive WHERE NOT task_gift_id=0 AND mid=?"
  168. row := d.db.QueryRow(c, sqlStr, mid)
  169. err = row.Scan(&res)
  170. if err != nil {
  171. log.Error("GiftRewardCount d.db.QueryRow error(%v)", err)
  172. }
  173. return
  174. }
  175. // BaseRewardCount get received base reward count
  176. func (d *Dao) BaseRewardCount(c context.Context, mid int64) (res int, err error) {
  177. sqlStr := "SELECT count(DISTINCT task_group_id) FROM newcomers_reward_receive WHERE NOT task_group_id=0 AND mid=?"
  178. row := d.db.QueryRow(c, sqlStr, mid)
  179. err = row.Scan(&res)
  180. if err != nil {
  181. log.Error("BaseRewardCount d.db.QueryRow error(%v)", err)
  182. }
  183. return
  184. }
  185. //CheckTasksForRewardNotify get finish task send notify to user.
  186. func (d *Dao) CheckTasksForRewardNotify(c context.Context, index string, id int64, startMtime, endMtime time.Time, limit int) (res []*model.UserTask, err error) {
  187. _getUserTaskSQL := "SELECT id, mid, task_id, task_group_id, task_type, state, ctime, mtime FROM newcomers_task_user_%s WHERE state=0 AND id>? AND mtime>=? AND mtime<=? order by id asc limit ?"
  188. rows, err := d.db.Query(c, fmt.Sprintf(_getUserTaskSQL, index), id, startMtime.Format("2006-01-02 15:04:05"), endMtime.Format("2006-01-02 15:04:05"), limit)
  189. if err != nil {
  190. log.Error("UserTasks d.db.Query error(%v)", err)
  191. return
  192. }
  193. defer rows.Close()
  194. res = make([]*model.UserTask, 0)
  195. for rows.Next() {
  196. r := &model.UserTask{}
  197. if err = rows.Scan(&r.ID, &r.MID, &r.TaskID, &r.TaskGroupID, &r.TaskType, &r.State, &r.CTime, &r.MTime); err != nil {
  198. log.Error("UserTasks rows.Scan error(%v)", err)
  199. return
  200. }
  201. res = append(res, r)
  202. }
  203. return
  204. }