up.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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/time"
  9. "go-common/library/xstr"
  10. )
  11. const (
  12. // insert
  13. _inUpsSQL = "INSERT INTO up_info_video (mid,nickname,account_type,original_archive_count,category_id,fans,account_state,sign_type,reason,is_deleted) VALUES (?,?,?,?,?,?,?,?,?,0) ON DUPLICATE KEY UPDATE nickname=?,account_type=?,original_archive_count=?,category_id=?,fans=?,account_state=?,sign_type=?,reason=?,is_deleted=0"
  14. _inUpColumnSQL = "INSERT INTO up_info_column (mid,nickname,category_id,fans,account_type,account_state,sign_type,is_deleted) VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE nickname=VALUES(nickname),account_type=VALUES(account_type),account_state=VALUES(account_state),category_id=VALUES(category_id),sign_type=VALUES(sign_type),is_deleted=VALUES(is_deleted)"
  15. _inUpBgmSQL = "INSERT INTO up_info_bgm(mid,nickname,bgms,play_count,apply_count,fans,account_state,account_type,sign_type,is_deleted) VALUES(?,?,?,?,?,?,?,?,?,0) ON DUPLICATE KEY UPDATE nickname=VALUES(nickname),bgms=VALUES(bgms),play_count=VALUES(play_count),apply_count=VALUES(apply_count),fans=VALUES(fans),account_state=VALUES(account_state),account_type=VALUES(account_type),sign_type=VALUES(sign_type),is_deleted=VALUES(is_deleted)"
  16. _inWhitelistSQL = "INSERT INTO up_white_list(mid,type) VALUES(?,?) ON DUPLICATE KEY UPDATE type=VALUES(type)"
  17. // select
  18. _upsCateInfoSQL = "SELECT nick_name,main_category_id FROM up_category_info WHERE mid=?"
  19. _upsStatInfoSQL = "SELECT fans,avs FROM up_base_statistics WHERE mid=?"
  20. _upsCountSQL = "SELECT count(*) FROM %s WHERE %s "
  21. _upsInfoSQL = "SELECT mid,nickname,account_type,original_archive_count,category_id,fans,account_state,sign_type,reason,apply_at,signed_at,reject_at,forbid_at,quit_at,dismiss_at,expired_in,ctime,mtime,is_deleted,credit_score,total_play_count,avs FROM up_info_video WHERE %s"
  22. _upsColumnInfoSQL = "SELECT mid,nickname,account_type,article_count,category_id,fans,account_state,sign_type,total_view_count,apply_at,signed_at,reject_at,forbid_at,quit_at,dismiss_at,expired_in FROM up_info_column WHERE %s"
  23. _upsBgmInfoSQL = "SELECT mid,nickname,bgms,play_count,apply_count,fans,account_state,signed_at,forbid_at,quit_at,dismiss_at,expired_in FROM up_info_bgm WHERE %s"
  24. _upInfoSQL = "SELECT mid,nickname,fans,signed_at FROM up_info_video WHERE mid=? AND account_state=? AND is_deleted=0"
  25. _upInfoStateSQL = "SELECT mid FROM %s WHERE account_state = ? AND mid in (%s) AND is_deleted = 0"
  26. _upStateSQL = "SELECT account_state FROM %s WHERE mid = ? AND is_deleted = 0 LIMIT 1"
  27. _pendingsSQL = "SELECT mid FROM %s WHERE mid IN (%s) AND account_state=2 AND is_deleted=0"
  28. _unusualSQL = "SELECT mid FROM %s WHERE mid IN (%s) AND account_state IN (5, 6, 7) AND is_deleted = 0"
  29. _bgmCountSQL = "SELECT count(distinct sid) FROM background_music WHERE mid=?"
  30. // update
  31. _rejectUpsSQL = "UPDATE %s SET account_state=?,reason=?,reject_at=?,expired_in=? WHERE mid IN (%s) AND is_deleted=0"
  32. _passUpsSQL = "UPDATE %s SET account_state=?,signed_at=? WHERE mid IN (%s) AND is_deleted = 0"
  33. _dismissUpSQL = "UPDATE %s SET account_state=?,reason=?,dismiss_at=?,quit_at=? WHERE mid=? AND account_state=? AND is_deleted=0"
  34. _forbidUpSQL = "UPDATE %s SET account_state=?,reason=?,forbid_at=?,expired_in=? WHERE mid=? AND account_state=? AND is_deleted=0"
  35. _updateAccStateSQL = "UPDATE %s SET account_state=? WHERE mid = ?"
  36. _updateUpInfoDelSQL = "UPDATE %s SET is_deleted=? WHERE mid=?"
  37. _delUpAccountSQL = "UPDATE up_account SET is_deleted=1 WHERE mid=?"
  38. _updateUpAccountSQL = "UPDATE up_account SET is_deleted = ?, withdraw_date_version = '%s' WHERE mid = ?"
  39. _delCreditRecordSQL = "UPDATE credit_score_record SET is_deleted=1 WHERE id=?"
  40. )
  41. // InsertWhitelist insert white mid
  42. func (d *Dao) InsertWhitelist(c context.Context, mid int64, typ int) (rows int64, err error) {
  43. res, err := d.rddb.Exec(c, _inWhitelistSQL, mid, typ)
  44. if err != nil {
  45. log.Error("db.inWhitelist.Exec(%s) error(%v)", _inWhitelistSQL, err)
  46. return
  47. }
  48. return res.RowsAffected()
  49. }
  50. // Pendings get mids for account_state=2
  51. func (d *Dao) Pendings(c context.Context, mids []int64, table string) (ms []int64, err error) {
  52. rows, err := d.rddb.Query(c, fmt.Sprintf(_pendingsSQL, table, xstr.JoinInts(mids)))
  53. if err != nil {
  54. return
  55. }
  56. defer rows.Close()
  57. for rows.Next() {
  58. var mid int64
  59. err = rows.Scan(&mid)
  60. if err != nil {
  61. return
  62. }
  63. ms = append(ms, mid)
  64. }
  65. return
  66. }
  67. // UnusualUps get mids for account_state=5,6,7
  68. func (d *Dao) UnusualUps(c context.Context, mids []int64, table string) (ms []int64, err error) {
  69. ms = make([]int64, 0)
  70. rows, err := d.rddb.Query(c, fmt.Sprintf(_unusualSQL, table, xstr.JoinInts(mids)))
  71. if err != nil {
  72. return
  73. }
  74. defer rows.Close()
  75. for rows.Next() {
  76. var mid int64
  77. err = rows.Scan(&mid)
  78. if err != nil {
  79. return
  80. }
  81. ms = append(ms, mid)
  82. }
  83. return
  84. }
  85. // InsertUpVideo add upinfo video
  86. func (d *Dao) InsertUpVideo(c context.Context, v *model.UpInfo) (rows int64, err error) {
  87. res, err := d.rddb.Exec(c, _inUpsSQL, v.MID, v.Nickname, v.AccountType, v.OriginalArchiveCount, v.MainCategory, v.Fans, v.AccountState, v.SignType, v.Reason, v.Nickname, v.AccountType, v.OriginalArchiveCount, v.MainCategory, v.Fans, v.AccountState, v.SignType, v.Reason)
  88. if err != nil {
  89. log.Error("db.inUpsStmt.Exec(%s) error(%v)", _inUpsSQL, err)
  90. return
  91. }
  92. return res.RowsAffected()
  93. }
  94. // InsertUpColumn insert up column
  95. func (d *Dao) InsertUpColumn(c context.Context, up *model.UpInfo) (rows int64, err error) {
  96. res, err := d.rddb.Exec(c, _inUpColumnSQL, up.MID, up.Nickname, up.MainCategory, up.Fans, up.AccountType, up.AccountState, up.SignType, 0)
  97. if err != nil {
  98. log.Error("db.inUpsStmt.Exec(%s) error(%v)", _inUpColumnSQL, err)
  99. return
  100. }
  101. return res.RowsAffected()
  102. }
  103. // InsertBgmUpInfo insert up bgm
  104. func (d *Dao) InsertBgmUpInfo(c context.Context, m *model.UpInfo) (rows int64, err error) {
  105. res, err := d.rddb.Exec(c, _inUpBgmSQL, m.MID, m.Nickname, m.BGMs, m.BgmPlayCount, m.BgmApplyCount, m.Fans, m.AccountState, m.AccountType, m.SignType)
  106. if err != nil {
  107. log.Error("db.inUpsStmt.Exec(%s) error(%v)", _inUpBgmSQL, err)
  108. return
  109. }
  110. return res.RowsAffected()
  111. }
  112. // CategoryInfo return nickname & categoryID
  113. func (d *Dao) CategoryInfo(c context.Context, mid int64) (nickname string, categoryID int, err error) {
  114. row := d.rddb.QueryRow(c, _upsCateInfoSQL, mid)
  115. if err = row.Scan(&nickname, &categoryID); err != nil {
  116. if err == sql.ErrNoRows {
  117. err = nil
  118. } else {
  119. log.Error("row.Scan error(%v)", err)
  120. }
  121. }
  122. return
  123. }
  124. // Stat return fans and avs count
  125. func (d *Dao) Stat(c context.Context, mid int64) (fans int, avs int, err error) {
  126. row := d.rddb.QueryRow(c, _upsStatInfoSQL, mid)
  127. if err = row.Scan(&fans, &avs); err != nil {
  128. if err == sql.ErrNoRows {
  129. err = nil
  130. } else {
  131. log.Error("row.Scan error(%v)", err)
  132. }
  133. }
  134. return
  135. }
  136. // UpsCount get count by query
  137. func (d *Dao) UpsCount(c context.Context, table, query string) (count int, err error) {
  138. row := d.rddb.QueryRow(c, fmt.Sprintf(_upsCountSQL, table, query))
  139. err = row.Scan(&count)
  140. return
  141. }
  142. // UpsVideoInfo get up infos by query
  143. func (d *Dao) UpsVideoInfo(c context.Context, query string) (ups []*model.UpInfo, err error) {
  144. rows, err := d.rddb.Query(c, fmt.Sprintf(_upsInfoSQL, query))
  145. if err != nil {
  146. return
  147. }
  148. defer rows.Close()
  149. for rows.Next() {
  150. up := &model.UpInfo{}
  151. err = rows.Scan(&up.MID, &up.Nickname, &up.AccountType, &up.OriginalArchiveCount, &up.MainCategory, &up.Fans, &up.AccountState, &up.SignType, &up.Reason, &up.ApplyAt, &up.SignedAt, &up.RejectAt, &up.ForbidAt, &up.QuitAt, &up.DismissAt, &up.ExpiredIn, &up.CTime, &up.MTime, &up.IsDeleted, &up.CreditScore, &up.TotalPlayCount, &up.Avs)
  152. if err != nil {
  153. return
  154. }
  155. ups = append(ups, up)
  156. }
  157. return
  158. }
  159. // UpsColumnInfo get up column infos by query
  160. func (d *Dao) UpsColumnInfo(c context.Context, query string) (ups []*model.UpInfo, err error) {
  161. rows, err := d.rddb.Query(c, fmt.Sprintf(_upsColumnInfoSQL, query))
  162. if err != nil {
  163. return
  164. }
  165. defer rows.Close()
  166. for rows.Next() {
  167. up := &model.UpInfo{}
  168. err = rows.Scan(&up.MID, &up.Nickname, &up.AccountType, &up.ArticleCount, &up.MainCategory, &up.Fans, &up.AccountState, &up.SignType, &up.TotalViewCount, &up.ApplyAt, &up.SignedAt, &up.RejectAt, &up.ForbidAt, &up.QuitAt, &up.DismissAt, &up.ExpiredIn)
  169. if err != nil {
  170. return
  171. }
  172. ups = append(ups, up)
  173. }
  174. return
  175. }
  176. // UpsBgmInfo get ups bgm infos by query
  177. func (d *Dao) UpsBgmInfo(c context.Context, query string) (ups []*model.UpInfo, err error) {
  178. rows, err := d.rddb.Query(c, fmt.Sprintf(_upsBgmInfoSQL, query))
  179. if err != nil {
  180. return
  181. }
  182. defer rows.Close()
  183. for rows.Next() {
  184. up := &model.UpInfo{}
  185. err = rows.Scan(&up.MID, &up.Nickname, &up.BGMs, &up.BgmPlayCount, &up.BgmApplyCount, &up.Fans, &up.AccountState, &up.SignedAt, &up.ForbidAt, &up.QuitAt, &up.DismissAt, &up.ExpiredIn)
  186. if err != nil {
  187. return
  188. }
  189. ups = append(ups, up)
  190. }
  191. return
  192. }
  193. // Reject batch update reject
  194. func (d *Dao) Reject(c context.Context, table string, state int, reason string, rejectAt, expiredIn time.Time, mids []int64) (rows int64, err error) {
  195. res, err := d.rddb.Exec(c, fmt.Sprintf(_rejectUpsSQL, table, xstr.JoinInts(mids)), state, reason, rejectAt, expiredIn)
  196. if err != nil {
  197. return
  198. }
  199. return res.RowsAffected()
  200. }
  201. // Pass pass the apply
  202. func (d *Dao) Pass(c context.Context, table string, state int, signedAt time.Time, mids []int64) (rows int64, err error) {
  203. res, err := d.rddb.Exec(c, fmt.Sprintf(_passUpsSQL, table, xstr.JoinInts(mids)), state, signedAt)
  204. if err != nil {
  205. return
  206. }
  207. return res.RowsAffected()
  208. }
  209. // Dismiss dismiss up
  210. func (d *Dao) Dismiss(c context.Context, table string, newState, oldState int, reason string, dismissAt, quitAt time.Time, mid int64) (rows int64, err error) {
  211. res, err := d.rddb.Exec(c, fmt.Sprintf(_dismissUpSQL, table), newState, reason, dismissAt, quitAt, mid, oldState)
  212. if err != nil {
  213. return
  214. }
  215. return res.RowsAffected()
  216. }
  217. // TxDismiss tx dismiss up
  218. func (d *Dao) TxDismiss(tx *sql.Tx, table string, newState, oldState int, reason string, dismissAt, quitAt time.Time, mid int64) (rows int64, err error) {
  219. res, err := tx.Exec(fmt.Sprintf(_dismissUpSQL, table), newState, reason, dismissAt, quitAt, mid, oldState)
  220. if err != nil {
  221. return
  222. }
  223. return res.RowsAffected()
  224. }
  225. // Forbid forbid up
  226. func (d *Dao) Forbid(c context.Context, table string, newState, oldState int, reason string, forbidAt, expiredIn time.Time, mid int64) (rows int64, err error) {
  227. res, err := d.rddb.Exec(c, fmt.Sprintf(_forbidUpSQL, table), newState, reason, forbidAt, expiredIn, mid, oldState)
  228. if err != nil {
  229. return
  230. }
  231. return res.RowsAffected()
  232. }
  233. // TxForbid tx forbid up
  234. func (d *Dao) TxForbid(tx *sql.Tx, table string, newState, oldState int, reason string, forbidAt, expiredIn time.Time, mid int64) (rows int64, err error) {
  235. res, err := tx.Exec(fmt.Sprintf(_forbidUpSQL, table), newState, reason, forbidAt, expiredIn, mid, oldState)
  236. if err != nil {
  237. return
  238. }
  239. return res.RowsAffected()
  240. }
  241. // UpdateAccountState update up account state
  242. func (d *Dao) UpdateAccountState(c context.Context, table string, state int, mid int64) (rows int64, err error) {
  243. res, err := d.rddb.Exec(c, fmt.Sprintf(_updateAccStateSQL, table), state, mid)
  244. if err != nil {
  245. return
  246. }
  247. return res.RowsAffected()
  248. }
  249. // DelUpInfo soft delete up info
  250. func (d *Dao) DelUpInfo(c context.Context, table string, mid int64) (rows int64, err error) {
  251. return d.updateUpInfoDel(c, table, mid, 1)
  252. }
  253. // RecUpInfo recover up info from soft delete
  254. func (d *Dao) RecUpInfo(c context.Context, table string, mid int64) (rows int64, err error) {
  255. return d.updateUpInfoDel(c, table, mid, 0)
  256. }
  257. func (d *Dao) updateUpInfoDel(c context.Context, table string, mid int64, isDeleted int) (rows int64, err error) {
  258. res, err := d.rddb.Exec(c, fmt.Sprintf(_updateUpInfoDelSQL, table), isDeleted, mid)
  259. if err != nil {
  260. return
  261. }
  262. return res.RowsAffected()
  263. }
  264. // DelUpAccount update mid is_deleted = 1 in up_account
  265. func (d *Dao) DelUpAccount(c context.Context, mid int64) (rows int64, err error) {
  266. res, err := d.rddb.Exec(c, _delUpAccountSQL, mid)
  267. if err != nil {
  268. return
  269. }
  270. return res.RowsAffected()
  271. }
  272. // UpdateUpAccount update up_account
  273. func (d *Dao) UpdateUpAccount(c context.Context, mid int64, isDeleted int, withdrawDate string) (rows int64, err error) {
  274. res, err := d.rddb.Exec(c, fmt.Sprintf(_updateUpAccountSQL, withdrawDate), isDeleted, mid)
  275. if err != nil {
  276. return
  277. }
  278. return res.RowsAffected()
  279. }
  280. // DelCreditRecord soft del credit record by id
  281. func (d *Dao) DelCreditRecord(c context.Context, id int64) (rows int64, err error) {
  282. res, err := d.rddb.Exec(c, _delCreditRecordSQL, id)
  283. if err != nil {
  284. log.Error("db.delCreditRecordSQL.Exec(%s) error(%v)", _delCreditRecordSQL, err)
  285. return
  286. }
  287. return res.RowsAffected()
  288. }
  289. // TxDelCreditRecord tx soft del credit record by id
  290. func (d *Dao) TxDelCreditRecord(tx *sql.Tx, id int64) (rows int64, err error) {
  291. res, err := tx.Exec(_delCreditRecordSQL, id)
  292. if err != nil {
  293. log.Error("tx.delCreditRecordSQL.Exec(%s) error(%v)", _delCreditRecordSQL, err)
  294. return
  295. }
  296. return res.RowsAffected()
  297. }
  298. // UpInfo get up info by account_state and mid
  299. func (d *Dao) UpInfo(c context.Context, mid, state int64) (info *model.UpInfo, err error) {
  300. row := d.rddb.QueryRow(c, _upInfoSQL, mid, state)
  301. info = &model.UpInfo{}
  302. if err = row.Scan(&info.MID, &info.Nickname, &info.Fans, &info.SignedAt); err != nil {
  303. if err == sql.ErrNoRows {
  304. err = nil
  305. } else {
  306. log.Error("row.Scan error(%v)", err)
  307. }
  308. }
  309. return
  310. }
  311. // GetUpInfoByState get up_info by state
  312. func (d *Dao) GetUpInfoByState(c context.Context, table string, mids []int64, state int) (info map[int64]struct{}, err error) {
  313. info = make(map[int64]struct{})
  314. rows, err := d.rddb.Query(c, fmt.Sprintf(_upInfoStateSQL, table, xstr.JoinInts(mids)), state)
  315. if err != nil {
  316. return
  317. }
  318. defer rows.Close()
  319. for rows.Next() {
  320. var mid int64
  321. err = rows.Scan(&mid)
  322. if err != nil {
  323. log.Error("GetUpInfoByState rows.Scan error(%v)", err)
  324. return
  325. }
  326. info[mid] = struct{}{}
  327. }
  328. return
  329. }
  330. // GetUpState get up state
  331. func (d *Dao) GetUpState(c context.Context, table string, mid int64) (state int, err error) {
  332. err = d.rddb.QueryRow(c, fmt.Sprintf(_upStateSQL, table), mid).Scan(&state)
  333. if err == sql.ErrNoRows {
  334. state = 0
  335. err = nil
  336. }
  337. return
  338. }
  339. // BGMCount bgm count by mid
  340. func (d *Dao) BGMCount(c context.Context, mid int64) (count int, err error) {
  341. row := d.rddb.QueryRow(c, _bgmCountSQL, mid)
  342. if err = row.Scan(&count); err != nil {
  343. if err == sql.ErrNoRows {
  344. err = nil
  345. count = 0
  346. } else {
  347. log.Error("db.QueryRow(%s) error(%v)", _bgmCountSQL, err)
  348. }
  349. }
  350. return
  351. }