package zyos_go_coupon import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db" "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/db/model" "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/md" "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/e" "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils" "fmt" "time" "xorm.io/xorm" ) //额度添加 func AddCoupon(session *xorm.Session, req *md.CouponCheckRequest) error { if req.PvdType == "" { req.PvdType = "ordinary" } //查出该用户有的券额 list, err := db.GetCouponUserTotalByOne(session, zhios_tool_utils.StrToInt(req.Uid), req.PvdType) if err != nil { return err } else if list == nil { list = &model.CommCouponUserTotal{ Uid: zhios_tool_utils.StrToInt(req.Uid), CreateAt: time.Now(), UpdateAt: time.Now(), PvdType: req.PvdType, } } LeaveCouponAmountValue := list.LeaveCouponAmountValue list.LeaveCouponAmountValue = zhios_tool_utils.Float64ToStr(zhios_tool_utils.FloatFormat(zhios_tool_utils.StrToFloat64(list.LeaveCouponAmountValue)+zhios_tool_utils.StrToFloat64(req.CouponAmount), 2)) ordType := 4 if req.Type == "" { list.CouponAmountValue = zhios_tool_utils.Float64ToStr(zhios_tool_utils.FloatFormat(zhios_tool_utils.StrToFloat64(list.CouponAmountValue)+zhios_tool_utils.StrToFloat64(req.CouponAmount), 2)) } if req.Type == "refund" { ordType = 5 } has, err := db.CouponUserTotalByInsertWithUpdate(session, list) if has == 0 || err != nil { fmt.Println(err) return err } //写入明细 var tmp = &model.CommCouponUserOrd{ Uid: list.Uid, PayMethod: 0, OrdType: ordType, UsePvd: list.UsePvd, PackageId: 0, CouponAmountValue: req.CouponAmount, PayAmountValue: "", Discount: "", PayAmount: "", CreateAt: time.Now(), UpdateAt: time.Now(), PvdType: list.PvdType, GoodsType: req.Pvd, Gid: req.Gid, GoodsTitle: req.GoodsTitle, BeforeAmout: LeaveCouponAmountValue, AfterAmout: list.LeaveCouponAmountValue, } bools := db.CouponUserOrdByInsert(session, tmp) if bools == false { return zhios_tool_e.NewErr(400, "失败") } return nil }