up.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/interface/main/growup/model"
  6. "go-common/library/database/sql"
  7. "go-common/library/log"
  8. "go-common/library/time"
  9. )
  10. const (
  11. // insert
  12. _inUpsSQL = "INSERT INTO %s (mid,nickname,account_type,category_id,fans,account_state,sign_type,apply_at,%s,is_deleted) VALUES (?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE nickname=?,account_type=?,category_id=?,fans=?,account_state=?,sign_type=?,apply_at=?,%s=?,is_deleted=?"
  13. _inUpBgmSQL = "INSERT INTO up_info_bgm(mid,nickname,bgms,account_type,fans,account_state,sign_type,signed_at,is_deleted) VALUES(?,?,?,?,?,3,?,?,0) ON DUPLICATE KEY UPDATE nickname=VALUES(nickname),bgms=VALUES(bgms),account_type=VALUES(account_type),fans=VALUES(fans),account_state=3,signed_at=VALUES(signed_at),sign_type=VALUES(sign_type),is_deleted=0"
  14. _inCreditRecordSQL = "INSERT INTO credit_score_record (mid,operate_at,operator,reason,deducted,remaining) VALUES(?,?,?,?,?,?)"
  15. _inCreditScoreSQL = "INSERT INTO credit_score(mid) VALUES(?) ON DUPLICATE KEY UPDATE mid=VALUES(mid)"
  16. // select
  17. _upsCateInfoSQL = "SELECT nick_name,main_category_id FROM up_category_info WHERE mid=?"
  18. _upsStatInfoSQL = "SELECT fans FROM up_base_statistics WHERE mid=?"
  19. _blockMIDSQL = "SELECT mid FROM up_blocked WHERE mid=? AND is_deleted=0"
  20. _upNicknameSQL = "SELECT nickname FROM up_info_video WHERE mid = ?"
  21. _upCreditScoreSQL = "SELECT score FROM credit_score WHERE mid = ?"
  22. _accountStateSQL = "SELECT account_state FROM %s WHERE mid = ? AND is_deleted = 0"
  23. _upSignedAtSQL = "SELECT signed_at FROM %s WHERE mid = ? AND account_state = 3 AND is_deleted = 0"
  24. // will delete next version
  25. _upWhiteListSQL = "SELECT type FROM up_white_list WHERE mid=? AND is_deleted=0"
  26. // _upsArchiveSQL = "SELECT account_type,account_state,reason,expired_in,quit_at,ctime FROM up_info_video WHERE mid=? AND is_deleted=0"
  27. _avAccountStateSQL = "SELECT account_type,account_state,reason,expired_in,quit_at,ctime FROM up_info_video WHERE mid=? AND is_deleted=0"
  28. _bgmAccountStateSQL = "SELECT account_type,account_state,reason,expired_in,quit_at,ctime FROM up_info_bgm WHERE mid=? AND is_deleted=0"
  29. _columnAccountStateSQL = "SELECT account_type,account_state,reason,expired_in,quit_at,ctime FROM up_info_column WHERE mid=? AND is_deleted=0"
  30. _bgmUpCountSQL = "SELECT count(*) FROM background_music WHERE mid=?"
  31. // update
  32. _upQuitSQL = "UPDATE %s SET account_state=5,quit_at=?,expired_in=?,reason=? WHERE mid=? AND account_state=3 AND is_deleted=0"
  33. _deductCreditScoreSQL = "UPDATE credit_score SET score=score-%d WHERE mid=?"
  34. )
  35. // GetAccountState account state
  36. func (d *Dao) GetAccountState(c context.Context, table string, mid int64) (state int, err error) {
  37. row := d.db.QueryRow(c, fmt.Sprintf(_accountStateSQL, table), mid)
  38. if err = row.Scan(&state); err != nil {
  39. if err == sql.ErrNoRows {
  40. err = nil
  41. } else {
  42. log.Error("row scan error(%v)", err)
  43. }
  44. }
  45. return
  46. }
  47. // GetUpSignedAt get up signed_at
  48. func (d *Dao) GetUpSignedAt(c context.Context, table string, mid int64) (signedAt time.Time, err error) {
  49. if err = d.db.QueryRow(c, fmt.Sprintf(_upSignedAtSQL, table), mid).Scan(&signedAt); err != nil {
  50. if err == sql.ErrNoRows {
  51. err = nil
  52. signedAt = 0
  53. } else {
  54. log.Error("row scan error(%v)", err)
  55. }
  56. }
  57. return
  58. }
  59. // InsertUpInfo add upinfo
  60. func (d *Dao) InsertUpInfo(c context.Context, table string, totalCountField string, v *model.UpInfo) (rows int64, err error) {
  61. res, err := d.db.Exec(c, fmt.Sprintf(_inUpsSQL, table, totalCountField, totalCountField), v.MID, v.Nickname, v.AccountType, v.MainCategory, v.Fans, v.AccountState, v.SignType, v.ApplyAt, v.TotalPlayCount, 0, v.Nickname, v.AccountType, v.MainCategory, v.Fans, v.AccountState, v.SignType, v.ApplyAt, v.TotalPlayCount, 0)
  62. if err != nil {
  63. log.Error("db.inUpsStmt.Exec(%s) error(%v)", _inUpsSQL, err)
  64. return
  65. }
  66. return res.RowsAffected()
  67. }
  68. // TxInsertBgmUpInfo insert bgm up info
  69. func (d *Dao) TxInsertBgmUpInfo(tx *sql.Tx, v *model.UpInfo) (rows int64, err error) {
  70. res, err := tx.Exec(_inUpBgmSQL, v.MID, v.Nickname, v.Bgms, v.AccountType, v.Fans, v.SignType, v.SignedAt)
  71. if err != nil {
  72. log.Error("db.inBgmUpStmt.Exec(%s) error(%v)", _inUpsSQL, err)
  73. return
  74. }
  75. return res.RowsAffected()
  76. }
  77. // TxInsertCreditScore insert credit score
  78. func (d *Dao) TxInsertCreditScore(tx *sql.Tx, mid int64) (rows int64, err error) {
  79. res, err := tx.Exec(_inCreditScoreSQL, mid)
  80. if err != nil {
  81. log.Error("db.Exec(%s) error(%v)", _inCreditScoreSQL, err)
  82. return
  83. }
  84. return res.RowsAffected()
  85. }
  86. // Blocked query mid in blacklist
  87. func (d *Dao) Blocked(c context.Context, mid int64) (id int64, err error) {
  88. row := d.db.QueryRow(c, _blockMIDSQL, mid)
  89. if err = row.Scan(&id); err != nil {
  90. if err == sql.ErrNoRows {
  91. err = nil
  92. } else {
  93. log.Error("row.Scan error(%v)", err)
  94. }
  95. }
  96. return
  97. }
  98. // White map: key: type value: bool
  99. func (d *Dao) White(c context.Context, mid int64) (m map[int]bool, err error) {
  100. m = make(map[int]bool)
  101. rows, err := d.db.Query(c, _upWhiteListSQL, mid)
  102. if err != nil {
  103. log.Error("row.Query error(%v)", err)
  104. return
  105. }
  106. defer rows.Close()
  107. for rows.Next() {
  108. var typ int
  109. err = rows.Scan(&typ)
  110. if err != nil {
  111. log.Error("rows.Scan error(%v)", err)
  112. }
  113. m[typ] = true
  114. }
  115. return
  116. }
  117. // AvUpStatus return av up status
  118. func (d *Dao) AvUpStatus(c context.Context, mid int64) (status *model.BusinessStatus, err error) {
  119. status = &model.BusinessStatus{}
  120. row := d.db.QueryRow(c, _avAccountStateSQL, mid)
  121. if err = row.Scan(&status.AccountType, &status.AccountState, &status.Reason, &status.ExpiredIn, &status.QuitAt, &status.CTime); err != nil {
  122. if err == sql.ErrNoRows {
  123. err = nil
  124. } else {
  125. log.Error("row.Scan error(%v)", err)
  126. }
  127. }
  128. return
  129. }
  130. // BgmUpStatus return bgm up status
  131. func (d *Dao) BgmUpStatus(c context.Context, mid int64) (status *model.BusinessStatus, err error) {
  132. status = &model.BusinessStatus{}
  133. row := d.db.QueryRow(c, _bgmAccountStateSQL, mid)
  134. if err = row.Scan(&status.AccountType, &status.AccountState, &status.Reason, &status.ExpiredIn, &status.QuitAt, &status.CTime); err != nil {
  135. if err == sql.ErrNoRows {
  136. err = nil
  137. } else {
  138. log.Error("row.Scan error(%v)", err)
  139. }
  140. }
  141. return
  142. }
  143. // ColumnUpStatus return bgm up status
  144. func (d *Dao) ColumnUpStatus(c context.Context, mid int64) (status *model.BusinessStatus, err error) {
  145. status = &model.BusinessStatus{}
  146. row := d.db.QueryRow(c, _columnAccountStateSQL, mid)
  147. if err = row.Scan(&status.AccountType, &status.AccountState, &status.Reason, &status.ExpiredIn, &status.QuitAt, &status.CTime); err != nil {
  148. if err == sql.ErrNoRows {
  149. err = nil
  150. } else {
  151. log.Error("row.Scan error(%v)", err)
  152. }
  153. }
  154. return
  155. }
  156. // CategoryInfo return nickname & categoryID
  157. func (d *Dao) CategoryInfo(c context.Context, mid int64) (nickname string, categoryID int, err error) {
  158. row := d.rddb.QueryRow(c, _upsCateInfoSQL, mid)
  159. if err = row.Scan(&nickname, &categoryID); err != nil {
  160. if err == sql.ErrNoRows {
  161. err = nil
  162. } else {
  163. log.Error("row.Scan error(%v)", err)
  164. }
  165. }
  166. return
  167. }
  168. // Fans return fans count
  169. func (d *Dao) Fans(c context.Context, mid int64) (fans int, err error) {
  170. row := d.rddb.QueryRow(c, _upsStatInfoSQL, mid)
  171. if err = row.Scan(&fans); err != nil {
  172. if err == sql.ErrNoRows {
  173. err = nil
  174. } else {
  175. log.Error("row.Scan error(%v)", err)
  176. }
  177. }
  178. return
  179. }
  180. // TxQuit update up status
  181. func (d *Dao) TxQuit(tx *sql.Tx, table string, mid int64, quitAt, expiredIn time.Time, reason string) (rows int64, err error) {
  182. res, err := tx.Exec(fmt.Sprintf(_upQuitSQL, table), quitAt, expiredIn, reason, mid)
  183. if err != nil {
  184. log.Error("db.TxQuit.Exec() error(%v)", err)
  185. return
  186. }
  187. return res.RowsAffected()
  188. }
  189. // TxInsertCreditRecord tx insert credit deduct record
  190. func (d *Dao) TxInsertCreditRecord(tx *sql.Tx, cr *model.CreditRecord) (rows int64, err error) {
  191. res, err := tx.Exec(_inCreditRecordSQL, cr.MID, cr.OperateAt, cr.Operator, cr.Reason, cr.Deducted, cr.Remaining)
  192. if err != nil {
  193. log.Error("db.TxInsertCreditRecord.Exec() error(%v)", err)
  194. return
  195. }
  196. return res.RowsAffected()
  197. }
  198. // Nickname get nickname from up_info_video
  199. func (d *Dao) Nickname(c context.Context, mid int64) (nickname string, err error) {
  200. row := d.rddb.QueryRow(c, _upNicknameSQL, mid)
  201. if err = row.Scan(&nickname); err != nil {
  202. if err == sql.ErrNoRows {
  203. err = nil
  204. } else {
  205. log.Error("row.Scan error(%v)", err)
  206. }
  207. }
  208. return
  209. }
  210. // CreditScore get current credit score from up_info_video
  211. func (d *Dao) CreditScore(c context.Context, mid int64) (score int, err error) {
  212. row := d.rddb.QueryRow(c, _upCreditScoreSQL, mid)
  213. if err = row.Scan(&score); err != nil {
  214. if err == sql.ErrNoRows {
  215. err = nil
  216. } else {
  217. log.Error("row.Scan error(%v)", err)
  218. }
  219. }
  220. return
  221. }
  222. // BgmUpCount bgm up count in table background_music
  223. func (d *Dao) BgmUpCount(c context.Context, mid int64) (count int, err error) {
  224. row := d.db.QueryRow(c, _bgmUpCountSQL, mid)
  225. if err = row.Scan(&count); err != nil {
  226. if err == sql.ErrNoRows {
  227. err = nil
  228. count = 0
  229. } else {
  230. log.Error("db.QueryRow(%s) error(%v)", _bgmUpCountSQL, err)
  231. }
  232. }
  233. return
  234. }
  235. // TxDeductCreditScore tx update credit score
  236. func (d *Dao) TxDeductCreditScore(tx *sql.Tx, score int, mid int64) (rows int64, err error) {
  237. res, err := tx.Exec(fmt.Sprintf(_deductCreditScoreSQL, score), mid)
  238. if err != nil {
  239. log.Error("tx.Exec(%s) error(%v)", _deductCreditScoreSQL, err)
  240. return
  241. }
  242. return res.RowsAffected()
  243. }