activity.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/admin/main/growup/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/xstr"
  9. )
  10. const (
  11. _activityByNameSQL = "SELECT id FROM creative_activity WHERE name = ?"
  12. _activityCountSQL = "SELECT count(*) FROM creative_activity %s"
  13. _activitisSQL = "SELECT id,name,signed_start,signed_end,sign_up,sign_up_start,sign_up_end,object,upload_start,upload_end,win_type,require_items,require_value,statistics_start,statistics_end,bonus_type,bonus_time,progress_frequency,update_page,progress_start,progress_end,progress_sync,bonus_query,bonus_query_start,bonus_query_end,background,win_desc,unwin_desc,details FROM creative_activity %s LIMIT ?,?"
  14. _activityBonusSQL = "SELECT activity_id,ranking,bonus_money FROM activity_bonus WHERE activity_id IN (%s)"
  15. _upActivityStateCountSQL = "SELECT count(*) FROM up_activity WHERE activity_id = ? AND state IN (%s)"
  16. _upActivitySQL = "SELECT mid,nickname,sign_up_time,bonus,ranking,state FROM up_activity WHERE activity_id = ? AND state != 0 AND is_deleted = 0 LIMIT ?,?"
  17. _upActivitySuccessSQL = "SELECT mid,nickname,aids,aid_num,bonus,success_time,state FROM up_activity WHERE activity_id = ? AND state IN (2,3) %s AND is_deleted = 0 LIMIT ?,?"
  18. _txInsertActivitySQL = "INSERT INTO creative_activity(name,creator,signed_start,signed_end,sign_up,sign_up_start,sign_up_end,object,upload_start,upload_end,win_type,require_items,require_value,statistics_start,statistics_end,bonus_type,bonus_time,progress_frequency,update_page,progress_start,progress_end,progress_sync,bonus_query,bonus_query_start,bonus_query_end,background,win_desc,unwin_desc,details) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) %s"
  19. _txInsertBonusRankSQL = "INSERT INTO activity_bonus(activity_id,ranking,bonus_money) VALUES %s ON DUPLICATE KEY UPDATE activity_id=VALUES(activity_id),ranking=VALUES(ranking),bonus_money=VALUES(bonus_money)"
  20. _updateUpActivityStateSQL = "UPDATE up_activity SET state = ? WHERE activity_id = ? AND mid IN (%s) AND state = ?"
  21. )
  22. // GetActivityByName get activity by name
  23. func (d *Dao) GetActivityByName(c context.Context, name string) (id int64, err error) {
  24. err = d.rddb.QueryRow(c, _activityByNameSQL, name).Scan(&id)
  25. return
  26. }
  27. // ActivityCount get activity count by query
  28. func (d *Dao) ActivityCount(c context.Context, query string) (count int, err error) {
  29. err = d.rddb.QueryRow(c, fmt.Sprintf(_activityCountSQL, query)).Scan(&count)
  30. return
  31. }
  32. // GetActivities get activity by query
  33. func (d *Dao) GetActivities(c context.Context, query string, from, limit int) (acs []*model.CActivity, err error) {
  34. acs = make([]*model.CActivity, 0)
  35. rows, err := d.rddb.Query(c, fmt.Sprintf(_activitisSQL, query), from, limit)
  36. if err != nil {
  37. log.Error("GetActivities d.db.Query error(%v)", err)
  38. return
  39. }
  40. defer rows.Close()
  41. for rows.Next() {
  42. ac := &model.CActivity{}
  43. err = rows.Scan(&ac.ID, &ac.Name, &ac.SignedStart, &ac.SignedEnd, &ac.SignUp, &ac.SignUpStart, &ac.SignUpEnd, &ac.Object, &ac.UploadStart, &ac.UploadEnd, &ac.WinType, &ac.RequireItems, &ac.RequireValue, &ac.StatisticsStart, &ac.StatisticsEnd, &ac.BonusType, &ac.BonusTime, &ac.ProgressFrequency, &ac.UpdatePage, &ac.ProgressStart, &ac.ProgressEnd, &ac.ProgressSync, &ac.BonusQuery, &ac.BonusQuerStart, &ac.BonusQueryEnd, &ac.Background, &ac.WinDesc, &ac.UnwinDesc, &ac.Details)
  44. if err != nil {
  45. log.Error("GetActivities row.Scan error(%v)", err)
  46. return
  47. }
  48. acs = append(acs, ac)
  49. }
  50. err = rows.Err()
  51. return
  52. }
  53. // TxGetActivityByName get activity by name
  54. func (d *Dao) TxGetActivityByName(tx *sql.Tx, name string) (id int64, err error) {
  55. err = tx.QueryRow(_activityByNameSQL, name).Scan(&id)
  56. return
  57. }
  58. // TxInsertActivity tx insert into creative_activity
  59. func (d *Dao) TxInsertActivity(tx *sql.Tx, ac *model.CActivity, update string) (rows int64, err error) {
  60. res, err := tx.Exec(fmt.Sprintf(_txInsertActivitySQL, update), ac.Name, ac.Creator, ac.SignedStart, ac.SignedEnd, ac.SignUp, ac.SignUpStart, ac.SignUpEnd, ac.Object, ac.UploadStart, ac.UploadEnd, ac.WinType, ac.RequireItems, ac.RequireValue, ac.StatisticsStart, ac.StatisticsEnd, ac.BonusType, ac.BonusTime, ac.ProgressFrequency, ac.UpdatePage, ac.ProgressStart, ac.ProgressEnd, ac.ProgressSync, ac.BonusQuery, ac.BonusQuerStart, ac.BonusQueryEnd, ac.Background, ac.WinDesc, ac.UnwinDesc, ac.Details)
  61. if err != nil {
  62. return
  63. }
  64. return res.RowsAffected()
  65. }
  66. // GetActivityBonus get activity_bonus by id
  67. func (d *Dao) GetActivityBonus(c context.Context, ids []int64) (brs []*model.BonusRank, err error) {
  68. brs = make([]*model.BonusRank, 0)
  69. rows, err := d.rddb.Query(c, fmt.Sprintf(_activityBonusSQL, xstr.JoinInts(ids)))
  70. if err != nil {
  71. log.Error("GetActivityBonus d.db.Query error(%v)", err)
  72. return
  73. }
  74. defer rows.Close()
  75. for rows.Next() {
  76. br := &model.BonusRank{}
  77. err = rows.Scan(&br.ID, &br.Rank, &br.Money)
  78. if err != nil {
  79. log.Error("GetActivityBonus row.Scan error(%v)", err)
  80. return
  81. }
  82. brs = append(brs, br)
  83. }
  84. err = rows.Err()
  85. return
  86. }
  87. // TxInsertActivityBonusBatch tx insert into activity_bonus
  88. func (d *Dao) TxInsertActivityBonusBatch(tx *sql.Tx, vals string) (rows int64, err error) {
  89. if vals == "" {
  90. return
  91. }
  92. res, err := tx.Exec(fmt.Sprintf(_txInsertBonusRankSQL, vals))
  93. if err != nil {
  94. return
  95. }
  96. return res.RowsAffected()
  97. }
  98. // UpActivityStateCount get up activity state count
  99. func (d *Dao) UpActivityStateCount(c context.Context, id int64, states []int64) (count int, err error) {
  100. err = d.rddb.QueryRow(c, fmt.Sprintf(_upActivityStateCountSQL, xstr.JoinInts(states)), id).Scan(&count)
  101. return
  102. }
  103. // ListUpActivity list up_activity where state != 0
  104. func (d *Dao) ListUpActivity(c context.Context, id int64, from, limit int) (ups []*model.UpActivity, err error) {
  105. ups = make([]*model.UpActivity, 0)
  106. rows, err := d.rddb.Query(c, _upActivitySQL, id, from, limit)
  107. if err != nil {
  108. log.Error("ListUpActivity d.db.Query error(%v)", err)
  109. return
  110. }
  111. defer rows.Close()
  112. for rows.Next() {
  113. up := &model.UpActivity{}
  114. err = rows.Scan(&up.MID, &up.Nickname, &up.SignUpTime, &up.Bonus, &up.Rank, &up.State)
  115. if err != nil {
  116. log.Error("ListUpActivity row.Scan error(%v)", err)
  117. return
  118. }
  119. ups = append(ups, up)
  120. }
  121. err = rows.Err()
  122. return
  123. }
  124. // ListUpActivitySuccess list up_activity where state != 0
  125. func (d *Dao) ListUpActivitySuccess(c context.Context, id, mid int64, from, limit int) (ups []*model.UpActivity, err error) {
  126. ups = make([]*model.UpActivity, 0)
  127. query := ""
  128. if mid != 0 {
  129. query = fmt.Sprintf("AND mid = %d", mid)
  130. }
  131. rows, err := d.rddb.Query(c, fmt.Sprintf(_upActivitySuccessSQL, query), id, from, limit)
  132. if err != nil {
  133. log.Error("ListUpActivitySuccess d.db.Query error(%v)", err)
  134. return
  135. }
  136. defer rows.Close()
  137. for rows.Next() {
  138. up := &model.UpActivity{}
  139. err = rows.Scan(&up.MID, &up.Nickname, &up.AIDs, &up.AIDNum, &up.Bonus, &up.SuccessTime, &up.State)
  140. if err != nil {
  141. log.Error("ListUpActivitySuccess row.Scan error(%v)", err)
  142. return
  143. }
  144. ups = append(ups, up)
  145. }
  146. err = rows.Err()
  147. return
  148. }
  149. // UpdateUpActivityState update up state
  150. func (d *Dao) UpdateUpActivityState(c context.Context, activityID int64, mids []int64, oldState, newState int) (rows int64, err error) {
  151. if oldState == newState {
  152. return
  153. }
  154. res, err := d.rddb.Exec(c, fmt.Sprintf(_updateUpActivityStateSQL, xstr.JoinInts(mids)), newState, activityID, oldState)
  155. if err != nil {
  156. return
  157. }
  158. return res.RowsAffected()
  159. }
  160. // TxUpdateUpActivityState tx update up state
  161. func (d *Dao) TxUpdateUpActivityState(tx *sql.Tx, activityID int64, mids []int64, oldState, newState int) (rows int64, err error) {
  162. if oldState == newState {
  163. return
  164. }
  165. res, err := tx.Exec(fmt.Sprintf(_updateUpActivityStateSQL, xstr.JoinInts(mids)), newState, activityID, oldState)
  166. if err != nil {
  167. return
  168. }
  169. return res.RowsAffected()
  170. }