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

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