promotion.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/library/cache/redis"
  9. xsql "go-common/library/database/sql"
  10. "go-common/library/log"
  11. )
  12. const (
  13. //insert
  14. _createPromo = "insert into promotion (promo_id,type,item_id,sku_id,extra,expire_sec,sku_count,amount,buyer_count,begin_time,end_time,status,priv_sku_id,usable_coupons) values(?,?,?,?,?,?,?,?,?,?,?,1,?,?)"
  15. //update
  16. _operatePromo = "update promotion set status = ? where promo_id = ? and status= ?"
  17. _updatePromoBuyerCount = "update promotion set buyer_count = buyer_count + ? where promo_id ="
  18. _updatePromo = "update promotion set amount = %v,expire_sec = %v,sku_count = %v,begin_time = %v,end_time = %v,priv_sku_id = %v,usable_coupons = '%v' where promo_id = %v"
  19. //select
  20. _promo = "select promo_id,type,item_id,sku_id,extra,expire_sec,sku_count,amount,buyer_count,begin_time,end_time,status,ctime,mtime,priv_sku_id,usable_coupons from promotion where promo_id = ?"
  21. _selctPromoBySKU = "select count(*) as num from promotion where end_time >= ? and begin_time <= ? and sku_id = ? and status = 1"
  22. _getPromoList = "select promo_id,type,item_id,sku_id,extra,expire_sec,sku_count,amount,buyer_count,begin_time,end_time,status,ctime,mtime,priv_sku_id,usable_coupons from promotion ? order by mtime desc limit ?,?"
  23. )
  24. //keyPromo 获取活动缓存key
  25. func keyPromo(promoID int64) string {
  26. return fmt.Sprintf(model.CacheKeyPromo, promoID)
  27. }
  28. //RawPromo get promo info from db
  29. func (d *Dao) RawPromo(c context.Context, promoID int64) (res *model.Promotion, err error) {
  30. res = new(model.Promotion)
  31. row := d.db.QueryRow(c, _promo, promoID)
  32. if err = row.Scan(&res.PromoID, &res.Type, &res.ItemID, &res.SKUID, &res.Extra, &res.ExpireSec, &res.SKUCount, &res.Amount, &res.BuyerCount, &res.BeginTime, &res.EndTime, &res.Status, &res.Ctime, &res.Mtime, &res.PrivSKUID, &res.UsableCoupons); err != nil {
  33. if err == sql.ErrNoRows {
  34. err = nil
  35. res = nil
  36. }
  37. return
  38. }
  39. return
  40. }
  41. //CachePromo get promo info from cache
  42. func (d *Dao) CachePromo(c context.Context, promoID int64) (res *model.Promotion, err error) {
  43. var (
  44. data []byte
  45. key = keyPromo(promoID)
  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. //AddCachePromo add promo info into cache
  59. func (d *Dao) AddCachePromo(c context.Context, promoID int64, promo *model.Promotion) (err error) {
  60. var (
  61. data []byte
  62. key = keyPromo(promoID)
  63. )
  64. conn := d.redis.Get(c)
  65. defer conn.Close()
  66. if data, err = json.Marshal(promo); err != nil {
  67. return
  68. }
  69. conn.Do("SET", key, data, "EX", model.RedisExpirePromo)
  70. return
  71. }
  72. //DelCachePromo delete promo cache
  73. func (d *Dao) DelCachePromo(c context.Context, promoID int64) {
  74. var key = keyPromo(promoID)
  75. conn := d.redis.Get(c)
  76. defer conn.Close()
  77. conn.Do("DEL", key)
  78. }
  79. //CreatePromo create user promo order
  80. func (d *Dao) CreatePromo(c context.Context, promo *model.Promotion) (id int64, err error) {
  81. if _, err = d.db.Exec(c, _createPromo, promo.PromoID, promo.Type, promo.ItemID, promo.SKUID, promo.Extra, promo.ExpireSec, promo.SKUCount, promo.Amount, promo.BuyerCount, promo.BeginTime, promo.EndTime, promo.PrivSKUID, promo.UsableCoupons); err != nil {
  82. log.Warn("创建拼团活动失败:%d", promo.PromoID)
  83. return
  84. }
  85. id = promo.PromoID
  86. return
  87. }
  88. //OperatePromo 修改拼团活动状态
  89. func (d *Dao) OperatePromo(c context.Context, promoID int64, fromStatus int16, toStatus int16) (num int64, err error) {
  90. var res sql.Result
  91. if res, err = d.db.Exec(c, _operatePromo, toStatus, promoID, fromStatus); err != nil {
  92. log.Warn("更新拼团状态失败:%d", promoID)
  93. return
  94. }
  95. num, err = res.RowsAffected()
  96. return
  97. }
  98. //HasPromoOfSKU 判断skuID是否有正在上架的拼团活动
  99. func (d *Dao) HasPromoOfSKU(c context.Context, skuID int64, beginTime int64, endTime int64) (num int64, err error) {
  100. var res *xsql.Row
  101. if res = d.db.QueryRow(c, _selctPromoBySKU, skuID, beginTime, endTime); err != nil {
  102. return
  103. }
  104. err = res.Scan(&num)
  105. return
  106. }
  107. //GetPromoList 获取拼团列表
  108. func (d *Dao) GetPromoList(c context.Context, where string, index int64, size int64) (res []*model.Promotion, err error) {
  109. var rows *xsql.Rows
  110. if rows, err = d.db.Query(c, _getPromoList, where, index, size); err != nil {
  111. return
  112. }
  113. defer rows.Close()
  114. for rows.Next() {
  115. r := new(model.Promotion)
  116. if err = rows.Scan(&r.PromoID, &r.Type, &r.ItemID, &r.SKUID, &r.Extra, &r.ExpireSec, &r.SKUCount, &r.Amount, &r.BuyerCount, &r.BeginTime, &r.EndTime, &r.Status, &r.Ctime, &r.Mtime, &r.PrivSKUID, &r.UsableCoupons); err != nil {
  117. return
  118. }
  119. res = append(res, r)
  120. }
  121. return
  122. }
  123. //TxUpdatePromoBuyerCount 更新活动的购买人数
  124. func (d *Dao) TxUpdatePromoBuyerCount(c context.Context, tx *xsql.Tx, promoID int64, count int64) (number int64, err error) {
  125. var res sql.Result
  126. if res, err = tx.Exec(_updatePromoBuyerCount, count, promoID); err != nil {
  127. return
  128. }
  129. return res.RowsAffected()
  130. }
  131. //UpdatePromo 更新拼团
  132. func (d *Dao) UpdatePromo(c context.Context, arg *model.Promotion) (num int64, err error) {
  133. var res sql.Result
  134. if res, err = d.db.Exec(c, fmt.Sprintf(_updatePromo, arg.Amount, arg.ExpireSec, arg.SKUCount, arg.BeginTime, arg.EndTime, arg.PrivSKUID, arg.UsableCoupons, arg.PromoID)); err != nil {
  135. log.Warn("更新拼团活动%d失败", arg.PromoID)
  136. return
  137. }
  138. return res.RowsAffected()
  139. }