|
|
@@ -0,0 +1,197 @@ |
|
|
|
package svc |
|
|
|
|
|
|
|
import ( |
|
|
|
"applet/app/md" |
|
|
|
"applet/app/utils" |
|
|
|
db "code.fnuoos.com/zhimeng/model.git/src" |
|
|
|
"code.fnuoos.com/zhimeng/model.git/src/super/implement" |
|
|
|
"code.fnuoos.com/zhimeng/model.git/src/super/model" |
|
|
|
"errors" |
|
|
|
"time" |
|
|
|
) |
|
|
|
|
|
|
|
func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData model.GenerateWxAdData) { |
|
|
|
//1、查找原始数据记录 |
|
|
|
originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db) |
|
|
|
originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(req.GenerateDataId) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
if originalWxAdData == nil { |
|
|
|
err = errors.New("未查询到原始数据记录") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//2、查询对应媒体、代理的分成策略 |
|
|
|
mediumDivisionStrategyDb := implement.NewMediumDivisionStrategyDb(db.Db) |
|
|
|
mediumDivisionStrategy, err := mediumDivisionStrategyDb.GetOriginalWxAdDataByMediumId(originalWxAdData.MediumId) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
if mediumDivisionStrategy == nil { |
|
|
|
err = errors.New("未查询到对应代理的分成策略") |
|
|
|
return |
|
|
|
} |
|
|
|
mediumDivisionStrategyWithAgentFlowDb := implement.NewMediumDivisionStrategyWithAgentFlowDb(db.Db) |
|
|
|
mediumDivisionStrategyWithAgentFlows, err := mediumDivisionStrategyWithAgentFlowDb.FindMediumDivisionStrategyWithAgentFlowByStrategyId(mediumDivisionStrategy.MediumId) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//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) //协议分成 |
|
|
|
|
|
|
|
//3、判断是否有调价留存 |
|
|
|
var priceAdjustmentRetention int |
|
|
|
if req.NowEcpm != req.OriginalEcpm || req.NowExposureCount != req.OriginalExposureCount { |
|
|
|
tmpMediaRevenue := int(utils.StrToFloat64(req.NowEcpm) * float64(req.NowExposureCount) / 1000) |
|
|
|
priceAdjustmentRetention = tmpMediaRevenue - mediaRevenue |
|
|
|
mediaRevenue = tmpMediaRevenue |
|
|
|
} |
|
|
|
|
|
|
|
//4、计算各代理收益 |
|
|
|
var extraRevenue int |
|
|
|
var agentRevenueFlows []struct { |
|
|
|
AgentId int `json:"agent_id"` |
|
|
|
AgentRevenueRate int `json:"agent_revenue_rate"` |
|
|
|
ExtraRevenueRate int `json:"extra_revenue_rate"` |
|
|
|
AgentRevenue int `json:"agent_revenue"` |
|
|
|
ExtraRevenue int `json:"extra_revenue"` |
|
|
|
} |
|
|
|
for _, v := range *mediumDivisionStrategyWithAgentFlows { |
|
|
|
tmpAgentRevenue := agentRevenue * v.AgentRevenueRate / 100 |
|
|
|
tmpExtraRevenue := commissionRetention * v.ExtraRevenueRate / 100 |
|
|
|
extraRevenue += tmpExtraRevenue |
|
|
|
agentRevenueFlows = append(agentRevenueFlows, struct { |
|
|
|
AgentId int `json:"agent_id"` |
|
|
|
AgentRevenueRate int `json:"agent_revenue_rate"` |
|
|
|
ExtraRevenueRate int `json:"extra_revenue_rate"` |
|
|
|
AgentRevenue int `json:"agent_revenue"` |
|
|
|
ExtraRevenue int `json:"extra_revenue"` |
|
|
|
}{ |
|
|
|
AgentId: v.AgentId, |
|
|
|
AgentRevenueRate: v.AgentRevenueRate, |
|
|
|
ExtraRevenueRate: v.ExtraRevenueRate, |
|
|
|
AgentRevenue: tmpAgentRevenue, |
|
|
|
ExtraRevenue: tmpExtraRevenue, |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
//5、插入 generate_wx_ad_data 、generate_wx_ad_data_with_agent_flow 数据 |
|
|
|
session := db.Db.NewSession() |
|
|
|
defer session.Close() |
|
|
|
session.Begin() |
|
|
|
|
|
|
|
now := time.Now() |
|
|
|
generateWxAdData = model.GenerateWxAdData{ |
|
|
|
Uuid: originalWxAdData.Uuid, |
|
|
|
AppId: originalWxAdData.AppId, |
|
|
|
GenerateDataId: originalWxAdData.Id, |
|
|
|
SlotId: originalWxAdData.SlotId, |
|
|
|
AdSlot: originalWxAdData.AdSlot, |
|
|
|
Date: originalWxAdData.Date, |
|
|
|
ReqSuccCount: originalWxAdData.ReqSuccCount, |
|
|
|
ExposureCount: req.NowExposureCount, //现-曝光量 |
|
|
|
ExposureRate: originalWxAdData.ExposureRate, |
|
|
|
ClickCount: originalWxAdData.ClickCount, |
|
|
|
ClickRate: originalWxAdData.ClickRate, |
|
|
|
Ecpm: req.NowEcpm, //现-ecpm |
|
|
|
PlatformRetention: platformRetention, |
|
|
|
CommissionRetention: commissionRetention, |
|
|
|
PriceAdjustmentRetention: priceAdjustmentRetention, |
|
|
|
MediaRevenue: mediaRevenue, |
|
|
|
AgentRevenue: agentRevenue, |
|
|
|
ExtraRevenue: extraRevenue, |
|
|
|
AgreementSharing: agreementSharing, |
|
|
|
AgreementSharingTotal: agreementSharingTotal, |
|
|
|
PlatformRetentionRate: mediumDivisionStrategy.PlatformRetentionRate, |
|
|
|
CommissionRetentionRate: mediumDivisionStrategy.CommissionRetentionRate, |
|
|
|
MediaRevenueRate: mediumDivisionStrategy.MediaRevenueRate, |
|
|
|
AgentRevenueRate: mediumDivisionStrategy.AgentRevenueRate, |
|
|
|
ExtraRevenueRate: mediumDivisionStrategy.ExtraRevenueRate, |
|
|
|
AgreementSharingRate: mediumDivisionStrategy.AgreementSharingRate, |
|
|
|
IsGenerateReport: 0, |
|
|
|
CreateAt: now.Format("2006-01-02 15:04:05"), |
|
|
|
UpdateAt: now.Format("2006-01-02 15:04:05"), |
|
|
|
} |
|
|
|
generateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db) |
|
|
|
_, err = generateWxAdDataDb.GenerateWxAdDataInsertBySession(session, &generateWxAdData) |
|
|
|
if err != nil { |
|
|
|
_ = session.Rollback() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
var generateWxAdDataWithAgentFlows []*model.GenerateWxAdDataWithAgentFlow |
|
|
|
for _, v := range agentRevenueFlows { |
|
|
|
generateWxAdDataWithAgentFlows = append(generateWxAdDataWithAgentFlows, &model.GenerateWxAdDataWithAgentFlow{ |
|
|
|
Uuid: generateWxAdData.Uuid, |
|
|
|
AppId: generateWxAdData.AppId, |
|
|
|
AgentId: v.AgentId, |
|
|
|
GenerateDataId: generateWxAdData.Id, |
|
|
|
SlotId: generateWxAdData.SlotId, |
|
|
|
AdSlot: generateWxAdData.AdSlot, |
|
|
|
Date: generateWxAdData.Date, |
|
|
|
AgentRevenue: v.AgentRevenue, |
|
|
|
AgentRevenueRate: v.AgentRevenueRate, |
|
|
|
ExtraRevenue: v.ExtraRevenue, |
|
|
|
ExtraRevenueRate: v.ExtraRevenueRate, |
|
|
|
CreateAt: now.Format("2006-01-02 15:04:05"), |
|
|
|
UpdateAt: now.Format("2006-01-02 15:04:05"), |
|
|
|
}) |
|
|
|
} |
|
|
|
generateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db) |
|
|
|
_, err = generateWxAdDataWithAgentFlowDb.BatchAddGenerateWxAdDataWithAgentFlow(session, generateWxAdDataWithAgentFlows) |
|
|
|
if err != nil { |
|
|
|
_ = session.Rollback() |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//6、修改 original_wx_ad_data 记录中的 is_apply(是否已应用(0:未 1:已) ) |
|
|
|
originalWxAdData.IsApply = 1 |
|
|
|
_, err = originalWxAdDataDb.UpdateOriginalWxAdDataBySession(session, originalWxAdData, "is_apply") |
|
|
|
if err != nil { |
|
|
|
_ = session.Rollback() |
|
|
|
return |
|
|
|
} |
|
|
|
return session.Commit(), generateWxAdData |
|
|
|
} |
|
|
|
|
|
|
|
func ClacEcpm(req md.ClacEcpmReq) (err error, ecpm string) { |
|
|
|
//1、查找原始数据记录 |
|
|
|
originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db) |
|
|
|
originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(req.GenerateDataId) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
if originalWxAdData == nil { |
|
|
|
err = errors.New("未查询到原始数据记录") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//2、查询对应媒体分成策略 |
|
|
|
mediumDivisionStrategyDb := implement.NewMediumDivisionStrategyDb(db.Db) |
|
|
|
mediumDivisionStrategy, err := mediumDivisionStrategyDb.GetOriginalWxAdDataByMediumId(originalWxAdData.MediumId) |
|
|
|
if err != nil { |
|
|
|
return |
|
|
|
} |
|
|
|
if mediumDivisionStrategy == nil { |
|
|
|
err = errors.New("未查询到对应代理的分成策略") |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//3、计算媒体、代理收益、协议总分成 |
|
|
|
publisherIncome := originalWxAdData.PublisherIncome |
|
|
|
mediaRevenue := publisherIncome * mediumDivisionStrategy.MediaRevenueRate / 100 //媒体收益 |
|
|
|
agentRevenue := publisherIncome * mediumDivisionStrategy.AgentRevenueRate / 100 //代理收益 |
|
|
|
agreementSharingTotal := (mediaRevenue + agentRevenue) / (100 - mediumDivisionStrategy.AgreementSharingRate) //协议总分成(倒推) |
|
|
|
|
|
|
|
//4、倒退出当前ecpm值 |
|
|
|
ecpm = utils.Float64ToStrPrec4(float64(agreementSharingTotal) / (float64(originalWxAdData.ExposureCount) / 1000)) |
|
|
|
return |
|
|
|
} |