优惠券额度包
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

coupon_reduce.go 4.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package zyos_go_coupon
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db"
  4. "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db/model"
  5. "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/e"
  6. "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/md"
  7. "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/utils"
  8. "fmt"
  9. "time"
  10. "xorm.io/xorm"
  11. )
  12. //优惠券扣钱
  13. func ReduceCoupon(eg *xorm.Engine, req md.CouponCheckRequest) error {
  14. r := CommCheck(eg, req)
  15. fmt.Println(r)
  16. if r["is_can_buy"] == "0" && req.IsMustReduce != "1" {
  17. return e.NewErr(400, "暂不能购买")
  18. }
  19. if r["is_show_coupon_list"] == "0" && req.IsMustReduce != "1" {
  20. return nil
  21. }
  22. //判断额度够不够
  23. sum, _ := db.GetCouponUserTotalByLeaveSumWithType(eg, utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value")
  24. fmt.Println(sum)
  25. if sum < utils.StrToFloat64(req.CouponAmount) && req.IsMustReduce != "1" {
  26. return e.NewErr(400, "额度不足")
  27. }
  28. session := eg.NewSession()
  29. defer session.Close()
  30. //查出该用户有的券额
  31. list, err := db.GetCouponUserTotalByLeaveSumWithTypeSession(session, utils.StrToInt(req.Uid), req.PvdType)
  32. if list == nil || err != nil {
  33. session.Rollback()
  34. return err
  35. }
  36. //负数的记录
  37. negative, _ := db.GetCouponUserTotalByNegative(eg, utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value")
  38. //循环出来判断扣钱
  39. var leaveAmount = utils.StrToFloat64(req.CouponAmount)
  40. num := 0
  41. one, err := db.CouponUserOrdByGid(session, utils.StrToInt(req.Uid), "1", req.Gid, req.Pvd)
  42. if err != nil {
  43. session.Rollback()
  44. return err
  45. }
  46. if one != nil {
  47. if req.Pvd == "oil" { //加油判断如果有了就不加了
  48. return err
  49. }
  50. num = one.Batch + 1
  51. }
  52. var count = len(*list)
  53. for k, v := range *list {
  54. LeaveCouponAmountValue := v.LeaveCouponAmountValue
  55. if leaveAmount == 0 || (utils.StrToFloat64(v.LeaveCouponAmountValue) == 0 && req.IsMustReduce != "1") { //没有了
  56. continue
  57. }
  58. //减去负数的金额
  59. var amount = utils.StrToFloat64(v.LeaveCouponAmountValue)
  60. //当前能使用的额度
  61. var canUseAmount = utils.StrToFloat64(v.LeaveCouponAmountValue) + negative
  62. var basicAmount = amount - canUseAmount
  63. if canUseAmount > 0 {
  64. amount = canUseAmount
  65. negative = 0
  66. } else {
  67. continue
  68. }
  69. var leave = amount - leaveAmount
  70. var reduceAmount = leaveAmount
  71. //如果大于等于0
  72. if leave >= 0 {
  73. leaveAmount = 0
  74. }
  75. //如果小于0
  76. if leave < 0 && k+1 != count { //如果不是最后一个
  77. reduceAmount = amount
  78. leave = 0 //被扣完了
  79. leaveAmount = leaveAmount - amount
  80. }
  81. leave = leave + basicAmount
  82. v.LeaveCouponAmountValue = utils.Float64ToStr(leave)
  83. fmt.Println(v.LeaveCouponAmountValue)
  84. has, err := db.CouponUserTotalByInsertWithUpdate(session, &v)
  85. if (has == 0 && leave != amount) || err != nil {
  86. fmt.Println(err)
  87. session.Rollback()
  88. return e.NewErr(400, "抵扣失败,暂不能购买!")
  89. }
  90. if reduceAmount != 0 {
  91. //写入明细
  92. var tmp = &model.CommCouponUserOrd{
  93. Uid: v.Uid,
  94. PayMethod: 0,
  95. OrdType: 3,
  96. UsePvd: v.UsePvd,
  97. PackageId: 0,
  98. CouponAmountValue: utils.Float64ToStr(reduceAmount),
  99. PayAmountValue: "",
  100. Discount: "",
  101. PayAmount: req.CouponAmount,
  102. CreateAt: time.Now(),
  103. UpdateAt: time.Now(),
  104. PvdType: v.PvdType,
  105. GoodsType: req.Pvd,
  106. Type: 1,
  107. Gid: req.Gid,
  108. Batch: num,
  109. ExpectedRefundTime: int(time.Now().Unix()) + utils.StrToInt(r["base_coupon_time"])*60,
  110. GoodsTitle: req.GoodsTitle,
  111. Oid: req.Oid,
  112. BeforeAmout: LeaveCouponAmountValue,
  113. AfterAmout: v.LeaveCouponAmountValue,
  114. }
  115. if req.CreateAt != "" {
  116. tmp.CreateAt = utils.TimeParseStd(req.CreateAt)
  117. }
  118. bools := db.CouponUserOrdByInsert(session, tmp)
  119. if bools == false {
  120. session.Rollback()
  121. return e.NewErr(400, "抵扣失败,暂不能购买!")
  122. }
  123. }
  124. }
  125. if req.IsChangeState == "1" {
  126. //查询优惠券使用记录
  127. couponList, _ := db.CouponUserOrdByGidAscBatch(session, req.Uid, req.Gid, 0, req.Pvd)
  128. if couponList != nil {
  129. couponAll, _ := db.CouponUserOrdByGidAscBatchALL(session, req.Uid, req.Gid, 0, couponList.Batch, req.Pvd)
  130. if couponAll != nil {
  131. for _, cv := range *couponAll {
  132. //更新使用状态
  133. cv.RefundState = 1
  134. db.CouponUserOrdByUpdate(session, &cv)
  135. }
  136. }
  137. }
  138. }
  139. session.Commit()
  140. return nil
  141. }