|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- 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"
- "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.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,
- 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: 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
- }
-
- 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,
- BusinessKind: 1,
- Kind: medium.SettlementType,
- BasicIncome: generateWxAdData.MediaRevenue,
- OtherIncome: 0,
- 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"),
- }
- _, err = mediumSettlementDb.MediumSettlementInsertBySession(session, mediumSettlement)
- if err != nil {
- _ = session.Rollback()
- return
- }
- } else {
- //更新数据
- basicIncomeBefore = mediumSettlement.BasicIncome
- mediumSettlement.BasicIncome += generateWxAdData.MediaRevenue
- mediumSettlement.State = mediumSettlementState
- mediumSettlement.PayState = mediumSettlementPayState
- _, err = mediumSettlementDb.UpdateMediumSettlementBySession(session, mediumSettlement, "basic_income", "pay_state", "state")
- 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,
- 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
- agentSettlement.State += agentSettlementState
- agentSettlement.PayState += agentSettlementPayState
-
- _, err = agentSettlementDb.UpdateAgentSettlementBySession(session, agentSettlement, "basic_income", "other_income", "pay_state", "state")
- 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()
- }
|