广告平台(站长使用)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_wx_data.go 17 KiB

2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. package svc
  2. import (
  3. "applet/app/enum"
  4. "applet/app/md"
  5. "applet/app/utils"
  6. db "code.fnuoos.com/zhimeng/model.git/src"
  7. "code.fnuoos.com/zhimeng/model.git/src/super/implement"
  8. "code.fnuoos.com/zhimeng/model.git/src/super/model"
  9. "errors"
  10. "time"
  11. )
  12. func GenerateWxAdData(req md.GenerateWxAdData) (err error, generateWxAdData model.GenerateWxAdData) {
  13. //1、查找原始数据记录
  14. originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db)
  15. originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(req.OriginalDataId)
  16. if err != nil {
  17. return
  18. }
  19. if originalWxAdData == nil {
  20. err = errors.New("未查询到原始数据记录")
  21. return
  22. }
  23. //2、查询对应媒体、代理的分成策略
  24. mediumDivisionStrategyDb := implement.NewMediumDivisionStrategyDb(db.Db)
  25. mediumDivisionStrategy, err := mediumDivisionStrategyDb.GetOriginalWxAdDataByMediumId(originalWxAdData.MediumId)
  26. if err != nil {
  27. return
  28. }
  29. if mediumDivisionStrategy == nil {
  30. err = errors.New("未查询到对应代理的分成策略")
  31. return
  32. }
  33. mediumDivisionStrategyWithAgentFlowDb := implement.NewMediumDivisionStrategyWithAgentFlowDb(db.Db)
  34. mediumDivisionStrategyWithAgentFlows, err := mediumDivisionStrategyWithAgentFlowDb.FindMediumDivisionStrategyWithAgentFlowByStrategyId(mediumDivisionStrategy.MediumId)
  35. if err != nil {
  36. return
  37. }
  38. //3、计算媒体、代理收益、平台留存、佣金留存、协议分成、协议总分成
  39. publisherIncome := originalWxAdData.PublisherIncome
  40. mediaRevenue := publisherIncome * mediumDivisionStrategy.MediaRevenueRate / 100 //媒体收益
  41. agentRevenue := publisherIncome * mediumDivisionStrategy.AgentRevenueRate / 100 //代理收益
  42. platformRetention := publisherIncome * mediumDivisionStrategy.PlatformRetentionRate / 100 //平台留存
  43. commissionRetention := publisherIncome * mediumDivisionStrategy.CommissionRetentionRate / 100 //佣金留存
  44. agreementSharingTotal := (mediaRevenue + agentRevenue) / (100 - mediumDivisionStrategy.AgreementSharingRate) //协议总分成(倒推)
  45. agreementSharing := agreementSharingTotal - (mediaRevenue + agentRevenue) //协议分成
  46. //3、判断是否有调价留存
  47. var priceAdjustmentRetention int
  48. if req.NowEcpm != req.OriginalEcpm || req.NowExposureCount != req.OriginalExposureCount {
  49. tmpMediaRevenue := int(utils.StrToFloat64(req.NowEcpm) * float64(req.NowExposureCount) / 1000)
  50. priceAdjustmentRetention = tmpMediaRevenue - mediaRevenue
  51. mediaRevenue = tmpMediaRevenue
  52. }
  53. //4、计算各代理收益
  54. var extraRevenue int
  55. var agentRevenueFlows []struct {
  56. AgentId int `json:"agent_id"`
  57. AgentRevenueRate int `json:"agent_revenue_rate"`
  58. ExtraRevenueRate int `json:"extra_revenue_rate"`
  59. AgentRevenue int `json:"agent_revenue"`
  60. ExtraRevenue int `json:"extra_revenue"`
  61. }
  62. for _, v := range *mediumDivisionStrategyWithAgentFlows {
  63. tmpAgentRevenue := agentRevenue * v.AgentRevenueRate / 100
  64. tmpExtraRevenue := commissionRetention * v.ExtraRevenueRate / 100
  65. extraRevenue += tmpExtraRevenue
  66. agentRevenueFlows = append(agentRevenueFlows, struct {
  67. AgentId int `json:"agent_id"`
  68. AgentRevenueRate int `json:"agent_revenue_rate"`
  69. ExtraRevenueRate int `json:"extra_revenue_rate"`
  70. AgentRevenue int `json:"agent_revenue"`
  71. ExtraRevenue int `json:"extra_revenue"`
  72. }{
  73. AgentId: v.AgentId,
  74. AgentRevenueRate: v.AgentRevenueRate,
  75. ExtraRevenueRate: v.ExtraRevenueRate,
  76. AgentRevenue: tmpAgentRevenue,
  77. ExtraRevenue: tmpExtraRevenue,
  78. })
  79. }
  80. //5、插入 generate_wx_ad_data 、generate_wx_ad_data_with_agent_flow 数据
  81. session := db.Db.NewSession()
  82. defer session.Close()
  83. session.Begin()
  84. now := time.Now()
  85. generateWxAdData = model.GenerateWxAdData{
  86. MediumId: originalWxAdData.MediumId,
  87. Uuid: originalWxAdData.Uuid,
  88. Platform: originalWxAdData.Platform,
  89. AppId: originalWxAdData.AppId,
  90. OriginalDataId: originalWxAdData.Id,
  91. SlotId: originalWxAdData.SlotId,
  92. AdSlot: originalWxAdData.AdSlot,
  93. Date: originalWxAdData.Date,
  94. ReqSuccCount: originalWxAdData.ReqSuccCount,
  95. ExposureCount: req.NowExposureCount, //现-曝光量
  96. ExposureRate: originalWxAdData.ExposureRate,
  97. ClickCount: originalWxAdData.ClickCount,
  98. ClickRate: originalWxAdData.ClickRate,
  99. Ecpm: req.NowEcpm, //现-ecpm
  100. PlatformRetention: platformRetention,
  101. CommissionRetention: commissionRetention,
  102. PriceAdjustmentRetention: priceAdjustmentRetention,
  103. MediaRevenue: mediaRevenue,
  104. AgentRevenue: agentRevenue,
  105. ExtraRevenue: extraRevenue,
  106. AgreementSharing: agreementSharing,
  107. AgreementSharingTotal: agreementSharingTotal,
  108. PlatformRetentionRate: mediumDivisionStrategy.PlatformRetentionRate,
  109. CommissionRetentionRate: mediumDivisionStrategy.CommissionRetentionRate,
  110. MediaRevenueRate: mediumDivisionStrategy.MediaRevenueRate,
  111. AgentRevenueRate: mediumDivisionStrategy.AgentRevenueRate,
  112. ExtraRevenueRate: mediumDivisionStrategy.ExtraRevenueRate,
  113. AgreementSharingRate: mediumDivisionStrategy.AgreementSharingRate,
  114. IsGenerateReport: 0,
  115. CreateAt: now.Format("2006-01-02 15:04:05"),
  116. UpdateAt: now.Format("2006-01-02 15:04:05"),
  117. }
  118. generateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  119. _, err = generateWxAdDataDb.GenerateWxAdDataInsertBySession(session, &generateWxAdData)
  120. if err != nil {
  121. _ = session.Rollback()
  122. return
  123. }
  124. var generateWxAdDataWithAgentFlows []*model.GenerateWxAdDataWithAgentFlow
  125. for _, v := range agentRevenueFlows {
  126. generateWxAdDataWithAgentFlows = append(generateWxAdDataWithAgentFlows, &model.GenerateWxAdDataWithAgentFlow{
  127. Uuid: generateWxAdData.Uuid,
  128. AppId: generateWxAdData.AppId,
  129. AgentId: v.AgentId,
  130. GenerateDataId: generateWxAdData.Id,
  131. SlotId: generateWxAdData.SlotId,
  132. AdSlot: generateWxAdData.AdSlot,
  133. Date: generateWxAdData.Date,
  134. AgentRevenue: v.AgentRevenue,
  135. AgentRevenueRate: v.AgentRevenueRate,
  136. ExtraRevenue: v.ExtraRevenue,
  137. ExtraRevenueRate: v.ExtraRevenueRate,
  138. CreateAt: now.Format("2006-01-02 15:04:05"),
  139. UpdateAt: now.Format("2006-01-02 15:04:05"),
  140. })
  141. }
  142. generateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db)
  143. _, err = generateWxAdDataWithAgentFlowDb.BatchAddGenerateWxAdDataWithAgentFlow(session, generateWxAdDataWithAgentFlows)
  144. if err != nil {
  145. _ = session.Rollback()
  146. return
  147. }
  148. //6、修改 original_wx_ad_data 记录中的 is_apply(是否已应用(0:未 1:已) )
  149. originalWxAdData.IsApply = 1
  150. _, err = originalWxAdDataDb.UpdateOriginalWxAdDataBySession(session, originalWxAdData, "is_apply")
  151. if err != nil {
  152. _ = session.Rollback()
  153. return
  154. }
  155. return session.Commit(), generateWxAdData
  156. }
  157. func ClacEcpm(req md.ClacEcpmReq) (err error, ecpm string) {
  158. //1、查找原始数据记录
  159. originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db)
  160. originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(req.GenerateDataId)
  161. if err != nil {
  162. return
  163. }
  164. if originalWxAdData == nil {
  165. err = errors.New("未查询到原始数据记录")
  166. return
  167. }
  168. //2、查询对应媒体分成策略
  169. mediumDivisionStrategyDb := implement.NewMediumDivisionStrategyDb(db.Db)
  170. mediumDivisionStrategy, err := mediumDivisionStrategyDb.GetOriginalWxAdDataByMediumId(originalWxAdData.MediumId)
  171. if err != nil {
  172. return
  173. }
  174. if mediumDivisionStrategy == nil {
  175. err = errors.New("未查询到对应代理的分成策略")
  176. return
  177. }
  178. //3、计算媒体、代理收益、协议总分成
  179. publisherIncome := originalWxAdData.PublisherIncome
  180. mediaRevenue := publisherIncome * mediumDivisionStrategy.MediaRevenueRate / 100 //媒体收益
  181. agentRevenue := publisherIncome * mediumDivisionStrategy.AgentRevenueRate / 100 //代理收益
  182. agreementSharingTotal := (mediaRevenue + agentRevenue) / (100 - mediumDivisionStrategy.AgreementSharingRate) //协议总分成(倒推)
  183. //4、倒退出当前ecpm值
  184. ecpm = utils.Float64ToStrPrec4(float64(agreementSharingTotal) / (float64(originalWxAdData.ExposureCount) / 1000))
  185. return
  186. }
  187. func SettlementWxAdData(req md.SettlementWxAdData) (err error) {
  188. //1、查找生成数据记录
  189. generateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  190. generateWxAdData, err := generateWxAdDataDb.GetGenerateWxAdData(req.GenerateDataId)
  191. if err != nil {
  192. return
  193. }
  194. if generateWxAdData == nil {
  195. err = errors.New("未查询到生成数据记录")
  196. return
  197. }
  198. originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db)
  199. originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(generateWxAdData.OriginalDataId)
  200. if err != nil {
  201. return
  202. }
  203. if originalWxAdData == nil {
  204. err = errors.New("未查询到原始数据记录")
  205. return
  206. }
  207. //2、查询 生成数据-代理明细 记录
  208. generateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db)
  209. generateWxAdDataWithAgentFlow, err := generateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(generateWxAdData.Id)
  210. if err != nil {
  211. return
  212. }
  213. //3、查询 媒体、代理的结算方式
  214. mediumListDb := implement.NewMediumListDb(db.Db)
  215. medium, err := mediumListDb.GetMediumList(originalWxAdData.MediumId)
  216. if err != nil {
  217. return
  218. }
  219. if medium == nil {
  220. return errors.New("媒体:" + utils.IntToStr(originalWxAdData.MediumId) + "未查询到对应记录")
  221. }
  222. var agentMap = map[int]model.AgentList{}
  223. agentListDb := implement.NewAgentListDb(db.Db)
  224. for _, v := range *generateWxAdDataWithAgentFlow {
  225. agent, err1 := agentListDb.GetAgentList(v.AgentId)
  226. if err1 != nil {
  227. return err1
  228. }
  229. if agent == nil {
  230. return errors.New("代理:" + utils.IntToStr(v.AgentId) + "未查询到对应记录")
  231. }
  232. agentMap[v.AgentId] = *agent
  233. }
  234. //4、生成数据
  235. now := time.Now()
  236. session := db.Db.NewSession()
  237. defer session.Close()
  238. session.Begin()
  239. //4.1 新增/更新 medium_settlement 数据
  240. mediumSettlementDb := implement.NewMediumSettlementDb(db.Db)
  241. mediumSettlement, err := mediumSettlementDb.GetMediumSettlementForAvailable(originalWxAdData.MediumId)
  242. if err != nil {
  243. return
  244. }
  245. var basicIncomeBefore, mediumSettlementState, mediumSettlementPayState int
  246. //判断是否为预付结算类型
  247. if medium.SettlementType == enum.MediumSettlementTypeForPaymentInAdvance {
  248. mediumSettlementState = enum.MediumSettlementStateForCompleteSign
  249. mediumSettlementPayState = enum.MediumSettlementPayStateForPaymentAlready
  250. }
  251. if mediumSettlement == nil {
  252. //新增一条数据
  253. mediumSettlement = &model.MediumSettlement{
  254. Uuid: originalWxAdData.Uuid,
  255. MediumId: originalWxAdData.MediumId,
  256. AppId: originalWxAdData.AppId,
  257. BusinessKind: 1,
  258. Kind: medium.SettlementType,
  259. BasicIncome: generateWxAdData.MediaRevenue,
  260. OtherIncome: 0,
  261. State: mediumSettlementState,
  262. PayState: mediumSettlementPayState,
  263. StartDate: now.Format("2006-01-02"),
  264. EndDate: "",
  265. CreateAt: now.Format("2006-01-02 15:04:05"),
  266. UpdateAt: now.Format("2006-01-02 15:04:05"),
  267. }
  268. _, err = mediumSettlementDb.MediumSettlementInsertBySession(session, mediumSettlement)
  269. if err != nil {
  270. _ = session.Rollback()
  271. return
  272. }
  273. } else {
  274. //更新数据
  275. basicIncomeBefore = mediumSettlement.BasicIncome
  276. mediumSettlement.BasicIncome += generateWxAdData.MediaRevenue
  277. mediumSettlement.State = mediumSettlementState
  278. mediumSettlement.PayState = mediumSettlementPayState
  279. _, err = mediumSettlementDb.UpdateMediumSettlementBySession(session, mediumSettlement, "basic_income", "pay_state", "state")
  280. if err != nil {
  281. _ = session.Rollback()
  282. return
  283. }
  284. }
  285. if medium.SettlementType == enum.MediumSettlementTypeForPaymentInAdvance {
  286. err = DealMediumAmount(session, md.DealMediumAmount{
  287. Mid: utils.IntToStr(originalWxAdData.Uuid),
  288. Type: md.FinMediumFlowDirectionExpenditure,
  289. Kind: md.SettlementSubKindForMediumFlow,
  290. OrdId: utils.IntToStr(mediumSettlement.Id),
  291. MediumId: originalWxAdData.MediumId,
  292. Amount: float64(generateWxAdData.MediaRevenue) / 100,
  293. Memo: md.SettlementSubTitleForMediumFlow,
  294. })
  295. if err != nil {
  296. _ = session.Rollback()
  297. return
  298. }
  299. }
  300. //4.2 新增 medium_settlement_with_flow 数据
  301. mediumSettlementWithFlowDb := implement.NewMediumSettlementWithFlowDb(db.Db)
  302. _, err = mediumSettlementWithFlowDb.MediumSettlementWithFlowInsertBySession(session, &model.MediumSettlementWithFlow{
  303. SettlementId: mediumSettlement.Id,
  304. GenerateDataId: generateWxAdData.Id,
  305. Amount: generateWxAdData.MediaRevenue,
  306. BasicIncomeBefore: basicIncomeBefore,
  307. BasicIncomeAfter: mediumSettlement.BasicIncome,
  308. OtherIncomeBefore: 0,
  309. OtherIncomeAfter: 0,
  310. Kind: enum.MediumSettlementWithFlowKindForBasicIncome,
  311. CreateAt: now.Format("2006-01-02 15:04:05"),
  312. UpdateAt: now.Format("2006-01-02 15:04:05"),
  313. })
  314. if err != nil {
  315. _ = session.Rollback()
  316. return
  317. }
  318. //4.3 新增 agent_settlement、agent_settlement_with_flow 数据
  319. agentSettlementDb := implement.NewAgentSettlementDb(db.Db)
  320. agentSettlementWithFlowDb := implement.NewAgentSettlementWithFlowDb(db.Db)
  321. for _, v := range *generateWxAdDataWithAgentFlow {
  322. agentSettlement, err1 := agentSettlementDb.GetAgentSettlementForAvailable(v.AgentId)
  323. if err1 != nil {
  324. _ = session.Rollback()
  325. return err1
  326. }
  327. var agentBasicIncomeBefore, agentOtherIncomeBefore, agentSettlementState, agentSettlementPayState int
  328. //判断是否为预付结算类型
  329. if agentMap[v.AgentId].SettlementType == enum.AgentSettlementTypeForPaymentInAdvance {
  330. agentSettlementState = enum.AgentSettlementStateForCompleteSign
  331. agentSettlementPayState = enum.AgentSettlementPayStateForPaymentAlready
  332. }
  333. if agentSettlement == nil {
  334. //新增一条数据
  335. agentSettlement = &model.AgentSettlement{
  336. Uuid: v.Uuid,
  337. AgentId: v.AgentId,
  338. MediumId: originalWxAdData.MediumId,
  339. AppId: v.AppId,
  340. BusinessKind: 1,
  341. Kind: agentMap[v.AgentId].SettlementType,
  342. BasicIncome: v.AgentRevenue,
  343. OtherIncome: v.ExtraRevenue,
  344. State: agentSettlementState,
  345. PayState: agentSettlementPayState,
  346. StartDate: now.Format("2006-01-02"),
  347. EndDate: "",
  348. CreateAt: now.Format("2006-01-02 15:04:05"),
  349. UpdateAt: now.Format("2006-01-02 15:04:05"),
  350. }
  351. _, err = agentSettlementDb.AgentSettlementInsertBySession(session, agentSettlement)
  352. if err != nil {
  353. _ = session.Rollback()
  354. return
  355. }
  356. } else {
  357. //更新数据
  358. agentBasicIncomeBefore = agentSettlement.BasicIncome
  359. agentOtherIncomeBefore = agentSettlement.OtherIncome
  360. agentSettlement.BasicIncome += v.AgentRevenue
  361. agentSettlement.OtherIncome += v.ExtraRevenue
  362. agentSettlement.State += agentSettlementState
  363. agentSettlement.PayState += agentSettlementPayState
  364. _, err = agentSettlementDb.UpdateAgentSettlementBySession(session, agentSettlement, "basic_income", "other_income", "pay_state", "state")
  365. if err != nil {
  366. _ = session.Rollback()
  367. return
  368. }
  369. }
  370. if agentMap[v.AgentId].SettlementType == enum.AgentSettlementTypeForPaymentInAdvance {
  371. err = DealAgentAmount(session, md.DealAgentAmount{
  372. Mid: utils.IntToStr(originalWxAdData.Uuid),
  373. Type: md.FinAgentFlowDirectionExpenditure,
  374. Kind: md.SettlementSubKindForAgentFlow,
  375. OrdId: utils.IntToStr(v.Id),
  376. Amount: float64(v.AgentRevenue)/100 + float64(v.ExtraRevenue)/100,
  377. Memo: md.SettlementSubTitleForMediumFlow,
  378. })
  379. if err != nil {
  380. _ = session.Rollback()
  381. return
  382. }
  383. }
  384. _, err = agentSettlementWithFlowDb.AgentSettlementWithFlowInsertBySession(session, &model.AgentSettlementWithFlow{
  385. SettlementId: mediumSettlement.Id,
  386. GenerateDataId: generateWxAdData.Id,
  387. Amount: v.AgentRevenue,
  388. BasicIncomeBefore: agentBasicIncomeBefore,
  389. BasicIncomeAfter: agentSettlement.BasicIncome,
  390. OtherIncomeBefore: agentOtherIncomeBefore,
  391. OtherIncomeAfter: agentSettlement.OtherIncome,
  392. Kind: enum.AgentSettlementWithFlowKindForBasicIncome,
  393. CreateAt: now.Format("2006-01-02 15:04:05"),
  394. UpdateAt: now.Format("2006-01-02 15:04:05"),
  395. })
  396. if err != nil {
  397. _ = session.Rollback()
  398. return
  399. }
  400. }
  401. //4、修改 generate_wx_ad_data 记录中的 is_generate_report (是否已应用(0:未 1:已) )
  402. generateWxAdData.IsGenerateReport = 1
  403. _, err = generateWxAdDataDb.UpdateGenerateWxAdDataBySession(session, generateWxAdData, "is_generate_report")
  404. if err != nil {
  405. _ = session.Rollback()
  406. return
  407. }
  408. return session.Commit()
  409. }