promotion_order.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "fmt"
  7. "go-common/app/service/openplatform/ticket-sales/model"
  8. "go-common/app/service/openplatform/ticket-sales/model/consts"
  9. "go-common/library/cache/redis"
  10. xsql "go-common/library/database/sql"
  11. "go-common/library/log"
  12. xtime "time"
  13. )
  14. const (
  15. _addPromoOrder = "insert into promotion_order (promo_id,group_id,order_id,is_master,uid,status,ctime,sku_id) values(?,?,?,?,?,?,?,?)"
  16. _updatePromoOrderStatus = "update promotion_order set status = ? where order_id = ?"
  17. _updatePromoOrderGroupIDAndStatus = "update promotion_order set group_id = ?,status = ? where order_id = ?"
  18. _promoOrderByStatus = "select promo_id,group_id,order_id,is_master,uid,status,ctime,mtime,sku_id from promotion_order where promo_id = ? and group_id = ? and uid = ? and status = ?"
  19. _promoOrderDoing = "select promo_id,group_id,order_id,is_master,uid,status,ctime,mtime,sku_id from promotion_order where promo_id = ? and group_id = ? and uid = ? and status in (?,?)"
  20. _groupOrdersByStatus = "select promo_id,group_id,order_id,is_master,uid,status,ctime,mtime,sku_id from promotion_order where group_id = ? and status = ?"
  21. _promoOrdersByGroupID = "select promo_id,group_id,order_id,is_master,uid,status,ctime,mtime,sku_id from promotion_order where group_id = ? and status in (?,?,?)"
  22. _promoOrder = "select promo_id,group_id,order_id,is_master,uid,status,ctime,mtime,sku_id from promotion_order where order_id = ?"
  23. )
  24. //keyPromoOrder 获取拼团订单缓存key
  25. func keyPromoOrder(orderID int64) string {
  26. return fmt.Sprintf(model.CacheKeyPromoOrder, orderID)
  27. }
  28. //RawPromoOrder get promo order info from db
  29. func (d *Dao) RawPromoOrder(c context.Context, orderID int64) (res *model.PromotionOrder, err error) {
  30. res = new(model.PromotionOrder)
  31. row := d.db.QueryRow(c, _promoOrder, orderID)
  32. if err = row.Scan(&res.PromoID, &res.GroupID, &res.OrderID, &res.IsMaster, &res.UID, &res.Status, &res.Ctime, &res.Mtime, &res.SKUID); err != nil {
  33. if err == sql.ErrNoRows {
  34. err = nil
  35. res = nil
  36. }
  37. return
  38. }
  39. return
  40. }
  41. //CachePromoOrder get promo order info from cache
  42. func (d *Dao) CachePromoOrder(c context.Context, orderID int64) (res *model.PromotionOrder, err error) {
  43. var (
  44. data []byte
  45. key = keyPromoOrder(orderID)
  46. )
  47. conn := d.redis.Get(c)
  48. defer conn.Close()
  49. if data, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  50. if err == redis.ErrNil {
  51. err = nil
  52. }
  53. return
  54. }
  55. json.Unmarshal(data, &res)
  56. return
  57. }
  58. //AddCachePromoOrder add promo order info into cache
  59. func (d *Dao) AddCachePromoOrder(c context.Context, orderID int64, promoOrder *model.PromotionOrder) (err error) {
  60. var (
  61. data []byte
  62. key = keyPromoOrder(orderID)
  63. )
  64. conn := d.redis.Get(c)
  65. defer conn.Close()
  66. if data, err = json.Marshal(promoOrder); err != nil {
  67. return
  68. }
  69. conn.Do("SET", key, data, "EX", model.RedisExpirePromoOrder)
  70. return
  71. }
  72. //DelCachePromoOrder delete promo order cache
  73. func (d *Dao) DelCachePromoOrder(c context.Context, orderID int64) {
  74. var key = keyPromoOrder(orderID)
  75. conn := d.redis.Get(c)
  76. defer conn.Close()
  77. conn.Do("DEL", key)
  78. }
  79. //keyPromoOrders 获取拼团团订单缓存key
  80. func keyPromoOrders(groupID int64) string {
  81. return fmt.Sprintf(model.CacheKeyPromoOrders, groupID)
  82. }
  83. //RawPromoOrders get promo orders info from db
  84. func (d *Dao) RawPromoOrders(c context.Context, groupID int64) (res []*model.PromotionOrder, err error) {
  85. var rows *xsql.Rows
  86. if rows, err = d.db.Query(c, _promoOrdersByGroupID, groupID, consts.PromoOrderUnpaid, consts.PromoOrderPaid, consts.PromoOrderRefund); err != nil {
  87. return
  88. }
  89. defer rows.Close()
  90. for rows.Next() {
  91. r := new(model.PromotionOrder)
  92. if err = rows.Scan(&r.PromoID, &r.GroupID, &r.OrderID, &r.IsMaster, &r.UID, &r.Status, &r.Ctime, &r.Mtime, &r.SKUID); err != nil {
  93. return
  94. }
  95. res = append(res, r)
  96. }
  97. return
  98. }
  99. //CachePromoOrders get promo orders info from cache
  100. func (d *Dao) CachePromoOrders(c context.Context, groupID int64) (res []*model.PromotionOrder, err error) {
  101. var (
  102. data []byte
  103. key = keyPromoOrders(groupID)
  104. )
  105. conn := d.redis.Get(c)
  106. defer conn.Close()
  107. if data, err = redis.Bytes(conn.Do("GET", key)); err != nil {
  108. if err == redis.ErrNil {
  109. err = nil
  110. }
  111. return
  112. }
  113. json.Unmarshal(data, &res)
  114. return
  115. }
  116. //AddCachePromoOrders add promo orders info into cache
  117. func (d *Dao) AddCachePromoOrders(c context.Context, groupID int64, promoOrders []*model.PromotionOrder) (err error) {
  118. var (
  119. data []byte
  120. key = keyPromoOrders(groupID)
  121. )
  122. conn := d.redis.Get(c)
  123. defer conn.Close()
  124. if data, err = json.Marshal(promoOrders); err != nil {
  125. return
  126. }
  127. conn.Do("SET", key, data, "EX", model.RedisExpirePromoOrders)
  128. return
  129. }
  130. //DelCachePromoOrders delete promo orders cache
  131. func (d *Dao) DelCachePromoOrders(c context.Context, groupID int64) {
  132. var key = keyPromoOrders(groupID)
  133. conn := d.redis.Get(c)
  134. defer conn.Close()
  135. conn.Do("DEL", key)
  136. }
  137. //PromoOrderByStatus get user promo order by status
  138. func (d *Dao) PromoOrderByStatus(c context.Context, promoID int64, groupID int64, uid int64, status int16) (res *model.PromotionOrder, err error) {
  139. res = new(model.PromotionOrder)
  140. row := d.db.QueryRow(c, _promoOrderByStatus, promoID, groupID, uid, status)
  141. if err = row.Scan(&res.PromoID, &res.GroupID, &res.OrderID, &res.IsMaster, &res.UID, &res.Status, &res.Ctime, &res.Mtime, &res.SKUID); err != nil {
  142. return
  143. }
  144. return
  145. }
  146. //PromoOrderDoing get user promo order where status in unpaid and paid
  147. func (d *Dao) PromoOrderDoing(c context.Context, promoID int64, groupID int64, uid int64) (res *model.PromotionOrder, err error) {
  148. res = new(model.PromotionOrder)
  149. row := d.db.QueryRow(c, _promoOrderDoing, promoID, groupID, uid, consts.PromoOrderUnpaid, consts.PromoOrderPaid)
  150. if err = row.Scan(&res.PromoID, &res.GroupID, &res.OrderID, &res.IsMaster, &res.UID, &res.Status, &res.Ctime, &res.Mtime, &res.SKUID); err != nil {
  151. return
  152. }
  153. return
  154. }
  155. //AddPromoOrder create user promo order
  156. func (d *Dao) AddPromoOrder(c context.Context, promoID int64, groupID int64, orderID int64, isMaster int16, uid int64, status int16, skuID int64, ctime int64) (id int64, err error) {
  157. var (
  158. res sql.Result
  159. ctimeDate = xtime.Unix(ctime, 0)
  160. )
  161. if res, err = d.db.Exec(c, _addPromoOrder, promoID, groupID, orderID, isMaster, uid, status, ctimeDate, skuID); err != nil {
  162. log.Warn("创建活动订单%d失败", orderID)
  163. return
  164. }
  165. return res.LastInsertId()
  166. }
  167. //TxAddPromoOrder create user promo order
  168. func (d *Dao) TxAddPromoOrder(c context.Context, tx *xsql.Tx, promoID int64, groupID int64, orderID int64, isMaster int16, uid int64, status int16, skuID int64, ctime int64) (id int64, err error) {
  169. var (
  170. res sql.Result
  171. ctimeDate = xtime.Unix(ctime, 0)
  172. )
  173. if res, err = tx.Exec(_addPromoOrder, promoID, groupID, orderID, isMaster, uid, status, ctimeDate, skuID); err != nil {
  174. log.Warn("创建活动订单%d失败", orderID)
  175. return
  176. }
  177. return res.LastInsertId()
  178. }
  179. //UpdatePromoOrderStatus update promo order status
  180. func (d *Dao) UpdatePromoOrderStatus(c context.Context, orderID int64, status int16) (number int64, err error) {
  181. var (
  182. res sql.Result
  183. )
  184. if res, err = d.db.Exec(c, _updatePromoOrderStatus, status, orderID); err != nil {
  185. log.Warn("更新活动订单%d失败", orderID)
  186. return
  187. }
  188. return res.RowsAffected()
  189. }
  190. //TxUpdatePromoOrderStatus update promo order status
  191. func (d *Dao) TxUpdatePromoOrderStatus(c context.Context, tx *xsql.Tx, orderID int64, status int16) (number int64, err error) {
  192. var res sql.Result
  193. if res, err = tx.Exec(_updatePromoOrderStatus, status, orderID); err != nil {
  194. log.Warn("更新活动订单%d失败", orderID)
  195. return
  196. }
  197. return res.RowsAffected()
  198. }
  199. //TxUpdatePromoOrderGroupIDAndStatus update promo order groupid and status
  200. func (d *Dao) TxUpdatePromoOrderGroupIDAndStatus(c context.Context, tx *xsql.Tx, orderID int64, groupID int64, status int16) (number int64, err error) {
  201. var res sql.Result
  202. if res, err = tx.Exec(_updatePromoOrderGroupIDAndStatus, groupID, status, orderID); err != nil {
  203. log.Warn("更新活动订单%d失败", orderID)
  204. return
  205. }
  206. return res.RowsAffected()
  207. }
  208. //GroupOrdersByStatus 根据groupid和status获取活动订单
  209. func (d *Dao) GroupOrdersByStatus(c context.Context, groupID int64, status int16) (res []*model.PromotionOrder, err error) {
  210. var rows *xsql.Rows
  211. if rows, err = d.db.Query(c, _groupOrdersByStatus, groupID, status); err != nil {
  212. return
  213. }
  214. defer rows.Close()
  215. for rows.Next() {
  216. temp := new(model.PromotionOrder)
  217. if err = rows.Scan(&temp.PromoID, &temp.GroupID, &temp.OrderID, &temp.IsMaster, &temp.UID, &temp.Status, &temp.Ctime, &temp.Mtime, &temp.SKUID); err != nil {
  218. return
  219. }
  220. res = append(res, temp)
  221. }
  222. return
  223. }