cheat.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "go-common/library/database/sql"
  8. "go-common/library/log"
  9. "go-common/library/xstr"
  10. "go-common/app/admin/main/growup/model"
  11. )
  12. const (
  13. // select
  14. _upSpySQL = "SELECT mid,signed_at,nickname,fans,cheat_fans,play_count,cheat_play_count,account_state FROM up_spy_statistics %s LIMIT ?, ?"
  15. _avSpySQL = "SELECT archive_id,mid,nickname,upload_time,total_income,cheat_play_count,cheat_favorite,cheat_coin,deducted FROM archive_spy_statistics %s LIMIT ?,?"
  16. // select count(*)
  17. _upSpyCountSQL = "SELECT count(*) FROM up_spy_statistics"
  18. _avSpyCountSQL = "SELECT count(*) FROM archive_spy_statistics %s"
  19. // update
  20. _updateUpState = "UPDATE up_spy_statistics SET account_state=? WHERE mid=?"
  21. _updateAvState = "UPDATE archive_spy_statistics SET deducted=? WHERE archive_id IN (%s)"
  22. // cheat fans
  23. _cheatFansSQL = "SELECT mid,nickname,real_fans,cheat_fans,signed_at,deduct_at FROM cheat_fans_info WHERE is_deleted = 0 LIMIT ?,?"
  24. _cheatFansCountSQL = "SELECT count(*) FROM cheat_fans_info WHERE is_deleted = 0"
  25. _delCheatUpSQL = "UPDATE cheat_fans_info SET is_deleted = 1 WHERE mid = ?"
  26. _insertCheatFansSQL = "INSERT INTO cheat_fans_info(mid, nickname, signed_at, real_fans, cheat_fans, deduct_at) VALUES %s ON DUPLICATE KEY UPDATE mid = values(mid), nickname = values(nickname), signed_at = values(signed_at), real_fans = values(real_fans), cheat_fans = values(cheat_fans), deduct_at = values(deduct_at), is_deleted = 0"
  27. )
  28. // TxUpdateUpSpyState tx update up_spy_state
  29. func (d *Dao) TxUpdateUpSpyState(tx *sql.Tx, state int, mid int64) (rows int64, err error) {
  30. res, err := tx.Exec(_updateUpState, state, mid)
  31. if err != nil {
  32. log.Error("d.db.TxUpdateUpSpyState error(%v)", err)
  33. return
  34. }
  35. return res.RowsAffected()
  36. }
  37. // TxUpdateAvSpyState tx update av_spy_state
  38. func (d *Dao) TxUpdateAvSpyState(tx *sql.Tx, state int, archives []int64) (rows int64, err error) {
  39. if len(archives) == 0 {
  40. return
  41. }
  42. res, err := tx.Exec(fmt.Sprintf(_updateAvState, xstr.JoinInts(archives)), state)
  43. if err != nil {
  44. log.Error("d.db.TxUpdateAvSpyState error(%v)", err)
  45. return
  46. }
  47. return res.RowsAffected()
  48. }
  49. // UpSpyCount get up spy count
  50. func (d *Dao) UpSpyCount(c context.Context) (count int, err error) {
  51. row := d.rddb.QueryRow(c, _upSpyCountSQL)
  52. if err = row.Scan(&count); err != nil {
  53. log.Error("d.rddb.UpSpyCount error(%v)", err)
  54. }
  55. return
  56. }
  57. // UpSpies get up spy.
  58. func (d *Dao) UpSpies(c context.Context, query string, offset, limit int) (spies []*model.UpSpy, err error) {
  59. rows, err := d.rddb.Query(c, fmt.Sprintf(_upSpySQL, query), offset, limit)
  60. if err != nil {
  61. log.Error("d.db.Query UpSpies error(%v)", err)
  62. return
  63. }
  64. defer rows.Close()
  65. for rows.Next() {
  66. spy := &model.UpSpy{}
  67. if err = rows.Scan(&spy.MID, &spy.SignedAt, &spy.Nickname, &spy.Fans, &spy.CheatFans, &spy.PlayCount, &spy.CheatPlayCount, &spy.AccountState); err != nil {
  68. log.Error("dao.UpSpies scan error(%v)", err)
  69. return
  70. }
  71. spies = append(spies, spy)
  72. }
  73. return
  74. }
  75. // ArchiveSpyCount get archive count
  76. func (d *Dao) ArchiveSpyCount(c context.Context, query string) (count int, err error) {
  77. row := d.rddb.QueryRow(c, fmt.Sprintf(_avSpyCountSQL, query))
  78. if err = row.Scan(&count); err != nil {
  79. log.Error("dao.GetArchiveSpy count error(%v)", err)
  80. }
  81. return
  82. }
  83. // ArchiveSpies get av spy.
  84. func (d *Dao) ArchiveSpies(c context.Context, query string, offset, limit int) (spies []*model.ArchiveSpy, err error) {
  85. rows, err := d.rddb.Query(c, fmt.Sprintf(_avSpySQL, query), offset, limit)
  86. if err != nil {
  87. log.Error("dao.GetArchiveSpy query error(%v)", err)
  88. return
  89. }
  90. defer rows.Close()
  91. for rows.Next() {
  92. spy := &model.ArchiveSpy{}
  93. if err = rows.Scan(&spy.ArchiveID, &spy.MID, &spy.Nickname, &spy.UploadTime, &spy.TotalIncome, &spy.CheatPlayCount, &spy.CheatFavorite, &spy.CheatCoin, &spy.Deducted); err != nil {
  94. log.Error("dao.GetArchiveSpy scan error(%v)", err)
  95. return
  96. }
  97. spies = append(spies, spy)
  98. }
  99. return
  100. }
  101. // CheatFansCount get cheat fans count.
  102. func (d *Dao) CheatFansCount(c context.Context) (count int64, err error) {
  103. row := d.rddb.QueryRow(c, _cheatFansCountSQL)
  104. if err = row.Scan(&count); err != nil {
  105. log.Error("dao.CheatFansInfo count error(%v)", err)
  106. }
  107. return
  108. }
  109. // CheatFans get cheat fans info.
  110. func (d *Dao) CheatFans(c context.Context, from, limit int64) (fans []*model.CheatFans, err error) {
  111. rows, err := d.rddb.Query(c, _cheatFansSQL, from, limit)
  112. if err != nil {
  113. log.Error("dao.CheatFansInfo query error(%v)", err)
  114. return
  115. }
  116. defer rows.Close()
  117. for rows.Next() {
  118. fan := &model.CheatFans{}
  119. if err = rows.Scan(&fan.MID, &fan.Nickname, &fan.RealFans, &fan.CheatFans, &fan.SignedAt, &fan.DeductAt); err != nil {
  120. log.Error("dao.CheatFansInfo scan error(%v)", err)
  121. return
  122. }
  123. fans = append(fans, fan)
  124. }
  125. err = rows.Err()
  126. return
  127. }
  128. // DelCheatUp update cheat_fans_info.
  129. func (d *Dao) DelCheatUp(c context.Context, mid int64) (rows int64, err error) {
  130. res, err := d.rddb.Exec(c, _delCheatUpSQL, mid)
  131. if err != nil {
  132. log.Error("dao.UpdateCheatFans error(%v)", err)
  133. return
  134. }
  135. return res.RowsAffected()
  136. }
  137. // InsertCheatFansInfo insert into cheat_fans_info.
  138. func (d *Dao) InsertCheatFansInfo(c context.Context, values string) (rows int64, err error) {
  139. res, err := d.rddb.Exec(c, fmt.Sprintf(_insertCheatFansSQL, values))
  140. if err != nil {
  141. log.Error("dao.InsertCheatFansInfo error(%v)", err)
  142. return
  143. }
  144. return res.RowsAffected()
  145. }
  146. const (
  147. _realFansCount = "/x/internal/relation/stat"
  148. _cheatFansCount = "/x/internal/v1/spy/stat"
  149. )
  150. // GetUpRealFansCount get up real fans count
  151. func (d *Dao) GetUpRealFansCount(c context.Context, host string, mid int64) (count int, err error) {
  152. params := url.Values{}
  153. params.Set("mid", strconv.FormatInt(mid, 10))
  154. var res struct {
  155. Code int `json:"code"`
  156. Data struct {
  157. FansCount int `json:"follower"`
  158. } `json:"data"`
  159. }
  160. if err = d.client.Get(c, host+_realFansCount, "", params, &res); err != nil {
  161. log.Error("dao.GetUpRealFansCount get cheat count error(%v)", err)
  162. return
  163. }
  164. if res.Code != 0 {
  165. err = fmt.Errorf("get real fans count error code: %d", res.Code)
  166. return
  167. }
  168. count = res.Data.FansCount
  169. return
  170. }
  171. // GetUpCheatFansCount get up cheat fan count.
  172. func (d *Dao) GetUpCheatFansCount(c context.Context, host string, mid int64) (count int, err error) {
  173. params := url.Values{}
  174. params.Set("mid", strconv.FormatInt(mid, 10))
  175. var res struct {
  176. Code int `json:"code"`
  177. Data []*model.CheatCount `json:"data"`
  178. }
  179. if err = d.client.Get(c, host+_cheatFansCount, "", params, &res); err != nil {
  180. log.Error("dao.GetUpCheatFansCount get cheat count error(%v)", err)
  181. return
  182. }
  183. if res.Code != 0 {
  184. log.Error("growup-job GetUpCheatFansCount code != 0. res.Code(%d) | params(%s) error(%v)", res.Code, params.Encode(), err)
  185. err = fmt.Errorf("get cheat fans count error code: %d", res.Code)
  186. return
  187. }
  188. for _, v := range res.Data {
  189. if v.EventID == "异常粉丝量" {
  190. count = v.Quantity
  191. }
  192. }
  193. return
  194. }