|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605 |
- package svc
-
- import (
- "applet/app/enum"
- "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"
- "fmt"
- "github.com/shopspring/decimal"
- "time"
- )
-
- func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData model.GenerateWxAdData) {
- // 1、查找原始数据记录
- originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db)
- originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(req.OriginalDataId)
- 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.Id)
- if err != nil {
- return
- }
-
- // 3、计算媒体、代理收益、平台留存、佣金留存、协议分成、协议总分成
- publisherIncome := float64(originalWxAdData.PublisherIncome)
- mediaRevenue := utils.RoundToTwoDecimalPlaces(publisherIncome * float64(mediumDivisionStrategy.MediaRevenueRate) / 100) // 媒体收益
- agentRevenue := utils.RoundToTwoDecimalPlaces(publisherIncome * float64(mediumDivisionStrategy.AgentRevenueRate) / 100) // 代理收益
- platformRetention := publisherIncome * float64(mediumDivisionStrategy.PlatformRetentionRate) / 100 // 平台留存
- commissionRetention := publisherIncome * float64(mediumDivisionStrategy.CommissionRetentionRate) / 100 // 佣金留存
- agreementSharingTotal := (mediaRevenue + agentRevenue) / ((100 - float64(mediumDivisionStrategy.AgreementSharingRate)) / 100) // 协议总分成(倒推)
- agreementSharing := agreementSharingTotal - (mediaRevenue + agentRevenue) // 协议分成
-
- // 3、判断是否有调价留存
- var priceAdjustmentRetention int
- ecpmReq := md.ClacEcpmReq{
- OriginalExposureCount: originalWxAdData.ExposureCount,
- OriginalEcpm: originalWxAdData.Ecpm,
- GenerateDataId: originalWxAdData.Id,
- }
- _, ecpm := ClacEcpm(ecpmReq)
- fmt.Println(utils.Float64ToStr(utils.StrToFloat64(ecpm) / 100))
- if req.NowEcpm != utils.Float64ToStr(utils.StrToFloat64(ecpm)/100) || req.NowExposureCount != req.OriginalExposureCount {
- tmpMediaRevenue := int(utils.StrToFloat64(req.NowEcpm) * float64(req.NowExposureCount) / 1000)
- priceAdjustmentRetention = int(mediaRevenue - float64(tmpMediaRevenue))
- mediaRevenue = float64(tmpMediaRevenue)
- }
-
- // 4、计算各代理收益
- var extraRevenue float64
- 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 := utils.RoundToTwoDecimalPlaces(agentRevenue * float64(v.AgentRevenueRate) / 100)
- tmpExtraRevenue := commissionRetention * float64(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: int(tmpAgentRevenue),
- ExtraRevenue: int(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{
- MediumId: originalWxAdData.MediumId,
- Uuid: originalWxAdData.Uuid,
- AppId: originalWxAdData.AppId,
- PlatformAppId: originalWxAdData.PlatformAppId,
- OriginalDataId: 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: int(platformRetention),
- CommissionRetention: int(commissionRetention),
- PriceAdjustmentRetention: priceAdjustmentRetention,
- MediaRevenue: int(mediaRevenue),
- AgentRevenue: int(agentRevenue),
- ExtraRevenue: int(extraRevenue),
- AgreementSharing: int(agreementSharing),
- AgreementSharingTotal: int(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"),
- Platform: originalWxAdData.Platform,
- }
- 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,
- PlatformAppId: generateWxAdData.PlatformAppId,
- 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"),
- })
- }
- if len(generateWxAdDataWithAgentFlows) > 0 {
- 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 := 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).Div(rateValue)) // 协议总分成(倒推)
-
- fmt.Println(mediaRevenue, agentRevenue, agreementSharingRate, rateValue.Sub(agreementSharingRate), agreementSharingTotal)
- // 4、倒退出当前ecpm值
- ecpm = agreementSharingTotal.Div(exposureCount.Div(thousandRateValue)).Round(2).String()
- return
- }
-
- func SettlementWxAdData(req md.SettlementWxAdData) (err error) {
- // 1、查找生成数据记录
- generateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
- generateWxAdData, err := generateWxAdDataDb.GetGenerateWxAdData(req.GenerateDataId)
- if err != nil {
- return
- }
- if generateWxAdData == nil {
- err = errors.New("未查询到生成数据记录")
- return
- }
-
- originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db)
- originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(generateWxAdData.OriginalDataId)
- if err != nil {
- return
- }
- if originalWxAdData == nil {
- err = errors.New("未查询到原始数据记录")
- return
- }
-
- // 2、查询 生成数据-代理明细 记录
- generateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db)
- generateWxAdDataWithAgentFlow, err := generateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(generateWxAdData.Id)
- if err != nil {
- return
- }
-
- // 3、查询 媒体、代理的结算方式
- mediumListDb := implement.NewMediumListDb(db.Db)
- medium, err := mediumListDb.GetMediumList(originalWxAdData.MediumId)
- if err != nil {
- return
- }
- if medium == nil {
- return errors.New("媒体:" + utils.IntToStr(originalWxAdData.MediumId) + "未查询到对应记录")
- }
- var agentMap = map[int]model.AgentList{}
- agentListDb := implement.NewAgentListDb(db.Db)
- for _, v := range *generateWxAdDataWithAgentFlow {
- agent, err1 := agentListDb.GetAgentList(v.AgentId)
- if err1 != nil {
- return err1
- }
- if agent == nil {
- return errors.New("代理:" + utils.IntToStr(v.AgentId) + "未查询到对应记录")
- }
- agentMap[v.AgentId] = *agent
- }
-
- // 4、生成数据
- now := time.Now()
- session := db.Db.NewSession()
- defer session.Close()
- session.Begin()
-
- // 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,
- AppId: originalWxAdData.AppId,
- PlatformAppId: originalWxAdData.PlatformAppId,
- BusinessKind: 1,
- Kind: medium.SettlementType,
- BasicIncome: generateWxAdData.MediaRevenue,
- OtherIncome: 0,
- OriginalIncome: originalWxAdData.PublisherIncome,
- PlatformRetention: generateWxAdData.PlatformRetention,
- CommissionRetention: generateWxAdData.CommissionRetention,
- PriceAdjustmentRetention: generateWxAdData.PriceAdjustmentRetention,
- State: mediumSettlementState,
- PayState: mediumSettlementPayState,
- StartDate: now.Format("2006-01-02"),
- EndDate: "",
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- SettleFile: "",
- }
- _, err = mediumSettlementDb.MediumSettlementInsertBySession(session, mediumSettlement)
- if err != nil {
- _ = session.Rollback()
- return
- }
- } else {
- // 更新数据
- if medium.SettlementType != mediumSettlement.Kind {
- // TODO::前后结算类型不一致,之前结算单状态自动变成"核算中"
- mediumSettlement.State = enum.MediumSettlementStateForAccountingInProgress
- // mediumSettlement.PayState = mediumSettlementPayState
- _, err = mediumSettlementDb.UpdateMediumSettlementBySession(session, mediumSettlement, "state")
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- // 新增一条数据
- mediumSettlement = &model.MediumSettlement{
- Uuid: originalWxAdData.Uuid,
- MediumId: originalWxAdData.MediumId,
- AppId: originalWxAdData.AppId,
- PlatformAppId: originalWxAdData.PlatformAppId,
- BusinessKind: 1,
- Kind: medium.SettlementType,
- BasicIncome: generateWxAdData.MediaRevenue,
- OtherIncome: 0,
- OriginalIncome: originalWxAdData.PublisherIncome,
- PlatformRetention: generateWxAdData.PlatformRetention,
- CommissionRetention: generateWxAdData.CommissionRetention,
- PriceAdjustmentRetention: generateWxAdData.PriceAdjustmentRetention,
- State: mediumSettlementState,
- PayState: mediumSettlementPayState,
- StartDate: now.Format("2006-01-02"),
- EndDate: "",
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- SettleFile: "",
- }
- _, err = mediumSettlementDb.MediumSettlementInsertBySession(session, mediumSettlement)
- if err != nil {
- _ = session.Rollback()
- return
- }
- } else {
- basicIncomeBefore = mediumSettlement.BasicIncome
- mediumSettlement.BasicIncome += generateWxAdData.MediaRevenue
- mediumSettlement.OriginalIncome += originalWxAdData.PublisherIncome
- mediumSettlement.PlatformRetention += generateWxAdData.PlatformRetention
- mediumSettlement.CommissionRetention += generateWxAdData.CommissionRetention
- mediumSettlement.PriceAdjustmentRetention += generateWxAdData.PriceAdjustmentRetention
- _, err = mediumSettlementDb.UpdateMediumSettlementBySession(session, mediumSettlement, "basic_income", "original_income", "platform_retention", "commission_retention", "price_adjustment_retention")
- if err != nil {
- _ = session.Rollback()
- return
- }
- }
- }
- if medium.SettlementType == enum.MediumSettlementTypeForPaymentInAdvance {
- err = DealMediumAmount(session, md.DealMediumAmount{
- Mid: utils.IntToStr(originalWxAdData.Uuid),
- Type: md.FinMediumFlowDirectionExpenditure,
- Kind: md.SettlementSubKindForMediumFlow,
- OrdId: utils.IntToStr(mediumSettlement.Id),
- MediumId: originalWxAdData.MediumId,
- Amount: float64(generateWxAdData.MediaRevenue) / 100,
- Memo: md.SettlementSubTitleForMediumFlow,
- })
- if err != nil {
- _ = session.Rollback()
- return
- }
- }
-
- // 4.2 新增 medium_settlement_with_flow 数据
- mediumSettlementWithFlowDb := implement.NewMediumSettlementWithFlowDb(db.Db)
- _, err = mediumSettlementWithFlowDb.MediumSettlementWithFlowInsertBySession(session, &model.MediumSettlementWithFlow{
- SettlementId: mediumSettlement.Id,
- GenerateDataId: generateWxAdData.Id,
- Amount: generateWxAdData.MediaRevenue,
- BasicIncomeBefore: basicIncomeBefore,
- BasicIncomeAfter: mediumSettlement.BasicIncome,
- OtherIncomeBefore: 0,
- OtherIncomeAfter: 0,
- Kind: enum.MediumSettlementWithFlowKindForBasicIncome,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- _ = session.Rollback()
- return
- }
- // 4.3 新增 agent_settlement、agent_settlement_with_flow 数据
- agentSettlementDb := implement.NewAgentSettlementDb(db.Db)
- agentSettlementWithFlowDb := implement.NewAgentSettlementWithFlowDb(db.Db)
- for _, v := range *generateWxAdDataWithAgentFlow {
- agentSettlement, err1 := agentSettlementDb.GetAgentSettlementForAvailable(v.AgentId)
- if err1 != nil {
- _ = session.Rollback()
- return err1
- }
-
- 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,
- MediumId: originalWxAdData.MediumId,
- AppId: v.AppId,
- PlatformAppId: v.PlatformAppId,
- BusinessKind: 1,
- Kind: agentMap[v.AgentId].SettlementType,
- BasicIncome: v.AgentRevenue,
- OtherIncome: v.ExtraRevenue,
- State: agentSettlementState,
- PayState: agentSettlementPayState,
- StartDate: now.Format("2006-01-02"),
- EndDate: "",
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- }
- _, err = agentSettlementDb.AgentSettlementInsertBySession(session, agentSettlement)
- if err != nil {
- _ = session.Rollback()
- return
- }
- } else {
- // 更新数据
- if agentMap[v.AgentId].SettlementType != agentSettlement.Kind {
- // TODO::前后结算类型不一致,之前结算单状态自动变成"核算中"
- agentSettlement.State = enum.AgentSettlementStateForAccountingInProgress
- _, err = agentSettlementDb.UpdateAgentSettlementBySession(session, agentSettlement, "state")
- if err != nil {
- _ = session.Rollback()
- return
- }
-
- // 新增一条数据
- agentSettlement = &model.AgentSettlement{
- Uuid: v.Uuid,
- AgentId: v.AgentId,
- MediumId: originalWxAdData.MediumId,
- AppId: v.AppId,
- PlatformAppId: v.PlatformAppId,
- BusinessKind: 1,
- Kind: agentMap[v.AgentId].SettlementType,
- BasicIncome: v.AgentRevenue,
- OtherIncome: v.ExtraRevenue,
- State: agentSettlementState,
- PayState: agentSettlementPayState,
- StartDate: now.Format("2006-01-02"),
- EndDate: "",
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- }
- _, err = agentSettlementDb.AgentSettlementInsertBySession(session, agentSettlement)
- if err != nil {
- _ = session.Rollback()
- return
- }
- } else {
- agentBasicIncomeBefore = agentSettlement.BasicIncome
- agentOtherIncomeBefore = agentSettlement.OtherIncome
- agentSettlement.BasicIncome += v.AgentRevenue
- agentSettlement.OtherIncome += v.ExtraRevenue
-
- _, err = agentSettlementDb.UpdateAgentSettlementBySession(session, agentSettlement, "basic_income", "other_income")
- if err != nil {
- _ = session.Rollback()
- return
- }
- }
- }
-
- if agentMap[v.AgentId].SettlementType == enum.AgentSettlementTypeForPaymentInAdvance {
- err = DealAgentAmount(session, md.DealAgentAmount{
- Mid: utils.IntToStr(originalWxAdData.Uuid),
- Type: md.FinAgentFlowDirectionExpenditure,
- Kind: md.SettlementSubKindForAgentFlow,
- OrdId: utils.IntToStr(v.Id),
- Amount: float64(v.AgentRevenue)/100 + float64(v.ExtraRevenue)/100,
- Memo: md.SettlementSubTitleForMediumFlow,
- })
- if err != nil {
- _ = session.Rollback()
- return
- }
- }
- _, err = agentSettlementWithFlowDb.AgentSettlementWithFlowInsertBySession(session, &model.AgentSettlementWithFlow{
- SettlementId: mediumSettlement.Id,
- GenerateDataId: generateWxAdData.Id,
- Amount: v.AgentRevenue,
- BasicIncomeBefore: agentBasicIncomeBefore,
- BasicIncomeAfter: agentSettlement.BasicIncome,
- OtherIncomeBefore: agentOtherIncomeBefore,
- OtherIncomeAfter: agentSettlement.OtherIncome,
- Kind: enum.AgentSettlementWithFlowKindForBasicIncome,
- CreateAt: now.Format("2006-01-02 15:04:05"),
- UpdateAt: now.Format("2006-01-02 15:04:05"),
- })
- if err != nil {
- _ = session.Rollback()
- return
- }
- }
-
- // 4、修改 generate_wx_ad_data 记录中的 is_generate_report (是否已应用(0:未 1:已) )
- generateWxAdData.IsGenerateReport = 1
- _, err = generateWxAdDataDb.UpdateGenerateWxAdDataBySession(session, generateWxAdData, "is_generate_report")
- if err != nil {
- _ = session.Rollback()
- return
- }
- return session.Commit()
- }
-
- func CalcApplicationData(req md.CalcApplicationDataReq) (err error, resp md.CalcApplicationDataResp) {
- // 1、查找原始数据记录
- originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db)
- originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(req.OriginalDataId)
- 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.Id)
- if err != nil {
- return
- }
-
- // 3、计算媒体、代理收益、平台留存、佣金留存、协议分成、协议总分成
- publisherIncome := float64(originalWxAdData.PublisherIncome)
- mediaRevenue := publisherIncome * float64(mediumDivisionStrategy.MediaRevenueRate) / 100 // 媒体收益
- agentRevenue := publisherIncome * float64(mediumDivisionStrategy.AgentRevenueRate) / 100 // 代理收益
- platformRetention := publisherIncome * float64(mediumDivisionStrategy.PlatformRetentionRate) / 100 // 平台留存
- commissionRetention := publisherIncome * float64(mediumDivisionStrategy.CommissionRetentionRate) / 100 // 佣金留存
- agreementSharingTotal := (mediaRevenue + agentRevenue) / ((100 - float64(mediumDivisionStrategy.AgreementSharingRate)) / 100) // 协议总分成(倒推)
- agreementSharing := agreementSharingTotal - (mediaRevenue + agentRevenue) // 协议分成
-
- // 3、判断是否有调价留存
- ecpmReq := md.ClacEcpmReq{
- OriginalExposureCount: originalWxAdData.ExposureCount,
- OriginalEcpm: originalWxAdData.Ecpm,
- GenerateDataId: originalWxAdData.Id,
- }
- _, ecpm := ClacEcpm(ecpmReq)
- fmt.Println(utils.Float64ToStr(utils.StrToFloat64(ecpm) / 100))
- if req.NowEcpm != utils.Float64ToStr(utils.StrToFloat64(ecpm)/100) || req.NowExposureCount != req.OriginalExposureCount {
- tmpMediaRevenue := int(utils.StrToFloat64(req.NowEcpm) * float64(req.NowExposureCount) / 1000)
- mediaRevenue = float64(tmpMediaRevenue)
- }
-
- // 4、计算各代理收益
- var extraRevenue float64
- for _, v := range *mediumDivisionStrategyWithAgentFlows {
- tmpExtraRevenue := commissionRetention * float64(v.ExtraRevenueRate) / 100
- extraRevenue += tmpExtraRevenue
- }
-
- resp = md.CalcApplicationDataResp{
- PlatformRetention: platformRetention,
- CommissionRetention: commissionRetention,
- MediaRevenue: mediaRevenue,
- AgentRevenue: agentRevenue,
- ExtraRevenue: extraRevenue,
- AgreementSharing: agreementSharing,
- }
- return
- }
|