广告平台(站长使用)
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.
 
 
 
 
 

527 lines
21 KiB

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