Browse Source

Merge remote-tracking branch 'origin/master'

master
huangjiajun 7 months ago
parent
commit
08d5021e43
11 changed files with 169 additions and 412 deletions
  1. +1
    -1
      rule/mw/mw_db.go
  2. +7
    -0
      rule/one_circles/md/app_redis_key.go
  3. +0
    -355
      rule/one_circles/one_circles_activity_coin_auto_exchange_green_energy.go
  4. +6
    -5
      rule/one_circles/one_circles_auto_adjust_prices.go
  5. +11
    -0
      rule/one_circles/one_circles_available_green_energy_settlement.go
  6. +9
    -10
      rule/one_circles/one_circles_community_dividends.go
  7. +17
    -35
      rule/one_circles/one_circles_deal_available_green_energy_points.go
  8. +7
    -5
      rule/one_circles/one_circles_sign_in_green_energy_settlement.go
  9. +1
    -1
      rule/one_circles/one_circles_star_level_dividends.go
  10. +84
    -0
      rule/one_circles/svc/svc_now_price_redis_mutex_lock.go
  11. +26
    -0
      rule/one_circles/svc/svc_price.go

+ 1
- 1
rule/mw/mw_db.go View File

@@ -27,7 +27,7 @@ func GetMasterId(baseDb *xorm.Engine, c *gin.Context) (masterId string) {
masterId = svc.GetWebSiteDomainMasterId(baseDb, md.PLATFORM_WAP, c.Request.Host)
}
}
if masterId == "" && c.GetHeader("Platform") == md.PLATFORM_WAP { // H5 要根据域名去获取mid
if masterId == "" && c.GetHeader("Platform") == md.PLATFORM_PC { // H5 要根据域名去获取mid
hostList := strings.Split(c.Request.Host, ".")
if isNumeric(hostList[0]) {
masterId = hostList[0]


+ 7
- 0
rule/one_circles/md/app_redis_key.go View File

@@ -0,0 +1,7 @@
package md

// 缓存key统一管理, %s格式化为masterId
const (
OneCirclesNowPriceUpdateLock = "%s:one_circles_now_price" // 当前价格(能拿到锁才能更新价格)
DealOneCirclesNowPriceRequestIdPrefix = "%s:one_circles_now_price:%d"
)

+ 0
- 355
rule/one_circles/one_circles_activity_coin_auto_exchange_green_energy.go View File

@@ -5,7 +5,6 @@ import (
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/md"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/enum"
md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/md"
zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils"
zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx"
@@ -16,360 +15,6 @@ import (
"xorm.io/xorm"
)

// ActivityCoinAutoExchangeGreenEnergyNew 活跃积分自动兑换成绿色能量
func ActivityCoinAutoExchangeGreenEnergyNew(engine *xorm.Engine, masterId string, ch *rabbit.Channel) (err error) {
//1、查找 `one_circles_green_energy_basic_setting` 基础设置 && `one_circles_public_platoon_basic_setting` 基础设置
//oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{
// "key": "is_open",
// "value": 1,
//})
//if err != nil {
// return
//}

oneCirclesPublicPlatoonBasicSetting, err := db.OneCirclesPublicPlatoonBasicSettingGetOneByParams(engine, map[string]interface{}{
"key": "is_open",
"value": 1,
})
if err != nil {
return
}
if oneCirclesPublicPlatoonBasicSetting.ExchangeRules == "" {
err = errors.New("自动兑换未设置!")
return
}
var exchangeRulesStruct *md2.ExchangeRulesStruct
err = json.Unmarshal([]byte(oneCirclesPublicPlatoonBasicSetting.ExchangeRules), &exchangeRulesStruct)
if err != nil {
return
}
var autoExchangeNumsByPerson = zhios_order_relate_utils.StrToFloat64(exchangeRulesStruct.AutoExchangeNumsByPerson) //个人活跃积分X个自动兑换
var autoExchangeNumsByTeam = zhios_order_relate_utils.StrToFloat64(exchangeRulesStruct.AutoExchangeNumsByTeam) //团队活跃积分X个自动兑换
fmt.Println("autoExchangeNumsByPerson>>>>>>>>>>>>", autoExchangeNumsByPerson)
fmt.Println("autoExchangeNumsByTeam>>>>>>>>>>>>", autoExchangeNumsByTeam)

//2、获取"个人活跃积分" && 获取"团队活跃积分"
var coin1, coin2 model.VirtualCoin
_, err = engine.Where("id = ?", oneCirclesPublicPlatoonBasicSetting.PersonActivePointsCoinId).Get(&coin1)
if err != nil {
return err
}
_, err = engine.Where("id = ?", oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId).Get(&coin2)
if err != nil {
return err
}
personActivePointsCoinExchangeRatioValue, _ := decimal.NewFromString(coin1.ExchangeRatio)
teamActivePointsCoinExchangeRatioValue, _ := decimal.NewFromString(coin2.ExchangeRatio)

//3、当前 "个人活跃积分"可以自动兑换的用户数据 && "团队活跃积分"可以自动兑换的用户数据
var list1, list2 []model.UserVirtualAmount
err = engine.Where("coin_id = ?", oneCirclesPublicPlatoonBasicSetting.PersonActivePointsCoinId).And("amount >=?", autoExchangeNumsByPerson).Find(&list1)
if err != nil {
fmt.Println("err:::::1111", err)
return
}
err = engine.Where("coin_id = ?", oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId).And("amount >=?", autoExchangeNumsByTeam).Find(&list2)
if err != nil {
fmt.Println("err:::::2222", err)
return
}

session := engine.NewSession()
defer func() {
session.Close()
if err := recover(); err != nil {
_ = zhios_order_relate_logx.Error(err)
}
}()
session.Begin()

for _, v := range list1 {
autoExchangeNumsByPersonValue, _ := decimal.NewFromString(v.Amount)
autoExchangeNumsByPersonAmount := autoExchangeNumsByPersonValue.Div(personActivePointsCoinExchangeRatioValue).String()
//TODO::推入rabbitmq 异步处理
ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForAutoExchangeGreenEnergy{
MasterId: masterId,
Uid: v.Uid,
Amount: v.Amount,
CoinId: coin1.Id,
AutoExchangeNumsAmount: autoExchangeNumsByPersonAmount,
}, md.OneCirclesRoutKeyForAutoExchangeGreenEnergyToPerson)
}

for _, v := range list2 {
autoExchangeNumsByTeamValue, _ := decimal.NewFromString(v.Amount)
autoExchangeNumsByTeamAmount := autoExchangeNumsByTeamValue.Div(teamActivePointsCoinExchangeRatioValue).String()
//TODO::推入rabbitmq 异步处理
ch.Publish(md.OneCirclesExchange, md.OneCirclesStructForAutoExchangeGreenEnergy{
MasterId: masterId,
Uid: v.Uid,
Amount: v.Amount,
CoinId: coin2.Id,
AutoExchangeNumsAmount: autoExchangeNumsByTeamAmount,
}, md.OneCirclesRoutKeyForAutoExchangeGreenEnergyToTeam)
}

return
}

// OldActivityCoinAutoExchangeGreenEnergy 活跃积分自动兑换成绿色能量
func OldActivityCoinAutoExchangeGreenEnergy(engine *xorm.Engine, masterId string) (err error) {
//1、查找 `one_circles_green_energy_basic_setting` 基础设置 && `one_circles_public_platoon_basic_setting` 基础设置
oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{
"key": "is_open",
"value": 1,
})
if err != nil {
return
}

oneCirclesPublicPlatoonBasicSetting, err := db.OneCirclesPublicPlatoonBasicSettingGetOneByParams(engine, map[string]interface{}{
"key": "is_open",
"value": 1,
})
if err != nil {
return
}
if oneCirclesPublicPlatoonBasicSetting.ExchangeRules == "" {
err = errors.New("自动兑换未设置!")
return
}
var exchangeRulesStruct *md2.ExchangeRulesStruct
err = json.Unmarshal([]byte(oneCirclesPublicPlatoonBasicSetting.ExchangeRules), &exchangeRulesStruct)
if err != nil {
return
}
var autoExchangeNumsByPerson = zhios_order_relate_utils.StrToFloat64(exchangeRulesStruct.AutoExchangeNumsByPerson) //个人活跃积分X个自动兑换
fmt.Println("autoExchangeNumsByPerson>>>>>>>>>>>>", autoExchangeNumsByPerson)

//2、获取"个人活跃积分" && 获取"团队活跃积分"
var coin1 model.VirtualCoin
_, err = engine.Where("id = ?", oneCirclesPublicPlatoonBasicSetting.PersonActivePointsCoinId).Get(&coin1)
if err != nil {
return err
}
personActivePointsCoinExchangeRatioValue, _ := decimal.NewFromString(coin1.ExchangeRatio)

//3、当前 "个人活跃积分"可以自动兑换的用户数据 && "团队活跃积分"可以自动兑换的用户数据
var list1 []model.UserVirtualAmount
err = engine.Where("coin_id = ?", oneCirclesPublicPlatoonBasicSetting.PersonActivePointsCoinId).And("amount >=?", autoExchangeNumsByPerson).Limit(1000, 0).Find(&list1)
if err != nil {
fmt.Println("err:::::1111", err.Error())
return
}

//4、处理"个人活跃积分"兑换
for _, v := range list1 {
session := engine.NewSession()
session.Begin()

autoExchangeNumsByPersonValue, _ := decimal.NewFromString(v.Amount)
autoExchangeNumsByPersonAmount := autoExchangeNumsByPersonValue.Div(personActivePointsCoinExchangeRatioValue).String()
//4.1计算涨价公式
err1, values, _, afterPriceValue := NewCalcPriceIncreaseFormula(autoExchangeNumsByPersonAmount, oneCirclesGreenEnergyBasicSetting)
if err1 != nil {
_ = session.Rollback()
return err1
}

//4.2给相应的用户加上个人的绿色积分(可用数量)
err = DealUserCoin(session, md.DealUserCoinReq{
Kind: "add",
Mid: masterId,
Title: md.OneCirclesPersonalActiveCoinExchangeGreenEnergy,
TransferType: md.OneCirclesPersonalActiveCoinExchangeGreenEnergyForUserVirtualCoinFlow,
OrdId: "",
CoinId: oneCirclesGreenEnergyBasicSetting.PersonGreenEnergyCoinId,
Uid: v.Uid,
Amount: zhios_order_relate_utils.StrToFloat64(values),
})
if err != nil {
_ = session.Rollback()
fmt.Println("err:::::33333", err.Error())
return err
}

//4.3给相应的用户减去个人活跃积分
err = DealUserCoin(session, md.DealUserCoinReq{
Kind: "sub",
Mid: masterId,
Title: md.OneCirclesPersonalActiveCoinExchangeToBeGreenEnergy,
TransferType: md.OneCirclesPersonalActiveCoinExchangeToBeGreenEnergyForUserVirtualCoinFlow,
OrdId: "",
CoinId: oneCirclesPublicPlatoonBasicSetting.PersonActivePointsCoinId,
Uid: v.Uid,
Amount: zhios_order_relate_utils.StrToFloat64(v.Amount),
})
if err != nil {
_ = session.Rollback()
fmt.Println("err:::::33333", err.Error())
return err
}

//4.4 减少“原始数量”中的绿色能量
err = DealAvailableGreenEnergyCoin(session, int(enum.PersonalActivePointRedemption), zhios_order_relate_utils.StrToFloat64(values), zhios_order_relate_utils.StrToFloat64(autoExchangeNumsByPersonAmount), enum.PersonalActivePointRedemption.String(), oneCirclesGreenEnergyBasicSetting, afterPriceValue)
if err != nil {
_ = session.Rollback()
fmt.Println("err:::::44444", err.Error())
return err
}
session.Commit()
session.Close()
}

session := engine.NewSession()
defer func() {
session.Close()
if err := recover(); err != nil {
_ = zhios_order_relate_logx.Error(err)
}
}()
session.Begin()

//6、修改 one_circles_green_energy_basic_setting 的 now_price
_, err = db.OneCirclesGreenEnergyBasicSettingUpdate(session, oneCirclesGreenEnergyBasicSetting.Id, oneCirclesGreenEnergyBasicSetting)
if err != nil {
_ = session.Rollback()
fmt.Println("err:::::77777", err.Error())
return err
}

if err != nil {
_ = session.Rollback()
return errors.New("事务提交失败")
}
return
}

// OldActivityCoinAutoExchangeGreenEnergyForTeam 活跃积分自动兑换成绿色能量
func OldActivityCoinAutoExchangeGreenEnergyForTeam(engine *xorm.Engine, masterId string) (err error) {
//1、查找 `one_circles_green_energy_basic_setting` 基础设置 && `one_circles_public_platoon_basic_setting` 基础设置
oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{
"key": "is_open",
"value": 1,
})
if err != nil {
return
}

oneCirclesPublicPlatoonBasicSetting, err := db.OneCirclesPublicPlatoonBasicSettingGetOneByParams(engine, map[string]interface{}{
"key": "is_open",
"value": 1,
})
if err != nil {
return
}
if oneCirclesPublicPlatoonBasicSetting.ExchangeRules == "" {
err = errors.New("自动兑换未设置!")
return
}
var exchangeRulesStruct *md2.ExchangeRulesStruct
err = json.Unmarshal([]byte(oneCirclesPublicPlatoonBasicSetting.ExchangeRules), &exchangeRulesStruct)
if err != nil {
return
}
var autoExchangeNumsByTeam = zhios_order_relate_utils.StrToFloat64(exchangeRulesStruct.AutoExchangeNumsByTeam) //团队活跃积分X个自动兑换
fmt.Println("autoExchangeNumsByTeam>>>>>>>>>>>>", autoExchangeNumsByTeam)

//2、获取"个人活跃积分" && 获取"团队活跃积分"
var coin2 model.VirtualCoin
_, err = engine.Where("id = ?", oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId).Get(&coin2)
if err != nil {
return err
}
teamActivePointsCoinExchangeRatioValue, _ := decimal.NewFromString(coin2.ExchangeRatio)

//3、当前 "个人活跃积分"可以自动兑换的用户数据 && "团队活跃积分"可以自动兑换的用户数据
var list2 []model.UserVirtualAmount
err = engine.Where("coin_id = ?", oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId).And("amount >=?", autoExchangeNumsByTeam).Limit(1000, 0).Find(&list2)
if err != nil {
fmt.Println("err:::::2222", err.Error())
return
}

//5、处理"团队活跃积分"兑换
for _, v := range list2 {
session := engine.NewSession()
session.Begin()

autoExchangeNumsByTeamValue, _ := decimal.NewFromString(v.Amount)
autoExchangeNumsByTeamAmount := autoExchangeNumsByTeamValue.Div(teamActivePointsCoinExchangeRatioValue).String()
//5.1计算涨价公式
err1, values, _, afterPriceValue := NewCalcPriceIncreaseFormula(autoExchangeNumsByTeamAmount, oneCirclesGreenEnergyBasicSetting)
if err1 != nil {
_ = session.Rollback()
return err1
}

//5.2给相应的用户加上个人的绿色积分(结算数量)
err = DealUserCoin(session, md.DealUserCoinReq{
Kind: "add",
Mid: masterId,
Title: md.OneCirclesTeamActiveCoinExchangeGreenEnergy,
TransferType: md.OneCirclesTeamActiveCoinExchangeGreenEnergyForUserVirtualCoinFlow,
OrdId: "",
CoinId: oneCirclesGreenEnergyBasicSetting.TeamGreenEnergyCoinId,
Uid: v.Uid,
Amount: zhios_order_relate_utils.StrToFloat64(values),
})
if err != nil {
_ = session.Rollback()
fmt.Println("err:::::33333", err.Error())
return err
}

//5.3给相应的用户减去个人团队积分
err = DealUserCoin(session, md.DealUserCoinReq{
Kind: "sub",
Mid: masterId,
Title: md.OneCirclesTeamActiveCoinExchangeToBeGreenEnergy,
TransferType: md.OneCirclesTeamActiveCoinExchangeToBeGreenEnergyForUserVirtualCoinFlow,
OrdId: "",
CoinId: oneCirclesPublicPlatoonBasicSetting.TeamActivePointsCoinId,
Uid: v.Uid,
Amount: zhios_order_relate_utils.StrToFloat64(v.Amount),
})
if err != nil {
_ = session.Rollback()
fmt.Println("err:::::66666", err.Error())
return err
}

//5.4 减少“原始数量”中的绿色能量
err = DealAvailableGreenEnergyCoin(session, int(enum.TeamActivePointRedemption), zhios_order_relate_utils.StrToFloat64(values), zhios_order_relate_utils.StrToFloat64(autoExchangeNumsByTeamAmount), enum.TeamActivePointRedemption.String(), oneCirclesGreenEnergyBasicSetting, afterPriceValue)
if err != nil {
_ = session.Rollback()
fmt.Println("err:::::44444", err.Error())
return err
}
err = session.Commit()
session.Close()
}

session := engine.NewSession()
defer func() {
session.Close()
if err := recover(); err != nil {
_ = zhios_order_relate_logx.Error(err)
}
}()
session.Begin()

//6、修改 one_circles_green_energy_basic_setting 的 now_price
_, err = db.OneCirclesGreenEnergyBasicSettingUpdate(session, oneCirclesGreenEnergyBasicSetting.Id, oneCirclesGreenEnergyBasicSetting)
if err != nil {
_ = session.Rollback()
fmt.Println("err:::::77777", err.Error())
return err
}

if err != nil {
_ = session.Rollback()
return errors.New("事务提交失败")
}
return
}

// ActivityCoinAutoExchangeGreenEnergy 活跃积分自动兑换成绿色能量(个人)
func ActivityCoinAutoExchangeGreenEnergy(engine *xorm.Engine, masterId string, ch *rabbit.Channel) (err error) {
//1、查找`one_circles_public_platoon_basic_setting` 基础设置


+ 6
- 5
rule/one_circles/one_circles_auto_adjust_prices.go View File

@@ -1,9 +1,9 @@
package one_circles

import (
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/enum"
md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/md"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/svc"
zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils"
zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx"
"encoding/json"
@@ -20,13 +20,13 @@ func AutoAdjustPrice(engine *xorm.Engine, masterId string) (err error) {
fmt.Println(now.Hour())

//1、查找 `one_circles_green_energy_basic_setting` 基础设置
oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{
"key": "is_open",
"value": 1,
})
oneCirclesGreenEnergyBasicSetting, cb, err := svc.GetPrice(engine, masterId)
if err != nil {
return
}
if cb != nil {
defer cb() // 释放锁
}

var priceSettingStruct *md2.PriceSettingStruct
err = json.Unmarshal([]byte(oneCirclesGreenEnergyBasicSetting.PriceSetting), &priceSettingStruct)
@@ -37,6 +37,7 @@ func AutoAdjustPrice(engine *xorm.Engine, masterId string) (err error) {
err = errors.New("价格设置未完全!")
return
}

session := engine.NewSession()
defer func() {
session.Close()


+ 11
- 0
rule/one_circles/one_circles_available_green_energy_settlement.go View File

@@ -45,6 +45,11 @@ func NewCalcPriceIncreaseFormula(userAmountValue string, oneCirclesGreenEnergyBa
values = userAmount.Div(afterPrice).Truncate(8).String() //用户得到绿色能量个数
nowPriceValue = nowPrice.String()
afterPriceValue = afterPrice.String()
zhios_order_relate_utils.FilePutContents("virtual_coin_not", zhios_order_relate_utils.SerializeStr(map[string]interface{}{
"before_price": nowPriceValue,
"after_price": afterPriceValue,
"user_amount": userAmountValue,
}))
return
}

@@ -121,6 +126,12 @@ func CalcPriceReductionFormula(engine *xorm.Engine, userExchangeNumsValue string
greenEnergyFee = userExchangeNums.Mul(exchangeAccountBalanceFee.Div(decimalRate)).Truncate(8).String() //绿色能量手续费
nowPriceValue = nowPrice.String()
afterPriceValue = afterPrice.String()

zhios_order_relate_utils.FilePutContents("virtual_coin_not", zhios_order_relate_utils.SerializeStr(map[string]interface{}{
"before_price": nowPriceValue,
"after_price": afterPriceValue,
"user_amount": userExchangeNumsValue,
}))
return
}



+ 9
- 10
rule/one_circles/one_circles_community_dividends.go View File

@@ -1,7 +1,6 @@
package one_circles

import (
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/md"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/enum"
@@ -16,14 +15,14 @@ import (

// CommunityDividends 社区分红
func CommunityDividends(engine *xorm.Engine, masterId string) (err error) {
//1、查找 `one_circles_green_energy_basic_setting` 基础设置 && `one_circles_public_platoon_basic_setting` 基础设置
oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{
"key": "is_open",
"value": 1,
})
if err != nil {
return
}
////1、查找 `one_circles_green_energy_basic_setting` 基础设置 && `one_circles_public_platoon_basic_setting` 基础设置
//oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{
// "key": "is_open",
// "value": 1,
//})
//if err != nil {
// return
//}

//2、查找当前是否有待分红的记录 "个人活跃积分"可以自动兑换的用户数据 && "团队活跃积分"可以自动兑换的用户数据
var m model.OneCirclesCommunityDividends
@@ -84,7 +83,7 @@ func CommunityDividends(engine *xorm.Engine, masterId string) (err error) {
}
//6、 减少“社区分红”钱包中的值
totalDividend, _ := totalValue.Float64()
err = DealAvailableGreenEnergyCoin(session, int(enum.CommunityDividends), totalDividend, 0, enum.CommunityDividends.String(), oneCirclesGreenEnergyBasicSetting, oneCirclesGreenEnergyBasicSetting.NowPrice)
err = DealAvailableGreenEnergyCoin(session, int(enum.CommunityDividends), totalDividend, 0, enum.CommunityDividends.String(), nil, masterId)
if err != nil {
_ = session.Rollback()
fmt.Println("err:::::444444", err)


+ 17
- 35
rule/one_circles/one_circles_deal_available_green_energy_points.go View File

@@ -5,6 +5,7 @@ import (
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/enum"
md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/md"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/svc"
"encoding/json"
"github.com/shopspring/decimal"
"time"
@@ -12,6 +13,17 @@ import (
)

func DealAvailableGreenEnergyCoin(session *xorm.Session, kind int, amount, amountFee float64, title string, chain *model.OneCirclesGreenEnergyBasicSetting, nowPriceValue string) error {
if chain == nil {
// 查找 `one_circles_green_energy_basic_setting` 基础设置
oneCirclesGreenEnergyBasicSetting, cb, err := svc.GetPrice(session.Engine(), nowPriceValue)
if err != nil {
return err
}
if cb != nil {
defer cb() // 释放锁
}
chain = oneCirclesGreenEnergyBasicSetting
}
amountValue := decimal.NewFromFloat(amount)
now := time.Now()
var oneCirclesAvailableGreenEnergyPointsFlow model.OneCirclesAvailableGreenEnergyPointsFlow
@@ -449,44 +461,14 @@ func DealAvailableGreenEnergyCoin(session *xorm.Session, kind int, amount, amoun
//更新 `one_circles_green_energy_basic_setting` 表
if chain.NowPrice != nowPriceValue {
chain.NowPrice = nowPriceValue
_, err := db.OneCirclesGreenEnergyBasicSettingUpdate(session, chain.Id, chain, "now_price")
if err != nil {
return err
}
//// 新增 / 更新 one_circles_green_energy_price 记录
//date := now.Format("2006-01-02")
//hour := zhios_order_relate_utils.IntToStr(now.Hour())
//oneCirclesGreenEnergyPrice, err := db.OneCirclesGreenEnergyPriceGetOneByParamsBySession(session, date, hour)
//if err != nil {
// return err
//}
//if oneCirclesGreenEnergyPrice == nil {
// oneCirclesGreenEnergyPrice = &model.OneCirclesGreenEnergyPrice{
// Price: nowPriceValue,
// Date: date,
// Hour: hour,
// }
// insertId, err1 := db.OneCirclesGreenEnergyPriceInsertBySession(session, oneCirclesGreenEnergyPrice)
// if err1 != nil {
// return err1
// }
// if insertId <= 0 {
// return errors.New("插入 one_circles_green_energy_price 失败")
// }
//} else {
// oneCirclesGreenEnergyPrice.Price = nowPriceValue
// _, err1 := db.OneCirclesGreenEnergyPriceUpdate(session, oneCirclesGreenEnergyPrice.Id, oneCirclesGreenEnergyPrice, "price")
// if err1 != nil {
// return err1
// }
// //if updateAffected <= 0 {
// // return errors.New("更新 one_circles_green_energy_price 失败")
// //}
//}
}
_, err := db.OneCirclesGreenEnergyBasicSettingUpdate(session, chain.Id, chain)
if err != nil {
return err
}

//插入 `one_circles_available_green_energy_points_flow` 记录
_, err := db.OneCirclesAvailableGreenEnergyPointsFlowInsert(session, &oneCirclesAvailableGreenEnergyPointsFlow)
_, err = db.OneCirclesAvailableGreenEnergyPointsFlowInsert(session, &oneCirclesAvailableGreenEnergyPointsFlow)
if err != nil {
return err
}


+ 7
- 5
rule/one_circles/one_circles_sign_in_green_energy_settlement.go View File

@@ -7,6 +7,7 @@ import (
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/md"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/enum"
md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/md"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/svc"
zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils"
zhios_order_relate_logx "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/logx"
"encoding/json"
@@ -71,13 +72,14 @@ func SettlementSignInGreenEnergy(engine *xorm.Engine, masterId string, ch *rabbi

func HandleSettlementSignInGreenEnergy(engine *xorm.Engine, masterId string, id int64, uid int) (err error) {
//1、查找 `one_circles_green_energy_basic_setting` 基础设置
oneCirclesGreenEnergyBasicSetting, err := db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{
"key": "is_open",
"value": 1,
})
oneCirclesGreenEnergyBasicSetting, cb, err := svc.GetPrice(engine, masterId)
if err != nil {
return
}
if cb != nil {
defer cb() // 释放锁
}

var teamRewardSetting *md2.TeamRewardSettingStruct
err = json.Unmarshal([]byte(oneCirclesGreenEnergyBasicSetting.TeamReward), &teamRewardSetting)
if err != nil {
@@ -139,10 +141,10 @@ func HandleSettlementSignInGreenEnergy(engine *xorm.Engine, masterId string, id
var parentRewardValue = zhios_order_relate_utils.StrToFloat64(rewardValue)
if relates != nil {
for _, relate := range *relates {
parentRewardValue = parentRewardValue * rewardDecrement
if parentRewardValue <= rewardEndValue {
break
}
parentRewardValue = parentRewardValue * rewardDecrement
//TODO::判断是否活跃
var m model.OneCirclesGreenEnergySignIn
has, err3333333 := engine.Where("uid =?", relate.ParentUid).And("end_time >=?", time.Now().Format("2006-01-02 15:04:05")).Get(&m)


+ 1
- 1
rule/one_circles/one_circles_star_level_dividends.go View File

@@ -288,7 +288,7 @@ func SettlementStarLevelDividends(engine *xorm.Engine, masterId string, ch *rabb

//6、 减少“星级分红”中的绿色能量
totalDividend, _ := totalDividendValue.Float64()
err = DealAvailableGreenEnergyCoin(session, int(enum.SettlementStarLevelDividends), totalDividend, 0, enum.SettlementStarLevelDividends.String(), oneCirclesGreenEnergyBasicSetting, oneCirclesGreenEnergyBasicSetting.NowPrice)
err = DealAvailableGreenEnergyCoin(session, int(enum.SettlementStarLevelDividends), totalDividend, 0, enum.SettlementStarLevelDividends.String(), nil, masterId)
if err != nil {
_ = session.Rollback()
fmt.Println("err:::::9999999", err)


+ 84
- 0
rule/one_circles/svc/svc_now_price_redis_mutex_lock.go View File

@@ -0,0 +1,84 @@
package svc

import (
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/md"
zhios_order_relate_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/utils/cache"
"errors"
"fmt"
"math/rand"
"reflect"
"time"
)

const redisMutexLockExpTime = 15

// TryGetDistributedLock 分布式锁获取
// requestId 用于标识请求客户端,可以是随机字符串,需确保唯一
func TryGetDistributedLock(lockKey, requestId string, isNegative bool) bool {
if isNegative { // 多次尝试获取
retry := 1
for {
ok, err := cache.Do("SET", lockKey, requestId, "EX", redisMutexLockExpTime, "NX")
// 获取锁成功
if err == nil && ok == "OK" {
return true
}
// 尝试多次没获取成功
if retry > 99 {
return false
}
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000)))
retry += 1
}
} else { // 只尝试一次
ok, err := cache.Do("SET", lockKey, requestId, "EX", redisMutexLockExpTime, "NX")
// 获取锁成功
if err == nil && ok == "OK" {
return true
}

return false
}
}

// ReleaseDistributedLock 释放锁,通过比较requestId,用于确保客户端只释放自己的锁,使用lua脚本保证操作的原子型
func ReleaseDistributedLock(lockKey, requestId string) (bool, error) {
luaScript := `
if redis.call("get",KEYS[1]) == ARGV[1]
then
return redis.call("del",KEYS[1])
else
return 0
end`

do, err := cache.Do("eval", luaScript, 1, lockKey, requestId)
fmt.Println(reflect.TypeOf(do))
fmt.Println(do)
if zhios_order_relate_utils.AnyToInt64(do) == 1 {
return true, err
} else {
return false, err
}
}

func GetDistributedLockRequestId(prefix string) string {
return prefix + zhios_order_relate_utils.IntToStr(rand.Intn(100000000))
}

// HandleDistributedLock 处理余额更新时获取锁和释放锁 如果加锁成功,使用语句 ` defer cb() ` 释放锁
func HandleDistributedLock(masterId, requestIdPrefix string) (cb func(), err error) {
// 获取余额更新锁
balanceLockKey := fmt.Sprintf(md.OneCirclesNowPriceUpdateLock, masterId)
requestId := GetDistributedLockRequestId(requestIdPrefix)
balanceLockOk := TryGetDistributedLock(balanceLockKey, requestId, true)
if !balanceLockOk {
return nil, errors.New("系统繁忙,请稍后再试")
}

cb = func() {
_, _ = ReleaseDistributedLock(balanceLockKey, requestId)
}

return cb, nil
}

+ 26
- 0
rule/one_circles/svc/svc_price.go View File

@@ -0,0 +1,26 @@
package svc

import (
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/db/model"
"code.fnuoos.com/go_rely_warehouse/zyos_go_order_relate_rule.git/rule/one_circles/md"
"fmt"
"time"
"xorm.io/xorm"
)

func GetPrice(engine *xorm.Engine, masterId string) (basic *model.OneCirclesGreenEnergyBasicSetting, cb func(), err error) {
requestIdPrefix := fmt.Sprintf(md.DealOneCirclesNowPriceRequestIdPrefix, masterId, time.Now().UnixMicro())
cb, err = HandleDistributedLock(masterId, requestIdPrefix)
if err != nil {
return
}
basic, err = db.OneCirclesGreenEnergyBasicSettingGetOneByParams(engine, map[string]interface{}{
"key": "is_open",
"value": 1,
})
if err != nil {
return
}
return
}

Loading…
Cancel
Save