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

402 lines
15 KiB

  1. package svc
  2. import (
  3. "applet/app/e"
  4. "applet/app/md"
  5. "applet/app/utils"
  6. "applet/app/utils/cache"
  7. db "code.fnuoos.com/zhimeng/model.git/src"
  8. implement2 "code.fnuoos.com/zhimeng/model.git/src/implement"
  9. "code.fnuoos.com/zhimeng/model.git/src/super/implement"
  10. "code.fnuoos.com/zhimeng/model.git/src/super/model"
  11. "errors"
  12. "fmt"
  13. "github.com/gin-gonic/gin"
  14. )
  15. func DataCenterIncomeDataListForOpen(c *gin.Context, req md.DataCenterGenerateDataForOpenReq) md.DataCenterIncomeDataRes {
  16. engine := db.Db
  17. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(engine)
  18. mediumId := req.MediumId
  19. MediumList, total, _ := NewGenerateWxAdDataDb.FindGenerateWxAdDataListMedium(c.GetString("mid"), "", mediumId, "", req.StartTime, req.EndTime, utils.StrToInt(req.Page), utils.StrToInt(req.Limit))
  20. data := make([]md.DataCenterIncomeDataData, 0)
  21. if len(MediumList) > 0 {
  22. for _, v := range MediumList {
  23. var tmp = md.DataCenterIncomeDataData{
  24. AgreementSharing: utils.Float64ToStr(float64(v.AgreementSharing) / 100),
  25. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  26. Id: utils.IntToStr(v.Id),
  27. ExposureCount: utils.IntToStr(v.ExposureCount),
  28. ClickCount: utils.IntToStr(v.ClickCount),
  29. ClickRate: v.ClickRate,
  30. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v.Ecpm) / 100),
  31. Date: v.Date,
  32. MediaRevenue: utils.Float64ToStr(float64(v.MediaRevenue) / 100),
  33. SettleAmount: utils.Float64ToStr(float64(v.MediaRevenue+v.AgentRevenue+v.AgreementSharing) / 100),
  34. }
  35. tmpApplet := GetAppletInfo(c, v.AppId)
  36. if tmpApplet["platform"] != "" {
  37. tmp.Platform = tmpApplet["platform"]
  38. }
  39. if tmpApplet["name"] != "" {
  40. tmp.Name = tmpApplet["name"]
  41. }
  42. tmpSlot := GetSlotInfo(c, v.SlotId)
  43. if tmpSlot["state"] != "" {
  44. tmp.State = tmpSlot["state"]
  45. }
  46. if tmpSlot["name"] != "" {
  47. tmp.AdvName = tmpSlot["name"]
  48. }
  49. data = append(data, tmp)
  50. }
  51. }
  52. res := md.DataCenterIncomeDataRes{
  53. List: data,
  54. Total: total,
  55. State: md.AdState,
  56. Platform: md.AdPlatform,
  57. }
  58. return res
  59. }
  60. func DataCenterIncomeDataList(c *gin.Context, req md.DataCenterGenerateDataReq) md.DataCenterIncomeDataRes {
  61. engine := db.Db
  62. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(engine)
  63. user := GetUser(c)
  64. appId := GetAppletId(c, req.Name, req.Platform)
  65. mediumId := GetMediumIdStr(c, user.AdmId, "", "")
  66. slotId := GetSlotId(c, req.State)
  67. MediumList, total, _ := NewGenerateWxAdDataDb.FindGenerateWxAdDataListMedium(c.GetString("mid"), appId, mediumId, slotId, req.StartTime, req.EndTime, utils.StrToInt(req.Page), utils.StrToInt(req.Limit))
  68. data := make([]md.DataCenterIncomeDataData, 0)
  69. if len(MediumList) > 0 {
  70. for _, v := range MediumList {
  71. var tmp = md.DataCenterIncomeDataData{
  72. AgreementSharing: utils.Float64ToStr(float64(v.AgreementSharing) / 100),
  73. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  74. Id: utils.IntToStr(v.Id),
  75. ExposureCount: utils.IntToStr(v.ExposureCount),
  76. ClickCount: utils.IntToStr(v.ClickCount),
  77. ClickRate: v.ClickRate,
  78. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v.Ecpm) / 100),
  79. Date: v.Date,
  80. MediaRevenue: utils.Float64ToStr(float64(v.MediaRevenue) / 100),
  81. SettleAmount: utils.Float64ToStr(float64(v.MediaRevenue+v.AgentRevenue+v.AgreementSharing) / 100),
  82. }
  83. tmpApplet := GetAppletInfo(c, v.AppId)
  84. if tmpApplet["platform"] != "" {
  85. tmp.Platform = tmpApplet["platform"]
  86. }
  87. if tmpApplet["name"] != "" {
  88. tmp.Name = tmpApplet["name"]
  89. }
  90. tmpSlot := GetSlotInfo(c, v.SlotId)
  91. if tmpSlot["state"] != "" {
  92. tmp.State = tmpSlot["state"]
  93. }
  94. if tmpSlot["name"] != "" {
  95. tmp.AdvName = tmpSlot["name"]
  96. }
  97. data = append(data, tmp)
  98. }
  99. }
  100. res := md.DataCenterIncomeDataRes{
  101. List: data,
  102. Total: total,
  103. State: md.AdState,
  104. Platform: md.AdPlatform,
  105. }
  106. return res
  107. }
  108. func DataCenterIncomeDataListOutput(c *gin.Context, req md.DataCenterGenerateDataReq) {
  109. engine := db.Db
  110. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(engine)
  111. user := GetUser(c)
  112. appId := GetAppletId(c, req.Name, req.Platform)
  113. mediumId := GetMediumIdStr(c, user.AdmId, "", "")
  114. slotId := GetSlotId(c, req.State)
  115. req.Limit = "3000"
  116. MediumList, _ := NewGenerateWxAdDataDb.FindGenerateWxAdDataListMediumAll(c.GetString("mid"), appId, mediumId, slotId, req.StartTime, req.EndTime, utils.StrToInt(req.Page), utils.StrToInt(req.Limit))
  117. data := map[string]string{
  118. "A1": "名称",
  119. "B1": "日期",
  120. "C1": "广告位",
  121. "D1": "曝光量",
  122. "E1": "点击率(%)",
  123. "F1": "点击量",
  124. "G1": "结算平台",
  125. "H1": "媒体-ECPM",
  126. "I1": "媒体收益(元)",
  127. "J1": "代理收益(元)",
  128. "K1": "平台收益(元)",
  129. }
  130. name := "运营报表第" + req.Page + "页"
  131. if len(MediumList) > 0 {
  132. for k, v := range MediumList {
  133. var tmp = md.DataCenterIncomeDataData{
  134. AgreementSharing: utils.Float64ToStr(float64(v.AgreementSharing) / 100),
  135. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  136. Id: utils.IntToStr(v.Id),
  137. ExposureCount: utils.IntToStr(v.ExposureCount),
  138. ClickCount: utils.IntToStr(v.ClickCount),
  139. ClickRate: v.ClickRate,
  140. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v.Ecpm) / 100),
  141. Date: v.Date,
  142. MediaRevenue: utils.Float64ToStr(float64(v.MediaRevenue) / 100),
  143. SettleAmount: utils.Float64ToStr(float64(v.MediaRevenue+v.AgentRevenue+v.AgreementSharing) / 100),
  144. }
  145. tmpApplet := GetAppletInfo(c, v.AppId)
  146. if tmpApplet["platform"] != "" {
  147. tmp.Platform = md.AdPlatformMap[tmpApplet["platform"]]
  148. }
  149. if tmpApplet["name"] != "" {
  150. tmp.Name = tmpApplet["name"]
  151. }
  152. tmpSlot := GetSlotInfo(c, v.SlotId)
  153. if tmpSlot["state"] != "" {
  154. tmp.State = tmpSlot["state"]
  155. }
  156. if tmpSlot["name"] != "" {
  157. tmp.AdvName = tmpSlot["name"]
  158. }
  159. i := utils.IntToStr(k + 2)
  160. data["A"+i] = tmp.Name
  161. data["B"+i] = tmp.Date
  162. data["C"+i] = tmp.AdvName
  163. data["D"+i] = tmp.ExposureCount
  164. data["E"+i] = tmp.ClickRate
  165. data["F"+i] = tmp.ClickCount
  166. data["G"+i] = tmp.Platform
  167. data["H"+i] = tmp.Ecpm
  168. data["I"+i] = tmp.MediaRevenue
  169. data["J"+i] = tmp.AgentRevenue
  170. data["K"+i] = tmp.AgreementSharing
  171. }
  172. }
  173. file := utils.Output(c, name, data)
  174. filename := name + ".xlsx"
  175. r := map[string]string{
  176. "file": file,
  177. "filename": filename,
  178. }
  179. e.OutSuc(c, r, nil)
  180. return
  181. }
  182. func DataCenterIncomeDataDetail(c *gin.Context, req md.DataCenterGenerateDataCommReq) {
  183. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  184. data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id))
  185. if data == nil {
  186. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  187. return
  188. }
  189. agentReward := make([]md.DataCenterGenerateDataDetailAgentRewardSecond, 0)
  190. NewGenerateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db)
  191. agent, _ := NewGenerateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(data.Id)
  192. if agent != nil {
  193. for _, v := range *agent {
  194. tmp := md.DataCenterGenerateDataDetailAgentRewardSecond{
  195. Name: "",
  196. Account: "",
  197. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  198. AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate),
  199. }
  200. tmpApplet := GetAgentInfo(c, v.AgentId)
  201. if tmpApplet["name"] != "" {
  202. tmp.Name = tmpApplet["name"]
  203. }
  204. if tmpApplet["account"] != "" {
  205. tmp.Account = tmpApplet["account"]
  206. }
  207. agentReward = append(agentReward, tmp)
  208. }
  209. }
  210. res := md.DataCenterIncomeDataDetail{
  211. MediaRevenue: utils.Float64ToStr(float64(data.MediaRevenue) / 100),
  212. AgreementSharing: utils.Float64ToStr(float64(data.AgreementSharing) / 100),
  213. AgentRevenue: utils.Float64ToStr(float64(data.AgentRevenue) / 100),
  214. DataSource: "手动同步",
  215. CreateAt: data.CreateAt,
  216. UpdateAt: data.UpdateAt,
  217. MediaRevenueRate: utils.IntToStr(data.MediaRevenueRate),
  218. AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate),
  219. AgreementSharingRate: utils.IntToStr(data.AgreementSharingRate),
  220. AgentReward: agentReward,
  221. }
  222. tmpApplet := GetAppletInfo(c, data.AppId)
  223. if tmpApplet["platform"] != "" {
  224. res.Platform = tmpApplet["platform"]
  225. }
  226. NewAppletApplicationDb := implement2.NewAppletApplicationDb(MasterDb(c))
  227. app, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(data.AppId)
  228. tmp := GetMediumInfo(c, app.MediumId)
  229. if tmp["name"] != "" {
  230. res.MediumName = tmp["name"]
  231. if tmp["account"] != "" {
  232. res.MediumName += "(" + tmp["account"] + ")"
  233. }
  234. }
  235. e.OutSuc(c, res, nil)
  236. return
  237. }
  238. func DataCenterGenerateDataList(c *gin.Context, req md.DataCenterGenerateDataReq) md.DataCenterGenerateDataRes {
  239. engine := db.Db
  240. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(engine)
  241. user := GetUser(c)
  242. appId := GetAppletId(c, req.Name, req.Platform)
  243. mediumId := GetMediumIdStr(c, user.AdmId, "", "")
  244. slotId := GetSlotId(c, req.State)
  245. MediumList, total, _ := NewGenerateWxAdDataDb.FindGenerateWxAdDataList(c.GetString("mid"), appId, mediumId, slotId, req.StartTime, req.EndTime, utils.StrToInt(req.Page), utils.StrToInt(req.Limit))
  246. data := make([]md.DataCenterGenerateDataData, 0)
  247. if len(MediumList) > 0 {
  248. NewOriginalWxAdDataDb := implement.NewOriginalWxAdDataDb(engine)
  249. for _, v := range MediumList {
  250. wxData, _ := NewOriginalWxAdDataDb.GetOriginalWxAdData(v.OriginalDataId)
  251. var tmp = md.DataCenterGenerateDataData{
  252. Id: utils.IntToStr(v.Id),
  253. ExposureCount: utils.IntToStr(v.ExposureCount),
  254. ClickCount: utils.IntToStr(v.ClickCount),
  255. ClickRate: v.ClickRate,
  256. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v.Ecpm) / 100),
  257. IsGenerateReport: utils.IntToStr(v.IsGenerateReport),
  258. Date: v.Date,
  259. PlatformRetention: utils.Float64ToStr(float64(v.PlatformRetention) / 100),
  260. CommissionRetention: utils.Float64ToStr(float64(v.CommissionRetention) / 100),
  261. PriceAdjustmentRetention: utils.Float64ToStr(float64(v.PriceAdjustmentRetention) / 100),
  262. MediaRevenue: utils.Float64ToStr(float64(v.MediaRevenue) / 100),
  263. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  264. ExtraRevenue: utils.Float64ToStr(float64(v.ExtraRevenue) / 100),
  265. AgreementSharing: utils.Float64ToStr(float64(v.AgreementSharing) / 100),
  266. AgreementSharingTotal: utils.Float64ToStr(float64(v.AgreementSharingTotal) / 100),
  267. }
  268. if wxData != nil {
  269. tmp.OldClickRate = wxData.ClickRate
  270. tmp.OldClickCount = utils.IntToStr(wxData.ClickCount)
  271. tmp.OldEcpm = utils.Float64ToStr(utils.StrToFloat64(wxData.Ecpm) / 100)
  272. tmp.OldExposureCount = utils.IntToStr(wxData.ExposureCount)
  273. }
  274. tmpApplet := GetAppletInfo(c, v.AppId)
  275. if tmpApplet["platform"] != "" {
  276. tmp.Platform = tmpApplet["platform"]
  277. }
  278. if tmpApplet["name"] != "" {
  279. tmp.Name = tmpApplet["name"]
  280. }
  281. tmpSlot := GetSlotInfo(c, v.SlotId)
  282. if tmpSlot["state"] != "" {
  283. tmp.State = tmpSlot["state"]
  284. }
  285. if tmpSlot["name"] != "" {
  286. tmp.AdvName = tmpSlot["name"]
  287. }
  288. data = append(data, tmp)
  289. }
  290. }
  291. res := md.DataCenterGenerateDataRes{
  292. List: data,
  293. Total: total,
  294. State: md.AdState,
  295. Platform: md.AdPlatform,
  296. }
  297. return res
  298. }
  299. func DataCenterGenerateDataDel(c *gin.Context, req md.DataCenterGenerateDataCommReq) error {
  300. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  301. data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id))
  302. if data == nil {
  303. return errors.New("记录不存在")
  304. }
  305. if data.IsGenerateReport == 1 {
  306. return errors.New("记录已应用,不能删除")
  307. }
  308. // 1、删除 generate_wx_ad_data 数据
  309. _, err := db.Db.Where("id=?", req.Id).Delete(&model.GenerateWxAdData{})
  310. if err != nil {
  311. return err
  312. }
  313. // 2、删除 generate_wx_ad_data_with_agent_flow 数据
  314. _, err = db.Db.Where("generate_data_id=?", req.Id).Delete(&model.GenerateWxAdDataWithAgentFlow{})
  315. if err != nil {
  316. return err
  317. }
  318. // 3、修改 original_wx_ad_data 状态值
  319. db.Db.Where("id=?", data.OriginalDataId).Cols("is_apply").Update(&model.OriginalWxAdData{IsApply: 0})
  320. return nil
  321. }
  322. func DataCenterGenerateDataDetail(c *gin.Context, req md.DataCenterGenerateDataCommReq) {
  323. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  324. data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id))
  325. if data == nil {
  326. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  327. return
  328. }
  329. agentReward := make([]md.DataCenterGenerateDataDetailAgentReward, 0)
  330. NewGenerateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db)
  331. agent, _ := NewGenerateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(data.Id)
  332. if agent != nil {
  333. for _, v := range *agent {
  334. tmp := md.DataCenterGenerateDataDetailAgentReward{
  335. Name: "",
  336. Account: "",
  337. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  338. AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate),
  339. ExtraRevenue: utils.Float64ToStr(float64(v.ExtraRevenue) / 100),
  340. ExtraRevenueRate: utils.IntToStr(data.ExtraRevenueRate),
  341. }
  342. tmpApplet := GetAgentInfo(c, v.AgentId)
  343. if tmpApplet["name"] != "" {
  344. tmp.Name = tmpApplet["name"]
  345. }
  346. if tmpApplet["account"] != "" {
  347. tmp.Account = tmpApplet["account"]
  348. }
  349. agentReward = append(agentReward, tmp)
  350. }
  351. }
  352. res := md.DataCenterGenerateDataDetailData{
  353. PlatformRetentionRate: utils.IntToStr(data.PlatformRetentionRate),
  354. CommissionRetentionRate: utils.IntToStr(data.CommissionRetentionRate),
  355. MediaRevenueRate: utils.IntToStr(data.MediaRevenueRate),
  356. AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate),
  357. ExtraRevenueRate: utils.IntToStr(data.ExtraRevenueRate),
  358. AgreementSharingRate: utils.IntToStr(data.AgreementSharingRate),
  359. AgentReward: agentReward,
  360. }
  361. e.OutSuc(c, res, nil)
  362. return
  363. }
  364. func DataCenterGenerateDataDoing(c *gin.Context, req md.DataCenterGenerateDataCommReq) error {
  365. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  366. data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id))
  367. if data == nil {
  368. return e.NewErr(400, "记录不存在")
  369. }
  370. if data.IsGenerateReport == 1 {
  371. return e.NewErr(400, "该记录已完成操作")
  372. }
  373. // 加锁 防止并发提取
  374. mutexKey := fmt.Sprintf("%s:DataCenterGenerateDataDoing:%s", c.GetString("mid"), req.Id)
  375. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  376. if err != nil {
  377. return err
  378. }
  379. if withdrawAvailable != "OK" {
  380. return e.NewErr(400000, "请求过于频繁,请稍后再试")
  381. }
  382. args := md.SettlementWxAdData{
  383. GenerateDataId: utils.StrToInt(req.Id),
  384. }
  385. err = SettlementWxAdData(args)
  386. if err != nil {
  387. return err
  388. }
  389. return nil
  390. }