|
|
@@ -1,64 +1,151 @@ |
|
|
|
package svc |
|
|
|
|
|
|
|
import ( |
|
|
|
"applet/app/db" |
|
|
|
"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" |
|
|
|
) |
|
|
|
|
|
|
|
func CancelOrder(eg *xorm.Engine, dbName string) { |
|
|
|
fmt.Println("cancel order...") |
|
|
|
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) |
|
|
|
} |
|
|
|
}() |
|
|
|
|
|
|
|
timeStr, err := getCancelCfg(eg, dbName) |
|
|
|
if err != nil { |
|
|
|
fmt.Println(err.Error()) |
|
|
|
return |
|
|
|
var tables *[]model.DbMapping |
|
|
|
if cfg.Prd { |
|
|
|
tables = db.GetAllDatabasePrd() //默认获取全部 |
|
|
|
} else { |
|
|
|
tables = db.GetAllDatabaseDev() //默认获取全部 |
|
|
|
} |
|
|
|
|
|
|
|
now := time.Now() |
|
|
|
// x 分钟后取消订单 |
|
|
|
expTime := now.Add(-time.Hour * time.Duration(utils.StrToInt64(timeStr))) |
|
|
|
expTimeStr := utils.Time2String(expTime, "") |
|
|
|
|
|
|
|
page := 1 |
|
|
|
|
|
|
|
for { |
|
|
|
isEmpty, err := handleOnePage(eg, dbName, expTimeStr) |
|
|
|
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 { |
|
|
|
_ = logx.Error(err) |
|
|
|
break |
|
|
|
} |
|
|
|
if isEmpty { |
|
|
|
break |
|
|
|
fmt.Println("err>>>>>>>", err.Error()) |
|
|
|
logx.Warn(err) |
|
|
|
} |
|
|
|
|
|
|
|
if page > 100 { |
|
|
|
break |
|
|
|
//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) |
|
|
|
} |
|
|
|
|
|
|
|
page += 1 |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
//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) |
|
|
|
} |
|
|
|
|
|
|
|
func handleOnePage(eg *xorm.Engine, dbName, expTimeStr string) (isEmpty bool, err error) { |
|
|
|
return false, nil |
|
|
|
} |
|
|
|
//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) |
|
|
|
} |
|
|
|
|
|
|
|
func getCancelCfg(eg *xorm.Engine, masterId string) (string, error) { |
|
|
|
cfg := db.SysCfgGetWithDb(eg, masterId, "order_expiration_time") |
|
|
|
//2、计算数据 |
|
|
|
var todayIncome, sevenDayIncome, thisMonthIncome, lastMonthIncome, |
|
|
|
mediumTodayIncome, mediumSevenDayIncome, mediumThisMonthIncome, mediumLastMonthIncome, |
|
|
|
agentTodayIncome, agentSevenDayIncome, agentThisMonthIncome, agentLastMonthIncome, |
|
|
|
mediumTodayExposureCount, mediumSevenDayExposureCount, mediumThisMonthExposureCount, mediumLastMonthExposureCount int |
|
|
|
|
|
|
|
if cfg == "" { |
|
|
|
return "", errors.New("order_expiration_time no found") |
|
|
|
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) |
|
|
|
} |
|
|
|
} |
|
|
|
return cfg, nil |
|
|
|
} |