广告平台(站长使用)
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
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.FindGenerateWxAdDataListMedium(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.OriginalDataId)
  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. db.Db.Where("id=?", data.OriginalDataId).Cols("is_apply").Update(&model.OriginalWxAdData{IsApply: 0})
  190. return nil
  191. }
  192. func DataCenterGenerateDataDetail(c *gin.Context, req md.DataCenterGenerateDataCommReq) {
  193. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  194. data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id))
  195. if data == nil {
  196. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  197. return
  198. }
  199. agentReward := make([]md.DataCenterGenerateDataDetailAgentReward, 0)
  200. NewGenerateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db)
  201. agent, _ := NewGenerateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(data.Id)
  202. if agent != nil {
  203. for _, v := range *agent {
  204. tmp := md.DataCenterGenerateDataDetailAgentReward{
  205. Name: "",
  206. Account: "",
  207. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  208. AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate),
  209. ExtraRevenue: utils.Float64ToStr(float64(v.ExtraRevenue) / 100),
  210. ExtraRevenueRate: utils.IntToStr(data.ExtraRevenueRate),
  211. }
  212. tmpApplet := GetAgentInfo(c, v.AgentId)
  213. if tmpApplet["name"] != "" {
  214. tmp.Name = tmpApplet["name"]
  215. }
  216. if tmpApplet["account"] != "" {
  217. tmp.Account = tmpApplet["account"]
  218. }
  219. agentReward = append(agentReward, tmp)
  220. }
  221. }
  222. res := md.DataCenterGenerateDataDetailData{
  223. PlatformRetentionRate: utils.IntToStr(data.PlatformRetentionRate),
  224. CommissionRetentionRate: utils.IntToStr(data.CommissionRetentionRate),
  225. MediaRevenueRate: utils.IntToStr(data.MediaRevenueRate),
  226. AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate),
  227. ExtraRevenueRate: utils.IntToStr(data.ExtraRevenueRate),
  228. AgreementSharingRate: utils.IntToStr(data.AgreementSharingRate),
  229. AgentReward: agentReward,
  230. }
  231. e.OutSuc(c, res, nil)
  232. return
  233. }
  234. func DataCenterGenerateDataDoing(c *gin.Context, req md.DataCenterGenerateDataCommReq) error {
  235. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  236. data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id))
  237. if data == nil {
  238. return e.NewErr(400, "记录不存在")
  239. }
  240. if data.IsGenerateReport == 1 {
  241. return e.NewErr(400, "该记录已完成操作")
  242. }
  243. // 加锁 防止并发提取
  244. mutexKey := fmt.Sprintf("%s:DataCenterGenerateDataDoing:%s", c.GetString("mid"), req.Id)
  245. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  246. if err != nil {
  247. return err
  248. }
  249. if withdrawAvailable != "OK" {
  250. return e.NewErr(400000, "请求过于频繁,请稍后再试")
  251. }
  252. args := md.SettlementWxAdData{
  253. GenerateDataId: utils.StrToInt(req.Id),
  254. }
  255. err = SettlementWxAdData(args)
  256. if err != nil {
  257. return err
  258. }
  259. return nil
  260. }