优惠券额度包
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

72 行
2.2 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/md"
  6. "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/e"
  7. "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.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, zhios_tool_utils.StrToInt(req.Uid), req.PvdType)
  19. if err != nil {
  20. return err
  21. } else if list == nil {
  22. list = &model.CommCouponUserTotal{
  23. Uid: zhios_tool_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 = zhios_tool_utils.Float64ToStr(zhios_tool_utils.FloatFormat(zhios_tool_utils.StrToFloat64(list.LeaveCouponAmountValue)+zhios_tool_utils.StrToFloat64(req.CouponAmount), 2))
  31. ordType := 4
  32. if req.Type == "" {
  33. list.CouponAmountValue = zhios_tool_utils.Float64ToStr(zhios_tool_utils.FloatFormat(zhios_tool_utils.StrToFloat64(list.CouponAmountValue)+zhios_tool_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 zhios_tool_e.NewErr(400, "失败")
  66. }
  67. return nil
  68. }