广告平台(总站长使用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

152 lines
6.5 KiB

  1. package svc
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/utils"
  5. "applet/app/utils/cache"
  6. "applet/app/utils/logx"
  7. db "code.fnuoos.com/zhimeng/model.git/src"
  8. "code.fnuoos.com/zhimeng/model.git/src/model"
  9. "code.fnuoos.com/zhimeng/model.git/src/super/implement"
  10. model2 "code.fnuoos.com/zhimeng/model.git/src/super/model"
  11. "errors"
  12. "fmt"
  13. "time"
  14. "xorm.io/xorm"
  15. )
  16. const PessimismLockKeyForCalcMasterDataStatistics = "calc_master_data_statistics_pessimism_lock_key"
  17. const PessimismLockValueForCalcMasterDataStatistics = "running"
  18. func CalcMasterDataStatistics(eg *xorm.Engine) {
  19. now := time.Now()
  20. if now.Hour() > 1 && now.Hour() > 8 {
  21. //TODO::只在凌晨 1 点 ~ 凌晨 8 点运行
  22. fmt.Println("err>>>>>>>", errors.New("非运行时间"))
  23. logx.Warn(errors.New("非运行时间"))
  24. return
  25. }
  26. //TODO::增加“悲观锁”防止串行
  27. getString, _ := cache.GetString(PessimismLockKeyForCalcMasterDataStatistics)
  28. //if err != nil {
  29. // return err
  30. //}
  31. if getString == PessimismLockValueForCalcMasterDataStatistics {
  32. fmt.Println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!", "上一次结算未执行完")
  33. logx.Warn(errors.New("上一次结算未执行完"))
  34. return
  35. }
  36. cache.SetEx(PessimismLockKeyForCalcMasterDataStatistics, PessimismLockValueForCalcMasterDataStatistics, 3600*8) //8小时
  37. fmt.Println("calc master data statistics...")
  38. defer func() {
  39. if err := recover(); err != nil {
  40. _ = logx.Error(err)
  41. }
  42. }()
  43. var tables *[]model.DbMapping
  44. if cfg.Prd {
  45. tables = db.GetAllDatabasePrd() //默认获取全部
  46. } else {
  47. tables = db.GetAllDatabaseDev() //默认获取全部
  48. }
  49. masterDataStatisticsDb := implement.NewMasterDataStatisticsDb(eg)
  50. year, month, _ := now.Date()
  51. todayDate := now.AddDate(0, 0, -1).Format("2006-01-02")
  52. sevenDayDate := now.AddDate(0, 0, -7).Format("2006-01-02")
  53. thisMonthDate := time.Date(year, month, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  54. lastMonthDate := time.Date(year, month-1, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02")
  55. for _, table := range *tables {
  56. //1.1、查询今日 generate_wx_ad_data 数据
  57. var todayGenerateWxAdDatas []model2.GenerateWxAdData
  58. err := eg.Where("uuid =? and date =?", table.DbMasterId, todayDate).Find(&todayGenerateWxAdDatas)
  59. if err != nil {
  60. fmt.Println("err>>>>>>>", err.Error())
  61. logx.Warn(err)
  62. }
  63. //1.2、查询7日 generate_wx_ad_data 数据
  64. var sevenDayGenerateWxAdDatas []model2.GenerateWxAdData
  65. err = eg.Where("uuid =? and date >= ?", table.DbMasterId, sevenDayDate).Find(&sevenDayGenerateWxAdDatas)
  66. if err != nil {
  67. fmt.Println("err>>>>>>>", err.Error())
  68. logx.Warn(err)
  69. }
  70. //1.3、查询本月 generate_wx_ad_data 数据
  71. var thisMonthGenerateWxAdDatas []model2.GenerateWxAdData
  72. err = eg.Where("uuid =? and date >= ?", table.DbMasterId, thisMonthDate).Find(&thisMonthGenerateWxAdDatas)
  73. if err != nil {
  74. fmt.Println("err>>>>>>>", err.Error())
  75. logx.Warn(err)
  76. }
  77. //1.4、查询上月 generate_wx_ad_data 数据
  78. var lastMonthGenerateWxAdDatas []model2.GenerateWxAdData
  79. err = eg.Where("uuid =? and date >= ? and date < ?", table.DbMasterId, lastMonthDate, thisMonthDate).Find(&lastMonthGenerateWxAdDatas)
  80. if err != nil {
  81. fmt.Println("err>>>>>>>", err.Error())
  82. logx.Warn(err)
  83. }
  84. //2、计算数据
  85. var todayIncome, sevenDayIncome, thisMonthIncome, lastMonthIncome,
  86. mediumTodayIncome, mediumSevenDayIncome, mediumThisMonthIncome, mediumLastMonthIncome,
  87. agentTodayIncome, agentSevenDayIncome, agentThisMonthIncome, agentLastMonthIncome,
  88. mediumTodayExposureCount, mediumSevenDayExposureCount, mediumThisMonthExposureCount, mediumLastMonthExposureCount int
  89. for _, todayGenerateWxAdData := range todayGenerateWxAdDatas {
  90. todayIncome += todayGenerateWxAdData.PlatformRetention + todayGenerateWxAdData.CommissionRetention + todayGenerateWxAdData.PriceAdjustmentRetention
  91. mediumTodayIncome += todayGenerateWxAdData.MediaRevenue
  92. mediumTodayExposureCount += todayGenerateWxAdData.ExposureCount
  93. agentTodayIncome += todayGenerateWxAdData.AgentRevenue
  94. }
  95. for _, sevenDayGenerateWxAdData := range sevenDayGenerateWxAdDatas {
  96. sevenDayIncome += sevenDayGenerateWxAdData.PlatformRetention + sevenDayGenerateWxAdData.CommissionRetention + sevenDayGenerateWxAdData.PriceAdjustmentRetention
  97. mediumSevenDayIncome += sevenDayGenerateWxAdData.MediaRevenue
  98. mediumSevenDayExposureCount += sevenDayGenerateWxAdData.ExposureCount
  99. agentSevenDayIncome += sevenDayGenerateWxAdData.AgentRevenue
  100. }
  101. for _, thisMonthGenerateWxAdData := range thisMonthGenerateWxAdDatas {
  102. thisMonthIncome += thisMonthGenerateWxAdData.PlatformRetention + thisMonthGenerateWxAdData.CommissionRetention + thisMonthGenerateWxAdData.PriceAdjustmentRetention
  103. mediumThisMonthIncome += thisMonthGenerateWxAdData.MediaRevenue
  104. mediumThisMonthExposureCount += thisMonthGenerateWxAdData.ExposureCount
  105. agentThisMonthIncome += thisMonthGenerateWxAdData.AgentRevenue
  106. }
  107. for _, lastMonthGenerateWxAdData := range lastMonthGenerateWxAdDatas {
  108. lastMonthIncome += lastMonthGenerateWxAdData.PlatformRetention + lastMonthGenerateWxAdData.CommissionRetention + lastMonthGenerateWxAdData.PriceAdjustmentRetention
  109. mediumLastMonthIncome += lastMonthGenerateWxAdData.MediaRevenue
  110. mediumLastMonthExposureCount += lastMonthGenerateWxAdData.ExposureCount
  111. agentLastMonthIncome += lastMonthGenerateWxAdData.AgentRevenue
  112. }
  113. _, err = masterDataStatisticsDb.MasterDataStatisticsInsert(&model2.MasterDataStatistics{
  114. Uuid: utils.StrToInt(table.DbMasterId),
  115. TodayIncome: todayIncome,
  116. SevenDayIncome: sevenDayIncome,
  117. ThisMonthIncome: thisMonthIncome,
  118. LastMonthIncome: lastMonthIncome,
  119. MediumTodayIncome: mediumTodayIncome,
  120. MediumSevenDayIncome: mediumSevenDayIncome,
  121. MediumThisMonthIncome: mediumThisMonthIncome,
  122. MediumLastMonthIncome: mediumLastMonthIncome,
  123. AgentTodayIncome: agentTodayIncome,
  124. AgentSevenDayIncome: agentSevenDayIncome,
  125. AgentThisMonthIncome: agentThisMonthIncome,
  126. AgentLastMonthIncome: agentLastMonthIncome,
  127. MediumTodayExposureCount: mediumTodayExposureCount,
  128. MediumSevenDayExposureCount: mediumSevenDayExposureCount,
  129. MediumThisMonthExposureCount: mediumThisMonthExposureCount,
  130. MediumLastMonthExposureCount: mediumLastMonthExposureCount,
  131. Date: todayDate,
  132. CreateAt: now.Format("2006-01-02 15:04:05"),
  133. UpdateAt: now.Format("2006-01-02 15:04:05"),
  134. })
  135. if err != nil {
  136. fmt.Println("err>>>>>>>", err.Error())
  137. logx.Warn(err)
  138. }
  139. }
  140. }