mysql.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package dao
  2. import (
  3. "context"
  4. xsql "database/sql"
  5. "go-common/app/service/main/vip/model"
  6. "go-common/library/database/sql"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _selVipUserInfoByMid = "select id,mid,vip_type,vip_status,vip_start_time,vip_overdue_time,annual_vip_overdue_time,wander,access_status,ctime,mtime,vip_recent_time from vip_user_info where mid = ?"
  11. _updateVipUserInfoByID = "update vip_user_info set vip_type=?,vip_status=?,vip_overdue_time=?,annual_vip_overdue_time=?,vip_recent_time=? where id=?"
  12. _insertVipUserInfo = "INSERT INTO vip_user_info(mid,vip_type,vip_status,vip_start_time,vip_overdue_time,annual_vip_overdue_time,wander,access_status,vip_recent_time) values(?,?,?,?,?,?,?,?,?)"
  13. _insertVipChangeHistory = "INSERT INTO vip_change_history(mid,change_type,change_time,days,month,operator_id,relation_id,batch_id,remark) values(?,?,?,?,?,?,?,?,?)"
  14. _selVipChangeHistory = "select id,mid,change_type,change_time,days,month,operator_id,relation_id,batch_id,remark,ctime from vip_change_history where relation_id=? AND batch_id=?"
  15. _selVipResourceBatchByID = "select id,pool_id,unit,count,ver,start_time,end_time,surplus_count,code_use_count,direct_use_count,ctime,mtime from vip_resource_batch where id=?"
  16. _updateBatchCountByID = "update vip_resource_batch set ver=ver+1,surplus_count=surplus_count-1,direct_use_count=direct_use_count+1,code_use_count=? where id=? and surplus_count>0"
  17. _selResourcePoolByID = "select id,pool_name,business_id,business_name,reason,code_expire_time,start_time,end_time,contacts,contacts_number,ctime,mtime from vip_resource_pool where id=?"
  18. _selBusinessByID = "select id,business_name,business_type,status,app_key,secret,contacts,contacts_number,ctime,mtime from vip_business_info where id=?"
  19. _selVipAppInfo = "select id,type,name,app_key,purge_url,ctime,mtime from vip_app_info where type=?"
  20. _selLastBCoinByMid = "select id,mid,status,give_now_status,month,amount,memo,ctime,mtime from vip_bcoin_salary where mid = ? order by month desc"
  21. _selAllConfig = "select id,config_key,content from vip_config"
  22. _insertVipBcoinSalary = "INSERT INTO vip_bcoin_salary(mid,status,give_now_status,month,amount,memo) values(?,?,?,?,?,?)"
  23. //resouce sql
  24. _selResourcePoolByIDSQL = "SELECT id,pool_name,business_id,reason,code_expire_time,start_time,end_time,contacts,contacts_number,ctime,mtime FROM vip_resource_pool WHERE id=?"
  25. _selBusinessByIDSQL = "SELECT id,business_name,business_type,status,app_key,secret,contacts,contacts_number,ctime,mtime FROM vip_business_info WHERE id=?"
  26. _selBusinessByAppkeySQL = "SELECT id,business_name,business_type,status,app_key,secret,contacts,contacts_number,ctime,mtime FROM vip_business_info WHERE app_key=?"
  27. _selCodeSQL = "SELECT id,batch_code_id,status,code,mid,use_time,relation_id FROM vip_resource_code WHERE code = ?"
  28. _selCodesSQL = "SELECT id,batch_code_id,status,code FROM vip_resource_code WHERE code IN ('%v')"
  29. _selCodeOpenedSQL = "SELECT id,code,use_time FROM vip_resource_code WHERE status = 2 AND batch_code_id IN(%v) AND id>%d AND use_time>='%s' AND use_time<='%s' ORDER BY id ASC LIMIT %d"
  30. _selBatchCodeSQL = "SELECT id,business_id,pool_id,status,batch_name,reason,unit,count,surplus_count,price,start_time,end_time,type,limit_day,max_count FROM vip_resource_batch_code WHERE id = ?"
  31. _selBatchCodeByBisSQL = "SELECT id,business_id,pool_id,status,batch_name,reason,unit,count,surplus_count,price,start_time,end_time,type,limit_day,max_count FROM vip_resource_batch_code WHERE business_id = ?"
  32. _selBatchCodesSQL = "SELECT id,business_id,pool_id,status,batch_name,reason,unit,count,surplus_count,price,start_time,end_time,type,limit_day,max_count FROM vip_resource_batch_code WHERE id IN(%v)"
  33. _updateCodeSQL = "UPDATE vip_resource_code SET mid=?,use_time=? WHERE id = ?"
  34. _updateCodeStatusSQL = "UPDATE vip_resource_code SET status=? WHERE id = ?"
  35. _updateBatchCodeSQL = "UPDATE vip_resource_batch_code SET surplus_count=? WHERE id = ?"
  36. _selActiveSQL = "SELECT id,type,product_name,product_pic,relation_id,bus_id,product_detail,use_type FROM vip_active_show WHERE relation_id IN ('%v')"
  37. _selCodesByBmidSQL = "SELECT code FROM vip_resource_code WHERE bmid =?"
  38. _selBatchCountSQL = "SELECT COUNT(1) FROM vip_resource_code WHERE mid=? AND batch_code_id=?"
  39. _updateOrderCancelSQL = "UPDATE vip_pay_order SET status = ? WHERE order_no = ? AND status = ?;"
  40. )
  41. //OldUpdateOrderCancel order update order cancel.
  42. func (d *Dao) OldUpdateOrderCancel(c context.Context, r *model.VipPayOrderOld) (a int64, err error) {
  43. var res xsql.Result
  44. if res, err = d.olddb.Exec(c, _updateOrderCancelSQL, r.Status, r.OrderNo, model.PAYING); err != nil {
  45. err = errors.WithStack(err)
  46. return
  47. }
  48. if a, err = res.RowsAffected(); err != nil {
  49. err = errors.WithStack(err)
  50. }
  51. return
  52. }
  53. //OldInsertVipBcoinSalary insert vip bcoin salary
  54. func (d *Dao) OldInsertVipBcoinSalary(c context.Context, r *model.VipBcoinSalary) (err error) {
  55. if _, err = d.olddb.Exec(c, _insertVipBcoinSalary, &r.Mid, &r.Status, &r.GiveNowStatus, &r.Month, &r.Amount, &r.Memo); err != nil {
  56. err = errors.WithStack(err)
  57. }
  58. return
  59. }
  60. //SelAllConfig sel all config
  61. func (d *Dao) SelAllConfig(c context.Context) (res []*model.VipConfig, err error) {
  62. var rows *sql.Rows
  63. if rows, err = d.olddb.Query(c, _selAllConfig); err != nil {
  64. err = errors.WithStack(err)
  65. return
  66. }
  67. defer rows.Close()
  68. for rows.Next() {
  69. r := new(model.VipConfig)
  70. if err = rows.Scan(&r.ID, &r.ConfigKey, &r.Content); err != nil {
  71. err = errors.WithStack(err)
  72. res = nil
  73. return
  74. }
  75. res = append(res, r)
  76. }
  77. err = rows.Err()
  78. return
  79. }
  80. //SelVipAppInfo selVipAppInfo
  81. func (d *Dao) SelVipAppInfo(c context.Context, t int) (res []*model.VipAppInfo, err error) {
  82. var rows *sql.Rows
  83. if rows, err = d.olddb.Query(c, _selVipAppInfo, t); err != nil {
  84. err = errors.WithStack(err)
  85. return
  86. }
  87. defer rows.Close()
  88. for rows.Next() {
  89. r := new(model.VipAppInfo)
  90. if err = rows.Scan(&r.ID, &r.Type, &r.Name, &r.AppKey, &r.PurgeURL, &r.Ctime, &r.Mtime); err != nil {
  91. err = errors.WithStack(err)
  92. res = nil
  93. return
  94. }
  95. res = append(res, r)
  96. }
  97. err = rows.Err()
  98. return
  99. }
  100. //OldSelLastBcoin sel last bcoin by mid
  101. func (d *Dao) OldSelLastBcoin(c context.Context, mid int64) (r *model.VipBcoinSalary, err error) {
  102. row := d.olddb.QueryRow(c, _selLastBCoinByMid, mid)
  103. r = new(model.VipBcoinSalary)
  104. if err = row.Scan(&r.ID, &r.Mid, &r.Status, &r.GiveNowStatus, &r.Month, &r.Amount, &r.Memo, &r.Ctime, &r.Mtime); err != nil {
  105. if err == sql.ErrNoRows {
  106. err = nil
  107. r = nil
  108. } else {
  109. err = errors.WithStack(err)
  110. }
  111. }
  112. return
  113. }
  114. //SelBusiness select businessInfo by id
  115. func (d *Dao) SelBusiness(c context.Context, id int64) (r *model.VipBusinessInfo, err error) {
  116. var row = d.olddb.QueryRow(c, _selBusinessByID, id)
  117. r = new(model.VipBusinessInfo)
  118. if err = row.Scan(&r.ID, &r.BusinessName, &r.BusinessType, &r.Status, &r.AppKey, &r.Secret, &r.Contacts, &r.ContactsNumber, &r.Ctime, &r.Mtime); err != nil {
  119. if err == sql.ErrNoRows {
  120. err = nil
  121. r = nil
  122. return
  123. }
  124. err = errors.WithStack(err)
  125. }
  126. return
  127. }
  128. //SelResourcePool select resource pool by id
  129. func (d *Dao) SelResourcePool(c context.Context, id int64) (r *model.VipResourcePool, err error) {
  130. var row = d.olddb.QueryRow(c, _selResourcePoolByID, id)
  131. r = new(model.VipResourcePool)
  132. if err = row.Scan(&r.ID, &r.PoolName, &r.BusinessID, &r.BusinessName, &r.Reason, &r.CodeExpireTime, &r.StartTime, &r.EndTime, &r.Contacts, &r.ContactsNumber, &r.Ctime, &r.Mtime); err != nil {
  133. if err == sql.ErrNoRows {
  134. r = nil
  135. err = nil
  136. return
  137. }
  138. err = errors.WithStack(err)
  139. }
  140. return
  141. }
  142. //OldSelVipUserInfo select user info by mid
  143. func (d *Dao) OldSelVipUserInfo(c context.Context, mid int64) (r *model.VipUserInfo, err error) {
  144. var row = d.olddb.QueryRow(c, _selVipUserInfoByMid, mid)
  145. r = new(model.VipUserInfo)
  146. if err = row.Scan(&r.ID, &r.Mid, &r.VipType, &r.VipStatus, &r.VipStartTime, &r.VipOverdueTime, &r.AnnualVipOverdueTime, &r.Wander, &r.AccessStatus, &r.Ctime, &r.Mtime, &r.VipRecentTime); err != nil {
  147. if err == sql.ErrNoRows {
  148. r = nil
  149. err = nil
  150. } else {
  151. err = errors.WithStack(err)
  152. }
  153. }
  154. return
  155. }
  156. //SelVipResourceBatch select vip resource Batch by id
  157. func (d *Dao) SelVipResourceBatch(c context.Context, id int64) (r *model.VipResourceBatch, err error) {
  158. var row = d.olddb.QueryRow(c, _selVipResourceBatchByID, id)
  159. r = new(model.VipResourceBatch)
  160. if err = row.Scan(&r.ID, &r.PoolID, &r.Unit, &r.Count, &r.Ver, &r.StartTime, &r.EndTime, &r.SurplusCount, &r.CodeUseCount, &r.DirectUseCount, &r.Ctime, &r.Mtime); err != nil {
  161. if err == sql.ErrNoRows {
  162. r = nil
  163. err = nil
  164. return
  165. }
  166. err = errors.WithStack(err)
  167. }
  168. return
  169. }
  170. //OldUpdateVipUserInfo update vip user info by id
  171. func (d *Dao) OldUpdateVipUserInfo(c context.Context, tx *sql.Tx, r *model.VipUserInfo) (a int64, err error) {
  172. var res xsql.Result
  173. if res, err = tx.Exec(_updateVipUserInfoByID, r.VipType, r.VipStatus, r.VipOverdueTime, r.AnnualVipOverdueTime, r.VipRecentTime, r.ID); err != nil {
  174. err = errors.WithStack(err)
  175. } else {
  176. if a, err = res.RowsAffected(); err != nil {
  177. err = errors.WithStack(err)
  178. }
  179. }
  180. return
  181. }
  182. //UpdateBatchCount updateBatch by Id
  183. func (d *Dao) UpdateBatchCount(c context.Context, tx *sql.Tx, r *model.VipResourceBatch, ver int64) (a int64, err error) {
  184. var res xsql.Result
  185. if res, err = tx.Exec(_updateBatchCountByID, r.CodeUseCount, r.ID); err != nil {
  186. err = errors.WithStack(err)
  187. } else {
  188. if a, err = res.RowsAffected(); err != nil {
  189. err = errors.WithStack(err)
  190. }
  191. }
  192. return
  193. }
  194. //OldInsertVipUserInfo insert vipUserInfo.
  195. func (d *Dao) OldInsertVipUserInfo(c context.Context, tx *sql.Tx, r *model.VipUserInfo) (err error) {
  196. if _, err = tx.Exec(_insertVipUserInfo, r.Mid, r.VipType, r.VipStatus, r.VipStartTime, r.VipOverdueTime, r.AnnualVipOverdueTime, r.Wander, r.AccessStatus, r.VipRecentTime); err != nil {
  197. err = errors.WithStack(err)
  198. }
  199. return
  200. }
  201. //OldInsertVipChangeHistory insert vipChangeHistory
  202. func (d *Dao) OldInsertVipChangeHistory(c context.Context, tx *sql.Tx, r *model.VipChangeHistory) (id int64, err error) {
  203. var res xsql.Result
  204. if res, err = tx.Exec(_insertVipChangeHistory, r.Mid, r.ChangeType, r.ChangeTime, r.Days, r.Month, r.OperatorID, r.RelationID, r.BatchID, r.Remark); err != nil {
  205. err = errors.WithStack(err)
  206. } else {
  207. if id, err = res.LastInsertId(); err != nil {
  208. err = errors.WithStack(err)
  209. }
  210. }
  211. return
  212. }
  213. //OldVipchangeHistory old vip change history.
  214. func (d *Dao) OldVipchangeHistory(c context.Context, relationID string, batchID int64) (r *model.VipChangeHistory, err error) {
  215. var row = d.olddb.QueryRow(c, _selVipChangeHistory, relationID, batchID)
  216. r = new(model.VipChangeHistory)
  217. if err = row.Scan(&r.ID, &r.Mid, &r.ChangeType, &r.ChangeTime, &r.Days, &r.Month, &r.OperatorID, &r.RelationID, &r.BatchID, &r.Remark, &r.Ctime); err != nil {
  218. if err == sql.ErrNoRows {
  219. r = nil
  220. err = nil
  221. return
  222. }
  223. err = errors.WithStack(err)
  224. d.errProm.Incr("scan_error")
  225. }
  226. return
  227. }