coupon_service.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package service
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "time"
  7. "xiaoniaokuaiyan.com/xiaoniao/cerror"
  8. "xiaoniaokuaiyan.com/xiaoniao/constants"
  9. "xiaoniaokuaiyan.com/xiaoniao/dal"
  10. "xiaoniaokuaiyan.com/xiaoniao/entity"
  11. "xiaoniaokuaiyan.com/xiaoniao/util"
  12. )
  13. type CouponService struct {
  14. dal.ICoupon
  15. }
  16. func (csrv *CouponService) AddCoupon(couponItem *entity.DiscountTicket) (interface{}, error) {
  17. if couponItem.Value <= 0 {
  18. return nil, errors.New("ticket value must bigger than zero")
  19. }
  20. if !util.IsMobile(couponItem.Mobile) {
  21. return nil, errors.New("wrong mobile value")
  22. }
  23. _, err := time.Parse("2006-01-02", couponItem.Deadline)
  24. if err != nil {
  25. return nil, errors.New("wrong deadline value")
  26. }
  27. dateStr := time.Now().Format("2006-01-02")
  28. if dateStr > couponItem.Deadline {
  29. return nil, errors.New("invalid deadline")
  30. }
  31. ctype := constants.CouponType(couponItem.TypeId)
  32. if ctype == constants.COUPONTYPE_LIMIT && couponItem.UseMaxValue <= 0 {
  33. return nil, errors.New("ticket UseMaxValue must bigger than zero")
  34. }
  35. return csrv.ICoupon.Save(couponItem)
  36. }
  37. func (csrv *CouponService) GetCoupon(id int) (interface{}, error) {
  38. return csrv.ICoupon.Get(id, "")
  39. }
  40. func (csrv *CouponService) ListCoupon(mobile string, status, pageIndex, pageSize int) (interface{}, error) {
  41. if !util.IsMobile(mobile) {
  42. return nil, errors.New("invalid mobile")
  43. }
  44. nowStr := time.Now().Format("2006-01-02")
  45. orderStr := "order by deadline desc"
  46. whereStr := " where mobile = ? and status = ?"
  47. params := []interface{}{
  48. mobile,
  49. status,
  50. }
  51. //未使用
  52. if status == 0 {
  53. orderStr = "order by deadline asc"
  54. whereStr += " and deadline >= ? "
  55. params = append(params, nowStr)
  56. } else if status == 2 { //已过期
  57. whereStr += " and deadline < ? "
  58. params[1] = 0
  59. params = append(params, nowStr)
  60. }
  61. whereStr += " and is_delete='N' "
  62. dataList := &[]entity.DiscountTicketDB{}
  63. return dal.List(dataList, "t_discount_ticket", "select * from t_discount_ticket ", orderStr, whereStr, params, dal.Pager{PageIndex: pageIndex, PageSize: pageSize})
  64. }
  65. func (csrv *CouponService) Exchange(codeStr string, coustomId int, mobile string) (interface{}, cerror.CError) {
  66. return csrv.ICoupon.Exchange(codeStr, coustomId, mobile)
  67. }
  68. func (csrv *CouponService) QueryFcode(codeStr string) (interface{}, error) {
  69. db := util.GetSqlDB()
  70. codeItem := &entity.Fcode{}
  71. err := db.Get(codeItem, "select * from t_fcode where code_str = ?", codeStr)
  72. if err != nil {
  73. if err == sql.ErrNoRows {
  74. return nil, nil
  75. }
  76. return nil, err
  77. }
  78. if err = codeItem.Valid(); err != nil {
  79. return nil, err
  80. }
  81. return codeItem, nil
  82. }
  83. func (csrv *CouponService) GetCouponByOrderId(orderId string) (interface{}, error) {
  84. return csrv.Get(0, orderId)
  85. }
  86. func (csrv *CouponService) CouponActList(mobile string, pid int) (interface{}, error) {
  87. db := util.GetSqlDB()
  88. sql := `select a.id,a.name,a.end_time,a.value,a.use_max_value,a.bind_products,a.type_id,count(t.id) as count
  89. from t_discount_activity a
  90. left join t_discount_ticket t on a.id = t.discount_activity_id and t.mobile = ?`
  91. where := " where a.is_delete = 0 and a.is_putaway=0 and a.end_time>CURRENT_DATE"
  92. if pid > 0 {
  93. where += fmt.Sprintf(" and (a.bind_products='' or a.bind_products is null or JSON_CONTAINS(a.bind_products,JSON_OBJECT('pid','%d')) )", pid)
  94. }
  95. group := " group by a.id "
  96. order := " order by a.created_at desc limit 20"
  97. var list []entity.DiscountActDB
  98. err := db.Select(&list, sql+where+group+order, mobile)
  99. if list == nil || len(list) == 0 {
  100. return nil, err
  101. }
  102. return list, nil
  103. }
  104. const coupon_lock = "COUPON:LOCK"
  105. func (csrv *CouponService) CouponReceive(id int, mobile string) (interface{}, error) {
  106. db := util.GetSqlDB()
  107. //查询是否存在
  108. var count int
  109. db.Get(&count, "select count(1) from t_discount_ticket where mobile = ? and discount_activity_id = ?", mobile, id)
  110. if count > 0 {
  111. return nil, fmt.Errorf("1::exists discount_activity_id :%d mobile:%s", id, mobile)
  112. }
  113. //考虑加锁
  114. err := util.SetLock(fmt.Sprintf("%s:%d:%s", coupon_lock, id, mobile))
  115. if err != nil {
  116. return nil, fmt.Errorf("5::someone get lock by id:%d mobile:%s", id, mobile)
  117. }
  118. //defer 去掉锁
  119. defer util.DelLock(fmt.Sprintf("%s:%d:%s", coupon_lock, id, mobile))
  120. tmp := entity.DiscountActDB{}
  121. sql := `select a.id,a.name,a.end_time,a.value,a.use_max_value,a.bind_products,a.type_id,count(t.id) as count
  122. from t_discount_activity a
  123. left join t_discount_ticket t on a.id = t.discount_activity_id and t.mobile = ? where a.id = ? and a.is_delete = 0 and a.is_putaway=0 and a.end_time>CURRENT_DATE `
  124. err = db.Get(&tmp, sql, mobile, id)
  125. if err != nil {
  126. return nil, fmt.Errorf("2:: not ")
  127. }
  128. if tmp.Count > 0 {
  129. return nil, fmt.Errorf("3:: ")
  130. }
  131. sql = "INSERT INTO t_discount_ticket(type_id, value, mobile, use_max_value, deadline,bind_products,discount_activity_id ) VALUES (?,?,?,?,?,?,?);"
  132. _, err = db.Exec(sql, tmp.TypeId, tmp.Value, mobile, tmp.UseMaxValue, tmp.EndTime, tmp.BindProducts, id)
  133. if err != nil {
  134. return nil, errors.New("4::db error")
  135. }
  136. return nil, nil
  137. }