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

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