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

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