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

277 lines
10 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 := GetAppletIdByAdminId(c, user.AdmId, req.Name, req.Platform)
  20. slotId := GetSlotId(c, req.State)
  21. MediumList, total, _ := NewGenerateWxAdDataDb.FindGenerateWxAdDataListMedium(c.GetString("mid"), appId, slotId, req.StartTime, req.EndTime, utils.StrToInt(req.Page), utils.StrToInt(req.Limit))
  22. data := make([]md.DataCenterIncomeDataData, 0)
  23. if len(MediumList) > 0 {
  24. for _, v := range MediumList {
  25. var tmp = md.DataCenterIncomeDataData{
  26. AgreementSharing: utils.Float64ToStr(float64(v.AgreementSharing) / 100),
  27. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  28. Id: utils.IntToStr(v.Id),
  29. ExposureCount: utils.IntToStr(v.ExposureCount),
  30. ClickCount: utils.IntToStr(v.ClickCount),
  31. ClickRate: v.ClickRate,
  32. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v.Ecpm) / 100),
  33. Date: v.Date,
  34. MediaRevenue: utils.Float64ToStr(float64(v.MediaRevenue) / 100),
  35. SettleAmount: utils.Float64ToStr(float64(v.MediaRevenue+v.AgentRevenue+v.AgreementSharing) / 100),
  36. }
  37. tmpApplet := GetAppletInfo(c, v.AppId)
  38. if tmpApplet["platform"] != "" {
  39. tmp.Platform = tmpApplet["platform"]
  40. }
  41. if tmpApplet["name"] != "" {
  42. tmp.Name = tmpApplet["name"]
  43. }
  44. tmpSlot := GetSlotInfo(c, v.SlotId)
  45. if tmpSlot["state"] != "" {
  46. tmp.State = tmpSlot["state"]
  47. }
  48. if tmpSlot["name"] != "" {
  49. tmp.AdvName = tmpSlot["name"]
  50. }
  51. data = append(data, tmp)
  52. }
  53. }
  54. res := md.DataCenterIncomeDataRes{
  55. List: data,
  56. Total: total,
  57. State: md.AdState,
  58. Platform: md.AdPlatform,
  59. }
  60. return res
  61. }
  62. func DataCenterIncomeDataDetail(c *gin.Context, req md.DataCenterGenerateDataCommReq) {
  63. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  64. data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id))
  65. if data == nil {
  66. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  67. return
  68. }
  69. agentReward := make([]md.DataCenterGenerateDataDetailAgentRewardSecond, 0)
  70. NewGenerateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db)
  71. agent, _ := NewGenerateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(data.Id)
  72. if agent != nil {
  73. for _, v := range *agent {
  74. tmp := md.DataCenterGenerateDataDetailAgentRewardSecond{
  75. Name: "",
  76. Account: "",
  77. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  78. AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate),
  79. }
  80. tmpApplet := GetAgentInfo(c, v.AgentId)
  81. if tmpApplet["name"] != "" {
  82. tmp.Name = tmpApplet["name"]
  83. }
  84. if tmpApplet["account"] != "" {
  85. tmp.Account = tmpApplet["account"]
  86. }
  87. agentReward = append(agentReward, tmp)
  88. }
  89. }
  90. res := md.DataCenterIncomeDataDetail{
  91. MediaRevenue: utils.Float64ToStr(float64(data.MediaRevenue) / 100),
  92. AgreementSharing: utils.Float64ToStr(float64(data.AgreementSharing) / 100),
  93. AgentRevenue: utils.Float64ToStr(float64(data.AgentRevenue) / 100),
  94. DataSource: "手动同步",
  95. CreateAt: data.CreateAt,
  96. UpdateAt: data.UpdateAt,
  97. MediaRevenueRate: utils.IntToStr(data.MediaRevenueRate),
  98. AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate),
  99. AgreementSharingRate: utils.IntToStr(data.AgreementSharingRate),
  100. AgentReward: agentReward,
  101. }
  102. tmpApplet := GetAppletInfo(c, data.AppId)
  103. if tmpApplet["platform"] != "" {
  104. res.Platform = tmpApplet["platform"]
  105. }
  106. NewAppletApplicationDb := implement2.NewAppletApplicationDb(MasterDb(c))
  107. app, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(data.AppId)
  108. tmp := GetMediumInfo(c, app.MediumId)
  109. if tmp["name"] != "" {
  110. res.MediumName = tmp["name"]
  111. if tmp["account"] != "" {
  112. res.MediumName += "(" + tmp["account"] + ")"
  113. }
  114. }
  115. e.OutSuc(c, res, nil)
  116. return
  117. }
  118. func DataCenterGenerateDataList(c *gin.Context, req md.DataCenterGenerateDataReq) md.DataCenterGenerateDataRes {
  119. engine := db.Db
  120. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(engine)
  121. user := GetUser(c)
  122. appId := GetAppletIdByAdminId(c, user.AdmId, req.Name, req.Platform)
  123. slotId := GetSlotId(c, req.State)
  124. MediumList, total, _ := NewGenerateWxAdDataDb.FindGenerateWxAdDataList(c.GetString("mid"), appId, slotId, req.StartTime, req.EndTime, utils.StrToInt(req.Page), utils.StrToInt(req.Limit))
  125. data := make([]md.DataCenterGenerateDataData, 0)
  126. if len(MediumList) > 0 {
  127. NewOriginalWxAdDataDb := implement.NewOriginalWxAdDataDb(engine)
  128. for _, v := range MediumList {
  129. wxData, _ := NewOriginalWxAdDataDb.GetOriginalWxAdData(v.OriginalDataId)
  130. var tmp = md.DataCenterGenerateDataData{
  131. Id: utils.IntToStr(v.Id),
  132. ExposureCount: utils.IntToStr(v.ExposureCount),
  133. ClickCount: utils.IntToStr(v.ClickCount),
  134. ClickRate: v.ClickRate,
  135. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v.Ecpm) / 100),
  136. IsGenerateReport: utils.IntToStr(v.IsGenerateReport),
  137. Date: v.Date,
  138. PlatformRetention: utils.Float64ToStr(float64(v.PlatformRetention) / 100),
  139. CommissionRetention: utils.Float64ToStr(float64(v.CommissionRetention) / 100),
  140. PriceAdjustmentRetention: utils.Float64ToStr(float64(v.PriceAdjustmentRetention) / 100),
  141. MediaRevenue: utils.Float64ToStr(float64(v.MediaRevenue) / 100),
  142. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  143. ExtraRevenue: utils.Float64ToStr(float64(v.ExtraRevenue) / 100),
  144. AgreementSharing: utils.Float64ToStr(float64(v.AgreementSharing) / 100),
  145. AgreementSharingTotal: utils.Float64ToStr(float64(v.AgreementSharingTotal) / 100),
  146. }
  147. if wxData != nil {
  148. tmp.OldClickRate = wxData.ClickRate
  149. tmp.OldClickCount = utils.IntToStr(wxData.ClickCount)
  150. tmp.OldEcpm = utils.Float64ToStr(utils.StrToFloat64(wxData.Ecpm) / 100)
  151. tmp.OldExposureCount = utils.IntToStr(wxData.ExposureCount)
  152. }
  153. tmpApplet := GetAppletInfo(c, v.AppId)
  154. if tmpApplet["platform"] != "" {
  155. tmp.Platform = tmpApplet["platform"]
  156. }
  157. if tmpApplet["name"] != "" {
  158. tmp.Name = tmpApplet["name"]
  159. }
  160. tmpSlot := GetSlotInfo(c, v.SlotId)
  161. if tmpSlot["state"] != "" {
  162. tmp.State = tmpSlot["state"]
  163. }
  164. if tmpSlot["name"] != "" {
  165. tmp.AdvName = tmpSlot["name"]
  166. }
  167. data = append(data, tmp)
  168. }
  169. }
  170. res := md.DataCenterGenerateDataRes{
  171. List: data,
  172. Total: total,
  173. State: md.AdState,
  174. Platform: md.AdPlatform,
  175. }
  176. return res
  177. }
  178. func DataCenterGenerateDataDel(c *gin.Context, req md.DataCenterGenerateDataCommReq) error {
  179. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  180. data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id))
  181. if data == nil {
  182. return errors.New("记录不存在")
  183. }
  184. if data.IsGenerateReport == 1 {
  185. return errors.New("记录已应用,不能删除")
  186. }
  187. // 1、删除 generate_wx_ad_data 数据
  188. _, err := db.Db.Where("id=?", req.Id).Delete(&model.GenerateWxAdData{})
  189. if err != nil {
  190. return err
  191. }
  192. // 2、删除 generate_wx_ad_data_with_agent_flow 数据
  193. _, err = db.Db.Where("generate_data_id=?", req.Id).Delete(&model.GenerateWxAdDataWithAgentFlow{})
  194. if err != nil {
  195. return err
  196. }
  197. // 3、修改 original_wx_ad_data 状态值
  198. db.Db.Where("id=?", data.OriginalDataId).Cols("is_apply").Update(&model.OriginalWxAdData{IsApply: 0})
  199. return nil
  200. }
  201. func DataCenterGenerateDataDetail(c *gin.Context, req md.DataCenterGenerateDataCommReq) {
  202. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  203. data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id))
  204. if data == nil {
  205. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  206. return
  207. }
  208. agentReward := make([]md.DataCenterGenerateDataDetailAgentReward, 0)
  209. NewGenerateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db)
  210. agent, _ := NewGenerateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(data.Id)
  211. if agent != nil {
  212. for _, v := range *agent {
  213. tmp := md.DataCenterGenerateDataDetailAgentReward{
  214. Name: "",
  215. Account: "",
  216. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  217. AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate),
  218. ExtraRevenue: utils.Float64ToStr(float64(v.ExtraRevenue) / 100),
  219. ExtraRevenueRate: utils.IntToStr(data.ExtraRevenueRate),
  220. }
  221. tmpApplet := GetAgentInfo(c, v.AgentId)
  222. if tmpApplet["name"] != "" {
  223. tmp.Name = tmpApplet["name"]
  224. }
  225. if tmpApplet["account"] != "" {
  226. tmp.Account = tmpApplet["account"]
  227. }
  228. agentReward = append(agentReward, tmp)
  229. }
  230. }
  231. res := md.DataCenterGenerateDataDetailData{
  232. PlatformRetentionRate: utils.IntToStr(data.PlatformRetentionRate),
  233. CommissionRetentionRate: utils.IntToStr(data.CommissionRetentionRate),
  234. MediaRevenueRate: utils.IntToStr(data.MediaRevenueRate),
  235. AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate),
  236. ExtraRevenueRate: utils.IntToStr(data.ExtraRevenueRate),
  237. AgreementSharingRate: utils.IntToStr(data.AgreementSharingRate),
  238. AgentReward: agentReward,
  239. }
  240. e.OutSuc(c, res, nil)
  241. return
  242. }
  243. func DataCenterGenerateDataDoing(c *gin.Context, req md.DataCenterGenerateDataCommReq) error {
  244. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  245. data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id))
  246. if data == nil {
  247. return e.NewErr(400, "记录不存在")
  248. }
  249. if data.IsGenerateReport == 1 {
  250. return e.NewErr(400, "该记录已完成操作")
  251. }
  252. // 加锁 防止并发提取
  253. mutexKey := fmt.Sprintf("%s:DataCenterGenerateDataDoing:%s", c.GetString("mid"), req.Id)
  254. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  255. if err != nil {
  256. return err
  257. }
  258. if withdrawAvailable != "OK" {
  259. return e.NewErr(400000, "请求过于频繁,请稍后再试")
  260. }
  261. args := md.SettlementWxAdData{
  262. GenerateDataId: utils.StrToInt(req.Id),
  263. }
  264. err = SettlementWxAdData(args)
  265. if err != nil {
  266. return err
  267. }
  268. return nil
  269. }