mysql.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. xsql "database/sql"
  6. "fmt"
  7. "strconv"
  8. "time"
  9. "go-common/app/service/main/coupon/model"
  10. "go-common/library/database/sql"
  11. "go-common/library/xstr"
  12. "github.com/pkg/errors"
  13. )
  14. const (
  15. _couponsSQL = "SELECT id,coupon_token,mid,state,start_time,expire_time,origin,coupon_type,order_no,oid,remark,use_ver,ver,ctime,mtime FROM coupon_info_%02d WHERE mid = ? AND state = ? AND coupon_type = ? AND expire_time > ? AND start_time < ? ORDER BY expire_time;"
  16. _couponsNoStartCheckSQL = "SELECT id,coupon_token,mid,state,start_time,expire_time,origin,coupon_type,order_no,oid,remark,use_ver,ver,ctime,mtime FROM coupon_info_%02d WHERE mid = ? AND state = ? AND coupon_type = ? AND expire_time > ? ORDER BY expire_time;"
  17. _couponByOrderNOAndTypeSQL = "SELECT id,coupon_token,mid,state,start_time,expire_time,origin,coupon_type,order_no,oid,remark,use_ver,ver,ctime,mtime FROM coupon_info_%02d WHERE order_no = ? AND coupon_type = ?;"
  18. _updateCouponInUseSQL = "UPDATE coupon_info_%02d SET state =?, order_no = ?, oid = ?, remark = ?,use_ver = ?,ver = ? WHERE coupon_token = ? AND ver = ?;"
  19. _addCouponChangeLogSQL = "INSERT INTO coupon_change_log_%02d (coupon_token,mid,state,ctime) VALUES(?,?,?,?);"
  20. _couponByTokenSQL = "SELECT id,coupon_token,mid,state,start_time,expire_time,origin,coupon_type,order_no,oid,remark,use_ver,ver,ctime,mtime FROM coupon_info_%02d WHERE coupon_token = ?;"
  21. _couponPageNotUsedSQL = "SELECT id,coupon_token,mid,state,start_time,expire_time,origin,coupon_type,order_no,oid,remark,use_ver,ver,ctime,mtime FROM coupon_info_%02d WHERE mid = ? AND state = ? AND expire_time > ? AND start_time < ? AND ctime > ? ORDER BY mtime DESC LIMIT ?,?;"
  22. _couponPageUsedSQL = "SELECT id,coupon_token,mid,state,start_time,expire_time,origin,coupon_type,order_no,oid,remark,use_ver,ver,ctime,mtime FROM coupon_info_%02d WHERE mid = ? AND state = ? AND ctime > ? ORDER BY mtime DESC LIMIT ?,?;"
  23. _couponPageExpireSQL = "SELECT id,coupon_token,mid,state,start_time,expire_time,origin,coupon_type,order_no,oid,remark,use_ver,ver,ctime,mtime FROM coupon_info_%02d WHERE mid = ? AND state = 0 AND expire_time < ? AND ctime > ? ORDER BY mtime DESC LIMIT ?,?;"
  24. _countNotUsedSQL = "SELECT COUNT(1) FROM coupon_info_%02d WHERE mid = ? AND state = ? AND expire_time > ? AND start_time < ? AND ctime > ? ;"
  25. _countUsedSQL = "SELECT COUNT(1) FROM coupon_info_%02d WHERE mid = ? AND state = ? AND ctime > ? ;"
  26. _countExpireSQL = "SELECT COUNT(1) FROM coupon_info_%02d WHERE mid = ? AND state = 0 AND expire_time < ? AND ctime > ? ;"
  27. _addCouponSQL = "INSERT INTO coupon_info_%02d (coupon_token,mid,state,start_time,expire_time,origin,coupon_type,ctime)VALUES(?,?,?,?,?,?,?,?);"
  28. _updateStateSQL = "UPDATE coupon_info_%02d SET state = ?,use_ver = ?,ver = ? WHERE coupon_token = ? AND ver = ? "
  29. _batchAddCouponSQL = "INSERT INTO coupon_info_%02d (coupon_token,mid,state,start_time,expire_time,origin,coupon_type,ctime,batch_token)VALUES "
  30. _batchCountByMid = "SELECT COUNT(1) FROM coupon_info_%02d WHERE mid = ? AND batch_token = ?;"
  31. //coupon blance
  32. _couponBlanceNoStartCheckSQL = "SELECT id,batch_token,mid,balance,start_time,expire_time,origin,coupon_type,ver,ctime,mtime FROM coupon_balance_info_%02d WHERE mid = ? AND expire_time > ? AND coupon_type = ? ORDER BY expire_time;"
  33. _couponBlanceSQL = "SELECT id,batch_token,mid,balance,start_time,expire_time,origin,coupon_type,ver,ctime,mtime FROM coupon_balance_info_%02d WHERE mid = ? AND expire_time > ? AND coupon_type = ? AND start_time < ? ORDER BY expire_time;"
  34. _orderByThirdTradeNoSQL = "SELECT id,order_no,mid,count,state,coupon_type,third_trade_no,remark,tips,use_ver,ver,ctime,mtime FROM coupon_order WHERE third_trade_no = ? AND coupon_type= ?;"
  35. _updateBlanceSQL = "UPDATE coupon_balance_info_%02d SET balance = ?,ver = ver + 1 WHERE id = ? AND ver = ?;"
  36. _addOrderSQL = "INSERT INTO coupon_order(order_no,mid,count,state,coupon_type,third_trade_no,remark,tips,use_ver,ver,ctime)VALUES(?,?,?,?,?,?,?,?,?,?,?);"
  37. _addOrderLogSQL = "INSERT INTO coupon_order_log(order_no,mid,state,ctime)VALUES(?,?,?,?);"
  38. _addBalanceLogSQL = "INSERT INTO coupon_balance_change_log_%02d(order_no,mid,batch_token,balance,change_balance,change_type,ctime)VALUES "
  39. _batchUpdateBalance = "UPDATE coupon_balance_info_%02d SET ver =ver + 1, balance = CASE id"
  40. _countBalanceNotUsed = "SELECT COUNT(1) FROM coupon_balance_info_%02d WHERE mid = ? AND expire_time > ? AND start_time < ? AND coupon_type = ? AND balance > 0 AND ctime > ? ;"
  41. _countUseListSQL = "SELECT COUNT(1) FROM coupon_order WHERE mid= ? AND state= ? AND coupon_type = ? AND ctime > ? ;"
  42. _countBalanceExpire = "SELECT COUNT(1) FROM coupon_balance_info_%02d WHERE mid = ? AND expire_time < ? AND coupon_type = ? AND balance > 0 AND ctime > ? ;"
  43. _balanceNotUsedPageSQL = "SELECT id,batch_token,mid,balance,start_time,expire_time,origin,coupon_type,ver,ctime,mtime FROM coupon_balance_info_%02d WHERE mid = ? AND expire_time > ? AND start_time < ? AND coupon_type = ? AND balance > 0 AND ctime > ? ORDER BY id DESC LIMIT ?,?;"
  44. _useOrderPageSQL = "SELECT id,order_no,mid,count,state,coupon_type,third_trade_no,remark,tips,use_ver,ver,ctime,mtime FROM coupon_order WHERE mid= ? AND state= ? AND coupon_type = ? AND ctime > ? ORDER BY id DESC LIMIT ?,?;"
  45. _balanceExpirePageSQL = "SELECT id,batch_token,mid,balance,start_time,expire_time,origin,coupon_type,ver,ctime,mtime FROM coupon_balance_info_%02d WHERE mid = ? AND expire_time < ? AND coupon_type = ? AND balance > 0 AND ctime > ? ORDER BY id DESC LIMIT ?,?;"
  46. _addBalanceCouponSQL = "INSERT INTO coupon_balance_info_%02d(batch_token,mid,balance,start_time,expire_time,origin,coupon_type,ver,ctime) VALUES(?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE balance = balance + ?,ver = ver + 1 ;"
  47. _byMidAndBatchTokenSQL = "SELECT id,batch_token,mid,balance,start_time,expire_time,origin,coupon_type,ver,ctime,mtime FROM coupon_balance_info_%02d WHERE mid = ? AND batch_token = ? ;"
  48. _addBlanceChangeLog = "INSERT INTO coupon_balance_change_log_%02d(order_no,mid,batch_token,balance,change_balance,change_type,ctime)VALUES(?,?,?,?,?,?,?);"
  49. _batchInfoSQL = "SELECT id,app_id,name,batch_token,max_count,current_count,start_time,expire_time,expire_day,ver,ctime,limit_count,full_amount,amount,state,coupon_type FROM coupon_batch_info WHERE batch_token = ?;"
  50. _updateBatchSQL = "UPDATE coupon_batch_info SET current_count = current_count + ? WHERE batch_token = ?;"
  51. _updateBatchLimitSQL = "UPDATE coupon_batch_info SET current_count = current_count + ? WHERE batch_token = ? AND current_count + ? <= max_count;"
  52. _grantCouponLogSQL = "SELECT id,order_no,mid,batch_token,balance,change_balance,change_type,ctime,mtime FROM coupon_balance_change_log_%02d WHERE mid = ? AND batch_token = ? AND change_type = ?;"
  53. _allBatchInfoSQL = "SELECT id,app_id,name,batch_token,max_count,current_count,start_time,expire_time,expire_day,ver,ctime,limit_count,full_amount,amount,state,coupon_type,platform_limit,product_limit_month,product_limit_renewal FROM coupon_batch_info;"
  54. _couponReceiveSQL = "SELECT id,appkey,order_no,mid,coupon_token,coupon_type FROM coupon_receive_log WHERE order_no=? AND appkey=? AND coupon_type=?"
  55. _addReceiveSQL = "INSERT INTO coupon_receive_log(appkey,order_no,mid,coupon_token,coupon_type) VALUES(?,?,?,?,?)"
  56. )
  57. func hitInfo(mid int64) int64 {
  58. return mid % 100
  59. }
  60. func hitChangeLog(mid int64) int64 {
  61. return mid % 100
  62. }
  63. func hitUser(mid int64) int64 {
  64. return mid % 10
  65. }
  66. func hitUserLog(mid int64) int64 {
  67. return mid % 10
  68. }
  69. // BeginTran begin transaction.
  70. func (d *Dao) BeginTran(c context.Context) (*sql.Tx, error) {
  71. return d.db.Begin(c)
  72. }
  73. // CouponList query .
  74. func (d *Dao) CouponList(c context.Context, mid int64, state int8, ct int8, t int64) (res []*model.CouponInfo, err error) {
  75. var rows *sql.Rows
  76. if rows, err = d.db.Query(c, fmt.Sprintf(_couponsSQL, hitInfo(mid)), mid, state, ct, t, t); err != nil {
  77. err = errors.WithStack(err)
  78. return
  79. }
  80. defer rows.Close()
  81. for rows.Next() {
  82. r := &model.CouponInfo{}
  83. if err = rows.Scan(&r.ID, &r.CouponToken, &r.Mid, &r.State, &r.StartTime, &r.ExpireTime, &r.Origin, &r.CouponType, &r.OrderNO, &r.Oid, &r.Remark,
  84. &r.UseVer, &r.Ver, &r.CTime, &r.MTime); err != nil {
  85. err = errors.WithStack(err)
  86. res = nil
  87. return
  88. }
  89. res = append(res, r)
  90. }
  91. err = rows.Err()
  92. return
  93. }
  94. // CouponNoStartCheckList had not check start query .
  95. func (d *Dao) CouponNoStartCheckList(c context.Context, mid int64, state int8, ct int8, t int64) (res []*model.CouponInfo, err error) {
  96. var rows *sql.Rows
  97. if rows, err = d.db.Query(c, fmt.Sprintf(_couponsNoStartCheckSQL, hitInfo(mid)), mid, state, ct, t); err != nil {
  98. err = errors.WithStack(err)
  99. return
  100. }
  101. defer rows.Close()
  102. for rows.Next() {
  103. r := &model.CouponInfo{}
  104. if err = rows.Scan(&r.ID, &r.CouponToken, &r.Mid, &r.State, &r.StartTime, &r.ExpireTime, &r.Origin, &r.CouponType, &r.OrderNO, &r.Oid, &r.Remark,
  105. &r.UseVer, &r.Ver, &r.CTime, &r.MTime); err != nil {
  106. err = errors.WithStack(err)
  107. res = nil
  108. return
  109. }
  110. res = append(res, r)
  111. }
  112. err = rows.Err()
  113. return
  114. }
  115. // BlanceNoStartCheckList had not check start query .
  116. func (d *Dao) BlanceNoStartCheckList(c context.Context, mid int64, ct int8, t int64) (res []*model.CouponBalanceInfo, err error) {
  117. var rows *sql.Rows
  118. if rows, err = d.db.Query(c, fmt.Sprintf(_couponBlanceNoStartCheckSQL, hitUser(mid)), mid, t, ct); err != nil {
  119. err = errors.WithStack(err)
  120. return
  121. }
  122. defer rows.Close()
  123. for rows.Next() {
  124. r := &model.CouponBalanceInfo{}
  125. if err = rows.Scan(&r.ID, &r.BatchToken, &r.Mid, &r.Balance, &r.StartTime, &r.ExpireTime, &r.Origin, &r.CouponType, &r.Ver, &r.CTime, &r.MTime); err != nil {
  126. err = errors.WithStack(err)
  127. res = nil
  128. return
  129. }
  130. res = append(res, r)
  131. }
  132. err = rows.Err()
  133. return
  134. }
  135. // ByOrderNO query coupon by orderno and type.
  136. func (d *Dao) ByOrderNO(c context.Context, mid int64, orderNO string, ct int8) (r *model.CouponInfo, err error) {
  137. var row *sql.Row
  138. r = &model.CouponInfo{}
  139. row = d.db.QueryRow(c, fmt.Sprintf(_couponByOrderNOAndTypeSQL, hitInfo(mid)), orderNO, ct)
  140. if err = row.Scan(&r.ID, &r.CouponToken, &r.Mid, &r.State, &r.StartTime, &r.ExpireTime, &r.Origin, &r.CouponType, &r.OrderNO, &r.Oid, &r.Remark,
  141. &r.UseVer, &r.Ver, &r.CTime, &r.MTime); err != nil {
  142. if err == sql.ErrNoRows {
  143. err = nil
  144. r = nil
  145. return
  146. }
  147. err = errors.WithStack(err)
  148. return
  149. }
  150. return
  151. }
  152. // UpdateCouponInUse update coupon in use.
  153. func (d *Dao) UpdateCouponInUse(c context.Context, tx *sql.Tx, cp *model.CouponInfo) (a int64, err error) {
  154. var res xsql.Result
  155. if res, err = tx.Exec(fmt.Sprintf(_updateCouponInUseSQL, hitInfo(cp.Mid)), cp.State, cp.OrderNO, cp.Oid, cp.Remark, cp.UseVer, cp.Ver+1,
  156. cp.CouponToken, cp.Ver); err != nil {
  157. err = errors.WithStack(err)
  158. return
  159. }
  160. if a, err = res.RowsAffected(); err != nil {
  161. err = errors.WithStack(err)
  162. return
  163. }
  164. return
  165. }
  166. //InsertPointHistory .
  167. func (d *Dao) InsertPointHistory(c context.Context, tx *sql.Tx, l *model.CouponChangeLog) (a int64, err error) {
  168. var res xsql.Result
  169. if res, err = tx.Exec(fmt.Sprintf(_addCouponChangeLogSQL, hitChangeLog(l.Mid)), l.CouponToken, l.Mid, l.State, l.Ctime); err != nil {
  170. err = errors.WithStack(err)
  171. return
  172. }
  173. if a, err = res.RowsAffected(); err != nil {
  174. err = errors.WithStack(err)
  175. }
  176. return
  177. }
  178. // CouponInfo coupon info.
  179. func (d *Dao) CouponInfo(c context.Context, mid int64, token string) (r *model.CouponInfo, err error) {
  180. var row *sql.Row
  181. r = &model.CouponInfo{}
  182. row = d.db.QueryRow(c, fmt.Sprintf(_couponByTokenSQL, hitInfo(mid)), token)
  183. if err = row.Scan(&r.ID, &r.CouponToken, &r.Mid, &r.State, &r.StartTime, &r.ExpireTime, &r.Origin, &r.CouponType, &r.OrderNO, &r.Oid, &r.Remark,
  184. &r.UseVer, &r.Ver, &r.CTime, &r.MTime); err != nil {
  185. if err == sql.ErrNoRows {
  186. err = nil
  187. r = nil
  188. return
  189. }
  190. err = errors.WithStack(err)
  191. return
  192. }
  193. return
  194. }
  195. // CountByState coupon count buy state.
  196. func (d *Dao) CountByState(c context.Context, mid int64, state int8, t int64, stime time.Time) (count int64, err error) {
  197. var row *sql.Row
  198. switch state {
  199. case model.NotUsed:
  200. row = d.db.QueryRow(c, fmt.Sprintf(_countNotUsedSQL, hitInfo(mid)), mid, state, t, t, stime)
  201. case model.Used:
  202. row = d.db.QueryRow(c, fmt.Sprintf(_countUsedSQL, hitInfo(mid)), mid, state, stime)
  203. case model.Expire:
  204. row = d.db.QueryRow(c, fmt.Sprintf(_countExpireSQL, hitInfo(mid)), mid, t, stime)
  205. default:
  206. return
  207. }
  208. if err = row.Scan(&count); err != nil {
  209. if err == sql.ErrNoRows {
  210. err = nil
  211. } else {
  212. err = errors.WithStack(err)
  213. }
  214. }
  215. return
  216. }
  217. // CouponPage page.
  218. func (d *Dao) CouponPage(c context.Context, mid int64, state int8, t int64, start int, ps int, stime time.Time) (res []*model.CouponInfo, err error) {
  219. var rows *sql.Rows
  220. switch state {
  221. case model.NotUsed:
  222. rows, err = d.db.Query(c, fmt.Sprintf(_couponPageNotUsedSQL, hitInfo(mid)), mid, state, t, t, stime, start, ps)
  223. case model.Used:
  224. rows, err = d.db.Query(c, fmt.Sprintf(_couponPageUsedSQL, hitInfo(mid)), mid, state, stime, start, ps)
  225. case model.Expire:
  226. rows, err = d.db.Query(c, fmt.Sprintf(_couponPageExpireSQL, hitInfo(mid)), mid, t, stime, start, ps)
  227. default:
  228. return
  229. }
  230. if err != nil {
  231. err = errors.WithStack(err)
  232. return
  233. }
  234. defer rows.Close()
  235. for rows.Next() {
  236. r := &model.CouponInfo{}
  237. if err = rows.Scan(&r.ID, &r.CouponToken, &r.Mid, &r.State, &r.StartTime, &r.ExpireTime, &r.Origin, &r.CouponType, &r.OrderNO, &r.Oid, &r.Remark,
  238. &r.UseVer, &r.Ver, &r.CTime, &r.MTime); err != nil {
  239. err = errors.WithStack(err)
  240. res = nil
  241. return
  242. }
  243. res = append(res, r)
  244. }
  245. err = rows.Err()
  246. return
  247. }
  248. // AddCoupon add coupon.
  249. func (d *Dao) AddCoupon(c context.Context, cp *model.CouponInfo) (a int64, err error) {
  250. var res xsql.Result
  251. if res, err = d.db.Exec(c, fmt.Sprintf(_addCouponSQL, hitInfo(cp.Mid)), cp.CouponToken, cp.Mid, cp.State, cp.StartTime, cp.ExpireTime, cp.Origin, cp.CouponType, cp.CTime); err != nil {
  252. err = errors.WithStack(err)
  253. return
  254. }
  255. if a, err = res.RowsAffected(); err != nil {
  256. err = errors.WithStack(err)
  257. return
  258. }
  259. return
  260. }
  261. // BatchAddCoupon batch add coupon.
  262. func (d *Dao) BatchAddCoupon(c context.Context, tx *sql.Tx, mid int64, cps []*model.CouponInfo) (a int64, err error) {
  263. var (
  264. buf bytes.Buffer
  265. res xsql.Result
  266. sql string
  267. )
  268. buf.WriteString(fmt.Sprintf(_batchAddCouponSQL, hitInfo(mid)))
  269. for _, v := range cps {
  270. buf.WriteString("('")
  271. buf.WriteString(v.CouponToken)
  272. buf.WriteString("',")
  273. buf.WriteString(strconv.FormatInt(v.Mid, 10))
  274. buf.WriteString(",")
  275. buf.WriteString(strconv.FormatInt(v.State, 10))
  276. buf.WriteString(",")
  277. buf.WriteString(strconv.FormatInt(v.StartTime, 10))
  278. buf.WriteString(",")
  279. buf.WriteString(strconv.FormatInt(v.ExpireTime, 10))
  280. buf.WriteString(",")
  281. buf.WriteString(strconv.FormatInt(v.Origin, 10))
  282. buf.WriteString(",")
  283. buf.WriteString(strconv.FormatInt(v.CouponType, 10))
  284. buf.WriteString(",'")
  285. buf.WriteString(v.CTime.Time().Format("2006-01-02 15:04:05"))
  286. buf.WriteString("','")
  287. buf.WriteString(v.BatchToken)
  288. buf.WriteString("'),")
  289. }
  290. sql = buf.String()
  291. if res, err = tx.Exec(sql[0 : len(sql)-1]); err != nil {
  292. err = errors.WithStack(err)
  293. return
  294. }
  295. if a, err = res.RowsAffected(); err != nil {
  296. err = errors.WithStack(err)
  297. }
  298. return
  299. }
  300. // UpdateCoupon update coupon in use.
  301. func (d *Dao) UpdateCoupon(c context.Context, mid int64, state int8, useVer int64, ver int64, couponToken string) (a int64, err error) {
  302. var res xsql.Result
  303. if res, err = d.db.Exec(c, fmt.Sprintf(_updateStateSQL, hitInfo(mid)), state, useVer, ver+1, couponToken, ver); err != nil {
  304. err = errors.WithStack(err)
  305. return
  306. }
  307. if a, err = res.RowsAffected(); err != nil {
  308. err = errors.WithStack(err)
  309. return
  310. }
  311. return
  312. }
  313. // ByThirdTradeNo query order by third trade no.
  314. func (d *Dao) ByThirdTradeNo(c context.Context, thirdTradeNo string, ct int8) (r *model.CouponOrder, err error) {
  315. var row *sql.Row
  316. r = &model.CouponOrder{}
  317. row = d.db.QueryRow(c, _orderByThirdTradeNoSQL, thirdTradeNo, ct)
  318. if err = row.Scan(&r.ID, &r.OrderNo, &r.Mid, &r.Count, &r.State, &r.CouponType, &r.ThirdTradeNo, &r.Remark, &r.Tips, &r.UseVer, &r.Ver, &r.Ctime, &r.Mtime); err != nil {
  319. if err == sql.ErrNoRows {
  320. err = nil
  321. r = nil
  322. return
  323. }
  324. err = errors.WithStack(err)
  325. return
  326. }
  327. return
  328. }
  329. // CouponBlances query coupon blances .
  330. func (d *Dao) CouponBlances(c context.Context, mid int64, ct int8, t int64) (res []*model.CouponBalanceInfo, err error) {
  331. var rows *sql.Rows
  332. if rows, err = d.db.Query(c, fmt.Sprintf(_couponBlanceSQL, hitUser(mid)), mid, t, ct, t); err != nil {
  333. err = errors.WithStack(err)
  334. return
  335. }
  336. defer rows.Close()
  337. for rows.Next() {
  338. r := &model.CouponBalanceInfo{}
  339. if err = rows.Scan(&r.ID, &r.BatchToken, &r.Mid, &r.Balance, &r.StartTime, &r.ExpireTime, &r.Origin, &r.CouponType, &r.Ver, &r.CTime, &r.MTime); err != nil {
  340. err = errors.WithStack(err)
  341. res = nil
  342. return
  343. }
  344. res = append(res, r)
  345. }
  346. err = rows.Err()
  347. return
  348. }
  349. // UpdateBlance update blance.
  350. func (d *Dao) UpdateBlance(c context.Context, tx *sql.Tx, id int64, mid int64, ver int64, balance int64) (a int64, err error) {
  351. var res xsql.Result
  352. if res, err = tx.Exec(fmt.Sprintf(_updateBlanceSQL, hitUser(mid)), balance, id, ver); err != nil {
  353. err = errors.WithStack(err)
  354. return
  355. }
  356. if a, err = res.RowsAffected(); err != nil {
  357. err = errors.WithStack(err)
  358. return
  359. }
  360. return
  361. }
  362. // BatchUpdateBlance batch update blance.
  363. func (d *Dao) BatchUpdateBlance(c context.Context, tx *sql.Tx, mid int64, blances []*model.CouponBalanceInfo) (a int64, err error) {
  364. var (
  365. res xsql.Result
  366. buf bytes.Buffer
  367. ids []int64
  368. )
  369. buf.WriteString(fmt.Sprintf(_batchUpdateBalance, hitUser(mid)))
  370. for _, v := range blances {
  371. buf.WriteString(" WHEN ")
  372. buf.WriteString(strconv.FormatInt(v.ID, 10))
  373. buf.WriteString(" THEN ")
  374. buf.WriteString(strconv.FormatInt(v.Balance, 10))
  375. ids = append(ids, v.ID)
  376. }
  377. buf.WriteString(" END WHERE `id` in (")
  378. buf.WriteString(xstr.JoinInts(ids))
  379. buf.WriteString(") AND `ver` = CASE id ")
  380. for _, v := range blances {
  381. buf.WriteString(" WHEN ")
  382. buf.WriteString(strconv.FormatInt(v.ID, 10))
  383. buf.WriteString(" THEN ")
  384. buf.WriteString(strconv.FormatInt(v.Ver, 10))
  385. }
  386. buf.WriteString(" END;")
  387. if res, err = tx.Exec(buf.String()); err != nil {
  388. err = errors.WithStack(err)
  389. return
  390. }
  391. if a, err = res.RowsAffected(); err != nil {
  392. err = errors.WithStack(err)
  393. return
  394. }
  395. return
  396. }
  397. // BatchInsertBlanceLog Batch Insert Balance log
  398. func (d *Dao) BatchInsertBlanceLog(c context.Context, tx *sql.Tx, mid int64, ls []*model.CouponBalanceChangeLog) (a int64, err error) {
  399. var (
  400. buf bytes.Buffer
  401. res xsql.Result
  402. sql string
  403. )
  404. buf.WriteString(fmt.Sprintf(_addBalanceLogSQL, hitUserLog(mid)))
  405. for _, v := range ls {
  406. buf.WriteString("('")
  407. buf.WriteString(v.OrderNo)
  408. buf.WriteString("',")
  409. buf.WriteString(strconv.FormatInt(v.Mid, 10))
  410. buf.WriteString(",'")
  411. buf.WriteString(v.BatchToken)
  412. buf.WriteString("',")
  413. buf.WriteString(strconv.FormatInt(v.Balance, 10))
  414. buf.WriteString(",")
  415. buf.WriteString(strconv.FormatInt(v.ChangeBalance, 10))
  416. buf.WriteString(",")
  417. buf.WriteString(strconv.Itoa(int(v.ChangeType)))
  418. buf.WriteString(",'")
  419. buf.WriteString(fmt.Sprintf("%v", v.Ctime.Time().Format("2006-01-02 15:04:05")))
  420. buf.WriteString("'),")
  421. }
  422. sql = buf.String()
  423. if res, err = tx.Exec(sql[0 : len(sql)-1]); err != nil {
  424. err = errors.WithStack(err)
  425. return
  426. }
  427. if a, err = res.RowsAffected(); err != nil {
  428. err = errors.WithStack(err)
  429. }
  430. return
  431. }
  432. // AddOrder add order.
  433. func (d *Dao) AddOrder(c context.Context, tx *sql.Tx, o *model.CouponOrder) (a int64, err error) {
  434. var res xsql.Result
  435. if res, err = tx.Exec(_addOrderSQL, o.OrderNo, o.Mid, o.Count, o.State, o.CouponType, o.ThirdTradeNo, o.Remark, o.Tips, o.UseVer, o.Ver, o.Ctime); err != nil {
  436. err = errors.WithStack(err)
  437. return
  438. }
  439. if a, err = res.RowsAffected(); err != nil {
  440. err = errors.WithStack(err)
  441. }
  442. return
  443. }
  444. // AddOrderLog add order log.
  445. func (d *Dao) AddOrderLog(c context.Context, tx *sql.Tx, o *model.CouponOrderLog) (a int64, err error) {
  446. var res xsql.Result
  447. if res, err = tx.Exec(_addOrderLogSQL, o.OrderNo, o.Mid, o.State, o.Ctime); err != nil {
  448. err = errors.WithStack(err)
  449. return
  450. }
  451. if a, err = res.RowsAffected(); err != nil {
  452. err = errors.WithStack(err)
  453. }
  454. return
  455. }
  456. // CouponCarToonCount coupon cartoon page.
  457. func (d *Dao) CouponCarToonCount(c context.Context, mid int64, t int64, ct int8, state int8, stime time.Time) (count int64, err error) {
  458. var row *sql.Row
  459. switch state {
  460. case model.NotUsed:
  461. row = d.db.QueryRow(c, fmt.Sprintf(_countBalanceNotUsed, hitUser(mid)), mid, t, t, ct, stime)
  462. case model.Used:
  463. row = d.db.QueryRow(c, _countUseListSQL, mid, state, ct, stime)
  464. case model.Expire:
  465. row = d.db.QueryRow(c, fmt.Sprintf(_countBalanceExpire, hitUser(mid)), mid, t, ct, stime)
  466. default:
  467. return
  468. }
  469. if err = row.Scan(&count); err != nil {
  470. if err == sql.ErrNoRows {
  471. err = nil
  472. } else {
  473. err = errors.WithStack(err)
  474. }
  475. }
  476. return
  477. }
  478. // CouponNotUsedPage query coupon page .
  479. func (d *Dao) CouponNotUsedPage(c context.Context, mid int64, ct int8, t int64, stime time.Time, pn int, ps int) (res []*model.CouponBalanceInfo, err error) {
  480. var rows *sql.Rows
  481. if rows, err = d.db.Query(c, fmt.Sprintf(_balanceNotUsedPageSQL, hitUser(mid)), mid, t, t, ct, stime, (pn-1)*ps, ps); err != nil {
  482. err = errors.WithStack(err)
  483. return
  484. }
  485. defer rows.Close()
  486. for rows.Next() {
  487. r := &model.CouponBalanceInfo{}
  488. if err = rows.Scan(&r.ID, &r.BatchToken, &r.Mid, &r.Balance, &r.StartTime, &r.ExpireTime, &r.Origin, &r.CouponType, &r.Ver, &r.CTime, &r.MTime); err != nil {
  489. err = errors.WithStack(err)
  490. res = nil
  491. return
  492. }
  493. res = append(res, r)
  494. }
  495. err = rows.Err()
  496. return
  497. }
  498. // CouponExpirePage query coupon page .
  499. func (d *Dao) CouponExpirePage(c context.Context, mid int64, ct int8, t int64, stime time.Time, pn int, ps int) (res []*model.CouponBalanceInfo, err error) {
  500. var rows *sql.Rows
  501. if rows, err = d.db.Query(c, fmt.Sprintf(_balanceExpirePageSQL, hitUser(mid)), mid, t, ct, stime, (pn-1)*ps, ps); err != nil {
  502. err = errors.WithStack(err)
  503. return
  504. }
  505. defer rows.Close()
  506. for rows.Next() {
  507. r := &model.CouponBalanceInfo{}
  508. if err = rows.Scan(&r.ID, &r.BatchToken, &r.Mid, &r.Balance, &r.StartTime, &r.ExpireTime, &r.Origin, &r.CouponType, &r.Ver, &r.CTime, &r.MTime); err != nil {
  509. err = errors.WithStack(err)
  510. res = nil
  511. return
  512. }
  513. res = append(res, r)
  514. }
  515. err = rows.Err()
  516. return
  517. }
  518. // OrderUsedPage order used page.
  519. func (d *Dao) OrderUsedPage(c context.Context, mid int64, state int8, ct int8, stime time.Time, pn int, ps int) (res []*model.CouponOrder, err error) {
  520. var rows *sql.Rows
  521. if rows, err = d.db.Query(c, _useOrderPageSQL, mid, state, ct, stime, (pn-1)*ps, ps); err != nil {
  522. err = errors.WithStack(err)
  523. return
  524. }
  525. defer rows.Close()
  526. for rows.Next() {
  527. r := &model.CouponOrder{}
  528. if err = rows.Scan(&r.ID, &r.OrderNo, &r.Mid, &r.Count, &r.State, &r.CouponType, &r.ThirdTradeNo, &r.Remark, &r.Tips, &r.UseVer,
  529. &r.Ver, &r.Ctime, &r.Mtime); err != nil {
  530. if err == sql.ErrNoRows {
  531. err = nil
  532. res = nil
  533. return
  534. }
  535. err = errors.WithStack(err)
  536. return
  537. }
  538. res = append(res, r)
  539. }
  540. err = rows.Err()
  541. return
  542. }
  543. // AddBalanceCoupon add balance coupon.
  544. func (d *Dao) AddBalanceCoupon(c context.Context, tx *sql.Tx, b *model.CouponBalanceInfo) (a int64, err error) {
  545. var res xsql.Result
  546. if res, err = tx.Exec(fmt.Sprintf(_addBalanceCouponSQL, hitUser(b.Mid)), b.BatchToken, b.Mid, b.Balance, b.StartTime, b.ExpireTime, b.Origin, b.CouponType, b.Ver, b.CTime,
  547. b.Balance); err != nil {
  548. err = errors.WithStack(err)
  549. return
  550. }
  551. if a, err = res.RowsAffected(); err != nil {
  552. err = errors.WithStack(err)
  553. }
  554. return
  555. }
  556. // ByMidAndBatchToken query coupon by batch token and mid.
  557. func (d *Dao) ByMidAndBatchToken(c context.Context, mid int64, batchToken string) (r *model.CouponBalanceInfo, err error) {
  558. var row *sql.Row
  559. r = &model.CouponBalanceInfo{}
  560. row = d.db.QueryRow(c, fmt.Sprintf(_byMidAndBatchTokenSQL, hitUser(mid)), mid, batchToken)
  561. if err = row.Scan(&r.ID, &r.BatchToken, &r.Mid, &r.Balance, &r.StartTime, &r.ExpireTime, &r.Origin, &r.CouponType, &r.Ver, &r.CTime, &r.MTime); err != nil {
  562. if err == sql.ErrNoRows {
  563. err = nil
  564. r = nil
  565. return
  566. }
  567. err = errors.WithStack(err)
  568. return
  569. }
  570. return
  571. }
  572. // AddBalanceChangeLog add coupon balance change log.
  573. func (d *Dao) AddBalanceChangeLog(c context.Context, tx *sql.Tx, bl *model.CouponBalanceChangeLog) (a int64, err error) {
  574. var res xsql.Result
  575. if res, err = tx.Exec(fmt.Sprintf(_addBlanceChangeLog, hitUserLog(bl.Mid)), bl.OrderNo, bl.Mid, bl.BatchToken, bl.Balance, bl.ChangeBalance, bl.ChangeType, bl.Ctime); err != nil {
  576. err = errors.WithStack(err)
  577. return
  578. }
  579. if a, err = res.RowsAffected(); err != nil {
  580. err = errors.WithStack(err)
  581. return
  582. }
  583. return
  584. }
  585. // BatchInfo batch info.
  586. func (d *Dao) BatchInfo(c context.Context, token string) (r *model.CouponBatchInfo, err error) {
  587. var row *sql.Row
  588. r = new(model.CouponBatchInfo)
  589. row = d.db.QueryRow(c, _batchInfoSQL, token)
  590. if err = row.Scan(&r.ID, &r.AppID, &r.Name, &r.BatchToken, &r.MaxCount, &r.CurrentCount, &r.StartTime, &r.ExpireTime, &r.ExpireDay, &r.Ver, &r.Ctime, &r.LimitCount,
  591. &r.FullAmount, &r.Amount, &r.State, &r.CouponType); err != nil {
  592. if err == sql.ErrNoRows {
  593. err = nil
  594. r = nil
  595. return
  596. }
  597. err = errors.WithStack(err)
  598. return
  599. }
  600. return
  601. }
  602. // UpdateBatchInfo update batch info.
  603. func (d *Dao) UpdateBatchInfo(c context.Context, tx *sql.Tx, token string, count int) (a int64, err error) {
  604. var res xsql.Result
  605. if res, err = tx.Exec(_updateBatchSQL, count, token); err != nil {
  606. err = errors.WithStack(err)
  607. return
  608. }
  609. if a, err = res.RowsAffected(); err != nil {
  610. err = errors.WithStack(err)
  611. return
  612. }
  613. return
  614. }
  615. // UpdateBatchLimitInfo update batch limit info.
  616. func (d *Dao) UpdateBatchLimitInfo(c context.Context, tx *sql.Tx, token string, count int) (a int64, err error) {
  617. var res xsql.Result
  618. if res, err = tx.Exec(_updateBatchLimitSQL, count, token, count); err != nil {
  619. err = errors.WithStack(err)
  620. return
  621. }
  622. if a, err = res.RowsAffected(); err != nil {
  623. err = errors.WithStack(err)
  624. return
  625. }
  626. return
  627. }
  628. // GrantCouponLog grant coupon log.
  629. func (d *Dao) GrantCouponLog(c context.Context, mid int64, token string, ct int8) (rs []*model.CouponBalanceChangeLog, err error) {
  630. var rows *sql.Rows
  631. if rows, err = d.db.Query(c, fmt.Sprintf(_grantCouponLogSQL, hitUserLog(mid)), mid, token, ct); err != nil {
  632. err = errors.WithStack(err)
  633. return
  634. }
  635. defer rows.Close()
  636. for rows.Next() {
  637. r := &model.CouponBalanceChangeLog{}
  638. if err = rows.Scan(&r.ID, &r.OrderNo, &r.Mid, &r.BatchToken, &r.Balance, &r.ChangeBalance, &r.ChangeType, &r.Ctime, &r.Mtime); err != nil {
  639. err = errors.WithStack(err)
  640. rs = nil
  641. return
  642. }
  643. rs = append(rs, r)
  644. }
  645. err = rows.Err()
  646. return
  647. }
  648. // AllBranchInfo query all branch info .
  649. func (d *Dao) AllBranchInfo(c context.Context) (res []*model.CouponBatchInfo, err error) {
  650. var rows *sql.Rows
  651. if rows, err = d.db.Query(c, _allBatchInfoSQL); err != nil {
  652. err = errors.WithStack(err)
  653. return
  654. }
  655. defer rows.Close()
  656. for rows.Next() {
  657. r := &model.CouponBatchInfo{}
  658. if err = rows.Scan(&r.ID, &r.AppID, &r.Name, &r.BatchToken, &r.MaxCount, &r.CurrentCount, &r.StartTime, &r.ExpireTime, &r.ExpireDay, &r.Ver, &r.Ctime,
  659. &r.LimitCount, &r.FullAmount, &r.Amount, &r.State, &r.CouponType, &r.PlatformLimit, &r.ProdLimMonth, &r.ProdLimRenewal); err != nil {
  660. err = errors.WithStack(err)
  661. res = nil
  662. return
  663. }
  664. res = append(res, r)
  665. }
  666. err = rows.Err()
  667. return
  668. }
  669. // CountByBranchToken get user count by bratch token.
  670. func (d *Dao) CountByBranchToken(c context.Context, mid int64, token string) (count int64, err error) {
  671. row := d.db.QueryRow(c, fmt.Sprintf(_batchCountByMid, hitInfo(mid)), mid, token)
  672. if err = row.Scan(&count); err != nil {
  673. err = errors.WithStack(err)
  674. }
  675. return
  676. }
  677. //ReceiveLog get receive log.
  678. func (d *Dao) ReceiveLog(c context.Context, appkey, orderNo string, ct int8) (r *model.CouponReceiveLog, err error) {
  679. row := d.db.QueryRow(c, _couponReceiveSQL, orderNo, appkey, ct)
  680. r = new(model.CouponReceiveLog)
  681. if err = row.Scan(&r.ID, &r.Appkey, &r.OrderNo, &r.Mid, &r.CouponToken, &r.CouponType); err != nil {
  682. if err == sql.ErrNoRows {
  683. r = nil
  684. err = nil
  685. return
  686. }
  687. err = errors.WithStack(err)
  688. }
  689. return
  690. }
  691. //TxAddReceiveLog add receive log.
  692. func (d *Dao) TxAddReceiveLog(tx *sql.Tx, rlog *model.CouponReceiveLog) (err error) {
  693. if _, err = tx.Exec(_addReceiveSQL, rlog.Appkey, rlog.OrderNo, rlog.Mid, rlog.CouponToken, rlog.CouponType); err != nil {
  694. err = errors.WithStack(err)
  695. }
  696. return
  697. }