package service import ( "designs/global" "designs/model" "fmt" "time" ) type adsSummary struct { Count int `json:"count" gorm:"column:count"` Pf string `json:"pf" gorm:"column:pf"` Gid string `json:"gid" gorm:"column:gid"` SumType1 int `json:"sumType1" gorm:"column:sumType1"` SumType2 int `json:"sumType2" gorm:"column:sumType2"` SumType0 int `json:"sumType0" gorm:"column:sumType0"` } // 汇总每日的广告数据 func SeeAdsSummary(lastDay string) { //计算出上一日的广告数据汇总 var adsSummarys []adsSummary err := global.App.DB.Table("user_see_ads"). Where("date", lastDay). Group("user_see_ads.gid,user_see_ads.pf"). Select("count(*) as count", "user_see_ads.pf", "user_see_ads.gid", "SUM(adsState = 1) AS sumType1", "SUM(adsState = 2) AS sumType2", "SUM(adsState = 0) AS sumType0"). Scan(&adsSummarys).Error if err != nil { global.App.Log.Error("查询广告汇总数据报错:", err) return } var adsSummaryToday []struct { TodayCount int `json:"todayCount" gorm:"column:todayCount"` Pf string `json:"pf" gorm:"column:pf"` Gid string `json:"gid" gorm:"column:gid"` TodaySumType1 int `json:"todaySumType1" gorm:"column:todaySumType1"` TodaySumType2 int `json:"todaySumType2" gorm:"column:todaySumType2"` TodaySumType0 int `json:"todaySumType0" gorm:"column:todaySumType0"` } query := global.App.DB.Table("user").WhereRaw("DATE_FORMAT(createdAt, '%Y%m%d') = ?", lastDay).Select("id").SubQuery() err = global.App.DB.Table("user_see_ads"). Where("date", lastDay). Group("user_see_ads.gid,user_see_ads.pf"). LeftJoin("user", "user.pf = user_see_ads.pf and user.gid = user_see_ads.gid and user.userId = user_see_ads.userId"). WhereRaw("user.id in (?)", query). Select("count(*) as todayCount", "user_see_ads.pf", "user_see_ads.gid", "SUM(adsState = 1) AS todaySumType1", "SUM(adsState = 2) AS todaySumType2", "SUM(adsState = 0) AS todaySumType0"). Scan(&adsSummaryToday).Error if err != nil { global.App.Log.Error("查询当日注册广告汇总数据报错:", err) return } var SummaryUserSeeAds []model.SummaryUserSeeAds now := model.XTime{ Time: time.Now(), } for _, summary := range adsSummarys { ds := model.SummaryUserSeeAds{ Date: lastDay, Count: summary.Count, Pf: summary.Pf, Gid: summary.Gid, SumType1: summary.SumType1, SumType2: summary.SumType2, SumType0: summary.SumType0, CreatedAt: now, } for _, SummaryToday := range adsSummaryToday { if SummaryToday.Pf == ds.Pf && SummaryToday.Gid == ds.Gid { ds.TodaySumType1 = SummaryToday.TodaySumType1 ds.TodaySumType2 = SummaryToday.TodaySumType2 ds.TodaySumType0 = SummaryToday.TodaySumType0 ds.TodayCount = SummaryToday.TodayCount break } } SummaryUserSeeAds = append(SummaryUserSeeAds, ds) } //将汇总数据存入数据库中 err = global.App.DB.Table("summary_user_see_ads").CreateInBatches(&SummaryUserSeeAds, 100).Error if err != nil { global.App.Log.Error("存入统计数据失败", err.Error()) fmt.Println(err.Error()) return } } func GetTodayAdsSummary(gid string, pf []string) (count, todayCount int64, err error) { today := time.Now().Format("20060102") err = global.App.DB.Table("user_see_ads"). Where("date", today). Where("gid", gid).WhereIn("pf", pf). Count(&count).Error if err != nil { return 0, 0, err } query := global.App.DB.Table("user").Where("gid", gid).WhereIn("pf", pf).WhereRaw("DATE_FORMAT(createdAt, '%Y%m%d') = ?", today).Select("id").SubQuery() err = global.App.DB.Table("user_see_ads"). Where("date", today). LeftJoin("user", "user.pf = user_see_ads.pf and user.gid = user_see_ads.gid and user.userId = user_see_ads.userId"). WhereRaw("user.id in (?)", query).Count(&todayCount).Error if err != nil { return 0, 0, err } return count, todayCount, nil }