up_info_video.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "go-common/app/job/main/growup/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. xtime "go-common/library/time"
  10. "go-common/library/xstr"
  11. )
  12. const (
  13. _upStateByMID = "SELECT account_state FROM up_info_video WHERE mid = ? LIMIT 1"
  14. _upInfoVideoSQL = "SELECT id,mid,nickname,signed_at,fans,total_play_count,account_type,account_state,credit_score,is_deleted FROM up_info_video WHERE id > ? ORDER BY id LIMIT ?"
  15. _upsState = "SELECT mid,expired_in FROM %s WHERE account_state = ?"
  16. _upsStateType = "SELECT mid,expired_in FROM %s WHERE account_type = ? AND account_state = ?"
  17. _updateAccountState = "UPDATE %s SET account_state = ? WHERE mid IN (%s)"
  18. // select count(*)
  19. _signedDayUpsSQL = "SELECT COUNT(*) FROM up_info_video WHERE account_state = 3 AND is_deleted = 0 AND signed_at < ? AND signed_at >= ?"
  20. _signedAllUpsSQL = "SELECT COUNT(*) FROM up_info_video WHERE account_state = 3 AND signed_at < ? AND is_deleted = 0"
  21. _videoApplyCountSQL = "SELECT COUNT(*) FROM up_info_video WHERE apply_at >= ? AND apply_at < ?"
  22. _upBaseInfoSQL = "SELECT mid,fans,play,avs_origin,avs FROM up_base_statistics WHERE mid IN (%s)"
  23. _insertUpInfoSQL = "INSERT INTO up_info_video(mid,fans,total_play_count,original_archive_count,avs) VALUES %s ON DUPLICATE KEY UPDATE fans=VALUES(fans),total_play_count=VALUES(total_play_count),original_archive_count=VALUES(original_archive_count),avs=VALUES(avs)"
  24. _uidSQL = "SELECT id,mid FROM up_info_video WHERE id > ? ORDER BY id LIMIT ?"
  25. _creditScoreByMIDSQL = "SELECT mid, score FROM credit_score WHERE mid IN (%s)"
  26. )
  27. // GetUpStateByMID get up account_state
  28. func (d *Dao) GetUpStateByMID(c context.Context, mid int64) (state int, err error) {
  29. err = d.db.QueryRow(c, _upStateByMID, mid).Scan(&state)
  30. if err == sql.ErrNoRows {
  31. err = nil
  32. state = 0
  33. }
  34. return
  35. }
  36. // GetUpCreditScore get up credit score
  37. func (d *Dao) GetUpCreditScore(c context.Context, mids []int64) (scores map[int64]int64, err error) {
  38. scores = make(map[int64]int64)
  39. if len(mids) == 0 {
  40. return
  41. }
  42. rows, err := d.db.Query(c, fmt.Sprintf(_creditScoreByMIDSQL, xstr.JoinInts(mids)))
  43. if err != nil {
  44. log.Error("db Query GetUpCreditScore error(%v)", err)
  45. return
  46. }
  47. defer rows.Close()
  48. for rows.Next() {
  49. var mid, score int64
  50. err = rows.Scan(&mid, &score)
  51. if err != nil {
  52. log.Error("rows scan error(%v)", err)
  53. return
  54. }
  55. scores[mid] = score
  56. }
  57. return
  58. }
  59. // UpInfoVideo get up_info_video
  60. func (d *Dao) UpInfoVideo(c context.Context, offset int64, limit int64) (last int64, ups map[int64]*model.UpInfoVideo, err error) {
  61. ups = make(map[int64]*model.UpInfoVideo)
  62. rows, err := d.db.Query(c, _upInfoVideoSQL, offset, limit)
  63. if err != nil {
  64. log.Error("db Query UpInfoVideo error(%v)", err)
  65. return
  66. }
  67. defer rows.Close()
  68. for rows.Next() {
  69. up := &model.UpInfoVideo{}
  70. err = rows.Scan(&last, &up.MID, &up.Nickname, &up.SignedAt, &up.Fans, &up.TotalPlayCount, &up.AccountType, &up.AccountState, &up.CreditScore, &up.IsDeleted)
  71. if err != nil {
  72. log.Error("rows scan error(%v)", err)
  73. return
  74. }
  75. ups[up.MID] = up
  76. }
  77. return
  78. }
  79. // MIDsByState get mids and expired
  80. func (d *Dao) MIDsByState(c context.Context, state int, table string) (result map[int64]xtime.Time, err error) {
  81. rows, err := d.db.Query(c, fmt.Sprintf(_upsState, table), state)
  82. if err != nil {
  83. log.Error("d.MIDsByState Query error(%v)", err)
  84. return
  85. }
  86. result = make(map[int64]xtime.Time)
  87. defer rows.Close()
  88. for rows.Next() {
  89. var mid int64
  90. var exp xtime.Time
  91. err = rows.Scan(&mid, &exp)
  92. if err != nil {
  93. log.Error("rows scan error (%v)", err)
  94. return
  95. }
  96. result[mid] = exp
  97. }
  98. return
  99. }
  100. // MIDsByStateType get mids and expired by account_type and account_state
  101. func (d *Dao) MIDsByStateType(c context.Context, typ int, state int, table string) (result map[int64]xtime.Time, err error) {
  102. rows, err := d.db.Query(c, fmt.Sprintf(_upsStateType, table), typ, state)
  103. if err != nil {
  104. log.Error("d.MIDsByStateType Query error(%v)", err)
  105. return
  106. }
  107. result = make(map[int64]xtime.Time)
  108. defer rows.Close()
  109. for rows.Next() {
  110. var mid int64
  111. var exp xtime.Time
  112. err = rows.Scan(&mid, &exp)
  113. if err != nil {
  114. log.Error("rows scan error (%v)", err)
  115. return
  116. }
  117. result[mid] = exp
  118. }
  119. return
  120. }
  121. // UpdateAccountState update account state
  122. func (d *Dao) UpdateAccountState(c context.Context, state int, mids []int64, table string) (rows int64, err error) {
  123. res, err := d.db.Exec(c, fmt.Sprintf(_updateAccountState, table, xstr.JoinInts(mids)), state)
  124. if err != nil {
  125. log.Error("d.UpdateAccountState Exec error (%v)", err)
  126. return
  127. }
  128. return res.RowsAffected()
  129. }
  130. // GetDateSignedUps get date signed ups
  131. func (d *Dao) GetDateSignedUps(c context.Context, startAt time.Time, endAt time.Time) (count int, err error) {
  132. row := d.db.QueryRow(c, _signedDayUpsSQL, endAt, startAt)
  133. if err = row.Scan(&count); err != nil {
  134. log.Error("dao.GetDateSignedUps error(%v)", err)
  135. }
  136. return
  137. }
  138. // GetAllSignedUps get all signed ups.
  139. func (d *Dao) GetAllSignedUps(c context.Context, data time.Time) (count int, err error) {
  140. row := d.db.QueryRow(c, _signedAllUpsSQL, data.Add(24*time.Hour))
  141. if err = row.Scan(&count); err != nil {
  142. log.Error("dao.GetAllSignedUps error(%v)", err)
  143. }
  144. return
  145. }
  146. // GetVideoApplyUpCount get up_info_video count
  147. func (d *Dao) GetVideoApplyUpCount(c context.Context, startAt, endAt time.Time) (count int, err error) {
  148. row := d.db.QueryRow(c, _videoApplyCountSQL, startAt, endAt)
  149. err = row.Scan(&count)
  150. return
  151. }
  152. // GetUpBaseInfo get up_base_info
  153. func (d *Dao) GetUpBaseInfo(c context.Context, mid []int64) (bs []*model.UpBaseInfo, err error) {
  154. rows, err := d.db.Query(c, fmt.Sprintf(_upBaseInfoSQL, xstr.JoinInts(mid)))
  155. if err != nil {
  156. log.Error("dao.GetUpBaseInfo error(%v)", err)
  157. return
  158. }
  159. defer rows.Close()
  160. for rows.Next() {
  161. b := &model.UpBaseInfo{}
  162. err = rows.Scan(&b.MID, &b.Fans, &b.TotalPlayCount, &b.OriginalArchiveCount, &b.Avs)
  163. if err != nil {
  164. log.Error("rows scan error(%v)", err)
  165. return
  166. }
  167. bs = append(bs, b)
  168. }
  169. return
  170. }
  171. // UpdateUpInfo update up_info_video
  172. func (d *Dao) UpdateUpInfo(c context.Context, values string) (rows int64, err error) {
  173. res, err := d.db.Exec(c, fmt.Sprintf(_insertUpInfoSQL, values))
  174. if err != nil {
  175. log.Error("dao.UpdateUpInfo error(%v)", err)
  176. return
  177. }
  178. return res.RowsAffected()
  179. }
  180. // MIDs get mids from up_info_viode
  181. func (d *Dao) MIDs(c context.Context, offset, limit int64) (last int64, mids []int64, err error) {
  182. rows, err := d.db.Query(c, _uidSQL, offset, limit)
  183. if err != nil {
  184. log.Error("dao.GetUpBaseInfo error(%v)", err)
  185. return
  186. }
  187. defer rows.Close()
  188. for rows.Next() {
  189. var mid int64
  190. err = rows.Scan(&last, &mid)
  191. if err != nil {
  192. log.Error("rows scan error(%v)", err)
  193. return
  194. }
  195. mids = append(mids, mid)
  196. }
  197. return
  198. }