|
|
@@ -91,6 +91,57 @@ func DealUserVirtualCoin(session *xorm.Session, req md.DealUserVirtualCoinReq) ( |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// DealUserVirtualCoinMinus 处理给用户虚拟币积分(可以扣到负数) |
|
|
|
func DealUserVirtualCoinMinus(session *xorm.Session, req md.DealUserVirtualCoinReq) (err error) { |
|
|
|
if req.Amount < 0 { |
|
|
|
req.Amount = 0 |
|
|
|
} |
|
|
|
|
|
|
|
//2、计算&&组装数据 |
|
|
|
now := time.Now() |
|
|
|
coinAmount, err := GetUserCoinAmount(session, req.CoinId, req.Uid) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
coinAmountValue := decimal.NewFromFloat(zhios_order_relate_utils.StrToFloat64(coinAmount)) |
|
|
|
amountValue := decimal.NewFromFloat(req.Amount).RoundFloor(4) |
|
|
|
|
|
|
|
var userVirtualCoinFlow model.UserVirtualCoinFlow |
|
|
|
userVirtualCoinFlow.CoinId = req.CoinId |
|
|
|
userVirtualCoinFlow.Title = req.Title |
|
|
|
userVirtualCoinFlow.TransferType = req.TransferType |
|
|
|
userVirtualCoinFlow.Uid = req.Uid |
|
|
|
userVirtualCoinFlow.BeforeAmount = coinAmount |
|
|
|
userVirtualCoinFlow.Amount = amountValue.String() |
|
|
|
userVirtualCoinFlow.CreateAt = now.Format("2006-01-02 15:04:05") |
|
|
|
|
|
|
|
if req.Kind == "add" { |
|
|
|
userVirtualCoinFlow.Direction = 1 |
|
|
|
userVirtualCoinFlow.AfterAmount = coinAmountValue.Add(amountValue).RoundFloor(8).String() |
|
|
|
} else if req.Kind == "sub" { |
|
|
|
userVirtualCoinFlow.Direction = 2 |
|
|
|
userVirtualCoinFlow.AfterAmount = coinAmountValue.Sub(amountValue).RoundFloor(8).String() |
|
|
|
} else { |
|
|
|
err = errors.New("错误的kind类型") |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
//3、插入 `user_virtual_coin_flow` 记录 |
|
|
|
userVirtualCoinFlowDb := implement.NewUserVirtualCoinFlowDb(session.Engine()) |
|
|
|
_, err = userVirtualCoinFlowDb.UserVirtualCoinFlowInsertBySession(session, &userVirtualCoinFlow) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
//4、修改 `user_virtual_amount`的amount值 && 及缓存 |
|
|
|
err = SetCacheUserVirtualAmount(session, userVirtualCoinFlow.AfterAmount, req.CoinId, req.Uid, true) |
|
|
|
if err != nil { |
|
|
|
return err |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
// GetUserCoinAmount 获取用户虚拟积分余额 |
|
|
|
func GetUserCoinAmount(session *xorm.Session, coinId int, uid int64) (amount string, err error) { |
|
|
|
redisKey := fmt.Sprintf(md.UserVirtualAmountRedisKey, coinId, uid) |
|
|
|