package svc import ( "applet/app/e" "applet/app/enum" "applet/app/md" "applet/app/utils" md2 "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/md" 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" ) 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 ` mediumId := GetAgentMediumId(c) where := "uuid=" + c.GetString("mid") + " and medium_id in(" + 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 DataCenterCommissionRecordTotal(c *gin.Context, req md.DataCenterTableReq) md.DataCenterCommissionTotalData { 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 ` mediumId := GetAgentMediumId(c) where := "uuid=" + c.GetString("mid") 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 + "'" } whereMedium := where + " and medium_id in(" + mediumId + ")" sql = fmt.Sprintf(sql, whereMedium) nativeString, _ := db.QueryNativeString(db.Db, sql) res := md.DataCenterCommissionTotalData{} for _, v := range nativeString { res = md.DataCenterCommissionTotalData{ Date: "-", AppName: "-", PlatformName: "-", AdvName: "-", ExposureCount: v["exposure_count"], Ecpm: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100), ClickRate: utils.GetPrec(v["click_rate"], "2") + "%", ClickCount: v["click_count"], AllCommission: "0", OtherCommission: "0", Commission: "0", } } sqlAgent := ` SELECT SUM(agent_revenue) as agent_revenue, SUM(extra_revenue) as extra_revenue FROM generate_wx_ad_data_with_agent_flow where %s ` user := GetUser(c) whereAgent := where + " and agent_id=" + utils.IntToStr(user.AgentId) sqlAgent = fmt.Sprintf(sqlAgent, whereAgent) nativeStringAgent, _ := db.QueryNativeString(db.Db, sqlAgent) for _, v := range nativeStringAgent { res.Commission = utils.Float64ToStr(utils.StrToFloat64(v["agent_revenue"]) / 100) res.OtherCommission = utils.Float64ToStr(utils.StrToFloat64(v["extra_revenue"]) / 100) res.AllCommission = utils.Float64ToStr(utils.StrToFloat64(res.Commission) + utils.StrToFloat64(res.OtherCommission)) } return res } func DataCenterCommissionRecordList(c *gin.Context, req md.DataCenterRecordReq) md.DataCenterCommissionRecordRes { nativeString, total := commAgent(c, 1, req) list := make([]md.DataCenterCommissionTotalData, 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.DataCenterCommissionTotalData{ Date: v["date"], AppName: name, PlatformName: md.AppletPlatformMap[platform], AdvName: adName, ExposureCount: v["exposure_count"], Ecpm: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100), ClickRate: v["click_rate"] + "%", ClickCount: v["click_count"], AllCommission: v["all_commission"], OtherCommission: v["other_commission"], Commission: v["commission"], } list = append(list, tmp) } res := md.DataCenterCommissionRecordRes{ List: list, Total: total, } return res } func DataCenterCommissionRecordOutPut(c *gin.Context, req md.DataCenterRecordOutPutReq) { var req1 md.DataCenterRecordReq copier.Copy(&req1, &req) req.Limit = "10000" nativeString, _ := commAgent(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": "预估佣金", "I1": "额外奖励", "J1": "预估总收益", } 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] = v["commission"] data["I"+i] = v["other_commission"] data["J"+i] = v["all_commission"] } file := utils.Output(c, name, data) filename := name + ".xlsx" r := map[string]string{ "file": file, "filename": filename, } e.OutSuc(c, r, nil) return } func DataCenterProfitRecordOutPut(c *gin.Context, req md.DataCenterProfitRecordOutPutReq) { var req1 md.DataCenterProfitRecordReq copier.Copy(&req1, &req) req.Limit = "10000" nativeString, _ := commAgentProfit(c, 0, req1) appId := make([]string, 0) for _, v := range nativeString { appId = append(appId, v["app_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 } name := req.StartDate + "~" + req.EndDate + "(第" + req.Page + "页 " + utils.IntToStr(len(nativeString)) + "条)" //写入数据 data := map[string]string{ "A1": "日期", "B1": "应用名称", "C1": "平台类型", "D1": "预估佣金", "E1": "额外奖励", "F1": "预估总收益", } 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] = v["commission"] data["E"+i] = v["other_commission"] data["F"+i] = v["all_commission"] } file := utils.Output(c, name, data) filename := name + ".xlsx" r := map[string]string{ "file": file, "filename": filename, } e.OutSuc(c, r, nil) return } func DataCenterProfitRecordList(c *gin.Context, req md.DataCenterProfitRecordReq) md.DataCenterProfitRecordRes { data, total := commAgentProfit(c, 1, req) list := make([]md.DataCenterProfitRecordData, 0) for _, v := range data { NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c)) app, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(v["app_id"]) platform := "" name := "" if app != nil { name = app.Name platform = app.Platform } tmp := md.DataCenterProfitRecordData{ Date: v["date"], AppName: name, PlatformName: md2.PlatformMap[platform], Commission: utils.Float64ToStr(utils.StrToFloat64(v["agent_revenue"]) / 100), OtherCommission: utils.Float64ToStr(utils.StrToFloat64(v["extra_revenue"]) / 100), } tmp.AllCommission = utils.Float64ToStr(utils.StrToFloat64(tmp.Commission) + utils.StrToFloat64(tmp.OtherCommission)) list = append(list, tmp) } res := md.DataCenterProfitRecordRes{ List: list, Total: total, } return res } 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 ` mediumId := GetAgentMediumId(c) where := "uuid=" + c.GetString("mid") + " and medium_id in(" + 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 commAgentProfit(c *gin.Context, isTotal int, req md.DataCenterProfitRecordReq) ([]map[string]string, int64) { appId := GetAppletId(c, req.AppId, req.Platform) sql := ` SELECT %s FROM generate_wx_ad_data_with_agent_flow where %s %s ` user := GetUser(c) where := "uuid=" + c.GetString("mid") + " and agent_id =" + utils.IntToStr(user.AgentId) if req.AppId != "" || req.Platform != "" { where += " and app_id in(" + appId + ")" } if req.StartDate != "" { where += " and date>='" + req.StartDate + "'" } if req.EndDate != "" { where += " and date<='" + req.EndDate + "'" } field := `%s,app_id,SUM(agent_revenue) as agent_revenue,SUM(extra_revenue) as extra_revenue` start := (utils.StrToInt(req.Page) - 1) * utils.StrToInt(req.Limit) groupBy := " group by %s,app_id order by date desc,id desc" if req.Page != "" { groupBy += " limit " + utils.IntToStr(start) + "," + req.Limit } else { groupBy = " group by %s,app_id order by date asc,id asc" } timeStr := "date" if req.Type == "1" { timeStr = "DATE_FORMAT(date, '%Y-%m')" } if req.Type == "2" { timeStr = "DATE_FORMAT(date, '%Y')" } field = fmt.Sprintf(field, timeStr) groupBy = fmt.Sprintf(groupBy, timeStr+" as date") sql1 := fmt.Sprintf(sql, field, where, groupBy) nativeString, _ := db.QueryNativeString(db.Db, sql1) var total int64 = 0 if isTotal == 1 { sqlTotal := fmt.Sprintf(sql, field, where, "") sqlTotal1 := `select COUNT(*) as count from (%s) as tmp` sqlTotal1 = fmt.Sprintf(sqlTotal1, sqlTotal) nativeStringTotal, _ := db.QueryNativeString(db.Db, sqlTotal1) for _, v := range nativeStringTotal { total = utils.StrToInt64(v["count"]) } } return nativeString, total } func commAgent(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 ` mediumId := GetAgentMediumId(c) where := "uuid=" + c.GetString("mid") + " and medium_id in(" + 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"]) } } var ids []string for _, v := range nativeString { ids = append(ids, v["id"]) } if len(ids) > 0 { sqlAgent := ` SELECT SUM(agent_revenue) as agent_revenue, SUM(extra_revenue) as extra_revenue FROM generate_wx_ad_data_with_agent_flow where %s ` user := GetUser(c) whereAgent := "original_data_id in(" + strings.Join(ids, ",") + ") and agent_id=" + utils.IntToStr(user.AgentId) sqlAgent = fmt.Sprintf(sqlAgent, whereAgent) nativeStringAgent, _ := db.QueryNativeString(db.Db, sqlAgent) agentMap := make(map[string]map[string]string) for _, v := range nativeStringAgent { _, ok := agentMap[v["uid"]] if ok == false { agentMap[v["uid"]] = make(map[string]string) } agentMap[v["uid"]] = v } for k, v := range nativeString { _, ok := agentMap[v["uid"]] if ok { nativeString[k]["commission"] = utils.Float64ToStr(utils.StrToFloat64(v["agent_revenue"]) / 100) nativeString[k]["other_commission"] = utils.Float64ToStr(utils.StrToFloat64(v["extra_revenue"]) / 100) nativeString[k]["all_commission"] = utils.Float64ToStr(utils.StrToFloat64(nativeString[k]["commission"]) + utils.StrToFloat64(nativeString[k]["other_commission"])) } else { nativeString[k]["commission"] = "0" nativeString[k]["other_commission"] = "0" nativeString[k]["all_commission"] = "0" } } } return nativeString, total } func DataCenterSelectData(c *gin.Context) { NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c)) appList, _ := NewAppletApplicationDb.FindAllAppletApplicationList() appMap := make(map[string][]map[string]interface{}) mediumId := GetAgentMediumId(c) for _, v := range appList { if strings.Contains(","+mediumId+",", ","+utils.IntToStr(v.MediumId)+",") == false { 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 }