mysql.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "go-common/app/job/main/vip/model"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _selAppInfo = "SELECT id,name,app_key,purge_url from vip_app_info WHERE `type` = 1"
  15. //bcoin sql
  16. _selBcoinSalarySQL = "SELECT id,mid,status,give_now_status,payday,amount,memo FROM vip_user_bcoin_salary WHERE 1=1"
  17. _selBcoinSalaryDataSQL = "SELECT id,mid,status,give_now_status,payday,amount,memo FROM vip_user_bcoin_salary WHERE id>? AND id<=?"
  18. _selBcoinSalaryByMidSQL = "SELECT id,mid,status,give_now_status,payday,amount,memo FROM vip_user_bcoin_salary WHERE mid IN (%v)"
  19. _selOldBcoinSalaryByMidSQL = "SELECT id,mid,IFNULL(status,0),IFNULL(give_now_status,0),month,IFNULL(amount,0),IFNULL(memo,'') FROM vip_bcoin_salary WHERE mid IN (%v)"
  20. _selOldBcoinSalarySQL = "SELECT id,mid,IFNULL(status,0),IFNULL(give_now_status,0),month,IFNULL(amount,0),IFNULL(memo,'') FROM vip_bcoin_salary WHERE id>? AND id<=?"
  21. _addBcoinSalarySQL = "INSERT INTO vip_user_bcoin_salary(mid,status,give_now_status,payday,amount,memo) VALUES(?,?,?,?,?,?)"
  22. _updateBcoinSalarySQL = "UPDATE vip_user_bcoin_salary set status = ? WHERE mid = ? AND payday = ?"
  23. _updateBcoinSalaryBatchSQL = "UPDATE vip_user_bcoin_salary set status = ? WHERE id in (?)"
  24. _batchAddBcoinSalarySQL = "INSERT INTO vip_user_bcoin_salary(mid,status,give_now_status,payday,amount,memo) VALUES "
  25. _selBcoinMaxIDSQL = "SELECT IFNULL(MAX(id),0) FROM vip_user_bcoin_salary"
  26. _selOldBcoinMaxIDSQL = "SELECT IFNULL(MAX(id),0) FROM vip_bcoin_salary"
  27. _delBcoinSalarySQL = "DELETE FROM vip_user_bcoin_salary WHERE mid = ? AND payday = ?"
  28. _getAbleCodeSQL = "SELECT code FROM vip_resource_code WHERE batch_code_id = ? AND status = 1 AND relation_id = '' LIMIT 1"
  29. _selBatchCodeSQL = "SELECT id,business_id,pool_id,status,type,batch_name,reason,unit,count,surplus_count,price,start_time,end_time FROM vip_resource_batch_code WHERE id = ?"
  30. _updateCodeRelationIDSQL = "UPDATE vip_resource_code SET relation_id=?,bmid=? WHERE code=?"
  31. _selEffectiveVipList = "SELECT id,mid,vip_type,vip_status,vip_overdue_time,annual_vip_overdue_time FROM vip_user_info WHERE id>? AND id <=? "
  32. //push
  33. _selPushDataSQL = "SELECT id,disable_type,group_name,title,content,push_total_count,pushed_count,progress_status,`status`,platform,link_type,link_url,error_code,expired_day_start,expired_day_end,effect_start_date,effect_end_date,push_start_time,push_end_time FROM vip_push_data WHERE effect_start_date <= ? AND effect_end_date >= ? "
  34. _updatePushDataSQL = "UPDATE vip_push_data SET progress_status=?,status=?, pushed_count=?,error_code=?,task_id=? WHERE id=?"
  35. )
  36. //SelOldBcoinMaxID sel oldbcoin maxID
  37. func (d *Dao) SelOldBcoinMaxID(c context.Context) (maxID int64, err error) {
  38. row := d.oldDb.QueryRow(c, _selOldBcoinMaxIDSQL)
  39. if err = row.Scan(&maxID); err != nil {
  40. if err == sql.ErrStmtNil {
  41. err = nil
  42. maxID = 0
  43. return
  44. }
  45. err = errors.WithStack(err)
  46. d.errProm.Incr("db_scan")
  47. }
  48. return
  49. }
  50. //SelBcoinMaxID sel bcoin maxID
  51. func (d *Dao) SelBcoinMaxID(c context.Context) (maxID int64, err error) {
  52. row := d.db.QueryRow(c, _selBcoinMaxIDSQL)
  53. if err = row.Scan(&maxID); err != nil {
  54. err = errors.WithStack(err)
  55. d.errProm.Incr("db_scan")
  56. return
  57. }
  58. return
  59. }
  60. //SelBcoinSalaryData sel bcoinSalary data
  61. func (d *Dao) SelBcoinSalaryData(c context.Context, startID int64, endID int64) (res []*model.VipBcoinSalary, err error) {
  62. var rows *sql.Rows
  63. if rows, err = d.db.Query(c, _selBcoinSalaryDataSQL, startID, endID); err != nil {
  64. err = errors.WithStack(err)
  65. d.errProm.Incr("db_query")
  66. return
  67. }
  68. defer rows.Close()
  69. for rows.Next() {
  70. r := new(model.VipBcoinSalary)
  71. if err = rows.Scan(&r.ID, &r.Mid, &r.Status, &r.GiveNowStatus, &r.Payday, &r.Amount, &r.Memo); err != nil {
  72. err = errors.WithStack(err)
  73. d.errProm.Incr("db_scan")
  74. return
  75. }
  76. res = append(res, r)
  77. }
  78. err = rows.Err()
  79. return
  80. }
  81. //SelBcoinSalaryDataMaps sel bcoin salary data convert map
  82. func (d *Dao) SelBcoinSalaryDataMaps(c context.Context, mids []int64) (res map[int64][]*model.VipBcoinSalary, err error) {
  83. var rows *sql.Rows
  84. if rows, err = d.db.Query(c, fmt.Sprintf(_selBcoinSalaryByMidSQL, xstr.JoinInts(mids))); err != nil {
  85. err = errors.WithStack(err)
  86. d.errProm.Incr("db_query")
  87. return
  88. }
  89. res = make(map[int64][]*model.VipBcoinSalary)
  90. defer rows.Close()
  91. for rows.Next() {
  92. r := new(model.VipBcoinSalary)
  93. if err = rows.Scan(&r.ID, &r.Mid, &r.Status, &r.GiveNowStatus, &r.Payday, &r.Amount, &r.Memo); err != nil {
  94. err = errors.WithStack(err)
  95. d.errProm.Incr("db_scan")
  96. return
  97. }
  98. salaries := res[r.Mid]
  99. salaries = append(salaries, r)
  100. res[r.Mid] = salaries
  101. }
  102. err = rows.Err()
  103. return
  104. }
  105. //SelOldBcoinSalaryDataMaps sel old bcoin salary data convert map
  106. func (d *Dao) SelOldBcoinSalaryDataMaps(c context.Context, mids []int64) (res map[int64][]*model.VipBcoinSalary, err error) {
  107. var rows *sql.Rows
  108. if rows, err = d.oldDb.Query(c, fmt.Sprintf(_selOldBcoinSalaryByMidSQL, xstr.JoinInts(mids))); err != nil {
  109. err = errors.WithStack(err)
  110. d.errProm.Incr("db_query")
  111. return
  112. }
  113. res = make(map[int64][]*model.VipBcoinSalary)
  114. defer rows.Close()
  115. for rows.Next() {
  116. r := new(model.VipBcoinSalary)
  117. if err = rows.Scan(&r.ID, &r.Mid, &r.Status, &r.GiveNowStatus, &r.Payday, &r.Amount, &r.Memo); err != nil {
  118. err = errors.WithStack(err)
  119. d.errProm.Incr("db_scan")
  120. return
  121. }
  122. salaries := res[r.Mid]
  123. salaries = append(salaries, r)
  124. res[r.Mid] = salaries
  125. }
  126. err = rows.Err()
  127. return
  128. }
  129. //SelBcoinSalary sel bcoin salary data by query
  130. func (d *Dao) SelBcoinSalary(c context.Context, arg *model.QueryBcoinSalary) (res []*model.VipBcoinSalary, err error) {
  131. var rows *sql.Rows
  132. sqlStr := ""
  133. if arg.StartID >= 0 {
  134. sqlStr += fmt.Sprintf(" AND id > %v ", arg.StartID)
  135. }
  136. if arg.EndID >= 0 {
  137. sqlStr += fmt.Sprintf(" AND id <= %v ", arg.EndID)
  138. }
  139. if arg.StartMonth > 0 {
  140. sqlStr += fmt.Sprintf(" AND payday>= '%v' ", arg.StartMonth.Time().Format("2006-01-02"))
  141. }
  142. if arg.EndMonth > 0 {
  143. sqlStr += fmt.Sprintf(" AND payday <= '%v' ", arg.EndMonth.Time().Format("2006-01-02"))
  144. }
  145. if arg.GiveNowStatus > -1 {
  146. sqlStr += fmt.Sprintf(" AND give_now_status = %v ", arg.GiveNowStatus)
  147. }
  148. if arg.Status > -1 {
  149. sqlStr += fmt.Sprintf(" AND status = %v ", arg.Status)
  150. }
  151. if rows, err = d.db.Query(c, _selBcoinSalarySQL+sqlStr); err != nil {
  152. err = errors.WithStack(err)
  153. d.errProm.Incr("db_query")
  154. return
  155. }
  156. defer rows.Close()
  157. for rows.Next() {
  158. r := new(model.VipBcoinSalary)
  159. if err = rows.Scan(&r.ID, &r.Mid, &r.Status, &r.GiveNowStatus, &r.Payday, &r.Amount, &r.Memo); err != nil {
  160. err = errors.WithStack(err)
  161. d.errProm.Incr("db_scan")
  162. }
  163. res = append(res, r)
  164. }
  165. err = rows.Err()
  166. return
  167. }
  168. //SelOldBcoinSalary sel old bcoin salary
  169. func (d *Dao) SelOldBcoinSalary(c context.Context, startID, endID int64) (res []*model.VipBcoinSalary, err error) {
  170. var rows *sql.Rows
  171. if rows, err = d.oldDb.Query(c, _selOldBcoinSalarySQL, startID, endID); err != nil {
  172. err = errors.WithStack(err)
  173. d.errProm.Incr("db_query")
  174. return
  175. }
  176. defer rows.Close()
  177. for rows.Next() {
  178. r := new(model.VipBcoinSalary)
  179. if err = rows.Scan(&r.ID, &r.Mid, &r.Status, &r.GiveNowStatus, &r.Payday, &r.Amount, &r.Memo); err != nil {
  180. err = errors.WithStack(err)
  181. d.errProm.Incr("db_scan")
  182. }
  183. res = append(res, r)
  184. }
  185. err = rows.Err()
  186. return
  187. }
  188. //AddBcoinSalary add bcoin salary
  189. func (d *Dao) AddBcoinSalary(c context.Context, arg *model.VipBcoinSalaryMsg) (err error) {
  190. if _, err = d.db.Exec(c, _addBcoinSalarySQL, &arg.Mid, &arg.Status, &arg.GiveNowStatus, &arg.Payday, &arg.Amount, &arg.Memo); err != nil {
  191. err = errors.WithStack(err)
  192. d.errProm.Incr("db_exec")
  193. }
  194. return
  195. }
  196. //UpdateBcoinSalary update bcoin salary
  197. func (d *Dao) UpdateBcoinSalary(c context.Context, payday string, mid int64, status int8) (err error) {
  198. if _, err = d.db.Exec(c, _updateBcoinSalarySQL, status, mid, payday); err != nil {
  199. err = errors.WithStack(err)
  200. d.errProm.Incr("db_exec")
  201. }
  202. return
  203. }
  204. //DelBcoinSalary del bcoin salary
  205. func (d *Dao) DelBcoinSalary(c context.Context, payday string, mid int64) (err error) {
  206. if _, err = d.db.Exec(c, _delBcoinSalarySQL, mid, payday); err != nil {
  207. err = errors.WithStack(err)
  208. return
  209. }
  210. return
  211. }
  212. //UpdateBcoinSalaryBatch update bcoin salary batch
  213. func (d *Dao) UpdateBcoinSalaryBatch(c context.Context, ids []int64, status int8) (err error) {
  214. if len(ids) <= 0 {
  215. return
  216. }
  217. sqlStr := ""
  218. for _, v := range ids {
  219. sqlStr += "," + strconv.FormatInt(v, 10)
  220. }
  221. if _, err = d.db.Exec(c, _updateBcoinSalaryBatchSQL, status, sqlStr[1:]); err != nil {
  222. err = errors.WithStack(err)
  223. return
  224. }
  225. return
  226. }
  227. //BatchAddBcoinSalary batch add bcoin salary data
  228. func (d *Dao) BatchAddBcoinSalary(bcoins []*model.VipBcoinSalary) (err error) {
  229. var values []string
  230. if len(bcoins) <= 0 {
  231. return
  232. }
  233. for _, v := range bcoins {
  234. str := fmt.Sprintf("('%v','%v','%v','%v','%v','%v')", v.Mid, v.Status, v.GiveNowStatus, v.Payday.Time().Format("2006-01-02"), v.Amount, v.Memo)
  235. values = append(values, str)
  236. }
  237. valueStr := strings.Join(values, ",")
  238. if _, err = d.db.Exec(context.TODO(), _batchAddBcoinSalarySQL+valueStr); err != nil {
  239. err = errors.WithStack(err)
  240. d.errProm.Incr("db_exec")
  241. return
  242. }
  243. return
  244. }
  245. //SelAppInfo sel vip_app_info data
  246. func (d *Dao) SelAppInfo(c context.Context) (res []*model.VipAppInfo, err error) {
  247. var rows *sql.Rows
  248. if rows, err = d.oldDb.Query(c, _selAppInfo); err != nil {
  249. log.Error("SelAppInfo db.query() error(%v)", err)
  250. return
  251. }
  252. defer rows.Close()
  253. for rows.Next() {
  254. r := new(model.VipAppInfo)
  255. if err = rows.Scan(&r.ID, &r.Name, &r.AppKey, &r.PurgeURL); err != nil {
  256. log.Error("row.scan() error(%v)", err)
  257. res = nil
  258. return
  259. }
  260. res = append(res, r)
  261. }
  262. err = rows.Err()
  263. return
  264. }
  265. //SelEffectiveVipList sel effective vip data
  266. func (d *Dao) SelEffectiveVipList(c context.Context, id, endID int) (res []*model.VipUserInfo, err error) {
  267. var rows *sql.Rows
  268. if rows, err = d.oldDb.Query(c, _selEffectiveVipList, id, endID); err != nil {
  269. err = errors.WithStack(err)
  270. return
  271. }
  272. defer rows.Close()
  273. for rows.Next() {
  274. r := new(model.VipUserInfo)
  275. if err = rows.Scan(&r.ID, &r.Mid, &r.Type, &r.Status, &r.OverdueTime, &r.AnnualVipOverdueTime); err != nil {
  276. err = errors.WithStack(err)
  277. res = nil
  278. return
  279. }
  280. res = append(res, r)
  281. }
  282. err = rows.Err()
  283. return
  284. }