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 DealFundDataKey = "deal_fund_data_key" func DealFundData(eg *xorm.Engine, dbName string, ch *rabbit.Channel) { fmt.Println("deal_fund_data...") defer func() { if err := recover(); err != nil { fmt.Println(err) return } }() // 悲观锁防止串行 getString, _ := cache.GetString(DealFundDataKey) if getString != "" { fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次处理未执行完") return } cache.SetEx(DealFundDataKey, "running", 60*30) //30分钟 // 查询所有未被执行完的数据 fundDataDb := implement.NewEggEnergyFundDataDb(eg) fundDataList, err := fundDataDb.EggEnergyFundDataFindNotFinish() if err != nil { fmt.Println("DealFundData_ERR:::::", err.Error()) return } 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) //session := eg.NewSession() //session.Begin() // //eggEnergyCoreData, cb, err2 := svc.GetEggEnergyCoreData(eg) //if err2 != nil { // _ = logx.Error(err2) //} //if cb != nil { // defer cb() // 释放锁 //} ////计算涨价公式 //err3, calcPriceIncreaseFormulaResp := egg_energy.CalcPriceIncreaseFormula(utils.AnyToString(amount), eggEnergyCoreData) //if err3 != nil { // _ = logx.Error(err3) // _ = session.Rollback() // session.Close() // continue //} // //dealAvailableEggEnergyCoinReq := md2.DealAvailableEggEnergyCoinReq{ // Amount: calcPriceIncreaseFormulaResp.GetEggEnergyNums, // AmountFee: "", // BeforePrice: calcPriceIncreaseFormulaResp.BeforePrice, // AfterPrice: calcPriceIncreaseFormulaResp.AfterPrice, // BeforePlanetTotalValue: calcPriceIncreaseFormulaResp.BeforePlanetTotalValue, // AfterPlanetTotalValue: calcPriceIncreaseFormulaResp.AfterPlanetTotalValue, // BeforeEnergyTotalNums: calcPriceIncreaseFormulaResp.BeforeEnergyTotalNums, // AfterEnergyTotalNums: calcPriceIncreaseFormulaResp.AfterEnergyTotalNums, //} // //err4 := egg_energy.DealAvailableEggEnergyCoin(session, int(enum.CapitalInjection), eggEnergyCoreData, dealAvailableEggEnergyCoinReq) //if err4 != nil { // fmt.Println("ActivityCoinAutoExchangeEggPersonEnergy:::::err111:::", err4) // _ = session.Rollback() // session.Close() // continue //} // //// 插入资金数据详细数据 //record := model.EggEnergyFundDataRecords{ // RecordsId: data.Id, // TotalAmount: data.TotalAmount, // BalanceAmount: utils.AnyToString(utils.AnyToFloat64(data.BalanceAmount) - amount), // BalanceTimes: data.BalanceTimes - 1, // BeforePrice: calcPriceIncreaseFormulaResp.BeforePrice, // AfterPrice: calcPriceIncreaseFormulaResp.AfterPrice, // BeforePlanetTotalValue: calcPriceIncreaseFormulaResp.BeforePlanetTotalValue, // AfterPlanetTotalValue: calcPriceIncreaseFormulaResp.AfterPlanetTotalValue, // CreateAt: now.Format("2006-01-02 15:04:05"), // UpdateAt: now.Format("2006-01-02 15:04:05"), //} //_, err5 := fundDataRecordsDb.EggEnergyFundDataRecordsInsertBySession(session, record) //if err5 != nil { // _ = session.Rollback() // continue //} // //// 更新当前数据 //fundData := model.EggEnergyFundData{ // Id: data.Id, // Kind: data.Kind, // TotalAmount: data.TotalAmount, // BalanceAmount: utils.AnyToString(utils.AnyToFloat64(data.BalanceAmount) - amount), // Hours: data.Hours, // BalanceTimes: data.BalanceTimes - 1, // Frequency: data.Frequency, // Memo: data.Memo, // CreateAt: data.CreateAt, // UpdateAt: now.Format("2006-01-02 15:04:05"), //} // //forceColumns := []string{ // "balance_times", // "balance_amount", //} //_, err6 := fundDataDb.EggEnergyFundDataUpdateBySession(session, fundData, forceColumns...) //if err6 != nil { // _ = logx.Error(err6) // _ = session.Rollback() // continue //} } cache.Del(DealFundDataKey) }