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

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
2 months ago
3 months ago
3 months ago
3 months ago
2 months ago
3 months ago
3 months ago
2 months ago
3 months ago
2 months ago
3 months ago
2 months ago
3 months ago
2 months ago
3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. AppId: originalWxAdData.AppId,
  89. OriginalDataId: originalWxAdData.Id,
  90. SlotId: originalWxAdData.SlotId,
  91. AdSlot: originalWxAdData.AdSlot,
  92. Date: originalWxAdData.Date,
  93. ReqSuccCount: originalWxAdData.ReqSuccCount,
  94. ExposureCount: req.NowExposureCount, //现-曝光量
  95. ExposureRate: originalWxAdData.ExposureRate,
  96. ClickCount: originalWxAdData.ClickCount,
  97. ClickRate: originalWxAdData.ClickRate,
  98. Ecpm: req.NowEcpm, //现-ecpm
  99. PlatformRetention: platformRetention,
  100. CommissionRetention: commissionRetention,
  101. PriceAdjustmentRetention: priceAdjustmentRetention,
  102. MediaRevenue: mediaRevenue,
  103. AgentRevenue: agentRevenue,
  104. ExtraRevenue: extraRevenue,
  105. AgreementSharing: agreementSharing,
  106. AgreementSharingTotal: agreementSharingTotal,
  107. PlatformRetentionRate: mediumDivisionStrategy.PlatformRetentionRate,
  108. CommissionRetentionRate: mediumDivisionStrategy.CommissionRetentionRate,
  109. MediaRevenueRate: mediumDivisionStrategy.MediaRevenueRate,
  110. AgentRevenueRate: mediumDivisionStrategy.AgentRevenueRate,
  111. ExtraRevenueRate: mediumDivisionStrategy.ExtraRevenueRate,
  112. AgreementSharingRate: mediumDivisionStrategy.AgreementSharingRate,
  113. IsGenerateReport: 0,
  114. CreateAt: now.Format("2006-01-02 15:04:05"),
  115. UpdateAt: now.Format("2006-01-02 15:04:05"),
  116. Platform: originalWxAdData.Platform,
  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. OriginalIncome: originalWxAdData.PublisherIncome,
  262. PlatformRetention: generateWxAdData.PlatformRetention,
  263. CommissionRetention: generateWxAdData.CommissionRetention,
  264. PriceAdjustmentRetention: generateWxAdData.PriceAdjustmentRetention,
  265. State: mediumSettlementState,
  266. PayState: mediumSettlementPayState,
  267. StartDate: now.Format("2006-01-02"),
  268. EndDate: "",
  269. CreateAt: now.Format("2006-01-02 15:04:05"),
  270. UpdateAt: now.Format("2006-01-02 15:04:05"),
  271. SettleFile: "",
  272. }
  273. _, err = mediumSettlementDb.MediumSettlementInsertBySession(session, mediumSettlement)
  274. if err != nil {
  275. _ = session.Rollback()
  276. return
  277. }
  278. } else {
  279. //更新数据
  280. if medium.SettlementType != mediumSettlement.Kind {
  281. //TODO::前后结算类型不一致,之前结算单状态自动变成"核算中"
  282. mediumSettlement.State = enum.MediumSettlementStateForAccountingInProgress
  283. //mediumSettlement.PayState = mediumSettlementPayState
  284. _, err = mediumSettlementDb.UpdateMediumSettlementBySession(session, mediumSettlement, "state")
  285. if err != nil {
  286. _ = session.Rollback()
  287. return
  288. }
  289. //新增一条数据
  290. mediumSettlement = &model.MediumSettlement{
  291. Uuid: originalWxAdData.Uuid,
  292. MediumId: originalWxAdData.MediumId,
  293. AppId: originalWxAdData.AppId,
  294. BusinessKind: 1,
  295. Kind: medium.SettlementType,
  296. BasicIncome: generateWxAdData.MediaRevenue,
  297. OtherIncome: 0,
  298. OriginalIncome: originalWxAdData.PublisherIncome,
  299. PlatformRetention: generateWxAdData.PlatformRetention,
  300. CommissionRetention: generateWxAdData.CommissionRetention,
  301. PriceAdjustmentRetention: generateWxAdData.PriceAdjustmentRetention,
  302. State: mediumSettlementState,
  303. PayState: mediumSettlementPayState,
  304. StartDate: now.Format("2006-01-02"),
  305. EndDate: "",
  306. CreateAt: now.Format("2006-01-02 15:04:05"),
  307. UpdateAt: now.Format("2006-01-02 15:04:05"),
  308. SettleFile: "",
  309. }
  310. _, err = mediumSettlementDb.MediumSettlementInsertBySession(session, mediumSettlement)
  311. if err != nil {
  312. _ = session.Rollback()
  313. return
  314. }
  315. } else {
  316. basicIncomeBefore = mediumSettlement.BasicIncome
  317. mediumSettlement.BasicIncome += generateWxAdData.MediaRevenue
  318. mediumSettlement.OriginalIncome += originalWxAdData.PublisherIncome
  319. mediumSettlement.PlatformRetention += generateWxAdData.PlatformRetention
  320. mediumSettlement.CommissionRetention += generateWxAdData.CommissionRetention
  321. mediumSettlement.PriceAdjustmentRetention += generateWxAdData.PriceAdjustmentRetention
  322. _, err = mediumSettlementDb.UpdateMediumSettlementBySession(session, mediumSettlement, "basic_income", "original_income", "platform_retention", "commission_retention", "price_adjustment_retention")
  323. if err != nil {
  324. _ = session.Rollback()
  325. return
  326. }
  327. }
  328. }
  329. if medium.SettlementType == enum.MediumSettlementTypeForPaymentInAdvance {
  330. err = DealMediumAmount(session, md.DealMediumAmount{
  331. Mid: utils.IntToStr(originalWxAdData.Uuid),
  332. Type: md.FinMediumFlowDirectionExpenditure,
  333. Kind: md.SettlementSubKindForMediumFlow,
  334. OrdId: utils.IntToStr(mediumSettlement.Id),
  335. MediumId: originalWxAdData.MediumId,
  336. Amount: float64(generateWxAdData.MediaRevenue) / 100,
  337. Memo: md.SettlementSubTitleForMediumFlow,
  338. })
  339. if err != nil {
  340. _ = session.Rollback()
  341. return
  342. }
  343. }
  344. //4.2 新增 medium_settlement_with_flow 数据
  345. mediumSettlementWithFlowDb := implement.NewMediumSettlementWithFlowDb(db.Db)
  346. _, err = mediumSettlementWithFlowDb.MediumSettlementWithFlowInsertBySession(session, &model.MediumSettlementWithFlow{
  347. SettlementId: mediumSettlement.Id,
  348. GenerateDataId: generateWxAdData.Id,
  349. Amount: generateWxAdData.MediaRevenue,
  350. BasicIncomeBefore: basicIncomeBefore,
  351. BasicIncomeAfter: mediumSettlement.BasicIncome,
  352. OtherIncomeBefore: 0,
  353. OtherIncomeAfter: 0,
  354. Kind: enum.MediumSettlementWithFlowKindForBasicIncome,
  355. CreateAt: now.Format("2006-01-02 15:04:05"),
  356. UpdateAt: now.Format("2006-01-02 15:04:05"),
  357. })
  358. if err != nil {
  359. _ = session.Rollback()
  360. return
  361. }
  362. //4.3 新增 agent_settlement、agent_settlement_with_flow 数据
  363. agentSettlementDb := implement.NewAgentSettlementDb(db.Db)
  364. agentSettlementWithFlowDb := implement.NewAgentSettlementWithFlowDb(db.Db)
  365. for _, v := range *generateWxAdDataWithAgentFlow {
  366. agentSettlement, err1 := agentSettlementDb.GetAgentSettlementForAvailable(v.AgentId)
  367. if err1 != nil {
  368. _ = session.Rollback()
  369. return err1
  370. }
  371. var agentBasicIncomeBefore, agentOtherIncomeBefore, agentSettlementState, agentSettlementPayState int
  372. //判断是否为预付结算类型
  373. if agentMap[v.AgentId].SettlementType == enum.AgentSettlementTypeForPaymentInAdvance {
  374. agentSettlementState = enum.AgentSettlementStateForCompleteSign
  375. agentSettlementPayState = enum.AgentSettlementPayStateForPaymentAlready
  376. }
  377. if agentSettlement == nil {
  378. //新增一条数据
  379. agentSettlement = &model.AgentSettlement{
  380. Uuid: v.Uuid,
  381. AgentId: v.AgentId,
  382. MediumId: originalWxAdData.MediumId,
  383. AppId: v.AppId,
  384. BusinessKind: 1,
  385. Kind: agentMap[v.AgentId].SettlementType,
  386. BasicIncome: v.AgentRevenue,
  387. OtherIncome: v.ExtraRevenue,
  388. State: agentSettlementState,
  389. PayState: agentSettlementPayState,
  390. StartDate: now.Format("2006-01-02"),
  391. EndDate: "",
  392. CreateAt: now.Format("2006-01-02 15:04:05"),
  393. UpdateAt: now.Format("2006-01-02 15:04:05"),
  394. }
  395. _, err = agentSettlementDb.AgentSettlementInsertBySession(session, agentSettlement)
  396. if err != nil {
  397. _ = session.Rollback()
  398. return
  399. }
  400. } else {
  401. //更新数据
  402. if agentMap[v.AgentId].SettlementType != agentSettlement.Kind {
  403. //TODO::前后结算类型不一致,之前结算单状态自动变成"核算中"
  404. agentSettlement.State = enum.AgentSettlementStateForAccountingInProgress
  405. _, err = agentSettlementDb.UpdateAgentSettlementBySession(session, agentSettlement, "state")
  406. if err != nil {
  407. _ = session.Rollback()
  408. return
  409. }
  410. //新增一条数据
  411. agentSettlement = &model.AgentSettlement{
  412. Uuid: v.Uuid,
  413. AgentId: v.AgentId,
  414. MediumId: originalWxAdData.MediumId,
  415. AppId: v.AppId,
  416. BusinessKind: 1,
  417. Kind: agentMap[v.AgentId].SettlementType,
  418. BasicIncome: v.AgentRevenue,
  419. OtherIncome: v.ExtraRevenue,
  420. State: agentSettlementState,
  421. PayState: agentSettlementPayState,
  422. StartDate: now.Format("2006-01-02"),
  423. EndDate: "",
  424. CreateAt: now.Format("2006-01-02 15:04:05"),
  425. UpdateAt: now.Format("2006-01-02 15:04:05"),
  426. }
  427. _, err = agentSettlementDb.AgentSettlementInsertBySession(session, agentSettlement)
  428. if err != nil {
  429. _ = session.Rollback()
  430. return
  431. }
  432. } else {
  433. agentBasicIncomeBefore = agentSettlement.BasicIncome
  434. agentOtherIncomeBefore = agentSettlement.OtherIncome
  435. agentSettlement.BasicIncome += v.AgentRevenue
  436. agentSettlement.OtherIncome += v.ExtraRevenue
  437. _, err = agentSettlementDb.UpdateAgentSettlementBySession(session, agentSettlement, "basic_income", "other_income")
  438. if err != nil {
  439. _ = session.Rollback()
  440. return
  441. }
  442. }
  443. }
  444. if agentMap[v.AgentId].SettlementType == enum.AgentSettlementTypeForPaymentInAdvance {
  445. err = DealAgentAmount(session, md.DealAgentAmount{
  446. Mid: utils.IntToStr(originalWxAdData.Uuid),
  447. Type: md.FinAgentFlowDirectionExpenditure,
  448. Kind: md.SettlementSubKindForAgentFlow,
  449. OrdId: utils.IntToStr(v.Id),
  450. Amount: float64(v.AgentRevenue)/100 + float64(v.ExtraRevenue)/100,
  451. Memo: md.SettlementSubTitleForMediumFlow,
  452. })
  453. if err != nil {
  454. _ = session.Rollback()
  455. return
  456. }
  457. }
  458. _, err = agentSettlementWithFlowDb.AgentSettlementWithFlowInsertBySession(session, &model.AgentSettlementWithFlow{
  459. SettlementId: mediumSettlement.Id,
  460. GenerateDataId: generateWxAdData.Id,
  461. Amount: v.AgentRevenue,
  462. BasicIncomeBefore: agentBasicIncomeBefore,
  463. BasicIncomeAfter: agentSettlement.BasicIncome,
  464. OtherIncomeBefore: agentOtherIncomeBefore,
  465. OtherIncomeAfter: agentSettlement.OtherIncome,
  466. Kind: enum.AgentSettlementWithFlowKindForBasicIncome,
  467. CreateAt: now.Format("2006-01-02 15:04:05"),
  468. UpdateAt: now.Format("2006-01-02 15:04:05"),
  469. })
  470. if err != nil {
  471. _ = session.Rollback()
  472. return
  473. }
  474. }
  475. //4、修改 generate_wx_ad_data 记录中的 is_generate_report (是否已应用(0:未 1:已) )
  476. generateWxAdData.IsGenerateReport = 1
  477. _, err = generateWxAdDataDb.UpdateGenerateWxAdDataBySession(session, generateWxAdData, "is_generate_report")
  478. if err != nil {
  479. _ = session.Rollback()
  480. return
  481. }
  482. return session.Commit()
  483. }