广告平台(站长下代理使用)
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

svc_data_center.go 20 KiB

2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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 as gf
  434. LEFT JOIN generate_wx_ad_data AS g
  435. ON gf.generate_data_id = g.id
  436. where %s %s
  437. `
  438. user := GetUser(c)
  439. where := "g.is_generate_report=1 and gf.uuid=" + c.GetString("mid") + " and gf.agent_id =" + utils.IntToStr(user.AgentId)
  440. if req.AppId != "" || req.Platform != "" {
  441. where += " and gf.app_id in(" + appId + ")"
  442. }
  443. if req.StartDate != "" {
  444. where += " and gf.date >= '" + req.StartDate + "'"
  445. }
  446. if req.EndDate != "" {
  447. where += " and gf.date<= '" + req.EndDate + "'"
  448. }
  449. field := `%s,gf.app_id,SUM(gf.agent_revenue) as agent_revenue,SUM(gf.extra_revenue) as extra_revenue`
  450. start := (utils.StrToInt(req.Page) - 1) * utils.StrToInt(req.Limit)
  451. groupBy := " group by %s,gf.app_id order by gf.date desc,gf.id desc"
  452. if req.Page != "" {
  453. groupBy += " limit " + utils.IntToStr(start) + "," + req.Limit
  454. } else {
  455. groupBy = " group by %s,app_id order by date asc,id asc"
  456. }
  457. timeStr := "gf.date"
  458. if req.Type == "1" {
  459. timeStr = "DATE_FORMAT(date, '%Y-%m')"
  460. }
  461. if req.Type == "2" {
  462. timeStr = "DATE_FORMAT(date, '%Y')"
  463. }
  464. field = fmt.Sprintf(field, timeStr+" as date")
  465. groupBy = fmt.Sprintf(groupBy, timeStr)
  466. sql1 := fmt.Sprintf(sql, field, where, groupBy)
  467. nativeString, _ := db.QueryNativeString(db.Db, sql1)
  468. var total int64 = 0
  469. if isTotal == 1 {
  470. sqlTotal := fmt.Sprintf(sql, "1", where, "")
  471. sqlTotal1 := `select COUNT(*) as count from (%s) as tmp`
  472. sqlTotal1 = fmt.Sprintf(sqlTotal1, sqlTotal)
  473. nativeStringTotal, _ := db.QueryNativeString(db.Db, sqlTotal1)
  474. for _, v := range nativeStringTotal {
  475. total = utils.StrToInt64(v["count"])
  476. }
  477. }
  478. return nativeString, total
  479. }
  480. func commAgent(c *gin.Context, isTotal int, req md.DataCenterRecordReq) ([]map[string]string, int64) {
  481. appId := GetAppletId(c, req.AppId, req.Platform)
  482. sql := `
  483. SELECT
  484. %s
  485. FROM generate_wx_ad_data
  486. where %s %s
  487. `
  488. mediumId := GetAgentMediumId(c)
  489. where := "is_generate_report=1 and uuid=" + c.GetString("mid") + " and medium_id in(" + mediumId + ")"
  490. if req.AppId != "" || req.Platform != "" {
  491. where += " and app_id in(" + appId + ")"
  492. }
  493. if req.AdType != "" {
  494. where += " and ad_slot='" + req.AdType + "'"
  495. }
  496. if req.StartDate != "" {
  497. where += " and date>='" + req.StartDate + "'"
  498. }
  499. if req.EndDate != "" {
  500. where += " and date<='" + req.EndDate + "'"
  501. }
  502. field := `*`
  503. start := (utils.StrToInt(req.Page) - 1) * utils.StrToInt(req.Limit)
  504. groupBy := " order by date desc,id desc"
  505. if req.Page != "" {
  506. groupBy += " limit " + utils.IntToStr(start) + "," + req.Limit
  507. } else {
  508. groupBy = " order by date asc,id asc"
  509. }
  510. sql1 := fmt.Sprintf(sql, field, where, groupBy)
  511. nativeString, _ := db.QueryNativeString(db.Db, sql1)
  512. var total int64 = 0
  513. if isTotal == 1 {
  514. sqlTotal := fmt.Sprintf(sql, "COUNT(*) as count ", where, "")
  515. nativeStringTotal, _ := db.QueryNativeString(db.Db, sqlTotal)
  516. for _, v := range nativeStringTotal {
  517. total = utils.StrToInt64(v["count"])
  518. }
  519. }
  520. var ids []string
  521. for _, v := range nativeString {
  522. ids = append(ids, v["id"])
  523. }
  524. if len(ids) > 0 {
  525. sqlAgent := `
  526. SELECT
  527. SUM(agent_revenue) as agent_revenue,
  528. SUM(extra_revenue) as extra_revenue
  529. FROM generate_wx_ad_data_with_agent_flow
  530. where %s
  531. `
  532. user := GetUser(c)
  533. whereAgent := "is_generate_report=1 and original_data_id in(" + strings.Join(ids, ",") + ") and agent_id=" + utils.IntToStr(user.AgentId)
  534. sqlAgent = fmt.Sprintf(sqlAgent, whereAgent)
  535. nativeStringAgent, _ := db.QueryNativeString(db.Db, sqlAgent)
  536. agentMap := make(map[string]map[string]string)
  537. for _, v := range nativeStringAgent {
  538. _, ok := agentMap[v["uid"]]
  539. if ok == false {
  540. agentMap[v["uid"]] = make(map[string]string)
  541. }
  542. agentMap[v["uid"]] = v
  543. }
  544. for k, v := range nativeString {
  545. _, ok := agentMap[v["uid"]]
  546. if ok {
  547. nativeString[k]["commission"] = utils.Float64ToStr(utils.StrToFloat64(v["agent_revenue"]) / 100)
  548. nativeString[k]["other_commission"] = utils.Float64ToStr(utils.StrToFloat64(v["extra_revenue"]) / 100)
  549. nativeString[k]["all_commission"] = utils.Float64ToStr(utils.StrToFloat64(nativeString[k]["commission"]) + utils.StrToFloat64(nativeString[k]["other_commission"]))
  550. } else {
  551. nativeString[k]["commission"] = "0"
  552. nativeString[k]["other_commission"] = "0"
  553. nativeString[k]["all_commission"] = "0"
  554. }
  555. }
  556. }
  557. return nativeString, total
  558. }
  559. func DataCenterSelectData(c *gin.Context) {
  560. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  561. appList, _ := NewAppletApplicationDb.FindAllAppletApplicationList()
  562. appMap := make(map[string][]map[string]interface{})
  563. mediumId := GetAgentMediumId(c)
  564. for _, v := range appList {
  565. if strings.Contains(","+mediumId+",", ","+utils.IntToStr(v.MediumId)+",") == false {
  566. continue
  567. }
  568. _, ok := appMap[v.Platform]
  569. if ok == false {
  570. appMap[v.Platform] = make([]map[string]interface{}, 0)
  571. }
  572. tmp := map[string]interface{}{
  573. "name": v.Name,
  574. "app_id": v.AppId,
  575. "ad_type": enum.AdTypeList,
  576. }
  577. appMap[v.Platform] = append(appMap[v.Platform], tmp)
  578. }
  579. platform := []map[string]interface{}{
  580. {
  581. "name": "微信小程序",
  582. "platform": "wx_applet",
  583. "app_list": appMap["wx_applet"],
  584. },
  585. }
  586. e.OutSuc(c, platform, nil)
  587. return
  588. }
  589. // 应用
  590. func GetAppletId(c *gin.Context, appId, platform string) string {
  591. mediumId := ""
  592. sess := MasterDb(c).Where("1=1")
  593. var ids = make([]string, 0)
  594. if appId != "" || platform != "" {
  595. ids = append(ids, "-1")
  596. }
  597. if platform != "" {
  598. var tmp []model2.AppletApplication
  599. if platform != "" {
  600. sess.And("platform = ? ", platform)
  601. }
  602. sess.Find(&tmp)
  603. for _, v := range tmp {
  604. ids = append(ids, utils.IntToStr(v.MediumId))
  605. }
  606. }
  607. if appId != "" {
  608. ids = []string{appId}
  609. }
  610. if appId != "" || platform != "" {
  611. mediumId = strings.Join(ids, ",")
  612. }
  613. return mediumId
  614. }
  615. func GetAppletInfo(c *gin.Context, id string) map[string]string {
  616. var res = map[string]string{
  617. "platform": "",
  618. "name": "",
  619. "logo": "",
  620. }
  621. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  622. data, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(id)
  623. if data != nil {
  624. res["platform"] = data.Platform
  625. res["name"] = data.Name
  626. res["logo"] = data.Logo
  627. }
  628. return res
  629. }