优惠券额度包
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 rivejä
2.1 KiB

  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 AddCoupon(session *xorm.Session, req *md.CouponCheckRequest) error {
  14. if req.PvdType == "" {
  15. req.PvdType = "ordinary"
  16. }
  17. //查出该用户有的券额
  18. list, err := db.GetCouponUserTotalByOne(session, utils.StrToInt(req.Uid), req.PvdType)
  19. if err != nil {
  20. return err
  21. } else if list == nil {
  22. list = &model.CommCouponUserTotal{
  23. Uid: utils.StrToInt(req.Uid),
  24. CreateAt: time.Now(),
  25. UpdateAt: time.Now(),
  26. PvdType: req.PvdType,
  27. }
  28. }
  29. LeaveCouponAmountValue := list.LeaveCouponAmountValue
  30. list.LeaveCouponAmountValue = utils.Float64ToStr(utils.FloatFormat(utils.StrToFloat64(list.LeaveCouponAmountValue)+utils.StrToFloat64(req.CouponAmount), 2))
  31. ordType := 4
  32. if req.Type == "" {
  33. list.CouponAmountValue = utils.Float64ToStr(utils.FloatFormat(utils.StrToFloat64(list.CouponAmountValue)+utils.StrToFloat64(req.CouponAmount), 2))
  34. }
  35. if req.Type == "refund" {
  36. ordType = 5
  37. }
  38. has, err := db.CouponUserTotalByInsertWithUpdate(session, list)
  39. if has == 0 || err != nil {
  40. fmt.Println(err)
  41. return err
  42. }
  43. //写入明细
  44. var tmp = &model.CommCouponUserOrd{
  45. Uid: list.Uid,
  46. PayMethod: 0,
  47. OrdType: ordType,
  48. UsePvd: list.UsePvd,
  49. PackageId: 0,
  50. CouponAmountValue: req.CouponAmount,
  51. PayAmountValue: "",
  52. Discount: "",
  53. PayAmount: "",
  54. CreateAt: time.Now(),
  55. UpdateAt: time.Now(),
  56. PvdType: list.PvdType,
  57. GoodsType: req.Pvd,
  58. Gid: req.Gid,
  59. GoodsTitle: req.GoodsTitle,
  60. BeforeAmout: LeaveCouponAmountValue,
  61. AfterAmout: list.LeaveCouponAmountValue,
  62. }
  63. bools := db.CouponUserOrdByInsert(session, tmp)
  64. if bools == false {
  65. return e.NewErr(400, "失败")
  66. }
  67. return nil
  68. }