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 ReduceCoupon(eg *xorm.Engine, req md.CouponCheckRequest) error { r := CommCheck(eg, req) fmt.Println(r) if r["is_can_buy"] == "0" && req.IsMustReduce != "1" { return zhios_tool_e.NewErr(400, "暂不能购买") } if r["is_show_coupon_list"] == "0" && req.IsMustReduce != "1" { return nil } //判断额度够不够 sum, _ := db.GetCouponUserTotalByLeaveSumWithType(eg, zhios_tool_utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value") fmt.Println(sum) if sum < zhios_tool_utils.StrToFloat64(req.CouponAmount) && req.IsMustReduce != "1" { return zhios_tool_e.NewErr(400, "额度不足") } session := eg.NewSession() defer session.Close() //查出该用户有的券额 list, err := db.GetCouponUserTotalByLeaveSumWithTypeSession(session, zhios_tool_utils.StrToInt(req.Uid), req.PvdType) if list == nil || err != nil { session.Rollback() return err } //负数的记录 negative, _ := db.GetCouponUserTotalByNegative(eg, zhios_tool_utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value") //循环出来判断扣钱 var leaveAmount = zhios_tool_utils.StrToFloat64(req.CouponAmount) num := 0 one, err := db.CouponUserOrdByGid(session, zhios_tool_utils.StrToInt(req.Uid), "1", req.Gid, req.Pvd) if err != nil { session.Rollback() return err } if one != nil { if req.Pvd == "oil" { //加油判断如果有了就不加了 return err } num = one.Batch + 1 } var count = len(*list) for k, v := range *list { LeaveCouponAmountValue := v.LeaveCouponAmountValue if leaveAmount == 0 || (zhios_tool_utils.StrToFloat64(v.LeaveCouponAmountValue) == 0 && req.IsMustReduce != "1") { //没有了 continue } //减去负数的金额 var amount = zhios_tool_utils.StrToFloat64(v.LeaveCouponAmountValue) //当前能使用的额度 var canUseAmount = zhios_tool_utils.StrToFloat64(v.LeaveCouponAmountValue) + negative var basicAmount = amount - canUseAmount if canUseAmount > 0 { amount = canUseAmount negative = 0 } else { continue } var leave = amount - leaveAmount var reduceAmount = leaveAmount //如果大于等于0 if leave >= 0 { leaveAmount = 0 } //如果小于0 if leave < 0 && k+1 != count { //如果不是最后一个 reduceAmount = amount leave = 0 //被扣完了 leaveAmount = leaveAmount - amount } leave = leave + basicAmount v.LeaveCouponAmountValue = zhios_tool_utils.Float64ToStr(leave) fmt.Println(v.LeaveCouponAmountValue) has, err := db.CouponUserTotalByInsertWithUpdate(session, &v) if (has == 0 && leave != amount) || err != nil { fmt.Println(err) session.Rollback() return zhios_tool_e.NewErr(400, "抵扣失败,暂不能购买!") } if reduceAmount != 0 { //写入明细 var tmp = &model.CommCouponUserOrd{ Uid: v.Uid, PayMethod: 0, OrdType: 3, UsePvd: v.UsePvd, PackageId: 0, CouponAmountValue: zhios_tool_utils.Float64ToStr(reduceAmount), PayAmountValue: "", Discount: "", PayAmount: req.CouponAmount, CreateAt: time.Now(), UpdateAt: time.Now(), PvdType: v.PvdType, GoodsType: req.Pvd, Type: 1, Gid: req.Gid, Batch: num, ExpectedRefundTime: int(time.Now().Unix()) + zhios_tool_utils.StrToInt(r["base_coupon_time"])*60, GoodsTitle: req.GoodsTitle, Oid: req.Oid, BeforeAmout: LeaveCouponAmountValue, AfterAmout: v.LeaveCouponAmountValue, } if req.CreateAt != "" { tmp.CreateAt = zhios_tool_utils.TimeParseStd(req.CreateAt) } bools := db.CouponUserOrdByInsert(session, tmp) if bools == false { session.Rollback() return zhios_tool_e.NewErr(400, "抵扣失败,暂不能购买!") } } } if req.IsChangeState == "1" { //查询优惠券使用记录 couponList, _ := db.CouponUserOrdByGidAscBatch(session, req.Uid, req.Gid, 0, req.Pvd) if couponList != nil { couponAll, _ := db.CouponUserOrdByGidAscBatchALL(session, req.Uid, req.Gid, 0, couponList.Batch, req.Pvd) if couponAll != nil { for _, cv := range *couponAll { //更新使用状态 cv.RefundState = 1 db.CouponUserOrdByUpdate(session, &cv) } } } } session.Commit() return nil }