package svc import ( "applet/app/e" "applet/app/enum" "applet/app/md" "applet/app/utils" db "code.fnuoos.com/zhimeng/model.git/src" "code.fnuoos.com/zhimeng/model.git/src/implement" model2 "code.fnuoos.com/zhimeng/model.git/src/model" "fmt" "github.com/gin-gonic/gin" "github.com/jinzhu/copier" "strings" "time" ) func DataCenterTable(c *gin.Context, req md.DataCenterTableReq) md.DataCenterTableRes { var req1 md.DataCenterRecordReq copier.Copy(&req1, &req) nativeString, _ := comm(c, 0, req1) list := make([]md.DataCenterTableData, 0) tmpMap := make(map[string]md.DataCenterTableData) for _, v := range nativeString { tmp := md.DataCenterTableData{ Date: v["date"], ExposureCount: v["exposure_count"], MediaRevenue: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100), Ecpm: v["ecpm"], } tmpMap[v["date"]] = tmp } day := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00") - utils.TimeStdParseUnix(req.StartDate+" 00:00:00")) / 86400 for i := day; i >= 0; i-- { date := utils.TimeStdParseUnix(req.EndDate+" 00:00:00") - i*86400 tmp, ok := tmpMap[time.Unix(date, 0).Format("2006-01-02")] if ok == false { tmp = md.DataCenterTableData{ Date: time.Unix(date, 0).Format("2006-01-02"), ExposureCount: "0", MediaRevenue: "0", Ecpm: "0", } } list = append(list, tmp) } res := md.DataCenterTableRes{ List: list, } return res } func DataCenterRecordTotal(c *gin.Context, req md.DataCenterTableReq) md.DataCenterTotalData { appId := GetAppletId(c, req.AppId, req.Platform) sql := ` SELECT SUM(exposure_count) as exposure_count, SUM(click_count) as click_count, SUM(click_count)/SUM(exposure_count)*100 as click_rate, SUM(ecpm) as ecpm, SUM(media_revenue) as media_revenue FROM generate_wx_ad_data where %s ` user := GetUser(c) where := "uuid=" + c.GetString("mid") + " and medium_id=" + utils.IntToStr(user.MediumId) if req.AppId != "" || req.Platform != "" { where += " and app_id in(" + appId + ")" } if req.AdType != "" { where += " and ad_slot='" + req.AdType + "'" } if req.StartDate != "" { where += " and date>='" + req.StartDate + "'" } if req.EndDate != "" { where += " and date<='" + req.EndDate + "'" } sql = fmt.Sprintf(sql, where) nativeString, _ := db.QueryNativeString(db.Db, sql) res := md.DataCenterTotalData{} for _, v := range nativeString { res = md.DataCenterTotalData{ Date: "-", AppName: "-", PlatformName: "-", AdvName: "-", ExposureCount: v["exposure_count"], MediaRevenue: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100), Ecpm: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100), ClickRate: utils.GetPrec(v["click_rate"], "2") + "%", ClickCount: v["click_count"], } } return res } func DataCenterRecordList(c *gin.Context, req md.DataCenterRecordReq) md.DataCenterRecordRes { nativeString, total := comm(c, 1, req) list := make([]md.DataCenterTotalData, 0) for _, v := range nativeString { NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c)) app, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(v["app_id"]) platform := "" name := "" if app != nil { name = app.Name platform = app.Platform } NewAppletApplicationAdSpaceListDb := implement.NewAppletApplicationAdSpaceListDb(MasterDb(c)) adData, _ := NewAppletApplicationAdSpaceListDb.GetAppletApplicationAdSpaceListByAdId(v["slot_id"]) adName := enum.AdunitType(v["ad_slot"]).String() if adData != nil { adName = adData.Name } tmp := md.DataCenterTotalData{ Date: v["date"], AppName: name, PlatformName: md.AppletPlatformMap[platform], AdvName: adName, ExposureCount: v["exposure_count"], MediaRevenue: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100), Ecpm: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100), ClickRate: v["click_rate"] + "%", ClickCount: v["click_count"], } list = append(list, tmp) } res := md.DataCenterRecordRes{ List: list, Total: total, } return res } func DataCenterRecordOutPut(c *gin.Context, req md.DataCenterRecordOutPutReq) { var req1 md.DataCenterRecordReq copier.Copy(&req1, &req) req.Limit = "10000" nativeString, _ := comm(c, 0, req1) appId := make([]string, 0) adId := make([]string, 0) for _, v := range nativeString { appId = append(appId, v["app_id"]) adId = append(adId, v["slot_id"]) } var app []model2.AppletApplication appMap := make(map[string]model2.AppletApplication) MasterDb(c).In("app_id=?", appId).Find(&app) for _, v := range app { appMap[v.AppId] = v } var ad []model2.AppletApplicationAdSpaceList adMap := make(map[string]model2.AppletApplicationAdSpaceList) MasterDb(c).In("ad_id=?", adId).Find(&adMap) for _, v := range ad { adMap[v.AdId] = v } name := req.StartDate + "~" + req.EndDate + "(第" + req.Page + "页 " + utils.IntToStr(len(nativeString)) + "条)" //写入数据 data := map[string]string{ "A1": "日期", "B1": "应用名称", "C1": "平台类型", "D1": "广告位", "E1": "曝光量", "F1": "点击量", "G1": "ECPM", "H1": "预估收益", } for k, v := range nativeString { i := utils.IntToStr(k + 2) data["A"+i] = v["date"] data["B"+i] = appMap[v["app_id"]].Name data["C"+i] = md.AppletPlatformMap[appMap[v["app_id"]].Platform] data["D"+i] = adMap[v["slot_id"]].Name data["E"+i] = v["exposure_count"] data["F"+i] = v["click_count"] data["G"+i] = utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100) data["H"+i] = utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100) } file := utils.Output(c, name, data) filename := name + ".xlsx" r := map[string]string{ "file": file, "filename": filename, } e.OutSuc(c, r, nil) return } func comm(c *gin.Context, isTotal int, req md.DataCenterRecordReq) ([]map[string]string, int64) { appId := GetAppletId(c, req.AppId, req.Platform) sql := ` SELECT %s FROM generate_wx_ad_data where %s %s ` user := GetUser(c) where := "uuid=" + c.GetString("mid") + " and medium_id=" + utils.IntToStr(user.MediumId) if req.AppId != "" || req.Platform != "" { where += " and app_id in(" + appId + ")" } if req.AdType != "" { where += " and ad_slot='" + req.AdType + "'" } if req.StartDate != "" { where += " and date>='" + req.StartDate + "'" } if req.EndDate != "" { where += " and date<='" + req.EndDate + "'" } field := `*` start := (utils.StrToInt(req.Page) - 1) * utils.StrToInt(req.Limit) groupBy := " order by date desc,id desc" if req.Page != "" { groupBy += " limit " + utils.IntToStr(start) + "," + req.Limit } else { groupBy = " order by date asc,id asc" } sql1 := fmt.Sprintf(sql, field, where, groupBy) nativeString, _ := db.QueryNativeString(db.Db, sql1) var total int64 = 0 if isTotal == 1 { sqlTotal := fmt.Sprintf(sql, "COUNT(*) as count ", where, "") nativeStringTotal, _ := db.QueryNativeString(db.Db, sqlTotal) for _, v := range nativeStringTotal { total = utils.StrToInt64(v["count"]) } } return nativeString, total } func DataCenterSelectData(c *gin.Context) { NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c)) appList, _ := NewAppletApplicationDb.FindAllAppletApplicationList() appMap := make(map[string][]map[string]interface{}) user := GetUser(c) for _, v := range appList { if v.MediumId != user.MediumId { continue } _, ok := appMap[v.Platform] if ok == false { appMap[v.Platform] = make([]map[string]interface{}, 0) } tmp := map[string]interface{}{ "name": v.Name, "app_id": v.AppId, "ad_type": enum.AdTypeList, } appMap[v.Platform] = append(appMap[v.Platform], tmp) } platform := []map[string]interface{}{ { "name": "微信小程序", "platform": "wx_applet", "app_list": appMap["wx_applet"], }, } e.OutSuc(c, platform, nil) return } // 应用 func GetAppletId(c *gin.Context, appId, platform string) string { mediumId := "" sess := MasterDb(c).Where("1=1") var ids = make([]string, 0) if appId != "" || platform != "" { ids = append(ids, "-1") } if platform != "" { var tmp []model2.AppletApplication if platform != "" { sess.And("platform = ? ", platform) } sess.Find(&tmp) for _, v := range tmp { ids = append(ids, utils.IntToStr(v.MediumId)) } } if appId != "" { ids = []string{appId} } if appId != "" || platform != "" { mediumId = strings.Join(ids, ",") } return mediumId } func GetAppletInfo(c *gin.Context, id string) map[string]string { var res = map[string]string{ "platform": "", "name": "", "logo": "", } NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c)) data, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(id) if data != nil { res["platform"] = data.Platform res["name"] = data.Name res["logo"] = data.Logo } return res }