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

2 months ago
3 weeks ago
3 weeks ago
3 weeks ago
2 months ago
4 weeks ago
3 weeks ago
4 weeks ago
2 months ago
1 month ago
3 weeks ago
1 month ago
2 months ago
3 weeks ago
2 months ago
4 weeks ago
1 month ago
3 weeks ago
1 month ago
3 weeks ago
1 month ago
3 weeks ago
1 month ago
1 month ago
1 month ago
2 months ago
1 month ago
3 weeks ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
2 months ago
1 month ago
1 month ago
2 months ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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. "github.com/jinzhu/copier"
  15. "strings"
  16. "time"
  17. )
  18. func DataCenterIncomeDataListForOpen(c *gin.Context, req md.DataCenterGenerateDataForOpenReq) md.DataCenterIncomeDataRes {
  19. engine := db.Db
  20. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(engine)
  21. mediumId := req.MediumId
  22. MediumList, total, _ := NewGenerateWxAdDataDb.FindGenerateWxAdDataListMedium(c.GetString("mid"), "", mediumId, "", "", 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 DataCenterIncomeDataList(c *gin.Context, req md.DataCenterGenerateDataReq) md.DataCenterIncomeDataRes {
  64. engine := db.Db
  65. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(engine)
  66. user := GetUser(c)
  67. appId := GetAppletId(c, req.Name, req.Platform, req.AppId)
  68. mediumId := GetMediumIdStr(c, user.AdmId, "", "")
  69. slotId := GetSlotId(c, req.State)
  70. _, adType := GetSlotIds(c, req.AdType)
  71. MediumList, total, _ := NewGenerateWxAdDataDb.FindGenerateWxAdDataListMedium(c.GetString("mid"), appId, mediumId, slotId, adType, req.StartTime, req.EndTime, utils.StrToInt(req.Page), utils.StrToInt(req.Limit))
  72. data := make([]md.DataCenterIncomeDataData, 0)
  73. if len(MediumList) > 0 {
  74. for _, v := range MediumList {
  75. var tmp = md.DataCenterIncomeDataData{
  76. AgreementSharing: utils.Float64ToStr(float64(v.AgreementSharing) / 100),
  77. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  78. Id: utils.IntToStr(v.Id),
  79. ExposureCount: utils.IntToStr(v.ExposureCount),
  80. ClickCount: utils.IntToStr(v.ClickCount),
  81. ClickRate: v.ClickRate,
  82. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v.Ecpm) / 100),
  83. Date: v.Date,
  84. MediaRevenue: utils.Float64ToStr(float64(v.MediaRevenue) / 100),
  85. SettleAmount: utils.Float64ToStr(float64(v.MediaRevenue+v.AgentRevenue+v.AgreementSharing) / 100),
  86. }
  87. tmpApplet := GetAppletInfo(c, v.AppId)
  88. if tmpApplet["platform"] != "" {
  89. tmp.Platform = tmpApplet["platform"]
  90. }
  91. if tmpApplet["name"] != "" {
  92. tmp.Name = tmpApplet["name"]
  93. }
  94. tmpSlot := GetSlotInfo(c, v.SlotId)
  95. if tmpSlot["state"] != "" {
  96. tmp.State = tmpSlot["state"]
  97. }
  98. if tmpSlot["name"] != "" {
  99. tmp.AdvName = tmpSlot["name"]
  100. }
  101. data = append(data, tmp)
  102. }
  103. }
  104. res := md.DataCenterIncomeDataRes{
  105. List: data,
  106. Total: total,
  107. State: md.AdState,
  108. Platform: md.AdPlatform,
  109. }
  110. return res
  111. }
  112. func DataCenterIncomeDataListOutput(c *gin.Context, req md.DataCenterGenerateDataReq) {
  113. engine := db.Db
  114. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(engine)
  115. user := GetUser(c)
  116. appId := GetAppletId(c, req.Name, req.Platform, req.AppId)
  117. mediumId := GetMediumIdStr(c, user.AdmId, "", "")
  118. slotId := GetSlotId(c, req.State)
  119. _, adType := GetSlotIds(c, req.AdType)
  120. req.Limit = "3000"
  121. MediumList, _ := NewGenerateWxAdDataDb.FindGenerateWxAdDataListMediumAll(c.GetString("mid"), appId, mediumId, slotId, adType, req.StartTime, req.EndTime, utils.StrToInt(req.Page), utils.StrToInt(req.Limit))
  122. data := map[string]string{
  123. "A1": "名称",
  124. "B1": "日期",
  125. "C1": "广告位",
  126. "D1": "曝光量",
  127. "E1": "点击率(%)",
  128. "F1": "点击量",
  129. "G1": "结算平台",
  130. "H1": "媒体-ECPM",
  131. "I1": "媒体收益(元)",
  132. "J1": "代理收益(元)",
  133. "K1": "平台收益(元)",
  134. }
  135. name := "运营报表第" + req.Page + "页"
  136. if len(MediumList) > 0 {
  137. for k, v := range MediumList {
  138. var tmp = md.DataCenterIncomeDataData{
  139. AgreementSharing: utils.Float64ToStr(float64(v.AgreementSharing) / 100),
  140. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  141. Id: utils.IntToStr(v.Id),
  142. ExposureCount: utils.IntToStr(v.ExposureCount),
  143. ClickCount: utils.IntToStr(v.ClickCount),
  144. ClickRate: v.ClickRate,
  145. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v.Ecpm) / 100),
  146. Date: v.Date,
  147. MediaRevenue: utils.Float64ToStr(float64(v.MediaRevenue) / 100),
  148. SettleAmount: utils.Float64ToStr(float64(v.MediaRevenue+v.AgentRevenue+v.AgreementSharing) / 100),
  149. }
  150. tmpApplet := GetAppletInfo(c, v.AppId)
  151. if tmpApplet["platform"] != "" {
  152. tmp.Platform = md.AdPlatformMap[tmpApplet["platform"]]
  153. }
  154. if tmpApplet["name"] != "" {
  155. tmp.Name = tmpApplet["name"]
  156. }
  157. tmpSlot := GetSlotInfo(c, v.SlotId)
  158. if tmpSlot["state"] != "" {
  159. tmp.State = tmpSlot["state"]
  160. }
  161. if tmpSlot["name"] != "" {
  162. tmp.AdvName = tmpSlot["name"]
  163. }
  164. i := utils.IntToStr(k + 2)
  165. data["A"+i] = tmp.Name
  166. data["B"+i] = tmp.Date
  167. data["C"+i] = tmp.AdvName
  168. data["D"+i] = tmp.ExposureCount
  169. data["E"+i] = tmp.ClickRate
  170. data["F"+i] = tmp.ClickCount
  171. data["G"+i] = tmp.Platform
  172. data["H"+i] = tmp.Ecpm
  173. data["I"+i] = tmp.MediaRevenue
  174. data["J"+i] = tmp.AgentRevenue
  175. data["K"+i] = tmp.AgreementSharing
  176. }
  177. }
  178. file := utils.Output(c, name, data)
  179. filename := name + ".xlsx"
  180. r := map[string]string{
  181. "file": file,
  182. "filename": filename,
  183. }
  184. e.OutSuc(c, r, nil)
  185. return
  186. }
  187. func DataCenterIncomeDataDetail(c *gin.Context, req md.DataCenterGenerateDataCommReq) {
  188. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  189. data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id))
  190. if data == nil {
  191. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  192. return
  193. }
  194. agentReward := make([]md.DataCenterGenerateDataDetailAgentRewardSecond, 0)
  195. NewGenerateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db)
  196. agent, _ := NewGenerateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(data.Id)
  197. if agent != nil {
  198. for _, v := range *agent {
  199. tmp := md.DataCenterGenerateDataDetailAgentRewardSecond{
  200. Name: "",
  201. Account: "",
  202. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  203. AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate),
  204. }
  205. tmpApplet := GetAgentInfo(c, v.AgentId)
  206. if tmpApplet["name"] != "" {
  207. tmp.Name = tmpApplet["name"]
  208. }
  209. if tmpApplet["account"] != "" {
  210. tmp.Account = tmpApplet["account"]
  211. }
  212. agentReward = append(agentReward, tmp)
  213. }
  214. }
  215. res := md.DataCenterIncomeDataDetail{
  216. MediaRevenue: utils.Float64ToStr(float64(data.MediaRevenue) / 100),
  217. AgreementSharing: utils.Float64ToStr(float64(data.AgreementSharing) / 100),
  218. AgentRevenue: utils.Float64ToStr(float64(data.AgentRevenue) / 100),
  219. DataSource: "手动同步",
  220. CreateAt: data.CreateAt,
  221. UpdateAt: data.UpdateAt,
  222. MediaRevenueRate: utils.IntToStr(data.MediaRevenueRate),
  223. AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate),
  224. AgreementSharingRate: utils.IntToStr(data.AgreementSharingRate),
  225. AgentReward: agentReward,
  226. }
  227. tmpApplet := GetAppletInfo(c, data.AppId)
  228. if tmpApplet["platform"] != "" {
  229. res.Platform = tmpApplet["platform"]
  230. }
  231. NewAppletApplicationDb := implement2.NewAppletApplicationDb(MasterDb(c))
  232. app, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(data.AppId)
  233. tmp := GetMediumInfo(c, app.MediumId)
  234. if tmp["name"] != "" {
  235. res.MediumName = tmp["name"]
  236. if tmp["account"] != "" {
  237. res.MediumName += "(" + tmp["account"] + ")"
  238. }
  239. }
  240. e.OutSuc(c, res, nil)
  241. return
  242. }
  243. func DataCenterGenerateDataList(c *gin.Context, req md.DataCenterGenerateDataReq) md.DataCenterGenerateDataRes {
  244. engine := db.Db
  245. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(engine)
  246. user := GetUser(c)
  247. appId := GetAppletId(c, req.Name, req.Platform, req.AppId)
  248. mediumId := GetMediumIdStr(c, user.AdmId, "", "")
  249. slotId := GetSlotId(c, req.State)
  250. MediumList, total, _ := NewGenerateWxAdDataDb.FindGenerateWxAdDataList(c.GetString("mid"), appId, mediumId, slotId, req.StartTime, req.EndTime, utils.StrToInt(req.Page), utils.StrToInt(req.Limit))
  251. data := make([]md.DataCenterGenerateDataData, 0)
  252. if len(MediumList) > 0 {
  253. NewOriginalWxAdDataDb := implement.NewOriginalWxAdDataDb(engine)
  254. for _, v := range MediumList {
  255. wxData, _ := NewOriginalWxAdDataDb.GetOriginalWxAdData(v.OriginalDataId)
  256. var tmp = md.DataCenterGenerateDataData{
  257. Id: utils.IntToStr(v.Id),
  258. ExposureCount: utils.IntToStr(v.ExposureCount),
  259. ClickCount: utils.IntToStr(v.ClickCount),
  260. ClickRate: v.ClickRate,
  261. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v.Ecpm) / 100),
  262. IsGenerateReport: utils.IntToStr(v.IsGenerateReport),
  263. Date: v.Date,
  264. PlatformRetention: utils.Float64ToStr(float64(v.PlatformRetention) / 100),
  265. CommissionRetention: utils.Float64ToStr(float64(v.CommissionRetention) / 100),
  266. PriceAdjustmentRetention: utils.Float64ToStr(float64(v.PriceAdjustmentRetention) / 100),
  267. MediaRevenue: utils.Float64ToStr(float64(v.MediaRevenue) / 100),
  268. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  269. ExtraRevenue: utils.Float64ToStr(float64(v.ExtraRevenue) / 100),
  270. AgreementSharing: utils.Float64ToStr(float64(v.AgreementSharing) / 100),
  271. AgreementSharingTotal: utils.Float64ToStr(float64(v.AgreementSharingTotal) / 100),
  272. }
  273. if wxData != nil {
  274. tmp.OldClickRate = wxData.ClickRate
  275. tmp.OldClickCount = utils.IntToStr(wxData.ClickCount)
  276. tmp.OldEcpm = utils.Float64ToStr(utils.StrToFloat64(wxData.Ecpm) / 100)
  277. tmp.OldExposureCount = utils.IntToStr(wxData.ExposureCount)
  278. }
  279. tmpApplet := GetAppletInfo(c, v.AppId)
  280. if tmpApplet["platform"] != "" {
  281. tmp.Platform = tmpApplet["platform"]
  282. }
  283. if tmpApplet["name"] != "" {
  284. tmp.Name = tmpApplet["name"]
  285. }
  286. tmpSlot := GetSlotInfo(c, v.SlotId)
  287. if tmpSlot["state"] != "" {
  288. tmp.State = tmpSlot["state"]
  289. }
  290. if tmpSlot["name"] != "" {
  291. tmp.AdvName = tmpSlot["name"]
  292. }
  293. data = append(data, tmp)
  294. }
  295. }
  296. res := md.DataCenterGenerateDataRes{
  297. List: data,
  298. Total: total,
  299. State: md.AdState,
  300. Platform: md.AdPlatform,
  301. }
  302. return res
  303. }
  304. func DataCenterGenerateDataDel(c *gin.Context, req md.DataCenterGenerateDataCommReq) error {
  305. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  306. data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id))
  307. if data == nil {
  308. return errors.New("记录不存在")
  309. }
  310. if data.IsGenerateReport == 1 {
  311. return errors.New("记录已应用,不能删除")
  312. }
  313. // 1、删除 generate_wx_ad_data 数据
  314. _, err := db.Db.Where("id=?", req.Id).Delete(&model.GenerateWxAdData{})
  315. if err != nil {
  316. return err
  317. }
  318. // 2、删除 generate_wx_ad_data_with_agent_flow 数据
  319. _, err = db.Db.Where("generate_data_id=?", req.Id).Delete(&model.GenerateWxAdDataWithAgentFlow{})
  320. if err != nil {
  321. return err
  322. }
  323. // 3、修改 original_wx_ad_data 状态值
  324. db.Db.Where("id=?", data.OriginalDataId).Cols("is_apply").Update(&model.OriginalWxAdData{IsApply: 0})
  325. return nil
  326. }
  327. func DataCenterGenerateDataDetail(c *gin.Context, req md.DataCenterGenerateDataCommReq) {
  328. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  329. data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id))
  330. if data == nil {
  331. e.OutErr(c, 400, e.NewErr(400, "记录不存在"))
  332. return
  333. }
  334. agentReward := make([]md.DataCenterGenerateDataDetailAgentReward, 0)
  335. NewGenerateWxAdDataWithAgentFlowDb := implement.NewGenerateWxAdDataWithAgentFlowDb(db.Db)
  336. agent, _ := NewGenerateWxAdDataWithAgentFlowDb.FindGenerateWxAdDataWithAgentFlowByStrategyId(data.Id)
  337. if agent != nil {
  338. for _, v := range *agent {
  339. tmp := md.DataCenterGenerateDataDetailAgentReward{
  340. Name: "",
  341. Account: "",
  342. AgentRevenue: utils.Float64ToStr(float64(v.AgentRevenue) / 100),
  343. AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate),
  344. ExtraRevenue: utils.Float64ToStr(float64(v.ExtraRevenue) / 100),
  345. ExtraRevenueRate: utils.IntToStr(data.ExtraRevenueRate),
  346. }
  347. tmpApplet := GetAgentInfo(c, v.AgentId)
  348. if tmpApplet["name"] != "" {
  349. tmp.Name = tmpApplet["name"]
  350. }
  351. if tmpApplet["account"] != "" {
  352. tmp.Account = tmpApplet["account"]
  353. }
  354. agentReward = append(agentReward, tmp)
  355. }
  356. }
  357. res := md.DataCenterGenerateDataDetailData{
  358. PlatformRetentionRate: utils.IntToStr(data.PlatformRetentionRate),
  359. CommissionRetentionRate: utils.IntToStr(data.CommissionRetentionRate),
  360. MediaRevenueRate: utils.IntToStr(data.MediaRevenueRate),
  361. AgentRevenueRate: utils.IntToStr(data.AgentRevenueRate),
  362. ExtraRevenueRate: utils.IntToStr(data.ExtraRevenueRate),
  363. AgreementSharingRate: utils.IntToStr(data.AgreementSharingRate),
  364. AgentReward: agentReward,
  365. }
  366. e.OutSuc(c, res, nil)
  367. return
  368. }
  369. func DataCenterGenerateDataDoing(c *gin.Context, req md.DataCenterGenerateDataCommReq) error {
  370. NewGenerateWxAdDataDb := implement.NewGenerateWxAdDataDb(db.Db)
  371. data, _ := NewGenerateWxAdDataDb.GetGenerateWxAdData(utils.StrToInt(req.Id))
  372. if data == nil {
  373. return e.NewErr(400, "记录不存在")
  374. }
  375. if data.IsGenerateReport == 1 {
  376. return e.NewErr(400, "该记录已完成操作")
  377. }
  378. // 加锁 防止并发提取
  379. mutexKey := fmt.Sprintf("%s:DataCenterGenerateDataDoing:%s", c.GetString("mid"), req.Id)
  380. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  381. if err != nil {
  382. return err
  383. }
  384. if withdrawAvailable != "OK" {
  385. return e.NewErr(400000, "请求过于频繁,请稍后再试")
  386. }
  387. args := md.SettlementWxAdData{
  388. GenerateDataId: utils.StrToInt(req.Id),
  389. }
  390. err = SettlementWxAdData(args)
  391. if err != nil {
  392. return err
  393. }
  394. return nil
  395. }
  396. func DataCenterSelectData(c *gin.Context) {
  397. NewAppletApplicationDb := implement2.NewAppletApplicationDb(MasterDb(c))
  398. appList, _ := NewAppletApplicationDb.FindAllAppletApplicationList()
  399. appMap := make(map[string][]map[string]interface{})
  400. user := GetUser(c)
  401. NewAdminBindMediumDb := implement2.NewAdminBindMediumDb(MasterDb(c))
  402. list := NewAdminBindMediumDb.FindAll(user.AdmId)
  403. ids := make([]string, 0)
  404. for _, v := range list {
  405. ids = append(ids, utils.IntToStr(v.MediumId))
  406. }
  407. for _, v := range appList {
  408. if user.IsSuperAdministrator != 1 {
  409. if utils.InArr(utils.IntToStr(v.MediumId), ids) == false && len(ids) > 0 {
  410. continue
  411. }
  412. if len(ids) == 0 {
  413. if user.ShowTime == "" {
  414. continue
  415. } else {
  416. showTime := utils.TimeStdParseUnix(user.ShowTime + " 00:00:00")
  417. if v.CreateAt.Unix() < showTime {
  418. continue
  419. }
  420. }
  421. }
  422. }
  423. _, ok := appMap[v.Platform]
  424. if ok == false {
  425. appMap[v.Platform] = make([]map[string]interface{}, 0)
  426. }
  427. NewAppletApplicationAdSpaceListDb := implement2.NewAppletApplicationAdSpaceListDb(MasterDb(c))
  428. adList, _ := NewAppletApplicationAdSpaceListDb.FindAppletApplicationAdSpaceListByAppidIds(v.AppId)
  429. adData := make([]md.SelectData, 0)
  430. if adList != nil {
  431. for _, v1 := range *adList {
  432. tmp1 := md.SelectData{
  433. Name: v1.Name,
  434. Value: v1.AdId,
  435. }
  436. adData = append(adData, tmp1)
  437. }
  438. }
  439. tmp := map[string]interface{}{
  440. "name": v.Name,
  441. "app_id": v.AppId,
  442. "ad_type": adData,
  443. }
  444. appMap[v.Platform] = append(appMap[v.Platform], tmp)
  445. }
  446. platform := []map[string]interface{}{
  447. {
  448. "name": "微信小程序",
  449. "platform": "wx_applet",
  450. "app_list": appMap["wx_applet"],
  451. },
  452. }
  453. e.OutSuc(c, platform, nil)
  454. return
  455. }
  456. func DataCenterTable(c *gin.Context, req md.DataCenterTableReq) md.DataCenterTableRes {
  457. var req1 md.DataCenterRecordReq
  458. copier.Copy(&req1, &req)
  459. nativeString, _ := comm(c, 0, req1)
  460. list := make([]md.DataCenterTableData, 0)
  461. tmpMap := make(map[string]md.DataCenterTableData)
  462. for _, v := range nativeString {
  463. tmp := md.DataCenterTableData{
  464. Date: v["date"],
  465. ExposureCount: v["exposure_count"],
  466. MediaRevenue: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100),
  467. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100),
  468. }
  469. tmpMap[v["date"]] = tmp
  470. }
  471. day := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00") - utils.TimeStdParseUnix(req.StartDate+" 00:00:00")) / 86400
  472. for i := day; i >= 0; i-- {
  473. date := utils.TimeStdParseUnix(req.EndDate+" 00:00:00") - i*86400
  474. tmp, ok := tmpMap[time.Unix(date, 0).Format("2006-01-02")]
  475. if ok == false {
  476. tmp = md.DataCenterTableData{
  477. Date: time.Unix(date, 0).Format("2006-01-02"),
  478. ExposureCount: "0",
  479. MediaRevenue: "0",
  480. Ecpm: "0",
  481. }
  482. }
  483. list = append(list, tmp)
  484. }
  485. res := md.DataCenterTableRes{
  486. List: list,
  487. }
  488. return res
  489. }
  490. func comm(c *gin.Context, isTotal int, req md.DataCenterRecordReq) ([]map[string]string, int64) {
  491. appId := GetAppletId(c, req.Name, req.Platform, req.AppId)
  492. sql := `
  493. SELECT
  494. %s
  495. FROM generate_wx_ad_data
  496. where %s %s
  497. `
  498. where := "is_generate_report=1 and uuid=" + c.GetString("mid")
  499. user := GetUser(c)
  500. mediumId := GetMediumIdStr(c, user.AdmId, "", "")
  501. if mediumId != "" {
  502. ex := strings.Split(mediumId, ",")
  503. str := ""
  504. for _, v := range ex {
  505. if str == "" {
  506. str += "'" + v + "'"
  507. } else {
  508. str += ",'" + v + "'"
  509. }
  510. }
  511. where += " and medium_id in(" + str + ")"
  512. }
  513. if req.AppId != "" || req.Platform != "" {
  514. ex := strings.Split(appId, ",")
  515. str := ""
  516. for _, v := range ex {
  517. if str == "" {
  518. str += "'" + v + "'"
  519. } else {
  520. str += ",'" + v + "'"
  521. }
  522. }
  523. where += " and app_id in(" + str + ")"
  524. }
  525. if req.AdType != "" {
  526. where += " and slot_id in('" + req.AdType + "')"
  527. }
  528. if req.StartDate != "" {
  529. where += " and date>='" + req.StartDate + "'"
  530. }
  531. if req.EndDate != "" {
  532. where += " and date<='" + req.EndDate + "'"
  533. }
  534. field := `*`
  535. start := (utils.StrToInt(req.Page) - 1) * utils.StrToInt(req.Limit)
  536. groupBy := " order by date desc,id desc"
  537. if req.Page != "" {
  538. groupBy += " limit " + utils.IntToStr(start) + "," + req.Limit
  539. } else {
  540. field = "SUM(ecpm) AS ecpm,SUM(media_revenue) AS media_revenue,SUM(exposure_count) AS exposure_count,date"
  541. groupBy = " group by date order by date asc,id asc"
  542. }
  543. sql1 := fmt.Sprintf(sql, field, where, groupBy)
  544. nativeString, _ := db.QueryNativeString(db.Db, sql1)
  545. var total int64 = 0
  546. if isTotal == 1 {
  547. sqlTotal := fmt.Sprintf(sql, "COUNT(*) as count ", where, "")
  548. nativeStringTotal, _ := db.QueryNativeString(db.Db, sqlTotal)
  549. for _, v := range nativeStringTotal {
  550. total = utils.StrToInt64(v["count"])
  551. }
  552. }
  553. return nativeString, total
  554. }