dengbiao 1 month ago
parent
commit
4356cf6683
1 changed files with 52 additions and 43 deletions
  1. +52
    -43
      app/svc/svc_wx_data.go

+ 52
- 43
app/svc/svc_wx_data.go View File

@@ -8,11 +8,12 @@ import (
"code.fnuoos.com/zhimeng/model.git/src/super/implement" "code.fnuoos.com/zhimeng/model.git/src/super/implement"
"code.fnuoos.com/zhimeng/model.git/src/super/model" "code.fnuoos.com/zhimeng/model.git/src/super/model"
"errors" "errors"
"github.com/shopspring/decimal"
"time" "time"
) )


func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData model.GenerateWxAdData) { func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData model.GenerateWxAdData) {
//1、查找原始数据记录
// 1、查找原始数据记录
originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db) originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db)
originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(req.OriginalDataId) originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(req.OriginalDataId)
if err != nil { if err != nil {
@@ -23,7 +24,7 @@ func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData mode
return return
} }


//2、查询对应媒体、代理的分成策略
// 2、查询对应媒体、代理的分成策略
mediumDivisionStrategyDb := implement.NewMediumDivisionStrategyDb(db.Db) mediumDivisionStrategyDb := implement.NewMediumDivisionStrategyDb(db.Db)
mediumDivisionStrategy, err := mediumDivisionStrategyDb.GetOriginalWxAdDataByMediumId(originalWxAdData.MediumId) mediumDivisionStrategy, err := mediumDivisionStrategyDb.GetOriginalWxAdDataByMediumId(originalWxAdData.MediumId)
if err != nil { if err != nil {
@@ -39,16 +40,16 @@ func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData mode
return return
} }


//3、计算媒体、代理收益、平台留存、佣金留存、协议分成、协议总分成
// 3、计算媒体、代理收益、平台留存、佣金留存、协议分成、协议总分成
publisherIncome := originalWxAdData.PublisherIncome 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 var priceAdjustmentRetention int
if req.NowEcpm != req.OriginalEcpm || req.NowExposureCount != req.OriginalExposureCount { if req.NowEcpm != req.OriginalEcpm || req.NowExposureCount != req.OriginalExposureCount {
tmpMediaRevenue := int(utils.StrToFloat64(req.NowEcpm) * float64(req.NowExposureCount) / 1000) 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 mediaRevenue = tmpMediaRevenue
} }


//4、计算各代理收益
// 4、计算各代理收益
var extraRevenue int var extraRevenue int
var agentRevenueFlows []struct { var agentRevenueFlows []struct {
AgentId int `json:"agent_id"` 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() session := db.Db.NewSession()
defer session.Close() defer session.Close()
session.Begin() session.Begin()
@@ -100,11 +101,11 @@ func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData mode
AdSlot: originalWxAdData.AdSlot, AdSlot: originalWxAdData.AdSlot,
Date: originalWxAdData.Date, Date: originalWxAdData.Date,
ReqSuccCount: originalWxAdData.ReqSuccCount, ReqSuccCount: originalWxAdData.ReqSuccCount,
ExposureCount: req.NowExposureCount, //现-曝光量
ExposureCount: req.NowExposureCount, // 现-曝光量
ExposureRate: originalWxAdData.ExposureRate, ExposureRate: originalWxAdData.ExposureRate,
ClickCount: originalWxAdData.ClickCount, ClickCount: originalWxAdData.ClickCount,
ClickRate: originalWxAdData.ClickRate, ClickRate: originalWxAdData.ClickRate,
Ecpm: req.NowEcpm, //现-ecpm
Ecpm: req.NowEcpm, // 现-ecpm
PlatformRetention: platformRetention, PlatformRetention: platformRetention,
CommissionRetention: commissionRetention, CommissionRetention: commissionRetention,
PriceAdjustmentRetention: priceAdjustmentRetention, PriceAdjustmentRetention: priceAdjustmentRetention,
@@ -157,7 +158,7 @@ func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData mode
return return
} }


//6、修改 original_wx_ad_data 记录中的 is_apply(是否已应用(0:未 1:已) )
// 6、修改 original_wx_ad_data 记录中的 is_apply(是否已应用(0:未 1:已) )
originalWxAdData.IsApply = 1 originalWxAdData.IsApply = 1
_, err = originalWxAdDataDb.UpdateOriginalWxAdDataBySession(session, originalWxAdData, "is_apply") _, err = originalWxAdDataDb.UpdateOriginalWxAdDataBySession(session, originalWxAdData, "is_apply")
if err != nil { 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) { func ClacEcpm(req md.ClacEcpmReq) (err error, ecpm string) {
//1、查找原始数据记录
// 1、查找原始数据记录
originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db) originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db)
originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(req.GenerateDataId) originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(req.GenerateDataId)
if err != nil { if err != nil {
@@ -179,7 +180,7 @@ func ClacEcpm(req md.ClacEcpmReq) (err error, ecpm string) {
return return
} }


//2、查询对应媒体分成策略
// 2、查询对应媒体分成策略
mediumDivisionStrategyDb := implement.NewMediumDivisionStrategyDb(db.Db) mediumDivisionStrategyDb := implement.NewMediumDivisionStrategyDb(db.Db)
mediumDivisionStrategy, err := mediumDivisionStrategyDb.GetOriginalWxAdDataByMediumId(originalWxAdData.MediumId) mediumDivisionStrategy, err := mediumDivisionStrategyDb.GetOriginalWxAdDataByMediumId(originalWxAdData.MediumId)
if err != nil { if err != nil {
@@ -190,19 +191,27 @@ func ClacEcpm(req md.ClacEcpmReq) (err error, ecpm string) {
return 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 return
} }


func SettlementWxAdData(req md.SettlementWxAdData) (err error) { func SettlementWxAdData(req md.SettlementWxAdData) (err error) {
//1、查找生成数据记录
// 1、查找生成数据记录
generateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db) generateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
generateWxAdData, err := generateWxAdDataDb.GetGenerateWxAdData(req.GenerateDataId) generateWxAdData, err := generateWxAdDataDb.GetGenerateWxAdData(req.GenerateDataId)
if err != nil { if err != nil {
@@ -223,14 +232,14 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) {
return return
} }


//2、查询 生成数据-代理明细 记录
// 2、查询 生成数据-代理明细 记录
generateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db) generateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db)
generateWxAdDataWithAgentFlow, err := generateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(generateWxAdData.Id) generateWxAdDataWithAgentFlow, err := generateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(generateWxAdData.Id)
if err != nil { if err != nil {
return return
} }


//3、查询 媒体、代理的结算方式
// 3、查询 媒体、代理的结算方式
mediumListDb := implement.NewMediumListDb(db.Db) mediumListDb := implement.NewMediumListDb(db.Db)
medium, err := mediumListDb.GetMediumList(originalWxAdData.MediumId) medium, err := mediumListDb.GetMediumList(originalWxAdData.MediumId)
if err != nil { if err != nil {
@@ -252,27 +261,27 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) {
agentMap[v.AgentId] = *agent agentMap[v.AgentId] = *agent
} }


//4、生成数据
// 4、生成数据
now := time.Now() now := time.Now()
session := db.Db.NewSession() session := db.Db.NewSession()
defer session.Close() defer session.Close()
session.Begin() session.Begin()


//4.1 新增/更新 medium_settlement 数据
// 4.1 新增/更新 medium_settlement 数据
mediumSettlementDb := implement.NewMediumSettlementDb(db.Db) mediumSettlementDb := implement.NewMediumSettlementDb(db.Db)
mediumSettlement, err := mediumSettlementDb.GetMediumSettlementForAvailable(originalWxAdData.MediumId) mediumSettlement, err := mediumSettlementDb.GetMediumSettlementForAvailable(originalWxAdData.MediumId)
if err != nil { if err != nil {
return return
} }
var basicIncomeBefore, mediumSettlementState, mediumSettlementPayState int var basicIncomeBefore, mediumSettlementState, mediumSettlementPayState int
//判断是否为预付结算类型
// 判断是否为预付结算类型
if medium.SettlementType == enum.MediumSettlementTypeForPaymentInAdvance { if medium.SettlementType == enum.MediumSettlementTypeForPaymentInAdvance {
mediumSettlementState = enum.MediumSettlementStateForCompleteSign mediumSettlementState = enum.MediumSettlementStateForCompleteSign
mediumSettlementPayState = enum.MediumSettlementPayStateForPaymentAlready mediumSettlementPayState = enum.MediumSettlementPayStateForPaymentAlready
} }


if mediumSettlement == nil { if mediumSettlement == nil {
//新增一条数据
// 新增一条数据
mediumSettlement = &model.MediumSettlement{ mediumSettlement = &model.MediumSettlement{
Uuid: originalWxAdData.Uuid, Uuid: originalWxAdData.Uuid,
MediumId: originalWxAdData.MediumId, MediumId: originalWxAdData.MediumId,
@@ -300,18 +309,18 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) {
return return
} }
} else { } else {
//更新数据
// 更新数据
if medium.SettlementType != mediumSettlement.Kind { if medium.SettlementType != mediumSettlement.Kind {
//TODO::前后结算类型不一致,之前结算单状态自动变成"核算中"
// TODO::前后结算类型不一致,之前结算单状态自动变成"核算中"
mediumSettlement.State = enum.MediumSettlementStateForAccountingInProgress mediumSettlement.State = enum.MediumSettlementStateForAccountingInProgress
//mediumSettlement.PayState = mediumSettlementPayState
// mediumSettlement.PayState = mediumSettlementPayState
_, err = mediumSettlementDb.UpdateMediumSettlementBySession(session, mediumSettlement, "state") _, err = mediumSettlementDb.UpdateMediumSettlementBySession(session, mediumSettlement, "state")
if err != nil { if err != nil {
_ = session.Rollback() _ = session.Rollback()
return return
} }


//新增一条数据
// 新增一条数据
mediumSettlement = &model.MediumSettlement{ mediumSettlement = &model.MediumSettlement{
Uuid: originalWxAdData.Uuid, Uuid: originalWxAdData.Uuid,
MediumId: originalWxAdData.MediumId, 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) mediumSettlementWithFlowDb := implement.NewMediumSettlementWithFlowDb(db.Db)
_, err = mediumSettlementWithFlowDb.MediumSettlementWithFlowInsertBySession(session, &model.MediumSettlementWithFlow{ _, err = mediumSettlementWithFlowDb.MediumSettlementWithFlowInsertBySession(session, &model.MediumSettlementWithFlow{
SettlementId: mediumSettlement.Id, SettlementId: mediumSettlement.Id,
@@ -386,7 +395,7 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) {
_ = session.Rollback() _ = session.Rollback()
return return
} }
//4.3 新增 agent_settlement、agent_settlement_with_flow 数据
// 4.3 新增 agent_settlement、agent_settlement_with_flow 数据
agentSettlementDb := implement.NewAgentSettlementDb(db.Db) agentSettlementDb := implement.NewAgentSettlementDb(db.Db)
agentSettlementWithFlowDb := implement.NewAgentSettlementWithFlowDb(db.Db) agentSettlementWithFlowDb := implement.NewAgentSettlementWithFlowDb(db.Db)
for _, v := range *generateWxAdDataWithAgentFlow { for _, v := range *generateWxAdDataWithAgentFlow {
@@ -397,13 +406,13 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) {
} }


var agentBasicIncomeBefore, agentOtherIncomeBefore, agentSettlementState, agentSettlementPayState int var agentBasicIncomeBefore, agentOtherIncomeBefore, agentSettlementState, agentSettlementPayState int
//判断是否为预付结算类型
// 判断是否为预付结算类型
if agentMap[v.AgentId].SettlementType == enum.AgentSettlementTypeForPaymentInAdvance { if agentMap[v.AgentId].SettlementType == enum.AgentSettlementTypeForPaymentInAdvance {
agentSettlementState = enum.AgentSettlementStateForCompleteSign agentSettlementState = enum.AgentSettlementStateForCompleteSign
agentSettlementPayState = enum.AgentSettlementPayStateForPaymentAlready agentSettlementPayState = enum.AgentSettlementPayStateForPaymentAlready
} }
if agentSettlement == nil { if agentSettlement == nil {
//新增一条数据
// 新增一条数据
agentSettlement = &model.AgentSettlement{ agentSettlement = &model.AgentSettlement{
Uuid: v.Uuid, Uuid: v.Uuid,
AgentId: v.AgentId, AgentId: v.AgentId,
@@ -427,9 +436,9 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) {
return return
} }
} else { } else {
//更新数据
// 更新数据
if agentMap[v.AgentId].SettlementType != agentSettlement.Kind { if agentMap[v.AgentId].SettlementType != agentSettlement.Kind {
//TODO::前后结算类型不一致,之前结算单状态自动变成"核算中"
// TODO::前后结算类型不一致,之前结算单状态自动变成"核算中"
agentSettlement.State = enum.AgentSettlementStateForAccountingInProgress agentSettlement.State = enum.AgentSettlementStateForAccountingInProgress
_, err = agentSettlementDb.UpdateAgentSettlementBySession(session, agentSettlement, "state") _, err = agentSettlementDb.UpdateAgentSettlementBySession(session, agentSettlement, "state")
if err != nil { if err != nil {
@@ -437,7 +446,7 @@ func SettlementWxAdData(req md.SettlementWxAdData) (err error) {
return return
} }


//新增一条数据
// 新增一条数据
agentSettlement = &model.AgentSettlement{ agentSettlement = &model.AgentSettlement{
Uuid: v.Uuid, Uuid: v.Uuid,
AgentId: v.AgentId, 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 generateWxAdData.IsGenerateReport = 1
_, err = generateWxAdDataDb.UpdateGenerateWxAdDataBySession(session, generateWxAdData, "is_generate_report") _, err = generateWxAdDataDb.UpdateGenerateWxAdDataBySession(session, generateWxAdData, "is_generate_report")
if err != nil { if err != nil {


Loading…
Cancel
Save