广告平台(站长下代理使用)
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

svc_data_center.go 20 KiB

vor 2 Monaten
vor 1 Monat
vor 2 Monaten
vor 1 Monat
vor 2 Monaten
vor 1 Monat
vor 2 Monaten
vor 1 Monat
vor 2 Monaten
vor 1 Monat
vor 2 Monaten
vor 1 Monat
vor 2 Monaten
vor 1 Monat
vor 2 Monaten
vor 1 Monat
vor 2 Monaten
vor 1 Monat
vor 2 Monaten
vor 1 Monat
vor 2 Monaten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. package svc
  2. import (
  3. "applet/app/e"
  4. "applet/app/enum"
  5. "applet/app/md"
  6. "applet/app/utils"
  7. db "code.fnuoos.com/zhimeng/model.git/src"
  8. "code.fnuoos.com/zhimeng/model.git/src/implement"
  9. model2 "code.fnuoos.com/zhimeng/model.git/src/model"
  10. "fmt"
  11. "github.com/gin-gonic/gin"
  12. "github.com/jinzhu/copier"
  13. "strings"
  14. )
  15. func DataCenterRecordTotal(c *gin.Context, req md.DataCenterTableReq) md.DataCenterTotalData {
  16. appId := GetAppletId(c, req.AppId, req.Platform)
  17. sql := `
  18. SELECT
  19. SUM(exposure_count) as exposure_count,
  20. SUM(click_count) as click_count,
  21. SUM(click_count)/SUM(exposure_count)*100 as click_rate,
  22. SUM(ecpm) as ecpm,
  23. SUM(media_revenue) as media_revenue
  24. FROM generate_wx_ad_data
  25. where %s
  26. `
  27. mediumId := GetAgentMediumId(c)
  28. where := "is_generate_report=1 and uuid=" + c.GetString("mid") + " and medium_id in(" + mediumId + ")"
  29. if req.AppId != "" || req.Platform != "" {
  30. where += " and app_id in(" + appId + ")"
  31. }
  32. if req.AdType != "" {
  33. where += " and ad_slot='" + req.AdType + "'"
  34. }
  35. if req.StartDate != "" {
  36. where += " and date>='" + req.StartDate + "'"
  37. }
  38. if req.EndDate != "" {
  39. where += " and date<='" + req.EndDate + "'"
  40. }
  41. sql = fmt.Sprintf(sql, where)
  42. nativeString, _ := db.QueryNativeString(db.Db, sql)
  43. res := md.DataCenterTotalData{}
  44. for _, v := range nativeString {
  45. res = md.DataCenterTotalData{
  46. Date: "-",
  47. AppName: "-",
  48. PlatformName: "-",
  49. AdvName: "-",
  50. ExposureCount: v["exposure_count"],
  51. MediaRevenue: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100),
  52. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100),
  53. ClickRate: utils.GetPrec(v["click_rate"], "2") + "%",
  54. ClickCount: v["click_count"],
  55. }
  56. }
  57. return res
  58. }
  59. func DataCenterRecordList(c *gin.Context, req md.DataCenterRecordReq) md.DataCenterRecordRes {
  60. nativeString, total := comm(c, 1, req)
  61. list := make([]md.DataCenterTotalData, 0)
  62. for _, v := range nativeString {
  63. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  64. app, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(v["app_id"])
  65. platform := ""
  66. name := ""
  67. if app != nil {
  68. name = app.Name
  69. platform = app.Platform
  70. }
  71. NewAppletApplicationAdSpaceListDb := implement.NewAppletApplicationAdSpaceListDb(MasterDb(c))
  72. adData, _ := NewAppletApplicationAdSpaceListDb.GetAppletApplicationAdSpaceListByAdId(v["slot_id"])
  73. adName := enum.AdunitType(v["ad_slot"]).String()
  74. if adData != nil {
  75. adName = adData.Name
  76. }
  77. tmp := md.DataCenterTotalData{
  78. Date: v["date"],
  79. AppName: name,
  80. PlatformName: md.AppletPlatformMap[platform],
  81. AdvName: adName,
  82. ExposureCount: v["exposure_count"],
  83. MediaRevenue: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100),
  84. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100),
  85. ClickRate: v["click_rate"] + "%",
  86. ClickCount: v["click_count"],
  87. }
  88. list = append(list, tmp)
  89. }
  90. res := md.DataCenterRecordRes{
  91. List: list,
  92. Total: total,
  93. }
  94. return res
  95. }
  96. func DataCenterRecordOutPut(c *gin.Context, req md.DataCenterRecordOutPutReq) {
  97. var req1 md.DataCenterRecordReq
  98. copier.Copy(&req1, &req)
  99. req.Limit = "10000"
  100. nativeString, _ := comm(c, 0, req1)
  101. appId := make([]string, 0)
  102. adId := make([]string, 0)
  103. for _, v := range nativeString {
  104. appId = append(appId, v["app_id"])
  105. adId = append(adId, v["slot_id"])
  106. }
  107. var app []model2.AppletApplication
  108. appMap := make(map[string]model2.AppletApplication)
  109. MasterDb(c).In("app_id=?", appId).Find(&app)
  110. for _, v := range app {
  111. appMap[v.AppId] = v
  112. }
  113. var ad []model2.AppletApplicationAdSpaceList
  114. adMap := make(map[string]model2.AppletApplicationAdSpaceList)
  115. MasterDb(c).In("ad_id=?", adId).Find(&adMap)
  116. for _, v := range ad {
  117. adMap[v.AdId] = v
  118. }
  119. name := req.StartDate + "~" + req.EndDate + "(第" + req.Page + "页 " + utils.IntToStr(len(nativeString)) + "条)"
  120. //写入数据
  121. data := map[string]string{
  122. "A1": "日期",
  123. "B1": "应用名称",
  124. "C1": "平台类型",
  125. "D1": "广告位",
  126. "E1": "曝光量",
  127. "F1": "点击量",
  128. "G1": "ECPM",
  129. "H1": "预估收益",
  130. }
  131. for k, v := range nativeString {
  132. i := utils.IntToStr(k + 2)
  133. data["A"+i] = v["date"]
  134. data["B"+i] = appMap[v["app_id"]].Name
  135. data["C"+i] = md.AppletPlatformMap[appMap[v["app_id"]].Platform]
  136. data["D"+i] = adMap[v["slot_id"]].Name
  137. data["E"+i] = v["exposure_count"]
  138. data["F"+i] = v["click_count"]
  139. data["G"+i] = utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100)
  140. data["H"+i] = utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100)
  141. }
  142. file := utils.Output(c, name, data)
  143. filename := name + ".xlsx"
  144. r := map[string]string{
  145. "file": file,
  146. "filename": filename,
  147. }
  148. e.OutSuc(c, r, nil)
  149. return
  150. }
  151. func DataCenterCommissionRecordTotal(c *gin.Context, req md.DataCenterTableReq) md.DataCenterCommissionTotalData {
  152. appId := GetAppletId(c, req.AppId, req.Platform)
  153. sql := `
  154. SELECT
  155. SUM(exposure_count) as exposure_count,
  156. SUM(click_count) as click_count,
  157. SUM(click_count)/SUM(exposure_count)*100 as click_rate,
  158. SUM(ecpm) as ecpm,
  159. SUM(media_revenue) as media_revenue
  160. FROM generate_wx_ad_data
  161. where %s
  162. `
  163. mediumId := GetAgentMediumId(c)
  164. where := "is_generate_report=1 and uuid=" + c.GetString("mid")
  165. if req.AppId != "" || req.Platform != "" {
  166. where += " and app_id in(" + appId + ")"
  167. }
  168. if req.AdType != "" {
  169. where += " and ad_slot='" + req.AdType + "'"
  170. }
  171. if req.StartDate != "" {
  172. where += " and date>='" + req.StartDate + "'"
  173. }
  174. if req.EndDate != "" {
  175. where += " and date<='" + req.EndDate + "'"
  176. }
  177. whereMedium := where + " and medium_id in(" + mediumId + ")"
  178. sql = fmt.Sprintf(sql, whereMedium)
  179. nativeString, _ := db.QueryNativeString(db.Db, sql)
  180. res := md.DataCenterCommissionTotalData{}
  181. for _, v := range nativeString {
  182. res = md.DataCenterCommissionTotalData{
  183. Date: "-",
  184. AppName: "-",
  185. PlatformName: "-",
  186. AdvName: "-",
  187. ExposureCount: v["exposure_count"],
  188. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100),
  189. ClickRate: utils.GetPrec(v["click_rate"], "2") + "%",
  190. ClickCount: v["click_count"],
  191. AllCommission: "0",
  192. OtherCommission: "0",
  193. Commission: "0",
  194. }
  195. }
  196. sqlAgent := `
  197. SELECT
  198. SUM(agent_revenue) as agent_revenue,
  199. SUM(extra_revenue) as extra_revenue
  200. FROM generate_wx_ad_data_with_agent_flow
  201. where %s
  202. `
  203. user := GetUser(c)
  204. whereAgent := where + " and agent_id=" + utils.IntToStr(user.AgentId)
  205. sqlAgent = fmt.Sprintf(sqlAgent, whereAgent)
  206. nativeStringAgent, _ := db.QueryNativeString(db.Db, sqlAgent)
  207. for _, v := range nativeStringAgent {
  208. res.Commission = utils.Float64ToStr(utils.StrToFloat64(v["agent_revenue"]) / 100)
  209. res.OtherCommission = utils.Float64ToStr(utils.StrToFloat64(v["extra_revenue"]) / 100)
  210. res.AllCommission = utils.Float64ToStr(utils.StrToFloat64(res.Commission) + utils.StrToFloat64(res.OtherCommission))
  211. }
  212. return res
  213. }
  214. func DataCenterCommissionRecordList(c *gin.Context, req md.DataCenterRecordReq) md.DataCenterCommissionRecordRes {
  215. nativeString, total := commAgent(c, 1, req)
  216. list := make([]md.DataCenterCommissionTotalData, 0)
  217. for _, v := range nativeString {
  218. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  219. app, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(v["app_id"])
  220. platform := ""
  221. name := ""
  222. if app != nil {
  223. name = app.Name
  224. platform = app.Platform
  225. }
  226. NewAppletApplicationAdSpaceListDb := implement.NewAppletApplicationAdSpaceListDb(MasterDb(c))
  227. adData, _ := NewAppletApplicationAdSpaceListDb.GetAppletApplicationAdSpaceListByAdId(v["slot_id"])
  228. adName := enum.AdunitType(v["ad_slot"]).String()
  229. if adData != nil {
  230. adName = adData.Name
  231. }
  232. tmp := md.DataCenterCommissionTotalData{
  233. Date: v["date"],
  234. AppName: name,
  235. PlatformName: md.AppletPlatformMap[platform],
  236. AdvName: adName,
  237. ExposureCount: v["exposure_count"],
  238. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100),
  239. ClickRate: v["click_rate"] + "%",
  240. ClickCount: v["click_count"],
  241. AllCommission: v["all_commission"],
  242. OtherCommission: v["other_commission"],
  243. Commission: v["commission"],
  244. }
  245. list = append(list, tmp)
  246. }
  247. res := md.DataCenterCommissionRecordRes{
  248. List: list,
  249. Total: total,
  250. }
  251. return res
  252. }
  253. func DataCenterCommissionRecordOutPut(c *gin.Context, req md.DataCenterRecordOutPutReq) {
  254. var req1 md.DataCenterRecordReq
  255. copier.Copy(&req1, &req)
  256. req.Limit = "10000"
  257. nativeString, _ := commAgent(c, 0, req1)
  258. appId := make([]string, 0)
  259. adId := make([]string, 0)
  260. for _, v := range nativeString {
  261. appId = append(appId, v["app_id"])
  262. adId = append(adId, v["slot_id"])
  263. }
  264. var app []model2.AppletApplication
  265. appMap := make(map[string]model2.AppletApplication)
  266. MasterDb(c).In("app_id=?", appId).Find(&app)
  267. for _, v := range app {
  268. appMap[v.AppId] = v
  269. }
  270. var ad []model2.AppletApplicationAdSpaceList
  271. adMap := make(map[string]model2.AppletApplicationAdSpaceList)
  272. MasterDb(c).In("ad_id=?", adId).Find(&adMap)
  273. for _, v := range ad {
  274. adMap[v.AdId] = v
  275. }
  276. name := req.StartDate + "~" + req.EndDate + "(第" + req.Page + "页 " + utils.IntToStr(len(nativeString)) + "条)"
  277. //写入数据
  278. data := map[string]string{
  279. "A1": "日期",
  280. "B1": "应用名称",
  281. "C1": "平台类型",
  282. "D1": "广告位",
  283. "E1": "曝光量",
  284. "F1": "点击量",
  285. "G1": "ECPM",
  286. "H1": "预估佣金",
  287. "I1": "额外奖励",
  288. "J1": "预估总收益",
  289. }
  290. for k, v := range nativeString {
  291. i := utils.IntToStr(k + 2)
  292. data["A"+i] = v["date"]
  293. data["B"+i] = appMap[v["app_id"]].Name
  294. data["C"+i] = md.AppletPlatformMap[appMap[v["app_id"]].Platform]
  295. data["D"+i] = adMap[v["slot_id"]].Name
  296. data["E"+i] = v["exposure_count"]
  297. data["F"+i] = v["click_count"]
  298. data["G"+i] = utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100)
  299. data["H"+i] = v["commission"]
  300. data["I"+i] = v["other_commission"]
  301. data["J"+i] = v["all_commission"]
  302. }
  303. file := utils.Output(c, name, data)
  304. filename := name + ".xlsx"
  305. r := map[string]string{
  306. "file": file,
  307. "filename": filename,
  308. }
  309. e.OutSuc(c, r, nil)
  310. return
  311. }
  312. func DataCenterProfitRecordOutPut(c *gin.Context, req md.DataCenterProfitRecordOutPutReq) {
  313. var req1 md.DataCenterProfitRecordReq
  314. copier.Copy(&req1, &req)
  315. req.Limit = "10000"
  316. nativeString, _ := commAgentProfit(c, 0, req1)
  317. appId := make([]string, 0)
  318. for _, v := range nativeString {
  319. appId = append(appId, v["app_id"])
  320. }
  321. var app []model2.AppletApplication
  322. appMap := make(map[string]model2.AppletApplication)
  323. MasterDb(c).In("app_id=?", appId).Find(&app)
  324. for _, v := range app {
  325. appMap[v.AppId] = v
  326. }
  327. name := req.StartDate + "~" + req.EndDate + "(第" + req.Page + "页 " + utils.IntToStr(len(nativeString)) + "条)"
  328. //写入数据
  329. data := map[string]string{
  330. "A1": "日期",
  331. "B1": "应用名称",
  332. "C1": "平台类型",
  333. "D1": "预估佣金",
  334. "E1": "额外奖励",
  335. "F1": "预估总收益",
  336. }
  337. for k, v := range nativeString {
  338. i := utils.IntToStr(k + 2)
  339. data["A"+i] = v["date"]
  340. data["B"+i] = appMap[v["app_id"]].Name
  341. data["C"+i] = md.AppletPlatformMap[appMap[v["app_id"]].Platform]
  342. data["D"+i] = v["commission"]
  343. data["E"+i] = v["other_commission"]
  344. data["F"+i] = v["all_commission"]
  345. }
  346. file := utils.Output(c, name, data)
  347. filename := name + ".xlsx"
  348. r := map[string]string{
  349. "file": file,
  350. "filename": filename,
  351. }
  352. e.OutSuc(c, r, nil)
  353. return
  354. }
  355. func DataCenterProfitRecordList(c *gin.Context, req md.DataCenterProfitRecordReq) md.DataCenterProfitRecordRes {
  356. data, total := commAgentProfit(c, 1, req)
  357. list := make([]md.DataCenterProfitRecordData, 0)
  358. for _, v := range data {
  359. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  360. app, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(v["app_id"])
  361. platform := ""
  362. name := ""
  363. if app != nil {
  364. name = app.Name
  365. platform = app.Platform
  366. if platform == "wx_applet" {
  367. platform = "微信小程序"
  368. }
  369. }
  370. tmp := md.DataCenterProfitRecordData{
  371. Date: v["date"],
  372. AppName: name,
  373. PlatformName: platform,
  374. Commission: utils.Float64ToStr(utils.StrToFloat64(v["agent_revenue"]) / 100),
  375. OtherCommission: utils.Float64ToStr(utils.StrToFloat64(v["extra_revenue"]) / 100),
  376. }
  377. tmp.AllCommission = utils.Float64ToStr(utils.StrToFloat64(tmp.Commission) + utils.StrToFloat64(tmp.OtherCommission))
  378. list = append(list, tmp)
  379. }
  380. res := md.DataCenterProfitRecordRes{
  381. List: list,
  382. Total: total,
  383. }
  384. return res
  385. }
  386. func comm(c *gin.Context, isTotal int, req md.DataCenterRecordReq) ([]map[string]string, int64) {
  387. appId := GetAppletId(c, req.AppId, req.Platform)
  388. sql := `
  389. SELECT
  390. %s
  391. FROM generate_wx_ad_data
  392. where %s %s
  393. `
  394. mediumId := GetAgentMediumId(c)
  395. where := "is_generate_report=1 and uuid=" + c.GetString("mid") + " and medium_id in(" + mediumId + ")"
  396. if req.AppId != "" || req.Platform != "" {
  397. where += " and app_id in(" + appId + ")"
  398. }
  399. if req.AdType != "" {
  400. where += " and ad_slot='" + req.AdType + "'"
  401. }
  402. if req.StartDate != "" {
  403. where += " and date>='" + req.StartDate + "'"
  404. }
  405. if req.EndDate != "" {
  406. where += " and date<='" + req.EndDate + "'"
  407. }
  408. field := `*`
  409. start := (utils.StrToInt(req.Page) - 1) * utils.StrToInt(req.Limit)
  410. groupBy := " order by date desc,id desc"
  411. if req.Page != "" {
  412. groupBy += " limit " + utils.IntToStr(start) + "," + req.Limit
  413. } else {
  414. groupBy = " order by date asc,id asc"
  415. }
  416. sql1 := fmt.Sprintf(sql, field, where, groupBy)
  417. nativeString, _ := db.QueryNativeString(db.Db, sql1)
  418. var total int64 = 0
  419. if isTotal == 1 {
  420. sqlTotal := fmt.Sprintf(sql, "COUNT(*) as count ", where, "")
  421. nativeStringTotal, _ := db.QueryNativeString(db.Db, sqlTotal)
  422. for _, v := range nativeStringTotal {
  423. total = utils.StrToInt64(v["count"])
  424. }
  425. }
  426. return nativeString, total
  427. }
  428. func commAgentProfit(c *gin.Context, isTotal int, req md.DataCenterProfitRecordReq) ([]map[string]string, int64) {
  429. appId := GetAppletId(c, req.AppId, req.Platform)
  430. sql := `
  431. SELECT
  432. %s
  433. FROM generate_wx_ad_data_with_agent_flow
  434. where %s %s
  435. `
  436. user := GetUser(c)
  437. where := "is_generate_report=1 and uuid=" + c.GetString("mid") + " and agent_id =" + utils.IntToStr(user.AgentId)
  438. if req.AppId != "" || req.Platform != "" {
  439. where += " and app_id in(" + appId + ")"
  440. }
  441. if req.StartDate != "" {
  442. where += " and date>='" + req.StartDate + "'"
  443. }
  444. if req.EndDate != "" {
  445. where += " and date<='" + req.EndDate + "'"
  446. }
  447. field := `%s,app_id,SUM(agent_revenue) as agent_revenue,SUM(extra_revenue) as extra_revenue`
  448. start := (utils.StrToInt(req.Page) - 1) * utils.StrToInt(req.Limit)
  449. groupBy := " group by %s,app_id order by date desc,id desc"
  450. if req.Page != "" {
  451. groupBy += " limit " + utils.IntToStr(start) + "," + req.Limit
  452. } else {
  453. groupBy = " group by %s,app_id order by date asc,id asc"
  454. }
  455. timeStr := "date"
  456. if req.Type == "1" {
  457. timeStr = "DATE_FORMAT(date, '%Y-%m')"
  458. }
  459. if req.Type == "2" {
  460. timeStr = "DATE_FORMAT(date, '%Y')"
  461. }
  462. field = fmt.Sprintf(field, timeStr+" as date")
  463. groupBy = fmt.Sprintf(groupBy, timeStr)
  464. sql1 := fmt.Sprintf(sql, field, where, groupBy)
  465. nativeString, _ := db.QueryNativeString(db.Db, sql1)
  466. var total int64 = 0
  467. if isTotal == 1 {
  468. sqlTotal := fmt.Sprintf(sql, "1", where, "")
  469. sqlTotal1 := `select COUNT(*) as count from (%s) as tmp`
  470. sqlTotal1 = fmt.Sprintf(sqlTotal1, sqlTotal)
  471. nativeStringTotal, _ := db.QueryNativeString(db.Db, sqlTotal1)
  472. for _, v := range nativeStringTotal {
  473. total = utils.StrToInt64(v["count"])
  474. }
  475. }
  476. return nativeString, total
  477. }
  478. func commAgent(c *gin.Context, isTotal int, req md.DataCenterRecordReq) ([]map[string]string, int64) {
  479. appId := GetAppletId(c, req.AppId, req.Platform)
  480. sql := `
  481. SELECT
  482. %s
  483. FROM generate_wx_ad_data
  484. where %s %s
  485. `
  486. mediumId := GetAgentMediumId(c)
  487. where := "is_generate_report=1 and uuid=" + c.GetString("mid") + " and medium_id in(" + mediumId + ")"
  488. if req.AppId != "" || req.Platform != "" {
  489. where += " and app_id in(" + appId + ")"
  490. }
  491. if req.AdType != "" {
  492. where += " and ad_slot='" + req.AdType + "'"
  493. }
  494. if req.StartDate != "" {
  495. where += " and date>='" + req.StartDate + "'"
  496. }
  497. if req.EndDate != "" {
  498. where += " and date<='" + req.EndDate + "'"
  499. }
  500. field := `*`
  501. start := (utils.StrToInt(req.Page) - 1) * utils.StrToInt(req.Limit)
  502. groupBy := " order by date desc,id desc"
  503. if req.Page != "" {
  504. groupBy += " limit " + utils.IntToStr(start) + "," + req.Limit
  505. } else {
  506. groupBy = " order by date asc,id asc"
  507. }
  508. sql1 := fmt.Sprintf(sql, field, where, groupBy)
  509. nativeString, _ := db.QueryNativeString(db.Db, sql1)
  510. var total int64 = 0
  511. if isTotal == 1 {
  512. sqlTotal := fmt.Sprintf(sql, "COUNT(*) as count ", where, "")
  513. nativeStringTotal, _ := db.QueryNativeString(db.Db, sqlTotal)
  514. for _, v := range nativeStringTotal {
  515. total = utils.StrToInt64(v["count"])
  516. }
  517. }
  518. var ids []string
  519. for _, v := range nativeString {
  520. ids = append(ids, v["id"])
  521. }
  522. if len(ids) > 0 {
  523. sqlAgent := `
  524. SELECT
  525. SUM(agent_revenue) as agent_revenue,
  526. SUM(extra_revenue) as extra_revenue
  527. FROM generate_wx_ad_data_with_agent_flow
  528. where %s
  529. `
  530. user := GetUser(c)
  531. whereAgent := "is_generate_report=1 and original_data_id in(" + strings.Join(ids, ",") + ") and agent_id=" + utils.IntToStr(user.AgentId)
  532. sqlAgent = fmt.Sprintf(sqlAgent, whereAgent)
  533. nativeStringAgent, _ := db.QueryNativeString(db.Db, sqlAgent)
  534. agentMap := make(map[string]map[string]string)
  535. for _, v := range nativeStringAgent {
  536. _, ok := agentMap[v["uid"]]
  537. if ok == false {
  538. agentMap[v["uid"]] = make(map[string]string)
  539. }
  540. agentMap[v["uid"]] = v
  541. }
  542. for k, v := range nativeString {
  543. _, ok := agentMap[v["uid"]]
  544. if ok {
  545. nativeString[k]["commission"] = utils.Float64ToStr(utils.StrToFloat64(v["agent_revenue"]) / 100)
  546. nativeString[k]["other_commission"] = utils.Float64ToStr(utils.StrToFloat64(v["extra_revenue"]) / 100)
  547. nativeString[k]["all_commission"] = utils.Float64ToStr(utils.StrToFloat64(nativeString[k]["commission"]) + utils.StrToFloat64(nativeString[k]["other_commission"]))
  548. } else {
  549. nativeString[k]["commission"] = "0"
  550. nativeString[k]["other_commission"] = "0"
  551. nativeString[k]["all_commission"] = "0"
  552. }
  553. }
  554. }
  555. return nativeString, total
  556. }
  557. func DataCenterSelectData(c *gin.Context) {
  558. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  559. appList, _ := NewAppletApplicationDb.FindAllAppletApplicationList()
  560. appMap := make(map[string][]map[string]interface{})
  561. mediumId := GetAgentMediumId(c)
  562. for _, v := range appList {
  563. if strings.Contains(","+mediumId+",", ","+utils.IntToStr(v.MediumId)+",") == false {
  564. continue
  565. }
  566. _, ok := appMap[v.Platform]
  567. if ok == false {
  568. appMap[v.Platform] = make([]map[string]interface{}, 0)
  569. }
  570. tmp := map[string]interface{}{
  571. "name": v.Name,
  572. "app_id": v.AppId,
  573. "ad_type": enum.AdTypeList,
  574. }
  575. appMap[v.Platform] = append(appMap[v.Platform], tmp)
  576. }
  577. platform := []map[string]interface{}{
  578. {
  579. "name": "微信小程序",
  580. "platform": "wx_applet",
  581. "app_list": appMap["wx_applet"],
  582. },
  583. }
  584. e.OutSuc(c, platform, nil)
  585. return
  586. }
  587. // 应用
  588. func GetAppletId(c *gin.Context, appId, platform string) string {
  589. mediumId := ""
  590. sess := MasterDb(c).Where("1=1")
  591. var ids = make([]string, 0)
  592. if appId != "" || platform != "" {
  593. ids = append(ids, "-1")
  594. }
  595. if platform != "" {
  596. var tmp []model2.AppletApplication
  597. if platform != "" {
  598. sess.And("platform = ? ", platform)
  599. }
  600. sess.Find(&tmp)
  601. for _, v := range tmp {
  602. ids = append(ids, utils.IntToStr(v.MediumId))
  603. }
  604. }
  605. if appId != "" {
  606. ids = []string{appId}
  607. }
  608. if appId != "" || platform != "" {
  609. mediumId = strings.Join(ids, ",")
  610. }
  611. return mediumId
  612. }
  613. func GetAppletInfo(c *gin.Context, id string) map[string]string {
  614. var res = map[string]string{
  615. "platform": "",
  616. "name": "",
  617. "logo": "",
  618. }
  619. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  620. data, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(id)
  621. if data != nil {
  622. res["platform"] = data.Platform
  623. res["name"] = data.Name
  624. res["logo"] = data.Logo
  625. }
  626. return res
  627. }