user.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "fmt"
  6. "go-common/app/service/main/vip/model"
  7. "go-common/library/database/sql"
  8. "go-common/library/time"
  9. "github.com/pkg/errors"
  10. )
  11. const (
  12. _vipUserInfoByMid = "SELECT id,mid,vip_type,vip_pay_type,vip_status,pay_channel_id,vip_start_time,vip_overdue_time,annual_vip_overdue_time,ctime,mtime,vip_recent_time,ios_overdue_time,ver FROM vip_user_info WHERE mid = ?;"
  13. _updateVipStatusTypeSQL = "UPDATE `vip_user_info` SET `vip_status`=? AND `vip_type`=? WHERE `mid`=? ;"
  14. _vipChangeHistorySQL = "SELECT id,mid,change_type,change_time,days,operator_id,relation_id,batch_id,remark FROM vip_user_change_history WHERE mid=? ORDER BY change_time DESC"
  15. _changeHistoryCountSQL = "SELECT COUNT(1) FROM vip_user_change_history WHERE mid=?"
  16. _updateChannelIDSQL = "UPDATE vip_user_info SET pay_channel_id=?,ver=? WHERE mid=? AND ver=?"
  17. _dupUserDiscountSQL = "INSERT INTO vip_user_discount_history(mid,discount_id,order_no,status) VALUES(?,?,?,?) ON DUPLICATE KEY UPDATE order_no= VALUES(order_no) ,status = VALUES(status);"
  18. _updatePayTypeSQL = "UPDATE vip_user_info SET vip_pay_type = ?,ver=? WHERE mid =? AND ver=?"
  19. _selVipChangeHistoryByRelationIDSQL = "SELECT id,mid,change_type,change_time,days,operator_id,relation_id,batch_id,remark,ctime FROM vip_user_change_history WHERE relation_id=?"
  20. _InsertVipChangeHistory = "INSERT INTO vip_user_change_history(mid,change_type,change_time,days,operator_id,relation_id,batch_id,remark,batch_code_id) VALUES(?,?,?,?,?,?,?,?,?)"
  21. _SelVipUserInfoByMid = "SELECT id,mid,vip_type,vip_pay_type,vip_status,ver,vip_start_time,vip_overdue_time,annual_vip_overdue_time,ctime,mtime,vip_recent_time,ios_overdue_time FROM vip_user_info WHERE mid = ?"
  22. _InsertVipUserInfo = "INSERT INTO vip_user_info(mid,vip_type,vip_pay_type,vip_status,vip_start_time,vip_overdue_time,annual_vip_overdue_time,vip_recent_time) VALUES(?,?,?,?,?,?,?,?)"
  23. _addIosVipUserInfo = "INSERT INTO vip_user_info(mid,vip_type,vip_status,vip_start_time,ios_overdue_time) VALUES(?,?,?,?,?)"
  24. _UpdateVipUserInfoByID = "UPDATE vip_user_info SET vip_type=?,vip_pay_type=?,vip_status=?,vip_overdue_time=?,annual_vip_overdue_time=?,vip_recent_time=?,ver=? WHERE mid=? AND ver=?"
  25. _updateIosUserInfoSQL = "UPDATE vip_user_info SET ios_overdue_time=? WHERE mid = ?"
  26. _updateIosRenewUserInfoSQL = "UPDATE vip_user_info SET vip_pay_type=?,ver=?,pay_channel_id=? WHERE mid = ? AND ver=?"
  27. _addUserDiscount = "INSERT IGNORE INTO vip_user_discount_history(mid,discount_id,order_no,status) VALUES(?,?,?,?)"
  28. //refund
  29. _oldUpdateVipUserInfoByID = "UPDATE vip_user_info SET vip_type=?,vip_status=?,vip_overdue_time=?,annual_vip_overdue_time=?,ver=? WHERE id=? AND ver=?"
  30. _oldSelVipUserInfoByMid = "SELECT id,mid,vip_type,vip_status,ver,vip_start_time,vip_overdue_time,annual_vip_overdue_time,vip_recent_time,ios_overdue_time FROM vip_user_info WHERE mid = ?"
  31. _oldAddVipChangeHistory = "INSERT INTO vip_change_history(mid,change_type,change_time,days,operator_id,relation_id,batch_id,remark,batch_code_id) VALUES(?,?,?,?,?,?,?,?,?)"
  32. //sync
  33. _syncAddUser = "INSERT INTO vip_user_info(mid,vip_type,vip_pay_type,vip_status,vip_start_time,vip_overdue_time,annual_vip_overdue_time,vip_recent_time,ios_overdue_time,pay_channel_id,ver) VALUES(?,?,?,?,?,?,?,?,?,?,?)"
  34. _syncUpdateUser = "UPDATE vip_user_info SET vip_type=?,vip_pay_type=?,vip_status=?,vip_overdue_time=?,annual_vip_overdue_time=?,vip_recent_time=?,ios_overdue_time=?,pay_channel_id=?,ver=? WHERE mid=? AND ver=?"
  35. //clean job cache
  36. _cleanjobSQL = "UPDATE vip_user_info SET ver = ver + 1 WHERE mid=?"
  37. )
  38. //VipInfo select user info by mid.
  39. func (d *Dao) VipInfo(c context.Context, mid int64) (r *model.VipInfoDB, err error) {
  40. var row = d.db.QueryRow(c, _vipUserInfoByMid, mid)
  41. r = new(model.VipInfoDB)
  42. if err = row.Scan(&r.ID, &r.Mid, &r.VipType, &r.VipPayType, &r.VipStatus, &r.PayChannelID, &r.VipStartTime, &r.VipOverdueTime, &r.AnnualVipOverdueTime,
  43. &r.Ctime, &r.Mtime, &r.VipRecentTime, &r.IosOverdueTime, &r.Ver); err != nil {
  44. if err == sql.ErrNoRows {
  45. r = nil
  46. err = nil
  47. } else {
  48. err = errors.WithStack(err)
  49. d.errProm.Incr("row_scan_db")
  50. }
  51. }
  52. return
  53. }
  54. // UpdateVipTypeAndStatus update vip type and status.
  55. func (d *Dao) UpdateVipTypeAndStatus(c context.Context, mid int64, vipStatus int32, vipType int32) (ret int64, err error) {
  56. var res xsql.Result
  57. if res, err = d.db.Exec(c, _updateVipStatusTypeSQL, vipStatus, vipType, mid); err != nil {
  58. err = errors.WithStack(err)
  59. d.errProm.Incr("exec_db")
  60. return
  61. }
  62. return res.RowsAffected()
  63. }
  64. //SelChangeHistoryCount .
  65. func (d *Dao) SelChangeHistoryCount(c context.Context, arg *model.ArgChangeHistory) (count int64, err error) {
  66. row := d.db.QueryRow(c, _changeHistoryCountSQL, arg.Mid)
  67. if err = row.Scan(&count); err != nil {
  68. if sql.ErrNoRows == err {
  69. err = nil
  70. count = 0
  71. }
  72. err = errors.WithStack(err)
  73. }
  74. return
  75. }
  76. //SelChangeHistory .
  77. func (d *Dao) SelChangeHistory(c context.Context, arg *model.ArgChangeHistory) (vcs []*model.VipChangeHistory, err error) {
  78. SQLStr := _vipChangeHistorySQL
  79. if arg.Pn > 0 && arg.Ps > 0 {
  80. SQLStr += fmt.Sprintf(" LIMIT %d,%d", (arg.Pn-1)*arg.Ps, arg.Ps)
  81. }
  82. var rows *sql.Rows
  83. if rows, err = d.db.Query(c, SQLStr, arg.Mid); err != nil {
  84. err = errors.WithStack(err)
  85. d.errProm.Incr("query_db")
  86. return
  87. }
  88. defer rows.Close()
  89. for rows.Next() {
  90. r := new(model.VipChangeHistory)
  91. if rows.Scan(&r.ID, &r.Mid, &r.ChangeType, &r.ChangeTime, &r.Days, &r.OperatorID, &r.RelationID, &r.BatchID, &r.Remark); err != nil {
  92. err = errors.WithStack(err)
  93. d.errProm.Incr("row_scan_db")
  94. vcs = nil
  95. return
  96. }
  97. vcs = append(vcs, r)
  98. }
  99. err = rows.Err()
  100. return
  101. }
  102. //TxUpdateChannelID .
  103. func (d *Dao) TxUpdateChannelID(tx *sql.Tx, mid int64, payChannelID int32, ver int64, oldVer int64) (err error) {
  104. if _, err = tx.Exec(_updateChannelIDSQL, payChannelID, ver, mid, oldVer); err != nil {
  105. err = errors.WithStack(err)
  106. return
  107. }
  108. return
  109. }
  110. //TxDupUserDiscount .
  111. func (d *Dao) TxDupUserDiscount(tx *sql.Tx, mid, discountID int64, orderNo string, status int8) (err error) {
  112. if _, err = tx.Exec(_dupUserDiscountSQL, mid, discountID, orderNo, status); err != nil {
  113. err = errors.WithStack(err)
  114. return
  115. }
  116. return
  117. }
  118. //UpdatePayType .
  119. func (d *Dao) UpdatePayType(tx *sql.Tx, mid int64, payType int8, ver, oldVer int64) (a int64, err error) {
  120. var result xsql.Result
  121. if result, err = tx.Exec(_updatePayTypeSQL, payType, ver, mid, oldVer); err != nil {
  122. err = errors.WithStack(err)
  123. return
  124. }
  125. if a, err = result.RowsAffected(); err != nil {
  126. err = errors.WithStack(err)
  127. return
  128. }
  129. return
  130. }
  131. //SelVipChangeHistory select vip_change_history by relationId
  132. func (d *Dao) SelVipChangeHistory(c context.Context, relationID string) (r *model.VipChangeHistory, err error) {
  133. var row = d.db.QueryRow(c, _selVipChangeHistoryByRelationIDSQL, relationID)
  134. r = new(model.VipChangeHistory)
  135. if err = row.Scan(&r.ID, &r.Mid, &r.ChangeType, &r.ChangeTime, &r.Days, &r.OperatorID, &r.RelationID, &r.BatchID, &r.Remark, &r.Ctime); err != nil {
  136. if err == sql.ErrNoRows {
  137. r = nil
  138. err = nil
  139. } else {
  140. err = errors.WithStack(err)
  141. d.errProm.Incr("scan_error")
  142. }
  143. }
  144. return
  145. }
  146. //InsertVipChangeHistory insert vipChangeHistory
  147. func (d *Dao) InsertVipChangeHistory(tx *sql.Tx, r *model.VipChangeHistory) (id int64, err error) {
  148. var res xsql.Result
  149. if res, err = tx.Exec(_InsertVipChangeHistory, r.Mid, r.ChangeType, r.ChangeTime, r.Days, r.OperatorID, r.RelationID, r.BatchID, r.Remark, r.BatchCodeID); err != nil {
  150. err = errors.WithStack(err)
  151. } else {
  152. if id, err = res.LastInsertId(); err != nil {
  153. err = errors.WithStack(err)
  154. }
  155. }
  156. return
  157. }
  158. //TxSelVipUserInfo .
  159. func (d *Dao) TxSelVipUserInfo(tx *sql.Tx, mid int64) (r *model.VipInfoDB, err error) {
  160. var row = tx.QueryRow(_SelVipUserInfoByMid, mid)
  161. r = new(model.VipInfoDB)
  162. if err = row.Scan(&r.ID, &r.Mid, &r.VipType, &r.VipPayType, &r.VipStatus, &r.Ver, &r.VipStartTime, &r.VipOverdueTime, &r.AnnualVipOverdueTime, &r.Ctime, &r.Mtime, &r.VipRecentTime, &r.IosOverdueTime); err != nil {
  163. if err == sql.ErrNoRows {
  164. r = nil
  165. err = nil
  166. } else {
  167. err = errors.WithStack(err)
  168. }
  169. }
  170. return
  171. }
  172. // TxAddIosVipUserInfo tx add ios vip user info.
  173. func (d *Dao) TxAddIosVipUserInfo(tx *sql.Tx, r *model.VipInfoDB) (eff int64, err error) {
  174. var result xsql.Result
  175. if result, err = tx.Exec(_addIosVipUserInfo, r.Mid, r.VipType, r.VipStatus, r.VipStartTime, r.IosOverdueTime); err != nil {
  176. err = errors.WithStack(err)
  177. return
  178. }
  179. if eff, err = result.RowsAffected(); err != nil {
  180. err = errors.WithStack(err)
  181. }
  182. return
  183. }
  184. //InsertVipUserInfo insert vipUserInfo
  185. func (d *Dao) InsertVipUserInfo(tx *sql.Tx, r *model.VipInfoDB) (err error) {
  186. if _, err = tx.Exec(_InsertVipUserInfo, r.Mid, r.VipType, r.VipPayType, r.VipStatus, r.VipStartTime, r.VipOverdueTime, r.AnnualVipOverdueTime, r.VipRecentTime); err != nil {
  187. err = errors.WithStack(err)
  188. }
  189. return
  190. }
  191. //TxUpdateIosUserInfo update ios time
  192. func (d *Dao) TxUpdateIosUserInfo(tx *sql.Tx, iosTime time.Time, mid int64) (eff int64, err error) {
  193. var result xsql.Result
  194. if result, err = tx.Exec(_updateIosUserInfoSQL, iosTime, mid); err != nil {
  195. err = errors.WithStack(err)
  196. return
  197. }
  198. if eff, err = result.RowsAffected(); err != nil {
  199. err = errors.WithStack(err)
  200. }
  201. return
  202. }
  203. //TxUpdateIosRenewUserInfo .
  204. func (d *Dao) TxUpdateIosRenewUserInfo(tx *sql.Tx, paychannelID, ver, oldVer, mid int64, payType int8) (err error) {
  205. if _, err = tx.Exec(_updateIosRenewUserInfoSQL, payType, ver, paychannelID, mid, oldVer); err != nil {
  206. err = errors.WithStack(err)
  207. return
  208. }
  209. return
  210. }
  211. //UpdateVipUserInfo update vip user info by id
  212. func (d *Dao) UpdateVipUserInfo(tx *sql.Tx, r *model.VipInfoDB, ver int64) (a int64, err error) {
  213. var res xsql.Result
  214. if res, err = tx.Exec(_UpdateVipUserInfoByID, r.VipType, r.VipPayType, r.VipStatus, r.VipOverdueTime, r.AnnualVipOverdueTime, r.VipRecentTime, r.Ver, r.Mid, ver); err != nil {
  215. err = errors.WithStack(err)
  216. } else {
  217. if a, err = res.RowsAffected(); err != nil {
  218. err = errors.WithStack(err)
  219. }
  220. }
  221. return
  222. }
  223. // TxAddUserDiscount add user discount history.
  224. func (d *Dao) TxAddUserDiscount(tx *sql.Tx, r *model.VipUserDiscountHistory) (eff int64, err error) {
  225. var res xsql.Result
  226. if res, err = tx.Exec(_addUserDiscount, r.Mid, r.DiscountID, r.OrderNo, r.Status); err != nil {
  227. err = errors.WithStack(err)
  228. return
  229. }
  230. eff, err = res.RowsAffected()
  231. return
  232. }
  233. //SyncAddUser insert vipUserInfo
  234. func (d *Dao) SyncAddUser(tx *sql.Tx, r *model.VipInfoDB) (err error) {
  235. if _, err = tx.Exec(_syncAddUser, r.Mid, r.VipType, r.VipPayType, r.VipStatus, r.VipStartTime, r.VipOverdueTime, r.AnnualVipOverdueTime, r.VipRecentTime, r.IosOverdueTime, r.PayChannelID, r.Ver); err != nil {
  236. err = errors.WithStack(err)
  237. }
  238. return
  239. }
  240. //SyncUpdateUser insert vipUserInfo
  241. func (d *Dao) SyncUpdateUser(tx *sql.Tx, r *model.VipInfoDB, ver int64) (eff int64, err error) {
  242. var res xsql.Result
  243. if res, err = tx.Exec(_syncUpdateUser, r.VipType, r.VipPayType, r.VipStatus, r.VipOverdueTime, r.AnnualVipOverdueTime, r.VipRecentTime, r.IosOverdueTime, r.PayChannelID, r.Ver, r.Mid, ver); err != nil {
  244. err = errors.WithStack(err)
  245. return
  246. }
  247. if eff, err = res.RowsAffected(); err != nil {
  248. err = errors.WithStack(err)
  249. }
  250. return
  251. }
  252. //OldTxSelVipUserInfo .
  253. func (d *Dao) OldTxSelVipUserInfo(tx *sql.Tx, mid int64) (r *model.VipInfoDB, err error) {
  254. var row = tx.QueryRow(_oldSelVipUserInfoByMid, mid)
  255. r = new(model.VipInfoDB)
  256. if err = row.Scan(&r.ID, &r.Mid, &r.VipType, &r.VipStatus, &r.Ver, &r.VipStartTime, &r.VipOverdueTime, &r.AnnualVipOverdueTime, &r.VipRecentTime, &r.IosOverdueTime); err != nil {
  257. if err == sql.ErrNoRows {
  258. r = nil
  259. err = nil
  260. } else {
  261. err = errors.WithStack(err)
  262. }
  263. }
  264. return
  265. }
  266. //OldTxUpdateVipUserInfo .
  267. func (d *Dao) OldTxUpdateVipUserInfo(tx *sql.Tx, r *model.VipInfoDB, ver int64) (eff int64, err error) {
  268. var res xsql.Result
  269. if res, err = tx.Exec(_oldUpdateVipUserInfoByID, r.VipType, r.VipStatus, r.VipOverdueTime, r.AnnualVipOverdueTime, r.Ver, r.ID, ver); err != nil {
  270. err = errors.WithStack(err)
  271. return
  272. }
  273. eff, err = res.RowsAffected()
  274. return
  275. }
  276. //OldTxAddChangeHistory .
  277. func (d *Dao) OldTxAddChangeHistory(tx *sql.Tx, r *model.VipChangeHistory) (err error) {
  278. if _, err = tx.Exec(_oldAddVipChangeHistory, r.Mid, r.ChangeType, r.ChangeTime, r.Days, r.OperatorID, r.RelationID, r.BatchID, r.Remark, r.BatchCodeID); err != nil {
  279. err = errors.WithStack(err)
  280. }
  281. return
  282. }
  283. //CleanCache clean job cache.
  284. func (d *Dao) CleanCache(c context.Context, mid int64) (err error) {
  285. if _, err = d.db.Exec(c, _cleanjobSQL, mid); err != nil {
  286. err = errors.WithStack(err)
  287. }
  288. return
  289. }