package svc import ( "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/db" "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/db/model" zhios_condition_statistics_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_condition_statistics.git/utils" "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/service_award_dividend" "errors" "fmt" "time" "xorm.io/xorm" ) func AddCouponRecord(eg *xorm.Engine, user *model.User, pvdType, coupon string) bool { changeUid := user.Uid //接收的用户 transformUserTotal, err := db.GetCouponUserTotalByOneEg(eg, changeUid, pvdType) if err != nil { return false } if transformUserTotal == nil { transformUserTotal = &model.CommCouponUserTotal{ Uid: changeUid, UsePvd: "", CouponAmountValue: "", CreateAt: time.Now(), UpdateAt: time.Now(), LeaveCouponAmountValue: "", PvdType: pvdType, } has, err := eg.InsertOne(transformUserTotal) if has == 0 || err != nil { fmt.Println("优惠券错误:", err) return false } } leaveAmount := zhios_condition_statistics_utils.StrToFloat64(coupon) beforeTotal := zhios_condition_statistics_utils.StrToFloat64(transformUserTotal.LeaveCouponAmountValue) transformUserTotal.CouponAmountValue = zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.StrToFloat64(transformUserTotal.CouponAmountValue)+leaveAmount, 9) transformUserTotal.LeaveCouponAmountValue = zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.StrToFloat64(transformUserTotal.LeaveCouponAmountValue)+leaveAmount, 9) eg.Where("id=?", transformUserTotal.Id).Update(transformUserTotal) //写入明细 var ord1 = &model.CommCouponUserOrd{ Uid: user.Uid, OrdType: 50, PackageId: 0, CouponAmountValue: zhios_condition_statistics_utils.Float64ToStr(leaveAmount), CreateAt: time.Now(), UpdateAt: time.Now(), PvdType: pvdType, UsePvd: "[\"GUIDE\",\"SELF_MALL\"]", GoodsTitle: "升级赠送", BeforeAmout: zhios_condition_statistics_utils.Float64ToStrByPrec(beforeTotal, 9), AfterAmout: transformUserTotal.LeaveCouponAmountValue, } eg.Insert(ord1) return true } func AddCoin(eg *xorm.Engine, money float64, Title, ordId string, types, transferType, uid, coinId int, amount string) (int64, error) { //定制客户存入购物金分期 req := map[string]string{ "coin_id": zhios_condition_statistics_utils.IntToStr(coinId), "pvd": "2", "uid": zhios_condition_statistics_utils.IntToStr(uid), "oid": ordId, "amount": zhios_condition_statistics_utils.Float64ToStr(money), "price": amount, } state := service_award_dividend.AddPeriodEg(eg, req) if state == 3 { return 0, errors.New("失败") } if state == 1 { return 1, nil } UserVirtualAmount, err := db.UserVirtualAmountFindById(eg, uid, coinId) if err != nil || UserVirtualAmount == nil { if err == nil { err = errors.New("获取用户余额信息失败") } return 0, err } beforeAmount := UserVirtualAmount.Amount afterAmount := "" if types == 1 { afterAmount = zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.AnyToFloat64(UserVirtualAmount.Amount)+money, 6) UserVirtualAmount.Amount = zhios_condition_statistics_utils.Truncate(zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.AnyToFloat64(UserVirtualAmount.Amount)+money, 5), 4) } else if types == 2 { afterAmount = zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.AnyToFloat64(UserVirtualAmount.Amount)-money, 6) UserVirtualAmount.Amount = zhios_condition_statistics_utils.Truncate(zhios_condition_statistics_utils.Float64ToStrByPrec(zhios_condition_statistics_utils.AnyToFloat64(UserVirtualAmount.Amount)-money, 5), 4) if zhios_condition_statistics_utils.StrToInt64(UserVirtualAmount.Amount) < 0 { err = errors.New("余额不足") return 0, err } } affected, err := db.UserVirtualAmountUpdate(eg, uid, coinId, UserVirtualAmount, "amount") if err != nil || affected == 0 { if err == nil { err = errors.New("更新用户余额信息失败") } return 0, err } id, err := virtualCoinFlowInsert(eg, uid, coinId, zhios_condition_statistics_utils.Float64ToStrByPrec(money, 8), ordId, Title, types, transferType, beforeAmount, afterAmount) if err != nil { return 0, err } return id, nil } func virtualCoinFlowInsert(eg *xorm.Engine, uid, coinId int, money string, ordId string, ItemTitle string, types, transferType int, beforeAmount string, afterAmount string) (int64, error) { now := time.Now() data := &model.UserVirtualCoinFlow{ Uid: uid, OrdId: ordId, CoinId: coinId, Direction: types, Title: ItemTitle, Amout: money, BeforeAmout: beforeAmount, AfterAmout: afterAmount, CreateTime: now, TransferType: transferType, Date: now.Format("2006-01"), } eg.Insert(data) return data.Id, nil }