package svc import ( "applet/app/e" "applet/app/md" "applet/app/utils" "applet/app/utils/cache" db "code.fnuoos.com/zhimeng/model.git/src" implement2 "code.fnuoos.com/zhimeng/model.git/src/implement" "code.fnuoos.com/zhimeng/model.git/src/super/implement" "code.fnuoos.com/zhimeng/model.git/src/super/model" "errors" "fmt" "github.com/gin-gonic/gin" ) func DataCenterIncomeDataList(c *gin.Context, req md.DataCenterGenerateDataReq) md.DataCenterIncomeDataRes { engine := db.Db NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(engine) user := GetUser(c) appId := GetAppletId(c, req.Name, req.Platform) mediumId := GetMediumIdStr(c, user.AdmId, "", "") slotId := GetSlotId(c, req.State) MediumList, total, _ := NewGenerateWxAdDataDb.FindGenerateWxAdDataListMedium(c.GetString("mid"), appId, mediumId, slotId, req.StartTime, req.EndTime, utils.StrToInt(req.Page), utils.StrToInt(req.Limit)) data := make([]md.DataCenterIncomeDataData, 0) if len(MediumList) > 0 { for _, v := range MediumList { var tmp = md.DataCenterIncomeDataData{ AgreementSharing: utils.Float64ToStr(float64(v.AgreementSharing) / 100), AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100), Id: utils.IntToStr(v.Id), ExposureCount: utils.IntToStr(v.ExposureCount), ClickCount: utils.IntToStr(v.ClickCount), ClickRate: v.ClickRate, Ecpm: utils.Float64ToStr(utils.StrToFloat64(v.Ecpm) / 100), Date: v.Date, MediaRevenue: utils.Float64ToStr(float64(v.MediaRevenue) / 100), SettleAmount: utils.Float64ToStr(float64(v.MediaRevenue+v.AgentRevenue+v.AgreementSharing) / 100), } tmpApplet := GetAppletInfo(c, v.AppId) if tmpApplet["platform"] != "" { tmp.Platform = tmpApplet["platform"] } if tmpApplet["name"] != "" { tmp.Name = tmpApplet["name"] } tmpSlot := GetSlotInfo(c, v.SlotId) if tmpSlot["state"] != "" { tmp.State = tmpSlot["state"] } if tmpSlot["name"] != "" { tmp.AdvName = tmpSlot["name"] } data = append(data, tmp) } } res := md.DataCenterIncomeDataRes{ List: data, Total: total, State: md.AdState, Platform: md.AdPlatform, } return res } func DataCenterIncomeDataListOutput(c *gin.Context, req md.DataCenterGenerateDataReq) { engine := db.Db NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(engine) user := GetUser(c) appId := GetAppletId(c, req.Name, req.Platform) mediumId := GetMediumIdStr(c, user.AdmId, "", "") slotId := GetSlotId(c, req.State) req.Limit = "3000" MediumList, _ := NewGenerateWxAdDataDb.FindGenerateWxAdDataListMediumAll(c.GetString("mid"), appId, mediumId, slotId, req.StartTime, req.EndTime, utils.StrToInt(req.Page), utils.StrToInt(req.Limit)) data := map[string]string{ "A1": "名称", "B1": "日期", "C1": "广告位", "D1": "曝光量", "E1": "点击率(%)", "F1": "点击量", "G1": "结算平台", "H1": "媒体-ECPM", "I1": "媒体收益(元)", "J1": "代理收益(元)", "K1": "平台收益(元)", } name := "运营报表第" + req.Page + "页" if len(MediumList) > 0 { for k, v := range MediumList { var tmp = md.DataCenterIncomeDataData{ AgreementSharing: utils.Float64ToStr(float64(v.AgreementSharing) / 100), AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100), Id: utils.IntToStr(v.Id), ExposureCount: utils.IntToStr(v.ExposureCount), ClickCount: utils.IntToStr(v.ClickCount), ClickRate: v.ClickRate, Ecpm: utils.Float64ToStr(utils.StrToFloat64(v.Ecpm) / 100), Date: v.Date, MediaRevenue: utils.Float64ToStr(float64(v.MediaRevenue) / 100), SettleAmount: utils.Float64ToStr(float64(v.MediaRevenue+v.AgentRevenue+v.AgreementSharing) / 100), } tmpApplet := GetAppletInfo(c, v.AppId) if tmpApplet["platform"] != "" { tmp.Platform = md.AdPlatformMap[tmpApplet["platform"]] } if tmpApplet["name"] != "" { tmp.Name = tmpApplet["name"] } tmpSlot := GetSlotInfo(c, v.SlotId) if tmpSlot["state"] != "" { tmp.State = tmpSlot["state"] } if tmpSlot["name"] != "" { tmp.AdvName = tmpSlot["name"] } i := utils.IntToStr(k + 2) data["A"+i] = tmp.Name data["B"+i] = tmp.Date data["C"+i] = tmp.AdvName data["D"+i] = tmp.ExposureCount data["E"+i] = tmp.ClickRate data["F"+i] = tmp.ClickCount data["G"+i] = tmp.Platform data["H"+i] = tmp.Ecpm data["I"+i] = tmp.MediaRevenue data["J"+i] = tmp.AgentRevenue data["K"+i] = tmp.AgreementSharing } } file := utils.Output(c, name, data) filename := name + ".xlsx" r := map[string]string{ "file": file, "filename": filename, } e.OutSuc(c, r, nil) return } func DataCenterIncomeDataDetail(c *gin.Context, req md.DataCenterGenerateDataCommReq) { NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db) data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id)) if data == nil { e.OutErr(c, 400, e.NewErr(400, "记录不存在")) return } agentReward := make([]md.DataCenterGenerateDataDetailAgentRewardSecond, 0) NewGenerateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db) agent, _ := NewGenerateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(data.Id) if agent != nil { for _, v := range *agent { tmp := md.DataCenterGenerateDataDetailAgentRewardSecond{ Name: "", Account: "", AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100), AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate), } tmpApplet := GetAgentInfo(c, v.AgentId) if tmpApplet["name"] != "" { tmp.Name = tmpApplet["name"] } if tmpApplet["account"] != "" { tmp.Account = tmpApplet["account"] } agentReward = append(agentReward, tmp) } } res := md.DataCenterIncomeDataDetail{ MediaRevenue: utils.Float64ToStr(float64(data.MediaRevenue) / 100), AgreementSharing: utils.Float64ToStr(float64(data.AgreementSharing) / 100), AgentRevenue: utils.Float64ToStr(float64(data.AgentRevenue) / 100), DataSource: "手动同步", CreateAt: data.CreateAt, UpdateAt: data.UpdateAt, MediaRevenueRate: utils.IntToStr(data.MediaRevenueRate), AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate), AgreementSharingRate: utils.IntToStr(data.AgreementSharingRate), AgentReward: agentReward, } tmpApplet := GetAppletInfo(c, data.AppId) if tmpApplet["platform"] != "" { res.Platform = tmpApplet["platform"] } NewAppletApplicationDb := implement2.NewAppletApplicationDb(MasterDb(c)) app, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(data.AppId) tmp := GetMediumInfo(c, app.MediumId) if tmp["name"] != "" { res.MediumName = tmp["name"] if tmp["account"] != "" { res.MediumName += "(" + tmp["account"] + ")" } } e.OutSuc(c, res, nil) return } func DataCenterGenerateDataList(c *gin.Context, req md.DataCenterGenerateDataReq) md.DataCenterGenerateDataRes { engine := db.Db NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(engine) user := GetUser(c) appId := GetAppletId(c, req.Name, req.Platform) mediumId := GetMediumIdStr(c, user.AdmId, "", "") slotId := GetSlotId(c, req.State) MediumList, total, _ := NewGenerateWxAdDataDb.FindGenerateWxAdDataList(c.GetString("mid"), appId, mediumId, slotId, req.StartTime, req.EndTime, utils.StrToInt(req.Page), utils.StrToInt(req.Limit)) data := make([]md.DataCenterGenerateDataData, 0) if len(MediumList) > 0 { NewOriginalWxAdDataDb := implement.NewOriginalWxAdDataDb(engine) for _, v := range MediumList { wxData, _ := NewOriginalWxAdDataDb.GetOriginalWxAdData(v.OriginalDataId) var tmp = md.DataCenterGenerateDataData{ Id: utils.IntToStr(v.Id), ExposureCount: utils.IntToStr(v.ExposureCount), ClickCount: utils.IntToStr(v.ClickCount), ClickRate: v.ClickRate, Ecpm: utils.Float64ToStr(utils.StrToFloat64(v.Ecpm) / 100), IsGenerateReport: utils.IntToStr(v.IsGenerateReport), Date: v.Date, PlatformRetention: utils.Float64ToStr(float64(v.PlatformRetention) / 100), CommissionRetention: utils.Float64ToStr(float64(v.CommissionRetention) / 100), PriceAdjustmentRetention: utils.Float64ToStr(float64(v.PriceAdjustmentRetention) / 100), MediaRevenue: utils.Float64ToStr(float64(v.MediaRevenue) / 100), AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100), ExtraRevenue: utils.Float64ToStr(float64(v.ExtraRevenue) / 100), AgreementSharing: utils.Float64ToStr(float64(v.AgreementSharing) / 100), AgreementSharingTotal: utils.Float64ToStr(float64(v.AgreementSharingTotal) / 100), } if wxData != nil { tmp.OldClickRate = wxData.ClickRate tmp.OldClickCount = utils.IntToStr(wxData.ClickCount) tmp.OldEcpm = utils.Float64ToStr(utils.StrToFloat64(wxData.Ecpm) / 100) tmp.OldExposureCount = utils.IntToStr(wxData.ExposureCount) } tmpApplet := GetAppletInfo(c, v.AppId) if tmpApplet["platform"] != "" { tmp.Platform = tmpApplet["platform"] } if tmpApplet["name"] != "" { tmp.Name = tmpApplet["name"] } tmpSlot := GetSlotInfo(c, v.SlotId) if tmpSlot["state"] != "" { tmp.State = tmpSlot["state"] } if tmpSlot["name"] != "" { tmp.AdvName = tmpSlot["name"] } data = append(data, tmp) } } res := md.DataCenterGenerateDataRes{ List: data, Total: total, State: md.AdState, Platform: md.AdPlatform, } return res } func DataCenterGenerateDataDel(c *gin.Context, req md.DataCenterGenerateDataCommReq) error { NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db) data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id)) if data == nil { return errors.New("记录不存在") } if data.IsGenerateReport == 1 { return errors.New("记录已应用,不能删除") } // 1、删除 generate_wx_ad_data 数据 _, err := db.Db.Where("id=?", req.Id).Delete(&model.GenerateWxAdData{}) if err != nil { return err } // 2、删除 generate_wx_ad_data_with_agent_flow 数据 _, err = db.Db.Where("generate_data_id=?", req.Id).Delete(&model.GenerateWxAdDataWithAgentFlow{}) if err != nil { return err } // 3、修改 original_wx_ad_data 状态值 db.Db.Where("id=?", data.OriginalDataId).Cols("is_apply").Update(&model.OriginalWxAdData{IsApply: 0}) return nil } func DataCenterGenerateDataDetail(c *gin.Context, req md.DataCenterGenerateDataCommReq) { NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db) data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id)) if data == nil { e.OutErr(c, 400, e.NewErr(400, "记录不存在")) return } agentReward := make([]md.DataCenterGenerateDataDetailAgentReward, 0) NewGenerateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db) agent, _ := NewGenerateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(data.Id) if agent != nil { for _, v := range *agent { tmp := md.DataCenterGenerateDataDetailAgentReward{ Name: "", Account: "", AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100), AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate), ExtraRevenue: utils.Float64ToStr(float64(v.ExtraRevenue) / 100), ExtraRevenueRate: utils.IntToStr(data.ExtraRevenueRate), } tmpApplet := GetAgentInfo(c, v.AgentId) if tmpApplet["name"] != "" { tmp.Name = tmpApplet["name"] } if tmpApplet["account"] != "" { tmp.Account = tmpApplet["account"] } agentReward = append(agentReward, tmp) } } res := md.DataCenterGenerateDataDetailData{ PlatformRetentionRate: utils.IntToStr(data.PlatformRetentionRate), CommissionRetentionRate: utils.IntToStr(data.CommissionRetentionRate), MediaRevenueRate: utils.IntToStr(data.MediaRevenueRate), AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate), ExtraRevenueRate: utils.IntToStr(data.ExtraRevenueRate), AgreementSharingRate: utils.IntToStr(data.AgreementSharingRate), AgentReward: agentReward, } e.OutSuc(c, res, nil) return } func DataCenterGenerateDataDoing(c *gin.Context, req md.DataCenterGenerateDataCommReq) error { NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db) data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id)) if data == nil { return e.NewErr(400, "记录不存在") } if data.IsGenerateReport == 1 { return e.NewErr(400, "该记录已完成操作") } // 加锁 防止并发提取 mutexKey := fmt.Sprintf("%s:DataCenterGenerateDataDoing:%s", c.GetString("mid"), req.Id) withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX") if err != nil { return err } if withdrawAvailable != "OK" { return e.NewErr(400000, "请求过于频繁,请稍后再试") } args := md.SettlementWxAdData{ GenerateDataId: utils.StrToInt(req.Id), } err = SettlementWxAdData(args) if err != nil { return err } return nil }