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

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