广告平台(站长使用)
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

547 řádky
19 KiB

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