coupon_dal.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package dal
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "errors"
  6. "time"
  7. "xiaoniaokuaiyan.com/xiaoniao/cerror"
  8. "xiaoniaokuaiyan.com/xiaoniao/constants"
  9. "xiaoniaokuaiyan.com/xiaoniao/entity"
  10. "xiaoniaokuaiyan.com/xiaoniao/util"
  11. )
  12. type Coupon struct {
  13. }
  14. func (cdal *Coupon) Get(id int, orderId string) (*entity.DiscountTicketDB, error) {
  15. db := util.GetSqlDB()
  16. var (
  17. strSql string
  18. paramValue interface{}
  19. )
  20. if id > 0 {
  21. strSql = "select * from t_discount_ticket where id = ?"
  22. paramValue = id
  23. } else {
  24. strSql = "select * from t_discount_ticket where order_id = ?"
  25. paramValue = orderId
  26. }
  27. citem := entity.DiscountTicketDB{}
  28. err := db.Get(&citem, strSql, paramValue)
  29. if err != nil {
  30. if err == sql.ErrNoRows {
  31. return nil, nil
  32. }
  33. return nil, err
  34. }
  35. return &citem, nil
  36. }
  37. func (cdal *Coupon) Save(item *entity.DiscountTicket) (*entity.DiscountTicket, error) {
  38. db := util.GetWriteSqlDB()
  39. sqlStr, kvm := util.GenerateInsertSqlFromStruct("t_discount_ticket", item)
  40. result, err := db.NamedExec(sqlStr, kvm)
  41. if err != nil {
  42. return nil, errors.New("db error")
  43. }
  44. cid, err := result.LastInsertId()
  45. if err != nil {
  46. return nil, err
  47. }
  48. item.Id = int(cid)
  49. return item, nil
  50. }
  51. func (cdal *Coupon) Update(item *entity.DiscountTicket) (*entity.DiscountTicket, error) {
  52. db := util.GetWriteSqlDB()
  53. sqlStr, kvm := util.GenerateUpdateSqlFromStruct("t_discount_ticket", item, " where id = :id")
  54. _, err := db.NamedExec(sqlStr, kvm)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return item, nil
  59. }
  60. func (cdal *Coupon) Exchange(codeStr string, customId int, mobile string) (interface{}, cerror.CError) {
  61. db := util.GetWriteSqlDB()
  62. strSql := "select * from t_fcode where code_str = ?"
  63. codeItem := &entity.Fcode{}
  64. err := db.Get(codeItem, strSql, codeStr)
  65. if err != nil {
  66. return nil, cerror.New(constants.ERRORCODE_FCODE_INVILID, "F码无效")
  67. }
  68. if err := codeItem.Valid(); err != nil {
  69. return nil, err
  70. }
  71. strSql = "select count(*) as num from t_fcode_custom where cid = ? and fid = ?"
  72. var num int
  73. db.Get(&num, strSql, customId, codeItem.Id)
  74. if num > 0 {
  75. return nil, cerror.New(constants.ERRORCODE_FCODE_ALREADY_GOT, "该用户已经使用此码兑换过优惠券")
  76. }
  77. var tlist = []*entity.DiscountTicket{}
  78. ctype := constants.CouponType(codeItem.TypeId)
  79. var ticketDeadline = codeItem.TicketDeadline
  80. if codeItem.TicketValidDays.Valid && codeItem.TicketValidDays.Int64 > 0 {
  81. ticketDeadline = time.Now().Add(time.Hour * 24 * time.Duration(codeItem.TicketValidDays.Int64)).Format("2006-01-02 15:04:05")
  82. }
  83. if ctype == constants.COUPONTYPE_COMPOSITION {
  84. tt := []struct {
  85. TypeId int `json:"type_id"`
  86. Value int `json:"ticket_value"`
  87. UseMaxValue int `json:"use_max_value"`
  88. BindProducts string `json:"bind_products"`
  89. }{}
  90. err = json.Unmarshal([]byte(codeItem.Remark.String), &tt)
  91. if err != nil {
  92. return nil, cerror.New(101, "unmarshal error")
  93. }
  94. for _, t := range tt {
  95. ticket := &entity.DiscountTicket{
  96. TypeId: t.TypeId,
  97. Mobile: mobile,
  98. Value: t.Value,
  99. Deadline: ticketDeadline,
  100. UseMaxValue: t.UseMaxValue,
  101. BindProducts: codeItem.BindProducts.String,
  102. }
  103. if t.BindProducts != "" {
  104. ticket.BindProducts = t.BindProducts
  105. }
  106. tlist = append(tlist, ticket)
  107. }
  108. } else {
  109. ticket := &entity.DiscountTicket{
  110. TypeId: codeItem.TypeId,
  111. Mobile: mobile,
  112. Value: codeItem.TicketValue,
  113. Deadline: ticketDeadline,
  114. UseMaxValue: codeItem.UseMaxValue,
  115. BindProducts: codeItem.BindProducts.String,
  116. }
  117. tlist = append(tlist, ticket)
  118. }
  119. for _, titem := range tlist {
  120. _, err = cdal.Save(titem)
  121. if err != nil {
  122. return nil, cerror.New(500, err.Error())
  123. }
  124. strSql = "insert into t_fcode_custom(cid, fid, qid) values(?,?,?)"
  125. _, err = db.Exec(strSql, customId, codeItem.Id, titem.Id)
  126. if err != nil {
  127. return nil, cerror.New(500, err.Error())
  128. }
  129. }
  130. db.Exec("update t_fcode set already_use_num = already_use_num + 1 where code_str = ?", codeStr)
  131. return tlist, nil
  132. }
  133. var DefaultCouponDal = &Coupon{}