Browse Source

update 绿色双链积分

master
DengBiao 9 months ago
parent
commit
aef7b2846e
2 changed files with 372 additions and 133 deletions
  1. +1
    -1
      db/db_green_coin_double_chain.go
  2. +371
    -132
      rule/green_coin_double_chain_settlement.go

+ 1
- 1
db/db_green_coin_double_chain.go View File

@@ -75,7 +75,7 @@ func GreenCoinDoubleChainUpdate(session *xorm.Session, id interface{}, GreenCoin
}

// GreenCoinDoubleChainGetOneByParams 通过传入的参数查询数据(单条)
func GreenCoinDoubleChainGetOneByParams(session *xorm.Session, params map[string]interface{}) (*model.GreenCoinDoubleChain, error) {
func GreenCoinDoubleChainGetOneByParams(session *xorm.Engine, params map[string]interface{}) (*model.GreenCoinDoubleChain, error) {
var m model.GreenCoinDoubleChain
var query = fmt.Sprintf("%s =?", params["key"])
has, err := session.Where(query, params["value"]).Get(&m)


+ 371
- 132
rule/green_coin_double_chain_settlement.go View File

@@ -23,7 +23,17 @@ func InitForGreenCoinDoubleChainIntegral(redisAddr string) (err error) {
return
}

func DealUserGreenCoinDoubleChainIntegral(Db *xorm.Engine, uid int, amount, ordId, masterId string) (isExchange bool, err error) {
func DealUserGreenCoinDoubleChainIntegral(Db *xorm.Engine, uid int, ordId, masterId string) (err error) {
//1、查找 `green_coin_double_chain` 基础设置
greenCoinDoubleChain, err := db.GreenCoinDoubleChainGetOneByParams(Db, map[string]interface{}{
"key": "is_use",
"value": 1,
})
if err != nil {
return
}

//2、判断当前会员等级
user, err := db.DbsUserFindByUid(Db, uid)
if err != nil {
return
@@ -33,25 +43,23 @@ func DealUserGreenCoinDoubleChainIntegral(Db *xorm.Engine, uid int, amount, ordI
return
}

amountValue, _ := decimal.NewFromString(amount)
session := Db.NewSession()
defer func() {
session.Close()
if err := recover(); err != nil {
_ = zhios_order_relate_logx.Error(err)
}
}()
session.Begin()
//1、查找 `green_coin_double_chain` 基础设置
greenCoinDoubleChain, err := db.GreenCoinDoubleChainGetOneByParams(session, map[string]interface{}{
"key": "is_use",
"value": 1,
})
//3、处理“贡献积分”作用于上级
err = HandleUserGreenCoinDoubleChainIntegralByParent(Db, uid, ordId, masterId, greenCoinDoubleChain)
if err != nil {
return
return err
}

//2、查找对应直推上级用户
//4、处理“绿色积分”作用于下级
err = HandleUserGreenCoinDoubleChainIntegralBySon(Db, uid, ordId, masterId, greenCoinDoubleChain)
if err != nil {
return err
}

return
}

func HandleUserGreenCoinDoubleChainIntegralByParent(Db *xorm.Engine, uid int, ordId, masterId string, greenCoinDoubleChain *model.GreenCoinDoubleChain) (err error) {
//1、查找对应直推上级用户
parentUserRelate, err := db.GetUserParentUserRelate(Db, uid)
if err != nil {
return
@@ -61,29 +69,18 @@ func DealUserGreenCoinDoubleChainIntegral(Db *xorm.Engine, uid int, amount, ordI
return
}

//3、查询用户和对应上级的虚拟币余额
userAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, uid)
//2、查询对应上级的虚拟币余额
parentAmount, err := svc.GetUserCoinAmount(Db.NewSession(), masterId, greenCoinDoubleChain.Coin1, parentUserRelate.ParentUid)
if err != nil {
return
}
parentAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin1, parentUserRelate.ParentUid)
if err != nil {
return
}
userAmountValue, _ := decimal.NewFromString(userAmount)
parentAmountValue, _ := decimal.NewFromString(parentAmount)
if parentAmountValue.LessThan(amountValue) {
//TODO::上级绿色积分小于当前需要兑换积分金额
amountValue = parentAmountValue
amount = amountValue.String()
return
}
if userAmountValue.LessThan(amountValue) {
err = errors.New("当前用户金额有误,不足以进行兑换")
if parentAmountValue.LessThan(decimal.NewFromFloat(0)) {
//TODO::上级绿色积分小于0不需要进行兑换
return
}

//4、查询上级直推的所有下级用户
//3、查询上级直推的所有下级用户
sonUserRelates, err := db.DbsUserRelateByParentUid(Db, parentUserRelate.ParentUid, 1)
if err != nil {
return
@@ -111,126 +108,368 @@ func DealUserGreenCoinDoubleChainIntegral(Db *xorm.Engine, uid int, amount, ordI
}
}

//5、查询上级直推的所有下级用户的虚拟币金额是否有大于当前用户的
var sonUserVirtualWallet model.UserVirtualAmount
isHas, err := session.Where(" coin_id = ? and amount >=?", greenCoinDoubleChain.Coin2, amount).In("uid", userIds).Get(&sonUserVirtualWallet)
//4、查询上级直推的所有下级用户的虚拟币金额(按升序)
var sonUserVirtualWallet []model.UserVirtualAmount
Db.Where(" coin_id = ?", greenCoinDoubleChain.Coin2).In("uid", userIds).Asc("amount").Find(&sonUserVirtualWallet)
if err != nil {
return
}
if isHas == false {
//TODO::没有符合匹配的下级用户
return
}

/*
TODO::6、进行“数据变更”
TODO::5、进行“数据变更”
*/
//6.1、上级减少amount 绿色积分
err = DealUserCoin(session, md.DealUserCoinReq{
Kind: "sub",
Mid: masterId,
Title: md.GreenCoinDoubleChainExchangeByCoin1,
TransferType: md.GreenCoinDoubleChainExchangeByCoin1ForForUserVirtualCoinFlow,
OrdId: ordId,
CoinId: greenCoinDoubleChain.Coin1,
Uid: parentUserRelate.ParentUid,
Amount: zhios_order_relate_utils.StrToFloat64(amount),
})
if err != nil {
session.Rollback()
return
}
//6.2、用户减少amount 贡献积分
err = DealUserCoin(session, md.DealUserCoinReq{
Kind: "sub",
Mid: masterId,
Title: md.GreenCoinDoubleChainExchangeByCoin2,
TransferType: md.GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow,
OrdId: ordId,
CoinId: greenCoinDoubleChain.Coin2,
Uid: uid,
Amount: zhios_order_relate_utils.StrToFloat64(amount),
})
//6.3、另一下级减少amount 贡献积分
err = DealUserCoin(session, md.DealUserCoinReq{
Kind: "sub",
Mid: masterId,
Title: md.GreenCoinDoubleChainExchangeByCoin2,
TransferType: md.GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow,
OrdId: ordId,
CoinId: greenCoinDoubleChain.Coin2,
Uid: sonUserVirtualWallet.Uid,
Amount: zhios_order_relate_utils.StrToFloat64(amount),
})
session := Db.NewSession()
defer func() {
session.Close()
if err := recover(); err != nil {
_ = zhios_order_relate_logx.Error(err)
}
}()
var amount string
var lastUid int
for _, v := range sonUserVirtualWallet {
if lastUid == -1 {
break
}

userAmount, _ := decimal.NewFromString(v.Amount)
if amount == "" {
amount = userAmount.String()
lastUid = v.Uid
continue
}

amountValue, _ := decimal.NewFromString(amount)
if userAmount.LessThan(amountValue) {
//当前用户虚拟币金额小于上一个用户虚拟币金额
amountValue = userAmount
}

if parentAmountValue.LessThan(amountValue) {
//TODO::上级绿色积分小于当前需要兑换积分金额
amountValue = parentAmountValue
}

session.Begin()
//5.1、上级减少amount 绿色积分
err = DealUserCoin(session, md.DealUserCoinReq{
Kind: "sub",
Mid: masterId,
Title: md.GreenCoinDoubleChainExchangeByCoin1,
TransferType: md.GreenCoinDoubleChainExchangeByCoin1ForForUserVirtualCoinFlow,
OrdId: ordId,
CoinId: greenCoinDoubleChain.Coin1,
Uid: parentUserRelate.ParentUid,
Amount: zhios_order_relate_utils.StrToFloat64(amountValue.String()),
})
if err != nil {
session.Rollback()
return
}
//5.2、用户减少amount 贡献积分
err = DealUserCoin(session, md.DealUserCoinReq{
Kind: "sub",
Mid: masterId,
Title: md.GreenCoinDoubleChainExchangeByCoin2,
TransferType: md.GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow,
OrdId: ordId,
CoinId: greenCoinDoubleChain.Coin2,
Uid: v.Uid,
Amount: zhios_order_relate_utils.StrToFloat64(amountValue.String()),
})
//5.3、另一下级减少amount 贡献积分
err = DealUserCoin(session, md.DealUserCoinReq{
Kind: "sub",
Mid: masterId,
Title: md.GreenCoinDoubleChainExchangeByCoin2,
TransferType: md.GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow,
OrdId: ordId,
CoinId: greenCoinDoubleChain.Coin2,
Uid: lastUid,
Amount: zhios_order_relate_utils.StrToFloat64(amountValue.String()),
})
if err != nil {
session.Rollback()
return
}
//5.4上级余额增加
var coin model.VirtualCoin
_, err = session.Where("id = ?", greenCoinDoubleChain.Coin1).Get(&coin)
if err != nil {
session.Rollback()
return
}
coinExchangeRatioValue, _ := decimal.NewFromString(coin.ExchangeRatio)
exchangeAmount, _ := amountValue.Div(coinExchangeRatioValue).Float64()
orderType := enum.FinUserFlowOrderActionString(md.GreenCoinDoubleChainExchangeForBalanceForFinUserFlow)
err = svc.DealUserAmount(session, md.DealUserAmount{
Kind: "add",
Mid: masterId,
Title: md.GreenCoinDoubleChainExchangeForBalanceTitleForFinUserFlow,
OrderType: orderType,
OrdAction: md.GreenCoinDoubleChainExchangeForBalanceForFinUserFlow,
OrdId: ordId,
Uid: parentUserRelate.ParentUid,
Amount: exchangeAmount,
Num: "",
})
if err != nil {
session.Rollback()
return
}

//6、新增 green_coin_double_chain_exchange_records 记录
afterUserCoinAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin1, parentUserRelate.ParentUid)
if err != nil {
session.Rollback()
return
}
afterCoinAmountContributeUser1, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, v.Uid)
if err != nil {
session.Rollback()
return
}
afterCoinAmountContributeUser2, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, lastUid)
if err != nil {
session.Rollback()
return
}
now := time.Now()
_, err = db.GreenCoinDoubleChainExchangeRecordsInsert(Db, &model.GreenCoinDoubleChainExchangeRecords{
Uid: parentUserRelate.ParentUid,
ContributeUid1: v.Uid,
ContributeUid2: lastUid,
CoinId1: greenCoinDoubleChain.Coin1,
CoinId2: greenCoinDoubleChain.Coin2,
Amount: amountValue.String(),
BeforeUserCoinAmount: parentAmount,
AfterUserCoinAmount: afterUserCoinAmount,
BeforeCoinAmountContributeUser1: v.Amount,
AfterCoinAmountContributeUser1: afterCoinAmountContributeUser1,
BeforeCoinAmountContributeUser2: amountValue.String(),
AfterCoinAmountContributeUser2: afterCoinAmountContributeUser2,
CreateAt: now.Format("2006-01-02 15:04:05"),
UpdateAt: now.Format("2006-01-02 15:04:05"),
})
if err != nil {
session.Rollback()
return
}
session.Commit()

if zhios_order_relate_utils.StrToFloat64(afterUserCoinAmount) == 0 {
lastUid = -1
} else {
if amountValue.String() == amount {
amount = afterCoinAmountContributeUser2
} else {
amount = afterCoinAmountContributeUser1
lastUid = v.Uid
}
}
}
return
}

func HandleUserGreenCoinDoubleChainIntegralBySon(Db *xorm.Engine, uid int, ordId, masterId string, greenCoinDoubleChain *model.GreenCoinDoubleChain) (err error) {
//1、查询对应上级的虚拟币余额
parentAmount, err := svc.GetUserCoinAmount(Db.NewSession(), masterId, greenCoinDoubleChain.Coin1, uid)
if err != nil {
session.Rollback()
return
}
//6.4上级余额增加 amount
var coin model.VirtualCoin
_, err = session.Where("id = ?", greenCoinDoubleChain.Coin1).Get(&coin)
if err != nil {
session.Rollback()
return
}
coinExchangeRatioValue, _ := decimal.NewFromString(coin.ExchangeRatio)
exchangeAmount, _ := amountValue.Div(coinExchangeRatioValue).Float64()
orderType := enum.FinUserFlowOrderActionString(md.GreenCoinDoubleChainExchangeForBalanceForFinUserFlow)
err = svc.DealUserAmount(session, md.DealUserAmount{
Kind: "add",
Mid: masterId,
Title: md.GreenCoinDoubleChainExchangeForBalanceTitleForFinUserFlow,
OrderType: orderType,
OrdAction: md.GreenCoinDoubleChainExchangeForBalanceForFinUserFlow,
OrdId: ordId,
Uid: parentUserRelate.ParentUid,
Amount: exchangeAmount,
Num: "",
})
if err != nil {
session.Rollback()
parentAmountValue, _ := decimal.NewFromString(parentAmount)
if parentAmountValue.LessThan(decimal.NewFromFloat(0)) {
//TODO::上级绿色积分小于0不需要进行兑换
return
}

//7、新增 green_coin_double_chain_exchange_records 记录
afterUserCoinAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin1, parentUserRelate.ParentUid)
//2、查询上级直推的所有下级用户
sonUserRelates, err := db.DbsUserRelateByParentUid(Db, uid, 1)
if err != nil {
session.Rollback()
return
}
afterCoinAmountContributeUser1, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, uid)
if err != nil {
session.Rollback()
if sonUserRelates == nil {
err = errors.New("未查询到直推下级")
return
}
afterCoinAmountContributeUser2, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, sonUserVirtualWallet.Uid)
var sonUserIds []int
for _, v := range *sonUserRelates {
if v.Uid != uid {
sonUserIds = append(sonUserIds, v.Uid)
}
}

users, err := db.DbsUserFindByIds(Db, sonUserIds)
if err != nil {
session.Rollback()
return
}
now := time.Now()
_, err = db.GreenCoinDoubleChainExchangeRecordsInsert(Db, &model.GreenCoinDoubleChainExchangeRecords{
Uid: parentUserRelate.ParentUid,
ContributeUid1: uid,
ContributeUid2: sonUserVirtualWallet.Uid,
CoinId1: greenCoinDoubleChain.Coin1,
CoinId2: greenCoinDoubleChain.Coin2,
Amount: amount,
BeforeUserCoinAmount: parentAmount,
AfterUserCoinAmount: afterUserCoinAmount,
BeforeCoinAmountContributeUser1: userAmount,
AfterCoinAmountContributeUser1: afterCoinAmountContributeUser1,
BeforeCoinAmountContributeUser2: sonUserVirtualWallet.Amount,
AfterCoinAmountContributeUser2: afterCoinAmountContributeUser2,
CreateAt: now.Format("2006-01-02 15:04:05"),
UpdateAt: now.Format("2006-01-02 15:04:05"),
})
return
}
var userIds []int
for _, v := range *users {
if v.Level >= 2 {
//TODO::暂时写死(至少是银卡会员等级才能兑换)
userIds = append(userIds, v.Uid)
}
}

//3、查询上级直推的所有下级用户的虚拟币金额(按升序)
var sonUserVirtualWallet []model.UserVirtualAmount
Db.Where(" coin_id = ?", greenCoinDoubleChain.Coin2).In("uid", userIds).Asc("amount").Find(&sonUserVirtualWallet)
if err != nil {
session.Rollback()
return
}

session.Commit()
isExchange = true
/*
TODO::4、进行“数据变更”
*/
session := Db.NewSession()
defer func() {
session.Close()
if err := recover(); err != nil {
_ = zhios_order_relate_logx.Error(err)
}
}()
var amount string
var lastUid int
for _, v := range sonUserVirtualWallet {
if lastUid == -1 {
break
}

userAmount, _ := decimal.NewFromString(v.Amount)
if amount == "" {
amount = userAmount.String()
lastUid = v.Uid
continue
}

amountValue, _ := decimal.NewFromString(amount)
if userAmount.LessThan(amountValue) {
//当前用户虚拟币金额小于上一个用户虚拟币金额
amountValue = userAmount
}

if parentAmountValue.LessThan(amountValue) {
//TODO::上级绿色积分小于当前需要兑换积分金额
amountValue = parentAmountValue
}

session.Begin()
//4.1、上级减少amount 绿色积分
err = DealUserCoin(session, md.DealUserCoinReq{
Kind: "sub",
Mid: masterId,
Title: md.GreenCoinDoubleChainExchangeByCoin1,
TransferType: md.GreenCoinDoubleChainExchangeByCoin1ForForUserVirtualCoinFlow,
OrdId: ordId,
CoinId: greenCoinDoubleChain.Coin1,
Uid: uid,
Amount: zhios_order_relate_utils.StrToFloat64(amountValue.String()),
})
if err != nil {
session.Rollback()
return
}
//4.2、用户减少amount 贡献积分
err = DealUserCoin(session, md.DealUserCoinReq{
Kind: "sub",
Mid: masterId,
Title: md.GreenCoinDoubleChainExchangeByCoin2,
TransferType: md.GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow,
OrdId: ordId,
CoinId: greenCoinDoubleChain.Coin2,
Uid: v.Uid,
Amount: zhios_order_relate_utils.StrToFloat64(amountValue.String()),
})
//4.3、另一下级减少amount 贡献积分
err = DealUserCoin(session, md.DealUserCoinReq{
Kind: "sub",
Mid: masterId,
Title: md.GreenCoinDoubleChainExchangeByCoin2,
TransferType: md.GreenCoinDoubleChainExchangeByCoin2ForForUserVirtualCoinFlow,
OrdId: ordId,
CoinId: greenCoinDoubleChain.Coin2,
Uid: lastUid,
Amount: zhios_order_relate_utils.StrToFloat64(amountValue.String()),
})
if err != nil {
session.Rollback()
return
}
//4.4上级余额增加
var coin model.VirtualCoin
_, err = session.Where("id = ?", greenCoinDoubleChain.Coin1).Get(&coin)
if err != nil {
session.Rollback()
return
}
coinExchangeRatioValue, _ := decimal.NewFromString(coin.ExchangeRatio)
exchangeAmount, _ := amountValue.Div(coinExchangeRatioValue).Float64()
orderType := enum.FinUserFlowOrderActionString(md.GreenCoinDoubleChainExchangeForBalanceForFinUserFlow)
err = svc.DealUserAmount(session, md.DealUserAmount{
Kind: "add",
Mid: masterId,
Title: md.GreenCoinDoubleChainExchangeForBalanceTitleForFinUserFlow,
OrderType: orderType,
OrdAction: md.GreenCoinDoubleChainExchangeForBalanceForFinUserFlow,
OrdId: ordId,
Uid: uid,
Amount: exchangeAmount,
Num: "",
})
if err != nil {
session.Rollback()
return
}

//5、新增 green_coin_double_chain_exchange_records 记录
afterUserCoinAmount, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin1, uid)
if err != nil {
session.Rollback()
return
}
afterCoinAmountContributeUser1, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, v.Uid)
if err != nil {
session.Rollback()
return
}
afterCoinAmountContributeUser2, err := svc.GetUserCoinAmount(session, masterId, greenCoinDoubleChain.Coin2, lastUid)
if err != nil {
session.Rollback()
return
}
now := time.Now()
_, err = db.GreenCoinDoubleChainExchangeRecordsInsert(Db, &model.GreenCoinDoubleChainExchangeRecords{
Uid: uid,
ContributeUid1: v.Uid,
ContributeUid2: lastUid,
CoinId1: greenCoinDoubleChain.Coin1,
CoinId2: greenCoinDoubleChain.Coin2,
Amount: amountValue.String(),
BeforeUserCoinAmount: parentAmount,
AfterUserCoinAmount: afterUserCoinAmount,
BeforeCoinAmountContributeUser1: v.Amount,
AfterCoinAmountContributeUser1: afterCoinAmountContributeUser1,
BeforeCoinAmountContributeUser2: amountValue.String(),
AfterCoinAmountContributeUser2: afterCoinAmountContributeUser2,
CreateAt: now.Format("2006-01-02 15:04:05"),
UpdateAt: now.Format("2006-01-02 15:04:05"),
})
if err != nil {
session.Rollback()
return
}
session.Commit()

if zhios_order_relate_utils.StrToFloat64(afterUserCoinAmount) == 0 {
lastUid = -1
} else {
if amountValue.String() == amount {
amount = afterCoinAmountContributeUser2
} else {
amount = afterCoinAmountContributeUser1
lastUid = v.Uid
}
}
}
return
}

Loading…
Cancel
Save