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

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