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

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