|
- package svc
-
- import (
- "applet/app/utils"
- "applet/app/utils/cache"
- "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
- md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
- "fmt"
- "github.com/shopspring/decimal"
- "time"
- "xorm.io/xorm"
- )
-
- const EggEnergyDealFundDataKey = "egg_energy_deal_fund_data_key"
-
- // EggEnergyDealFundData 处理价值投入
- func EggEnergyDealFundData(eg *xorm.Engine) {
- fmt.Println("egg_energy_deal_fund_data...")
- defer func() {
- if err := recover(); err != nil {
- fmt.Println(err)
- return
- }
- }()
-
- // 悲观锁防止串行
- getString, _ := cache.GetString(EggEnergyDealFundDataKey)
- if getString != "" {
- fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次处理未执行完")
- return
- }
- cache.SetEx(EggEnergyDealFundDataKey, "running", 60*30) //30分钟
- defer cache.Del(EggEnergyDealFundDataKey)
-
- // 查询所有未被执行完的数据
- fundDataDb := implement.NewEggEnergyFundDataDb(eg)
- fundDataList, err := fundDataDb.EggEnergyFundDataFindNotFinish()
- if err != nil {
- fmt.Println("DealFundData_ERR:::::", err.Error())
- return
- }
-
- ch, err := rabbit.Cfg.Pool.GetChannel()
- if err != nil {
- return
- }
- defer ch.Release()
-
- now := time.Now()
- fundDataRecordsDb := implement.NewEggEnergyFundDataRecordsDb(eg)
- for _, data := range fundDataList {
- // 1、判断是否是第一次执行
- lastRecord, err1 := fundDataRecordsDb.EggEnergyFundDataRecordsGetLast(data.Id)
- if err1 != nil {
- fmt.Println("DealFundData_ERR:::::", err1.Error())
- continue
- }
- if lastRecord == nil {
- // 2.1 首次执行 判断当前是否可以执行
- if utils.TimeParseStd(data.CreateAt).Add(time.Duration(data.Frequency) * time.Minute).After(now) {
- continue
- }
- } else {
- // 2.2 已经被执行过
- if utils.TimeParseStd(lastRecord.CreateAt).Add(time.Duration(data.Frequency) * time.Minute).After(now) {
- continue
- }
- }
-
- // 3、计算总执行次数、每次更新金额 (每次更新金额 = 金额 / 总执行次数 总执行次数 = 时间 / 频次)
- frequency := decimal.NewFromInt(int64(data.Frequency))
- times := decimal.NewFromInt(int64(data.Hours * 60)).Div(frequency).RoundFloor(8)
- totalAmount, err2 := decimal.NewFromString(data.TotalAmount)
- if err2 != nil {
- fmt.Println("DealFundData_ERR:::::", err2.Error())
- continue
- }
- amount := totalAmount.Div(times)
-
- // 4、推入rabbitmq 异步处理
- ch.Publish(md2.EggEnergyExchange, md2.EggEnergyStructForEggEnergyFundData{
- ID: data.Id,
- Amount: amount.String(),
- }, md2.EggEnergyRoutKeyForEggEnergyFundData)
- }
- }
|