|
- package svc
-
- import (
- "applet/app/cfg"
- "applet/app/utils"
- "applet/app/utils/cache"
- "applet/app/utils/logx"
- db "code.fnuoos.com/zhimeng/model.git/src"
- "code.fnuoos.com/zhimeng/model.git/src/model"
- "code.fnuoos.com/zhimeng/model.git/src/super/implement"
- model2 "code.fnuoos.com/zhimeng/model.git/src/super/model"
- "errors"
- "fmt"
- "time"
- "xorm.io/xorm"
- )
-
- const PessimismLockKeyForCalcMasterDataStatistics = "calc_master_data_statistics_pessimism_lock_key"
- const PessimismLockValueForCalcMasterDataStatistics = "running"
-
- func CalcMasterDataStatistics(eg *xorm.Engine) {
- now := time.Now()
- if now.Hour() > 1 && now.Hour() > 8 {
- //TODO::只在凌晨 1 点 ~ 凌晨 8 点运行
- fmt.Println("err>>>>>>>", errors.New("非运行时间"))
- logx.Warn(errors.New("非运行时间"))
- return
- }
-
- //TODO::增加“悲观锁”防止串行
- getString, _ := cache.GetString(PessimismLockKeyForCalcMasterDataStatistics)
- //if err != nil {
- // return err
- //}
- if getString == PessimismLockValueForCalcMasterDataStatistics {
- fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完")
- logx.Warn(errors.New("上一次结算未执行完"))
- return
- }
- cache.SetEx(PessimismLockKeyForCalcMasterDataStatistics, PessimismLockValueForCalcMasterDataStatistics, 3600*8) //8小时
-
- fmt.Println("calc master data statistics...")
- defer func() {
- if err := recover(); err != nil {
- _ = logx.Error(err)
- }
- }()
- var tables *[]model.DbMapping
- if cfg.Prd {
- tables = db.GetAllDatabasePrd() //默认获取全部
- } else {
- tables = db.GetAllDatabaseDev() //默认获取全部
- }
-
- masterDataStatisticsDb := implement.NewMasterDataStatisticsDb(eg)
- year, month, _ := now.Date()
- todayDate := now.AddDate(0, 0, -1).Format("2006-01-02")
- sevenDayDate := now.AddDate(0, 0, -7).Format("2006-01-02")
- thisMonthDate := time.Date(year, month, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
- lastMonthDate := time.Date(year, month-1, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
- for _, table := range *tables {
- //1.1、查询今日 generate_wx_ad_data 数据
- var todayGenerateWxAdDatas []model2.GenerateWxAdData
- err := eg.Where("uuid =? and date =?", table.DbMasterId, todayDate).Find(&todayGenerateWxAdDatas)
- if err != nil {
- fmt.Println("err>>>>>>>", err.Error())
- logx.Warn(err)
- }
-
- //1.2、查询7日 generate_wx_ad_data 数据
- var sevenDayGenerateWxAdDatas []model2.GenerateWxAdData
- err = eg.Where("uuid =? and date >= ?", table.DbMasterId, sevenDayDate).Find(&sevenDayGenerateWxAdDatas)
- if err != nil {
- fmt.Println("err>>>>>>>", err.Error())
- logx.Warn(err)
- }
-
- //1.3、查询本月 generate_wx_ad_data 数据
- var thisMonthGenerateWxAdDatas []model2.GenerateWxAdData
- err = eg.Where("uuid =? and date >= ?", table.DbMasterId, thisMonthDate).Find(&thisMonthGenerateWxAdDatas)
- if err != nil {
- fmt.Println("err>>>>>>>", err.Error())
- logx.Warn(err)
- }
-
- //1.4、查询上月 generate_wx_ad_data 数据
- var lastMonthGenerateWxAdDatas []model2.GenerateWxAdData
- err = eg.Where("uuid =? and date >= ? and date < ?", table.DbMasterId, lastMonthDate, thisMonthDate).Find(&lastMonthGenerateWxAdDatas)
- if err != nil {
- fmt.Println("err>>>>>>>", err.Error())
- logx.Warn(err)
- }
-
- //2、计算数据
- var todayIncome, sevenDayIncome, thisMonthIncome, lastMonthIncome,
- mediumTodayIncome, mediumSevenDayIncome, mediumThisMonthIncome, mediumLastMonthIncome,
- agentTodayIncome, agentSevenDayIncome, agentThisMonthIncome, agentLastMonthIncome,
- mediumTodayExposureCount, mediumSevenDayExposureCount, mediumThisMonthExposureCount, mediumLastMonthExposureCount int
-
- for _, todayGenerateWxAdData := range todayGenerateWxAdDatas {
- todayIncome += todayGenerateWxAdData.PlatformRetention + todayGenerateWxAdData.CommissionRetention + todayGenerateWxAdData.PriceAdjustmentRetention
- mediumTodayIncome += todayGenerateWxAdData.MediaRevenue
- mediumTodayExposureCount += todayGenerateWxAdData.ExposureCount
- agentTodayIncome += todayGenerateWxAdData.AgentRevenue
- }
- for _, sevenDayGenerateWxAdData := range sevenDayGenerateWxAdDatas {
- sevenDayIncome += sevenDayGenerateWxAdData.PlatformRetention + sevenDayGenerateWxAdData.CommissionRetention + sevenDayGenerateWxAdData.PriceAdjustmentRetention
- mediumSevenDayIncome += sevenDayGenerateWxAdData.MediaRevenue
- mediumSevenDayExposureCount += sevenDayGenerateWxAdData.ExposureCount
- agentSevenDayIncome += sevenDayGenerateWxAdData.AgentRevenue
- }
- for _, thisMonthGenerateWxAdData := range thisMonthGenerateWxAdDatas {
- thisMonthIncome += thisMonthGenerateWxAdData.PlatformRetention + thisMonthGenerateWxAdData.CommissionRetention + thisMonthGenerateWxAdData.PriceAdjustmentRetention
- mediumThisMonthIncome += thisMonthGenerateWxAdData.MediaRevenue
- mediumThisMonthExposureCount += thisMonthGenerateWxAdData.ExposureCount
- agentThisMonthIncome += thisMonthGenerateWxAdData.AgentRevenue
- }
- for _, lastMonthGenerateWxAdData := range lastMonthGenerateWxAdDatas {
- lastMonthIncome += lastMonthGenerateWxAdData.PlatformRetention + lastMonthGenerateWxAdData.CommissionRetention + lastMonthGenerateWxAdData.PriceAdjustmentRetention
- mediumLastMonthIncome += lastMonthGenerateWxAdData.MediaRevenue
- mediumLastMonthExposureCount += lastMonthGenerateWxAdData.ExposureCount
- agentLastMonthIncome += lastMonthGenerateWxAdData.AgentRevenue
- }
- _, err = masterDataStatisticsDb.MasterDataStatisticsInsert(&model2.MasterDataStatistics{
- Uuid: utils.StrToInt(table.DbMasterId),
- TodayIncome: todayIncome,
- SevenDayIncome: sevenDayIncome,
- ThisMonthIncome: thisMonthIncome,
- LastMonthIncome: lastMonthIncome,
- MediumTodayIncome: mediumTodayIncome,
- MediumSevenDayIncome: mediumSevenDayIncome,
- MediumThisMonthIncome: mediumThisMonthIncome,
- MediumLastMonthIncome: mediumLastMonthIncome,
- AgentTodayIncome: agentTodayIncome,
- AgentSevenDayIncome: agentSevenDayIncome,
- AgentThisMonthIncome: agentThisMonthIncome,
- AgentLastMonthIncome: agentLastMonthIncome,
- MediumTodayExposureCount: mediumTodayExposureCount,
- MediumSevenDayExposureCount: mediumSevenDayExposureCount,
- MediumThisMonthExposureCount: mediumThisMonthExposureCount,
- MediumLastMonthExposureCount: mediumLastMonthExposureCount,
- Date: todayDate,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- fmt.Println("err>>>>>>>", err.Error())
- logx.Warn(err)
- }
- }
- }
|