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

svc_data_center_generate_data.go 10 KiB

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