From 4356cf6683d7ed23c9f29be3a5126008506d4170 Mon Sep 17 00:00:00 2001 From: dengbiao Date: Mon, 30 Sep 2024 20:11:13 +0800 Subject: [PATCH] 1 --- app/svc/svc_wx_data.go | 95 +++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 43 deletions(-) diff --git a/app/svc/svc_wx_data.go b/app/svc/svc_wx_data.go index 82ac062..d9df95e 100644 --- a/app/svc/svc_wx_data.go +++ b/app/svc/svc_wx_data.go @@ -8,11 +8,12 @@ import ( "code.fnuoos.com/zhimeng/model.git/src/super/implement" "code.fnuoos.com/zhimeng/model.git/src/super/model" "errors" + "github.com/shopspring/decimal" "time" ) func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData model.GenerateWxAdData) { - //1、查找原始数据记录 + // 1、查找原始数据记录 originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db) originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(req.OriginalDataId) if err != nil { @@ -23,7 +24,7 @@ func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData mode return } - //2、查询对应媒体、代理的分成策略 + // 2、查询对应媒体、代理的分成策略 mediumDivisionStrategyDb := implement.NewMediumDivisionStrategyDb(db.Db) mediumDivisionStrategy, err := mediumDivisionStrategyDb.GetOriginalWxAdDataByMediumId(originalWxAdData.MediumId) if err != nil { @@ -39,16 +40,16 @@ func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData mode return } - //3、计算媒体、代理收益、平台留存、佣金留存、协议分成、协议总分成 + // 3、计算媒体、代理收益、平台留存、佣金留存、协议分成、协议总分成 publisherIncome := originalWxAdData.PublisherIncome - mediaRevenue := publisherIncome * mediumDivisionStrategy.MediaRevenueRate / 100 //媒体收益 - agentRevenue := publisherIncome * mediumDivisionStrategy.AgentRevenueRate / 100 //代理收益 - platformRetention := publisherIncome * mediumDivisionStrategy.PlatformRetentionRate / 100 //平台留存 - commissionRetention := publisherIncome * mediumDivisionStrategy.CommissionRetentionRate / 100 //佣金留存 - agreementSharingTotal := (mediaRevenue + agentRevenue) / (100 - mediumDivisionStrategy.AgreementSharingRate) //协议总分成(倒推) - agreementSharing := agreementSharingTotal - (mediaRevenue + agentRevenue) //协议分成 + mediaRevenue := publisherIncome * mediumDivisionStrategy.MediaRevenueRate / 100 // 媒体收益 + agentRevenue := publisherIncome * mediumDivisionStrategy.AgentRevenueRate / 100 // 代理收益 + platformRetention := publisherIncome * mediumDivisionStrategy.PlatformRetentionRate / 100 // 平台留存 + commissionRetention := publisherIncome * mediumDivisionStrategy.CommissionRetentionRate / 100 // 佣金留存 + agreementSharingTotal := (mediaRevenue + agentRevenue) / (100 - mediumDivisionStrategy.AgreementSharingRate) // 协议总分成(倒推) + agreementSharing := agreementSharingTotal - (mediaRevenue + agentRevenue) // 协议分成 - //3、判断是否有调价留存 + // 3、判断是否有调价留存 var priceAdjustmentRetention int if req.NowEcpm != req.OriginalEcpm || req.NowExposureCount != req.OriginalExposureCount { tmpMediaRevenue := int(utils.StrToFloat64(req.NowEcpm) * float64(req.NowExposureCount) / 1000) @@ -56,7 +57,7 @@ func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData mode mediaRevenue = tmpMediaRevenue } - //4、计算各代理收益 + // 4、计算各代理收益 var extraRevenue int var agentRevenueFlows []struct { AgentId int `json:"agent_id"` @@ -84,7 +85,7 @@ func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData mode }) } - //5、插入 generate_wx_ad_data 、generate_wx_ad_data_with_agent_flow 数据 + // 5、插入 generate_wx_ad_data 、generate_wx_ad_data_with_agent_flow 数据 session := db.Db.NewSession() defer session.Close() session.Begin() @@ -100,11 +101,11 @@ func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData mode AdSlot: originalWxAdData.AdSlot, Date: originalWxAdData.Date, ReqSuccCount: originalWxAdData.ReqSuccCount, - ExposureCount: req.NowExposureCount, //现-曝光量 + ExposureCount: req.NowExposureCount, // 现-曝光量 ExposureRate: originalWxAdData.ExposureRate, ClickCount: originalWxAdData.ClickCount, ClickRate: originalWxAdData.ClickRate, - Ecpm: req.NowEcpm, //现-ecpm + Ecpm: req.NowEcpm, // 现-ecpm PlatformRetention: platformRetention, CommissionRetention: commissionRetention, PriceAdjustmentRetention: priceAdjustmentRetention, @@ -157,7 +158,7 @@ func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData mode return } - //6、修改 original_wx_ad_data 记录中的 is_apply(是否已应用(0:未 1:已) ) + // 6、修改 original_wx_ad_data 记录中的 is_apply(是否已应用(0:未 1:已) ) originalWxAdData.IsApply = 1 _, err = originalWxAdDataDb.UpdateOriginalWxAdDataBySession(session, originalWxAdData, "is_apply") if err != nil { @@ -168,7 +169,7 @@ func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData mode } func ClacEcpm(req md.ClacEcpmReq) (err error, ecpm string) { - //1、查找原始数据记录 + // 1、查找原始数据记录 originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db) originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(req.GenerateDataId) if err != nil { @@ -179,7 +180,7 @@ func ClacEcpm(req md.ClacEcpmReq) (err error, ecpm string) { return } - //2、查询对应媒体分成策略 + // 2、查询对应媒体分成策略 mediumDivisionStrategyDb := implement.NewMediumDivisionStrategyDb(db.Db) mediumDivisionStrategy, err := mediumDivisionStrategyDb.GetOriginalWxAdDataByMediumId(originalWxAdData.MediumId) if err != nil { @@ -190,19 +191,27 @@ func ClacEcpm(req md.ClacEcpmReq) (err error, ecpm string) { return } - //3、计算媒体、代理收益、协议总分成 - publisherIncome := originalWxAdData.PublisherIncome - mediaRevenue := publisherIncome * mediumDivisionStrategy.MediaRevenueRate / 100 //媒体收益 - agentRevenue := publisherIncome * mediumDivisionStrategy.AgentRevenueRate / 100 //代理收益 - agreementSharingTotal := (mediaRevenue + agentRevenue) / (100 - mediumDivisionStrategy.AgreementSharingRate) //协议总分成(倒推) + // 3、计算媒体、代理收益、协议总分成 + publisherIncome := decimal.NewFromInt(int64(originalWxAdData.PublisherIncome)) + mediaRevenueRate := decimal.NewFromInt(int64(mediumDivisionStrategy.MediaRevenueRate)) + agentRevenueRate := decimal.NewFromInt(int64(mediumDivisionStrategy.AgentRevenueRate)) + agreementSharingRate := decimal.NewFromInt(int64(mediumDivisionStrategy.AgreementSharingRate)) + exposureCount := decimal.NewFromInt(int64(originalWxAdData.ExposureCount)) + rateValue := decimal.NewFromInt(100) + thousandRateValue := decimal.NewFromInt(1000) + + mediaRevenue := publisherIncome.Mul(mediaRevenueRate.Div(rateValue)) // 媒体收益 + agentRevenue := publisherIncome.Mul(agentRevenueRate.Div(rateValue)) // 代理收益 + + agreementSharingTotal := (mediaRevenue.Add(agentRevenue)).Div(rateValue.Sub(agreementSharingRate)) // 协议总分成(倒推) - //4、倒退出当前ecpm值 - ecpm = utils.Float64ToStrPrec4(float64(agreementSharingTotal) / (float64(originalWxAdData.ExposureCount) / 1000)) + // 4、倒退出当前ecpm值 + ecpm = agreementSharingTotal.Div(exposureCount.Div(thousandRateValue)).Round(2).String() return } func SettlementWxAdData(req md.SettlementWxAdData) (err error) { - //1、查找生成数据记录 + // 1、查找生成数据记录 generateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db) generateWxAdData, err := generateWxAdDataDb.GetGenerateWxAdData(req.GenerateDataId) if err != nil { @@ -223,14 +232,14 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) { return } - //2、查询 生成数据-代理明细 记录 + // 2、查询 生成数据-代理明细 记录 generateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db) generateWxAdDataWithAgentFlow, err := generateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(generateWxAdData.Id) if err != nil { return } - //3、查询 媒体、代理的结算方式 + // 3、查询 媒体、代理的结算方式 mediumListDb := implement.NewMediumListDb(db.Db) medium, err := mediumListDb.GetMediumList(originalWxAdData.MediumId) if err != nil { @@ -252,27 +261,27 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) { agentMap[v.AgentId] = *agent } - //4、生成数据 + // 4、生成数据 now := time.Now() session := db.Db.NewSession() defer session.Close() session.Begin() - //4.1 新增/更新 medium_settlement 数据 + // 4.1 新增/更新 medium_settlement 数据 mediumSettlementDb := implement.NewMediumSettlementDb(db.Db) mediumSettlement, err := mediumSettlementDb.GetMediumSettlementForAvailable(originalWxAdData.MediumId) if err != nil { return } var basicIncomeBefore, mediumSettlementState, mediumSettlementPayState int - //判断是否为预付结算类型 + // 判断是否为预付结算类型 if medium.SettlementType == enum.MediumSettlementTypeForPaymentInAdvance { mediumSettlementState = enum.MediumSettlementStateForCompleteSign mediumSettlementPayState = enum.MediumSettlementPayStateForPaymentAlready } if mediumSettlement == nil { - //新增一条数据 + // 新增一条数据 mediumSettlement = &model.MediumSettlement{ Uuid: originalWxAdData.Uuid, MediumId: originalWxAdData.MediumId, @@ -300,18 +309,18 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) { return } } else { - //更新数据 + // 更新数据 if medium.SettlementType != mediumSettlement.Kind { - //TODO::前后结算类型不一致,之前结算单状态自动变成"核算中" + // TODO::前后结算类型不一致,之前结算单状态自动变成"核算中" mediumSettlement.State = enum.MediumSettlementStateForAccountingInProgress - //mediumSettlement.PayState = mediumSettlementPayState + // mediumSettlement.PayState = mediumSettlementPayState _, err = mediumSettlementDb.UpdateMediumSettlementBySession(session, mediumSettlement, "state") if err != nil { _ = session.Rollback() return } - //新增一条数据 + // 新增一条数据 mediumSettlement = &model.MediumSettlement{ Uuid: originalWxAdData.Uuid, MediumId: originalWxAdData.MediumId, @@ -368,7 +377,7 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) { } } - //4.2 新增 medium_settlement_with_flow 数据 + // 4.2 新增 medium_settlement_with_flow 数据 mediumSettlementWithFlowDb := implement.NewMediumSettlementWithFlowDb(db.Db) _, err = mediumSettlementWithFlowDb.MediumSettlementWithFlowInsertBySession(session, &model.MediumSettlementWithFlow{ SettlementId: mediumSettlement.Id, @@ -386,7 +395,7 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) { _ = session.Rollback() return } - //4.3 新增 agent_settlement、agent_settlement_with_flow 数据 + // 4.3 新增 agent_settlement、agent_settlement_with_flow 数据 agentSettlementDb := implement.NewAgentSettlementDb(db.Db) agentSettlementWithFlowDb := implement.NewAgentSettlementWithFlowDb(db.Db) for _, v := range *generateWxAdDataWithAgentFlow { @@ -397,13 +406,13 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) { } var agentBasicIncomeBefore, agentOtherIncomeBefore, agentSettlementState, agentSettlementPayState int - //判断是否为预付结算类型 + // 判断是否为预付结算类型 if agentMap[v.AgentId].SettlementType == enum.AgentSettlementTypeForPaymentInAdvance { agentSettlementState = enum.AgentSettlementStateForCompleteSign agentSettlementPayState = enum.AgentSettlementPayStateForPaymentAlready } if agentSettlement == nil { - //新增一条数据 + // 新增一条数据 agentSettlement = &model.AgentSettlement{ Uuid: v.Uuid, AgentId: v.AgentId, @@ -427,9 +436,9 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) { return } } else { - //更新数据 + // 更新数据 if agentMap[v.AgentId].SettlementType != agentSettlement.Kind { - //TODO::前后结算类型不一致,之前结算单状态自动变成"核算中" + // TODO::前后结算类型不一致,之前结算单状态自动变成"核算中" agentSettlement.State = enum.AgentSettlementStateForAccountingInProgress _, err = agentSettlementDb.UpdateAgentSettlementBySession(session, agentSettlement, "state") if err != nil { @@ -437,7 +446,7 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) { return } - //新增一条数据 + // 新增一条数据 agentSettlement = &model.AgentSettlement{ Uuid: v.Uuid, AgentId: v.AgentId, @@ -506,7 +515,7 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) { } } - //4、修改 generate_wx_ad_data 记录中的 is_generate_report (是否已应用(0:未 1:已) ) + // 4、修改 generate_wx_ad_data 记录中的 is_generate_report (是否已应用(0:未 1:已) ) generateWxAdData.IsGenerateReport = 1 _, err = generateWxAdDataDb.UpdateGenerateWxAdDataBySession(session, generateWxAdData, "is_generate_report") if err != nil {