广告平台(站长使用)
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

svc_wx_data.go 20 KiB

3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
3ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
3ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. PlatformAppId: originalWxAdData.PlatformAppId,
  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. Platform: originalWxAdData.Platform,
  118. }
  119. generateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  120. _, err = generateWxAdDataDb.GenerateWxAdDataInsertBySession(session, &generateWxAdData)
  121. if err != nil {
  122. _ = session.Rollback()
  123. return
  124. }
  125. var generateWxAdDataWithAgentFlows []*model.GenerateWxAdDataWithAgentFlow
  126. for _, v := range agentRevenueFlows {
  127. generateWxAdDataWithAgentFlows = append(generateWxAdDataWithAgentFlows, &model.GenerateWxAdDataWithAgentFlow{
  128. Uuid: generateWxAdData.Uuid,
  129. AppId: generateWxAdData.AppId,
  130. PlatformAppId: generateWxAdData.PlatformAppId,
  131. AgentId: v.AgentId,
  132. GenerateDataId: generateWxAdData.Id,
  133. SlotId: generateWxAdData.SlotId,
  134. AdSlot: generateWxAdData.AdSlot,
  135. Date: generateWxAdData.Date,
  136. AgentRevenue: v.AgentRevenue,
  137. AgentRevenueRate: v.AgentRevenueRate,
  138. ExtraRevenue: v.ExtraRevenue,
  139. ExtraRevenueRate: v.ExtraRevenueRate,
  140. CreateAt: now.Format("2006-01-02 15:04:05"),
  141. UpdateAt: now.Format("2006-01-02 15:04:05"),
  142. })
  143. }
  144. generateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db)
  145. _, err = generateWxAdDataWithAgentFlowDb.BatchAddGenerateWxAdDataWithAgentFlow(session, generateWxAdDataWithAgentFlows)
  146. if err != nil {
  147. _ = session.Rollback()
  148. return
  149. }
  150. //6、修改 original_wx_ad_data 记录中的 is_apply(是否已应用(0:未 1:已) )
  151. originalWxAdData.IsApply = 1
  152. _, err = originalWxAdDataDb.UpdateOriginalWxAdDataBySession(session, originalWxAdData, "is_apply")
  153. if err != nil {
  154. _ = session.Rollback()
  155. return
  156. }
  157. return session.Commit(), generateWxAdData
  158. }
  159. func ClacEcpm(req md.ClacEcpmReq) (err error, ecpm string) {
  160. //1、查找原始数据记录
  161. originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db)
  162. originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(req.GenerateDataId)
  163. if err != nil {
  164. return
  165. }
  166. if originalWxAdData == nil {
  167. err = errors.New("未查询到原始数据记录")
  168. return
  169. }
  170. //2、查询对应媒体分成策略
  171. mediumDivisionStrategyDb := implement.NewMediumDivisionStrategyDb(db.Db)
  172. mediumDivisionStrategy, err := mediumDivisionStrategyDb.GetOriginalWxAdDataByMediumId(originalWxAdData.MediumId)
  173. if err != nil {
  174. return
  175. }
  176. if mediumDivisionStrategy == nil {
  177. err = errors.New("未查询到对应代理的分成策略")
  178. return
  179. }
  180. //3、计算媒体、代理收益、协议总分成
  181. publisherIncome := originalWxAdData.PublisherIncome
  182. mediaRevenue := publisherIncome * mediumDivisionStrategy.MediaRevenueRate / 100 //媒体收益
  183. agentRevenue := publisherIncome * mediumDivisionStrategy.AgentRevenueRate / 100 //代理收益
  184. agreementSharingTotal := (mediaRevenue + agentRevenue) / (100 - mediumDivisionStrategy.AgreementSharingRate) //协议总分成(倒推)
  185. //4、倒退出当前ecpm值
  186. ecpm = utils.Float64ToStrPrec4(float64(agreementSharingTotal) / (float64(originalWxAdData.ExposureCount) / 1000))
  187. return
  188. }
  189. func SettlementWxAdData(req md.SettlementWxAdData) (err error) {
  190. //1、查找生成数据记录
  191. generateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  192. generateWxAdData, err := generateWxAdDataDb.GetGenerateWxAdData(req.GenerateDataId)
  193. if err != nil {
  194. return
  195. }
  196. if generateWxAdData == nil {
  197. err = errors.New("未查询到生成数据记录")
  198. return
  199. }
  200. originalWxAdDataDb := implement.NewOriginalWxAdDataDb(db.Db)
  201. originalWxAdData, err := originalWxAdDataDb.GetOriginalWxAdData(generateWxAdData.OriginalDataId)
  202. if err != nil {
  203. return
  204. }
  205. if originalWxAdData == nil {
  206. err = errors.New("未查询到原始数据记录")
  207. return
  208. }
  209. //2、查询 生成数据-代理明细 记录
  210. generateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db)
  211. generateWxAdDataWithAgentFlow, err := generateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(generateWxAdData.Id)
  212. if err != nil {
  213. return
  214. }
  215. //3、查询 媒体、代理的结算方式
  216. mediumListDb := implement.NewMediumListDb(db.Db)
  217. medium, err := mediumListDb.GetMediumList(originalWxAdData.MediumId)
  218. if err != nil {
  219. return
  220. }
  221. if medium == nil {
  222. return errors.New("媒体:" + utils.IntToStr(originalWxAdData.MediumId) + "未查询到对应记录")
  223. }
  224. var agentMap = map[int]model.AgentList{}
  225. agentListDb := implement.NewAgentListDb(db.Db)
  226. for _, v := range *generateWxAdDataWithAgentFlow {
  227. agent, err1 := agentListDb.GetAgentList(v.AgentId)
  228. if err1 != nil {
  229. return err1
  230. }
  231. if agent == nil {
  232. return errors.New("代理:" + utils.IntToStr(v.AgentId) + "未查询到对应记录")
  233. }
  234. agentMap[v.AgentId] = *agent
  235. }
  236. //4、生成数据
  237. now := time.Now()
  238. session := db.Db.NewSession()
  239. defer session.Close()
  240. session.Begin()
  241. //4.1 新增/更新 medium_settlement 数据
  242. mediumSettlementDb := implement.NewMediumSettlementDb(db.Db)
  243. mediumSettlement, err := mediumSettlementDb.GetMediumSettlementForAvailable(originalWxAdData.MediumId)
  244. if err != nil {
  245. return
  246. }
  247. var basicIncomeBefore, mediumSettlementState, mediumSettlementPayState int
  248. //判断是否为预付结算类型
  249. if medium.SettlementType == enum.MediumSettlementTypeForPaymentInAdvance {
  250. mediumSettlementState = enum.MediumSettlementStateForCompleteSign
  251. mediumSettlementPayState = enum.MediumSettlementPayStateForPaymentAlready
  252. }
  253. if mediumSettlement == nil {
  254. //新增一条数据
  255. mediumSettlement = &model.MediumSettlement{
  256. Uuid: originalWxAdData.Uuid,
  257. MediumId: originalWxAdData.MediumId,
  258. AppId: originalWxAdData.AppId,
  259. PlatformAppId: originalWxAdData.PlatformAppId,
  260. BusinessKind: 1,
  261. Kind: medium.SettlementType,
  262. BasicIncome: generateWxAdData.MediaRevenue,
  263. OtherIncome: 0,
  264. OriginalIncome: originalWxAdData.PublisherIncome,
  265. PlatformRetention: generateWxAdData.PlatformRetention,
  266. CommissionRetention: generateWxAdData.CommissionRetention,
  267. PriceAdjustmentRetention: generateWxAdData.PriceAdjustmentRetention,
  268. State: mediumSettlementState,
  269. PayState: mediumSettlementPayState,
  270. StartDate: now.Format("2006-01-02"),
  271. EndDate: "",
  272. CreateAt: now.Format("2006-01-02 15:04:05"),
  273. UpdateAt: now.Format("2006-01-02 15:04:05"),
  274. SettleFile: "",
  275. }
  276. _, err = mediumSettlementDb.MediumSettlementInsertBySession(session, mediumSettlement)
  277. if err != nil {
  278. _ = session.Rollback()
  279. return
  280. }
  281. } else {
  282. //更新数据
  283. if medium.SettlementType != mediumSettlement.Kind {
  284. //TODO::前后结算类型不一致,之前结算单状态自动变成"核算中"
  285. mediumSettlement.State = enum.MediumSettlementStateForAccountingInProgress
  286. //mediumSettlement.PayState = mediumSettlementPayState
  287. _, err = mediumSettlementDb.UpdateMediumSettlementBySession(session, mediumSettlement, "state")
  288. if err != nil {
  289. _ = session.Rollback()
  290. return
  291. }
  292. //新增一条数据
  293. mediumSettlement = &model.MediumSettlement{
  294. Uuid: originalWxAdData.Uuid,
  295. MediumId: originalWxAdData.MediumId,
  296. AppId: originalWxAdData.AppId,
  297. PlatformAppId: originalWxAdData.PlatformAppId,
  298. BusinessKind: 1,
  299. Kind: medium.SettlementType,
  300. BasicIncome: generateWxAdData.MediaRevenue,
  301. OtherIncome: 0,
  302. OriginalIncome: originalWxAdData.PublisherIncome,
  303. PlatformRetention: generateWxAdData.PlatformRetention,
  304. CommissionRetention: generateWxAdData.CommissionRetention,
  305. PriceAdjustmentRetention: generateWxAdData.PriceAdjustmentRetention,
  306. State: mediumSettlementState,
  307. PayState: mediumSettlementPayState,
  308. StartDate: now.Format("2006-01-02"),
  309. EndDate: "",
  310. CreateAt: now.Format("2006-01-02 15:04:05"),
  311. UpdateAt: now.Format("2006-01-02 15:04:05"),
  312. SettleFile: "",
  313. }
  314. _, err = mediumSettlementDb.MediumSettlementInsertBySession(session, mediumSettlement)
  315. if err != nil {
  316. _ = session.Rollback()
  317. return
  318. }
  319. } else {
  320. basicIncomeBefore = mediumSettlement.BasicIncome
  321. mediumSettlement.BasicIncome += generateWxAdData.MediaRevenue
  322. mediumSettlement.OriginalIncome += originalWxAdData.PublisherIncome
  323. mediumSettlement.PlatformRetention += generateWxAdData.PlatformRetention
  324. mediumSettlement.CommissionRetention += generateWxAdData.CommissionRetention
  325. mediumSettlement.PriceAdjustmentRetention += generateWxAdData.PriceAdjustmentRetention
  326. _, err = mediumSettlementDb.UpdateMediumSettlementBySession(session, mediumSettlement, "basic_income", "original_income", "platform_retention", "commission_retention", "price_adjustment_retention")
  327. if err != nil {
  328. _ = session.Rollback()
  329. return
  330. }
  331. }
  332. }
  333. if medium.SettlementType == enum.MediumSettlementTypeForPaymentInAdvance {
  334. err = DealMediumAmount(session, md.DealMediumAmount{
  335. Mid: utils.IntToStr(originalWxAdData.Uuid),
  336. Type: md.FinMediumFlowDirectionExpenditure,
  337. Kind: md.SettlementSubKindForMediumFlow,
  338. OrdId: utils.IntToStr(mediumSettlement.Id),
  339. MediumId: originalWxAdData.MediumId,
  340. Amount: float64(generateWxAdData.MediaRevenue) / 100,
  341. Memo: md.SettlementSubTitleForMediumFlow,
  342. })
  343. if err != nil {
  344. _ = session.Rollback()
  345. return
  346. }
  347. }
  348. //4.2 新增 medium_settlement_with_flow 数据
  349. mediumSettlementWithFlowDb := implement.NewMediumSettlementWithFlowDb(db.Db)
  350. _, err = mediumSettlementWithFlowDb.MediumSettlementWithFlowInsertBySession(session, &model.MediumSettlementWithFlow{
  351. SettlementId: mediumSettlement.Id,
  352. GenerateDataId: generateWxAdData.Id,
  353. Amount: generateWxAdData.MediaRevenue,
  354. BasicIncomeBefore: basicIncomeBefore,
  355. BasicIncomeAfter: mediumSettlement.BasicIncome,
  356. OtherIncomeBefore: 0,
  357. OtherIncomeAfter: 0,
  358. Kind: enum.MediumSettlementWithFlowKindForBasicIncome,
  359. CreateAt: now.Format("2006-01-02 15:04:05"),
  360. UpdateAt: now.Format("2006-01-02 15:04:05"),
  361. })
  362. if err != nil {
  363. _ = session.Rollback()
  364. return
  365. }
  366. //4.3 新增 agent_settlement、agent_settlement_with_flow 数据
  367. agentSettlementDb := implement.NewAgentSettlementDb(db.Db)
  368. agentSettlementWithFlowDb := implement.NewAgentSettlementWithFlowDb(db.Db)
  369. for _, v := range *generateWxAdDataWithAgentFlow {
  370. agentSettlement, err1 := agentSettlementDb.GetAgentSettlementForAvailable(v.AgentId)
  371. if err1 != nil {
  372. _ = session.Rollback()
  373. return err1
  374. }
  375. var agentBasicIncomeBefore, agentOtherIncomeBefore, agentSettlementState, agentSettlementPayState int
  376. //判断是否为预付结算类型
  377. if agentMap[v.AgentId].SettlementType == enum.AgentSettlementTypeForPaymentInAdvance {
  378. agentSettlementState = enum.AgentSettlementStateForCompleteSign
  379. agentSettlementPayState = enum.AgentSettlementPayStateForPaymentAlready
  380. }
  381. if agentSettlement == nil {
  382. //新增一条数据
  383. agentSettlement = &model.AgentSettlement{
  384. Uuid: v.Uuid,
  385. AgentId: v.AgentId,
  386. MediumId: originalWxAdData.MediumId,
  387. AppId: v.AppId,
  388. PlatformAppId: v.PlatformAppId,
  389. BusinessKind: 1,
  390. Kind: agentMap[v.AgentId].SettlementType,
  391. BasicIncome: v.AgentRevenue,
  392. OtherIncome: v.ExtraRevenue,
  393. State: agentSettlementState,
  394. PayState: agentSettlementPayState,
  395. StartDate: now.Format("2006-01-02"),
  396. EndDate: "",
  397. CreateAt: now.Format("2006-01-02 15:04:05"),
  398. UpdateAt: now.Format("2006-01-02 15:04:05"),
  399. }
  400. _, err = agentSettlementDb.AgentSettlementInsertBySession(session, agentSettlement)
  401. if err != nil {
  402. _ = session.Rollback()
  403. return
  404. }
  405. } else {
  406. //更新数据
  407. if agentMap[v.AgentId].SettlementType != agentSettlement.Kind {
  408. //TODO::前后结算类型不一致,之前结算单状态自动变成"核算中"
  409. agentSettlement.State = enum.AgentSettlementStateForAccountingInProgress
  410. _, err = agentSettlementDb.UpdateAgentSettlementBySession(session, agentSettlement, "state")
  411. if err != nil {
  412. _ = session.Rollback()
  413. return
  414. }
  415. //新增一条数据
  416. agentSettlement = &model.AgentSettlement{
  417. Uuid: v.Uuid,
  418. AgentId: v.AgentId,
  419. MediumId: originalWxAdData.MediumId,
  420. AppId: v.AppId,
  421. PlatformAppId: v.PlatformAppId,
  422. BusinessKind: 1,
  423. Kind: agentMap[v.AgentId].SettlementType,
  424. BasicIncome: v.AgentRevenue,
  425. OtherIncome: v.ExtraRevenue,
  426. State: agentSettlementState,
  427. PayState: agentSettlementPayState,
  428. StartDate: now.Format("2006-01-02"),
  429. EndDate: "",
  430. CreateAt: now.Format("2006-01-02 15:04:05"),
  431. UpdateAt: now.Format("2006-01-02 15:04:05"),
  432. }
  433. _, err = agentSettlementDb.AgentSettlementInsertBySession(session, agentSettlement)
  434. if err != nil {
  435. _ = session.Rollback()
  436. return
  437. }
  438. } else {
  439. agentBasicIncomeBefore = agentSettlement.BasicIncome
  440. agentOtherIncomeBefore = agentSettlement.OtherIncome
  441. agentSettlement.BasicIncome += v.AgentRevenue
  442. agentSettlement.OtherIncome += v.ExtraRevenue
  443. _, err = agentSettlementDb.UpdateAgentSettlementBySession(session, agentSettlement, "basic_income", "other_income")
  444. if err != nil {
  445. _ = session.Rollback()
  446. return
  447. }
  448. }
  449. }
  450. if agentMap[v.AgentId].SettlementType == enum.AgentSettlementTypeForPaymentInAdvance {
  451. err = DealAgentAmount(session, md.DealAgentAmount{
  452. Mid: utils.IntToStr(originalWxAdData.Uuid),
  453. Type: md.FinAgentFlowDirectionExpenditure,
  454. Kind: md.SettlementSubKindForAgentFlow,
  455. OrdId: utils.IntToStr(v.Id),
  456. Amount: float64(v.AgentRevenue)/100 + float64(v.ExtraRevenue)/100,
  457. Memo: md.SettlementSubTitleForMediumFlow,
  458. })
  459. if err != nil {
  460. _ = session.Rollback()
  461. return
  462. }
  463. }
  464. _, err = agentSettlementWithFlowDb.AgentSettlementWithFlowInsertBySession(session, &model.AgentSettlementWithFlow{
  465. SettlementId: mediumSettlement.Id,
  466. GenerateDataId: generateWxAdData.Id,
  467. Amount: v.AgentRevenue,
  468. BasicIncomeBefore: agentBasicIncomeBefore,
  469. BasicIncomeAfter: agentSettlement.BasicIncome,
  470. OtherIncomeBefore: agentOtherIncomeBefore,
  471. OtherIncomeAfter: agentSettlement.OtherIncome,
  472. Kind: enum.AgentSettlementWithFlowKindForBasicIncome,
  473. CreateAt: now.Format("2006-01-02 15:04:05"),
  474. UpdateAt: now.Format("2006-01-02 15:04:05"),
  475. })
  476. if err != nil {
  477. _ = session.Rollback()
  478. return
  479. }
  480. }
  481. //4、修改 generate_wx_ad_data 记录中的 is_generate_report (是否已应用(0:未 1:已) )
  482. generateWxAdData.IsGenerateReport = 1
  483. _, err = generateWxAdDataDb.UpdateGenerateWxAdDataBySession(session, generateWxAdData, "is_generate_report")
  484. if err != nil {
  485. _ = session.Rollback()
  486. return
  487. }
  488. return session.Commit()
  489. }