mysql.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. xsql "database/sql"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "go-common/app/admin/main/coupon/model"
  10. "go-common/library/database/sql"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _addbatch = "INSERT INTO coupon_batch_info(app_id,name,batch_token,max_count,current_count,start_time,expire_time,ver,ctime,limit_count,operator)VALUES(?,?,?,?,?,?,?,?,?,?,?);"
  15. _addAllowancebatch = "INSERT INTO coupon_batch_info(app_id,name,batch_token,max_count,current_count,start_time,expire_time,expire_day,ver,ctime,limit_count,operator,full_amount,amount,state,coupon_type,platform_limit,product_limit_month,product_limit_renewal)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"
  16. _batchList = "SELECT id,app_id,name,batch_token,max_count,current_count,start_time,expire_time,expire_day,ver,ctime,mtime,limit_count,operator,full_amount,amount,state,coupon_type,platform_limit,product_limit_month,product_limit_renewal FROM coupon_batch_info WHERE 1=1 "
  17. _appAppInfoSQL = "SELECT id,name,app_key,notify_url,ctime,mtime FROM coupon_app_info;"
  18. _updateAllowanceBatchSQL = "UPDATE coupon_batch_info SET app_id = ?,name = ?,max_count = ?,limit_count = ?,operator = ?,platform_limit = ?,product_limit_month = ?,product_limit_renewal = ? WHERE id = ?;"
  19. _updateBatchStatusSQL = "UPDATE coupon_batch_info SET state = ?, operator = ? WHERE id = ?;"
  20. _batchInfoSQL = "SELECT id,app_id,name,batch_token,max_count,current_count,start_time,expire_time,expire_day,ver,ctime,mtime,limit_count,operator,full_amount,amount,state,coupon_type,platform_limit,product_limit_month,product_limit_renewal FROM coupon_batch_info WHERE batch_token = ? "
  21. _batchInfoByIDSQL = "SELECT id,app_id,name,batch_token,max_count,current_count,start_time,expire_time,expire_day,ver,ctime,mtime,limit_count,operator,full_amount,amount,state,coupon_type,platform_limit,product_limit_month,product_limit_renewal FROM coupon_batch_info WHERE id = ? "
  22. _updateAllowanceStateSQL = "UPDATE coupon_allowance_info_%02d SET state = ?,ver = ver+1 WHERE coupon_token = ? AND ver = ?;"
  23. _couponAllowanceByTokenSQL = "SELECT id,coupon_token,mid,state,start_time,expire_time,origin,ver,batch_token,order_no,amount,full_amount,ctime,mtime FROM coupon_allowance_info_%02d WHERE coupon_token = ?;"
  24. _addCouponAllowanceChangeLogSQL = "INSERT INTO coupon_allowance_change_log_%02d (coupon_token,order_no,mid,state,ctime, change_type) VALUES(?,?,?,?,?,?);"
  25. _couponAllowancePageSQL = "SELECT id,coupon_token,mid,state,start_time,expire_time,origin,ver,batch_token,order_no,amount,full_amount,ctime,mtime,remark FROM coupon_allowance_info_%02d WHERE mid = ? %s ORDER BY id DESC"
  26. _batchAddAllowanceCouponSQL = "INSERT INTO coupon_allowance_info_%02d(coupon_token,mid,state,start_time,expire_time,origin,batch_token,amount,full_amount,ctime,app_id) VALUES "
  27. _updateBatchSQL = "UPDATE coupon_batch_info SET current_count = current_count + ? WHERE batch_token = ?;"
  28. _updateCodeBatchSQL = "UPDATE coupon_batch_info SET app_id = ?,name = ?,limit_count = ?,operator = ?,platform_limit = ?,product_limit_month = ?,product_limit_renewal = ? WHERE id = ?;"
  29. //view
  30. _addViewBatchSQL = "INSERT INTO coupon_batch_info(app_id,name,batch_token,max_count,current_count,start_time,expire_time,ver,limit_count,coupon_type,operator)VALUES(?,?,?,?,?,?,?,?,?,?,?)"
  31. _updateViewBatchSQL = "UPDATE coupon_batch_info SET app_id=?,name=?,max_count=?,limit_count=?,operator = ?,ver=? WHERE id=?"
  32. _updateViewSQL = "UPDATE coupon_info_%02d SET state=? WHERE coupon_token=?"
  33. _searchViewSQL = "SELECT coupon_token,mid,batch_token,state,order_no,oid,start_time,expire_time,ctime,mtime FROM coupon_info_%02d WHERE 1=1"
  34. _searchViewCountSQL = "SELECT COUNT(1) FROM coupon_info_%02d WHERE 1=1"
  35. _viewInfoSQL = "SELECT coupon_token,mid,batch_token,state,order_no,oid,start_time,expire_time,ctime FROM coupon_info_%02d WHERE coupon_token=?"
  36. _addViewChangeLog = "INSERT INTO coupon_change_log_%02d(coupon_token,mid,state) VALUES(?,?,?)"
  37. )
  38. // BeginTran begin transaction.
  39. func (d *Dao) BeginTran(c context.Context) (*sql.Tx, error) {
  40. return d.db.Begin(c)
  41. }
  42. func hitAllowanceInfo(mid int64) int64 {
  43. return mid % 10
  44. }
  45. func hitAllowanceChangeLog(mid int64) int64 {
  46. return mid % 10
  47. }
  48. func hitViewInfo(mid int64) int64 {
  49. return mid % 100
  50. }
  51. // BatchList query batch list.
  52. func (d *Dao) BatchList(c context.Context, appid int64, t int8) (res []*model.CouponBatchInfo, err error) {
  53. var (
  54. rows *sql.Rows
  55. sql = _batchList
  56. )
  57. if appid != 0 {
  58. sql += fmt.Sprintf(" AND `app_id` = %d", appid)
  59. }
  60. if t != 0 {
  61. sql += fmt.Sprintf(" AND `coupon_type` = %d", t)
  62. }
  63. if rows, err = d.db.Query(c, sql); err != nil {
  64. err = errors.WithStack(err)
  65. return
  66. }
  67. defer rows.Close()
  68. for rows.Next() {
  69. r := &model.CouponBatchInfo{}
  70. if err = rows.Scan(&r.ID, &r.AppID, &r.Name, &r.BatchToken, &r.MaxCount, &r.CurrentCount, &r.StartTime, &r.ExpireTime, &r.ExpireDay, &r.Ver,
  71. &r.Ctime, &r.Mtime, &r.LimitCount, &r.Operator, &r.FullAmount, &r.Amount, &r.State, &r.CouponType, &r.PlatformLimit, &r.ProdLimMonth, &r.ProdLimRenewal); err != nil {
  72. err = errors.WithStack(err)
  73. res = nil
  74. return
  75. }
  76. res = append(res, r)
  77. }
  78. err = rows.Err()
  79. return
  80. }
  81. // BatchViewList query batch list.
  82. func (d *Dao) BatchViewList(c context.Context, appid int64, batchToken string, t int8) (res []*model.CouponBatchInfo, err error) {
  83. var (
  84. rows *sql.Rows
  85. sql = _batchList
  86. )
  87. if appid != 0 {
  88. sql += fmt.Sprintf(" AND app_id = %d", appid)
  89. }
  90. if t != 0 {
  91. sql += fmt.Sprintf(" AND coupon_type = %d", t)
  92. }
  93. if len(batchToken) > 0 {
  94. sql += fmt.Sprintf(" AND batch_token='%v'", batchToken)
  95. }
  96. if rows, err = d.db.Query(c, sql); err != nil {
  97. err = errors.WithStack(err)
  98. return
  99. }
  100. defer rows.Close()
  101. for rows.Next() {
  102. r := &model.CouponBatchInfo{}
  103. if err = rows.Scan(&r.ID, &r.AppID, &r.Name, &r.BatchToken, &r.MaxCount, &r.CurrentCount, &r.StartTime, &r.ExpireTime, &r.ExpireDay, &r.Ver,
  104. &r.Ctime, &r.Mtime, &r.LimitCount, &r.Operator, &r.FullAmount, &r.Amount, &r.State, &r.CouponType, &r.PlatformLimit, &r.ProdLimMonth, &r.ProdLimRenewal); err != nil {
  105. err = errors.WithStack(err)
  106. res = nil
  107. return
  108. }
  109. res = append(res, r)
  110. }
  111. err = rows.Err()
  112. return
  113. }
  114. // AddBatchInfo add batch info.
  115. func (d *Dao) AddBatchInfo(c context.Context, b *model.CouponBatchInfo) (a int64, err error) {
  116. var res xsql.Result
  117. if res, err = d.db.Exec(c, _addbatch, b.AppID, b.Name, b.BatchToken, b.MaxCount, b.CurrentCount, b.StartTime, b.ExpireTime,
  118. b.Ver, b.Ctime, b.LimitCount, b.Operator); err != nil {
  119. err = errors.WithStack(err)
  120. return
  121. }
  122. if a, err = res.RowsAffected(); err != nil {
  123. err = errors.WithStack(err)
  124. }
  125. return
  126. }
  127. // AllAppInfo all app info.
  128. func (d *Dao) AllAppInfo(c context.Context) (res []*model.AppInfo, err error) {
  129. var rows *sql.Rows
  130. if rows, err = d.db.Query(c, _appAppInfoSQL); err != nil {
  131. err = errors.WithStack(err)
  132. return
  133. }
  134. defer rows.Close()
  135. for rows.Next() {
  136. r := &model.AppInfo{}
  137. if err = rows.Scan(&r.ID, &r.Name, &r.Appkey, &r.NotifyURL, &r.Ctime, &r.Mtime); err != nil {
  138. err = errors.WithStack(err)
  139. res = nil
  140. return
  141. }
  142. res = append(res, r)
  143. }
  144. err = rows.Err()
  145. return
  146. }
  147. // AddAllowanceBatchInfo add allowance batch info.
  148. func (d *Dao) AddAllowanceBatchInfo(c context.Context, b *model.CouponBatchInfo) (a int64, err error) {
  149. var res xsql.Result
  150. if res, err = d.db.Exec(c, _addAllowancebatch, b.AppID, b.Name, b.BatchToken, b.MaxCount, b.CurrentCount, b.StartTime, b.ExpireTime, b.ExpireDay,
  151. b.Ver, b.Ctime, b.LimitCount, b.Operator, b.FullAmount, b.Amount, b.State, b.CouponType, b.PlatformLimit, b.ProdLimMonth, b.ProdLimRenewal); err != nil {
  152. err = errors.WithStack(err)
  153. return
  154. }
  155. if a, err = res.RowsAffected(); err != nil {
  156. err = errors.WithStack(err)
  157. }
  158. return
  159. }
  160. // UpdateAllowanceBatchInfo update allowance batch info.
  161. func (d *Dao) UpdateAllowanceBatchInfo(c context.Context, b *model.CouponBatchInfo) (a int64, err error) {
  162. var res xsql.Result
  163. if res, err = d.db.Exec(c, _updateAllowanceBatchSQL, b.AppID, b.Name, b.MaxCount, b.LimitCount, b.Operator, b.PlatformLimit, b.ProdLimMonth, b.ProdLimRenewal, b.ID); err != nil {
  164. err = errors.WithStack(err)
  165. return
  166. }
  167. if a, err = res.RowsAffected(); err != nil {
  168. err = errors.WithStack(err)
  169. }
  170. return
  171. }
  172. // UpdateCodeBatchInfo update code batch info.
  173. func (d *Dao) UpdateCodeBatchInfo(c context.Context, b *model.CouponBatchInfo) (a int64, err error) {
  174. var res xsql.Result
  175. if res, err = d.db.Exec(c, _updateCodeBatchSQL, b.AppID, b.Name, b.LimitCount, b.Operator, b.PlatformLimit, b.ProdLimMonth, b.ProdLimRenewal, b.ID); err != nil {
  176. err = errors.WithStack(err)
  177. return
  178. }
  179. if a, err = res.RowsAffected(); err != nil {
  180. err = errors.WithStack(err)
  181. }
  182. return
  183. }
  184. // UpdateBatchStatus update batch status.
  185. func (d *Dao) UpdateBatchStatus(c context.Context, status int8, operator string, id int64) (a int64, err error) {
  186. var res xsql.Result
  187. if res, err = d.db.Exec(c, _updateBatchStatusSQL, status, operator, id); err != nil {
  188. err = errors.WithStack(err)
  189. return
  190. }
  191. if a, err = res.RowsAffected(); err != nil {
  192. err = errors.WithStack(err)
  193. }
  194. return
  195. }
  196. //BatchInfo batch info.
  197. func (d *Dao) BatchInfo(c context.Context, token string) (r *model.CouponBatchInfo, err error) {
  198. var row *sql.Row
  199. r = new(model.CouponBatchInfo)
  200. row = d.db.Master().QueryRow(c, _batchInfoSQL, token)
  201. if err = row.Scan(&r.ID, &r.AppID, &r.Name, &r.BatchToken, &r.MaxCount, &r.CurrentCount, &r.StartTime, &r.ExpireTime, &r.ExpireDay, &r.Ver,
  202. &r.Ctime, &r.Mtime, &r.LimitCount, &r.Operator, &r.FullAmount, &r.Amount, &r.State, &r.CouponType, &r.PlatformLimit, &r.ProdLimMonth, &r.ProdLimRenewal); err != nil {
  203. if err == sql.ErrNoRows {
  204. err = nil
  205. r = nil
  206. return
  207. }
  208. err = errors.WithStack(err)
  209. return
  210. }
  211. return
  212. }
  213. //BatchInfoByID batch info by id.
  214. func (d *Dao) BatchInfoByID(c context.Context, id int64) (r *model.CouponBatchInfo, err error) {
  215. var row *sql.Row
  216. r = new(model.CouponBatchInfo)
  217. row = d.db.QueryRow(c, _batchInfoByIDSQL, id)
  218. if err = row.Scan(&r.ID, &r.AppID, &r.Name, &r.BatchToken, &r.MaxCount, &r.CurrentCount, &r.StartTime, &r.ExpireTime, &r.ExpireDay, &r.Ver,
  219. &r.Ctime, &r.Mtime, &r.LimitCount, &r.Operator, &r.FullAmount, &r.Amount, &r.State, &r.CouponType, &r.PlatformLimit, &r.ProdLimMonth, &r.ProdLimRenewal); err != nil {
  220. if err == sql.ErrNoRows {
  221. err = nil
  222. r = nil
  223. return
  224. }
  225. err = errors.WithStack(err)
  226. return
  227. }
  228. return
  229. }
  230. // UpdateAllowanceStatus update allowance status.
  231. func (d *Dao) UpdateAllowanceStatus(c context.Context, tx *sql.Tx, state int8, mid int64, token string, ver int64) (a int64, err error) {
  232. var res xsql.Result
  233. if res, err = tx.Exec(fmt.Sprintf(_updateAllowanceStateSQL, hitAllowanceInfo(mid)), state, token, ver); err != nil {
  234. err = errors.WithStack(err)
  235. return
  236. }
  237. if a, err = res.RowsAffected(); err != nil {
  238. err = errors.WithStack(err)
  239. }
  240. return
  241. }
  242. // AllowanceByToken query coupon by token.
  243. func (d *Dao) AllowanceByToken(c context.Context, mid int64, token string) (r *model.CouponAllowanceInfo, err error) {
  244. var row *sql.Row
  245. r = &model.CouponAllowanceInfo{}
  246. row = d.db.QueryRow(c, fmt.Sprintf(_couponAllowanceByTokenSQL, hitAllowanceInfo(mid)), token)
  247. if err = row.Scan(&r.ID, &r.CouponToken, &r.Mid, &r.State, &r.StartTime, &r.ExpireTime, &r.Origin, &r.Ver, &r.BatchToken,
  248. &r.OrderNO, &r.Amount, &r.FullAmount, &r.CTime, &r.MTime); err != nil {
  249. if err == sql.ErrNoRows {
  250. err = nil
  251. r = nil
  252. return
  253. }
  254. err = errors.WithStack(err)
  255. return
  256. }
  257. return
  258. }
  259. //InsertCouponAllowanceHistory insert coupon history .
  260. func (d *Dao) InsertCouponAllowanceHistory(c context.Context, tx *sql.Tx, l *model.CouponAllowanceChangeLog) (a int64, err error) {
  261. var res xsql.Result
  262. if res, err = tx.Exec(fmt.Sprintf(_addCouponAllowanceChangeLogSQL, hitAllowanceChangeLog(l.Mid)), l.CouponToken, l.OrderNO, l.Mid, l.State, l.Ctime, l.ChangeType); err != nil {
  263. err = errors.WithStack(err)
  264. return
  265. }
  266. if a, err = res.RowsAffected(); err != nil {
  267. err = errors.WithStack(err)
  268. }
  269. return
  270. }
  271. //AllowanceList allowance list.
  272. func (d *Dao) AllowanceList(c context.Context, arg *model.ArgAllowanceSearch) (res []*model.CouponAllowanceInfo, err error) {
  273. var (
  274. rows *sql.Rows
  275. whereSQL = " "
  276. )
  277. if arg.AppID != 0 {
  278. whereSQL += fmt.Sprintf(" AND `app_id` = %d ", arg.AppID)
  279. }
  280. if arg.CouponToken != "" {
  281. whereSQL += fmt.Sprintf(" AND `coupon_token` = '%s' ", arg.CouponToken)
  282. }
  283. if arg.OrderNO != "" {
  284. whereSQL += fmt.Sprintf(" AND `order_no` = '%s' ", arg.OrderNO)
  285. }
  286. if arg.BatchToken != "" {
  287. whereSQL += fmt.Sprintf(" AND `batch_token` = '%s' ", arg.BatchToken)
  288. }
  289. if rows, err = d.db.Query(c, fmt.Sprintf(_couponAllowancePageSQL, hitAllowanceInfo(arg.Mid), whereSQL), arg.Mid); err != nil {
  290. err = errors.WithStack(err)
  291. return
  292. }
  293. defer rows.Close()
  294. for rows.Next() {
  295. r := new(model.CouponAllowanceInfo)
  296. if err = rows.Scan(&r.ID, &r.CouponToken, &r.Mid, &r.State, &r.StartTime, &r.ExpireTime, &r.Origin, &r.Ver, &r.BatchToken, &r.OrderNO, &r.Amount, &r.FullAmount,
  297. &r.CTime, &r.MTime, &r.Remark); err != nil {
  298. err = errors.WithStack(err)
  299. res = nil
  300. return
  301. }
  302. res = append(res, r)
  303. }
  304. err = rows.Err()
  305. return
  306. }
  307. //AddViewBatch add view batch.
  308. func (d *Dao) AddViewBatch(c context.Context, arg *model.ArgCouponViewBatch) (err error) {
  309. if _, err = d.db.Exec(c, _addViewBatchSQL, arg.AppID, arg.Name, arg.BatchToken, arg.MaxCount, arg.CurrentCount, arg.StartTime, arg.ExpireTime, arg.Ver, arg.LimitCount, arg.CouponType, arg.Operator); err != nil {
  310. err = errors.WithStack(err)
  311. }
  312. return
  313. }
  314. //UpdateViewBatch update viewBatch
  315. func (d *Dao) UpdateViewBatch(c context.Context, arg *model.ArgCouponViewBatch) (err error) {
  316. if _, err = d.db.Exec(c, _updateViewBatchSQL, arg.AppID, arg.Name, arg.MaxCount, arg.LimitCount, arg.Operator, arg.Ver, arg.ID); err != nil {
  317. err = errors.WithStack(err)
  318. }
  319. return
  320. }
  321. //TxUpdateViewInfo update view info.
  322. func (d *Dao) TxUpdateViewInfo(tx *sql.Tx, status int8, couponToken string, mid int64) (err error) {
  323. if _, err = tx.Exec(fmt.Sprintf(_updateViewSQL, hitViewInfo(mid)), status, couponToken); err != nil {
  324. err = errors.WithStack(err)
  325. }
  326. return
  327. }
  328. //TxCouponViewLog tx add view log.
  329. func (d *Dao) TxCouponViewLog(tx *sql.Tx, arg *model.CouponChangeLog) (err error) {
  330. if _, err = tx.Exec(fmt.Sprintf(_addViewChangeLog, hitViewInfo(arg.Mid)), arg.CouponToken, arg.Mid, arg.State); err != nil {
  331. err = errors.WithStack(err)
  332. }
  333. return
  334. }
  335. //CouponViewInfo .
  336. func (d *Dao) CouponViewInfo(c context.Context, couponToken string, mid int64) (r *model.CouponInfo, err error) {
  337. row := d.db.QueryRow(c, fmt.Sprintf(_viewInfoSQL, hitViewInfo(mid)), couponToken)
  338. r = new(model.CouponInfo)
  339. if err = row.Scan(&r.CouponToken, &r.Mid, &r.BatchToken, &r.State, &r.OrderNo, &r.OID, &r.StartTime, &r.ExpireTime, &r.Ctime); err != nil {
  340. err = errors.WithStack(err)
  341. }
  342. return
  343. }
  344. //SearchViewCouponCount search view count.
  345. func (d *Dao) SearchViewCouponCount(c context.Context, arg *model.ArgSearchCouponView) (count int64, err error) {
  346. whereSQL := fmt.Sprintf(_searchViewCountSQL, hitViewInfo(arg.Mid))
  347. whereSQL += fmt.Sprintf(" AND mid=%v", arg.Mid)
  348. if len(arg.CouponToken) > 0 {
  349. whereSQL += fmt.Sprintf(" AND coupon_token='%v'", arg.CouponToken)
  350. }
  351. if len(arg.BatchTokens) > 0 {
  352. whereSQL += fmt.Sprintf(" AND batch_token IN('%v')", strings.Join(arg.BatchTokens, "','"))
  353. }
  354. row := d.db.QueryRow(c, whereSQL)
  355. if err = row.Scan(&count); err != nil {
  356. err = errors.WithStack(err)
  357. }
  358. return
  359. }
  360. //SearchViewCouponInfo search view coupon info .
  361. func (d *Dao) SearchViewCouponInfo(c context.Context, arg *model.ArgSearchCouponView) (res []*model.CouponInfo, err error) {
  362. var rows *sql.Rows
  363. whereSQL := fmt.Sprintf(_searchViewSQL, hitViewInfo(arg.Mid))
  364. whereSQL += fmt.Sprintf(" AND mid=%v", arg.Mid)
  365. if len(arg.CouponToken) > 0 {
  366. whereSQL += fmt.Sprintf(" AND coupon_token='%v'", arg.CouponToken)
  367. }
  368. if len(arg.BatchTokens) > 0 {
  369. whereSQL += fmt.Sprintf(" AND batch_token IN('%v')", strings.Join(arg.BatchTokens, "','"))
  370. }
  371. if arg.PN <= 0 {
  372. arg.PN = 1
  373. }
  374. if arg.PS <= 0 || arg.PS >= 100 {
  375. arg.PS = 20
  376. }
  377. whereSQL += fmt.Sprintf(" ORDER BY ID DESC LIMIT %v,%v", (arg.PN-1)*arg.PS, arg.PS)
  378. if rows, err = d.db.Query(c, whereSQL); err != nil {
  379. err = errors.WithStack(err)
  380. return
  381. }
  382. defer rows.Close()
  383. for rows.Next() {
  384. r := new(model.CouponInfo)
  385. if err = rows.Scan(&r.CouponToken, &r.Mid, &r.BatchToken, &r.State, &r.OrderNo, &r.OID, &r.StartTime, &r.ExpireTime, &r.Ctime, &r.Mtime); err != nil {
  386. err = errors.WithStack(err)
  387. return
  388. }
  389. res = append(res, r)
  390. }
  391. err = rows.Err()
  392. return
  393. }
  394. // BatchAddAllowanceCoupon batch add allowance coupon.
  395. func (d *Dao) BatchAddAllowanceCoupon(c context.Context, tx *sql.Tx, cps []*model.CouponAllowanceInfo) (a int64, err error) {
  396. var (
  397. buf bytes.Buffer
  398. res xsql.Result
  399. sql string
  400. )
  401. buf.WriteString(fmt.Sprintf(_batchAddAllowanceCouponSQL, hitAllowanceInfo(cps[0].Mid)))
  402. for _, v := range cps {
  403. buf.WriteString("('")
  404. buf.WriteString(v.CouponToken)
  405. buf.WriteString("',")
  406. buf.WriteString(strconv.FormatInt(v.Mid, 10))
  407. buf.WriteString(",")
  408. buf.WriteString(fmt.Sprintf("%d", v.State))
  409. buf.WriteString(",")
  410. buf.WriteString(strconv.FormatInt(v.StartTime, 10))
  411. buf.WriteString(",")
  412. buf.WriteString(strconv.FormatInt(v.ExpireTime, 10))
  413. buf.WriteString(",")
  414. buf.WriteString(fmt.Sprintf("%d", v.Origin))
  415. buf.WriteString(",'")
  416. buf.WriteString(v.BatchToken)
  417. buf.WriteString("',")
  418. buf.WriteString(fmt.Sprintf("%f", v.Amount))
  419. buf.WriteString(",")
  420. buf.WriteString(fmt.Sprintf("%f", v.FullAmount))
  421. buf.WriteString(",'")
  422. buf.WriteString(v.CTime.Time().Format("2006-01-02 15:04:05"))
  423. buf.WriteString("',")
  424. buf.WriteString(strconv.FormatInt(v.AppID, 10))
  425. buf.WriteString("),")
  426. }
  427. sql = buf.String()
  428. if res, err = tx.Exec(sql[0 : len(sql)-1]); err != nil {
  429. err = errors.WithStack(err)
  430. return
  431. }
  432. if a, err = res.RowsAffected(); err != nil {
  433. err = errors.WithStack(err)
  434. }
  435. return
  436. }
  437. // UpdateBatchInfo update batch info.
  438. func (d *Dao) UpdateBatchInfo(c context.Context, tx *sql.Tx, token string, count int) (a int64, err error) {
  439. var res xsql.Result
  440. if res, err = tx.Exec(_updateBatchSQL, count, token); err != nil {
  441. err = errors.WithStack(err)
  442. return
  443. }
  444. if a, err = res.RowsAffected(); err != nil {
  445. err = errors.WithStack(err)
  446. return
  447. }
  448. return
  449. }