|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- 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/e"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.git/md"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_coupon.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 e.NewErr(400, "暂不能购买")
- }
- if r["is_show_coupon_list"] == "0" && req.IsMustReduce != "1" {
- return nil
- }
- //判断额度够不够
- sum, _ := db.GetCouponUserTotalByLeaveSumWithType(eg, utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value")
- fmt.Println(sum)
- if sum < utils.StrToFloat64(req.CouponAmount) && req.IsMustReduce != "1" {
- return e.NewErr(400, "额度不足")
- }
- session := eg.NewSession()
- defer session.Close()
- //查出该用户有的券额
- list, err := db.GetCouponUserTotalByLeaveSumWithTypeSession(session, utils.StrToInt(req.Uid), req.PvdType)
- if list == nil || err != nil {
- session.Rollback()
- return err
- }
- //负数的记录
- negative, _ := db.GetCouponUserTotalByNegative(eg, utils.StrToInt(req.Uid), req.PvdType, "leave_coupon_amount_value")
- //循环出来判断扣钱
- var leaveAmount = utils.StrToFloat64(req.CouponAmount)
- num := 0
- one, err := db.CouponUserOrdByGid(session, 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 || (utils.StrToFloat64(v.LeaveCouponAmountValue) == 0 && req.IsMustReduce != "1") { //没有了
- continue
- }
- //减去负数的金额
- var amount = utils.StrToFloat64(v.LeaveCouponAmountValue)
- //当前能使用的额度
- var canUseAmount = 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 = 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 e.NewErr(400, "抵扣失败,暂不能购买!")
- }
-
- if reduceAmount != 0 {
- //写入明细
- var tmp = &model.CommCouponUserOrd{
- Uid: v.Uid,
- PayMethod: 0,
- OrdType: 3,
- UsePvd: v.UsePvd,
- PackageId: 0,
- CouponAmountValue: 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()) + utils.StrToInt(r["base_coupon_time"])*60,
- GoodsTitle: req.GoodsTitle,
- Oid: req.Oid,
- BeforeAmout: LeaveCouponAmountValue,
- AfterAmout: v.LeaveCouponAmountValue,
- }
- if req.CreateAt != "" {
- tmp.CreateAt = utils.TimeParseStd(req.CreateAt)
- }
- bools := db.CouponUserOrdByInsert(session, tmp)
- if bools == false {
- session.Rollback()
- return 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
- }
|