package svc import ( "applet/app/db" "applet/app/db/model" "applet/app/e" "applet/app/md" "applet/app/utils" "applet/app/utils/logx" "github.com/gin-gonic/gin" "time" "xorm.io/xorm" ) func BalancePay(c *gin.Context, money, oid string, types string) error { user, err := CheckUser(c) if user == nil || err != nil { return err } // 获取余额更新锁 cb, err := HandleBalanceDistributedLock(c.GetString("mid"), utils.IntToStr(user.Info.Uid), "balance_pay") if err != nil { return err } // 释放锁 if cb != nil { defer cb() } finValid := utils.AnyToFloat64(user.Profile.FinValid) needMoney := utils.AnyToFloat64(money) if finValid < needMoney { return e.NewErrCode(e.ERR_BALANCE_NOT_ENOUGH) } user.Profile.FinValid = utils.AnyToString(finValid - needMoney) affect, err := db.UserProfileUpdate(db.DBs[c.GetString("mid")], user.Profile.Uid, user.Profile, "fin_valid") if err != nil || affect != 1 { return err } var str = "" if types == md.CommunityTeam { str = "小店下单" } flowInsert(db.DBs[c.GetString("mid")], user.Profile.Uid, money, 21, utils.StrToInt64(oid), 0, 0, str+"余额支付", types, 1, utils.Float64ToStr(finValid), user.Profile.FinValid) return nil } func flowInsert(eg *xorm.Engine, uid int, paidPrice string, orderAction int, ordId int64, id int64, goodsId int, ItemTitle string, ordType string, types int, beforeAmount string, afterAmount string) { session := eg.NewSession() now := time.Now() if err := db.FinUserFlowInsertOneWithSession( session, &model.FinUserFlow{ Type: types, Uid: uid, Amount: paidPrice, BeforeAmount: beforeAmount, AfterAmount: afterAmount, OrdType: ordType, OrdId: utils.Int64ToStr(ordId), OrdAction: orderAction, OrdDetail: utils.IntToStr(goodsId), State: 2, OtherId: id, OrdTitle: ItemTitle, OrdTime: int(now.Unix()), CreateAt: now, UpdateAt: now, }); err != nil { _ = session.Rollback() _ = logx.Warn(err) return } }