|
- package svc
-
- import (
- "applet/app/db"
- "applet/app/db/model"
- "applet/app/utils"
- "applet/app/utils/logx"
- "time"
- "xorm.io/xorm"
- )
-
- //公共处理记录
- func DealMoneyWithEg(eg *xorm.Engine, uid int, paidPrice string, orderAction int, ordId int64, id int64, goodsId int, ItemTitle string, ordType string, is_reduce int) {
- if utils.StrToFloat64(paidPrice) == 0 {
- return
- }
- //TODO 暂时退到余额
- session := eg.NewSession()
- userProfile, err := db.UserProfileFindByIdWithSession(session, uid)
- if err != nil || userProfile == nil {
- _ = session.Rollback()
- return
- }
- // 更新用户余额
- beforeAmount := userProfile.FinValid
- var types = 0
- if is_reduce == 1 {
- types = 1
- userProfile.FinValid = utils.AnyToString(utils.AnyToFloat64(userProfile.FinValid) - utils.StrToFloat64(paidPrice))
- } else {
- userProfile.FinValid = utils.AnyToString(utils.AnyToFloat64(userProfile.FinValid) + utils.StrToFloat64(paidPrice))
- }
- userProfile.FinTotal = userProfile.FinTotal + utils.StrToFloat32(paidPrice)
- affected, err := db.UserProfileUpdateWithSession(session, uid, userProfile, "fin_valid", "fin_total")
- if affected == 0 {
- _ = session.Rollback()
- return
- }
- if err != nil {
- _ = session.Rollback()
- return
- }
- // 开始写入流水
- FlowInsert(eg, uid, paidPrice, orderAction, ordId, id, goodsId, ItemTitle, ordType, types, beforeAmount, userProfile.FinValid)
- }
-
- // 开始写入流水
- 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
- }
- }
|