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

svc_data_center.go 9.7 KiB

2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2週間前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
2週間前
2ヶ月前
2週間前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2週間前
2ヶ月前
2ヶ月前
2週間前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
1ヶ月前
2ヶ月前
2週間前
2ヶ月前
2週間前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2週間前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2ヶ月前
2週間前
2ヶ月前
2週間前
2ヶ月前
2ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. "time"
  15. )
  16. func DataCenterTable(c *gin.Context, req md.DataCenterTableReq) md.DataCenterTableRes {
  17. var req1 md.DataCenterRecordReq
  18. copier.Copy(&req1, &req)
  19. nativeString, _ := comm(c, 0, req1)
  20. list := make([]md.DataCenterTableData, 0)
  21. tmpMap := make(map[string]md.DataCenterTableData)
  22. for _, v := range nativeString {
  23. tmp := md.DataCenterTableData{
  24. Date: v["date"],
  25. ExposureCount: v["exposure_count"],
  26. MediaRevenue: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100),
  27. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100),
  28. }
  29. tmpMap[v["date"]] = tmp
  30. }
  31. day := (utils.TimeStdParseUnix(req.EndDate+" 00:00:00") - utils.TimeStdParseUnix(req.StartDate+" 00:00:00")) / 86400
  32. for i := day; i >= 0; i-- {
  33. date := utils.TimeStdParseUnix(req.EndDate+" 00:00:00") - i*86400
  34. tmp, ok := tmpMap[time.Unix(date, 0).Format("2006-01-02")]
  35. if ok == false {
  36. tmp = md.DataCenterTableData{
  37. Date: time.Unix(date, 0).Format("2006-01-02"),
  38. ExposureCount: "0",
  39. MediaRevenue: "0",
  40. Ecpm: "0",
  41. }
  42. }
  43. list = append(list, tmp)
  44. }
  45. res := md.DataCenterTableRes{
  46. List: list,
  47. }
  48. return res
  49. }
  50. func DataCenterRecordTotal(c *gin.Context, req md.DataCenterTableReq) md.DataCenterTotalData {
  51. appId := GetAppletId(c, req.AppId, req.Platform)
  52. sql := `
  53. SELECT
  54. SUM(exposure_count) as exposure_count,
  55. SUM(click_count) as click_count,
  56. SUM(click_count)/SUM(exposure_count)*100 as click_rate,
  57. SUM(ecpm) as ecpm,
  58. SUM(media_revenue) as media_revenue
  59. FROM generate_wx_ad_data
  60. where %s
  61. `
  62. user := GetUser(c)
  63. where := "is_generate_report=1 and uuid=" + c.GetString("mid") + " and medium_id=" + utils.IntToStr(user.MediumId)
  64. if req.AppId != "" || req.Platform != "" {
  65. ex := strings.Split(appId, ",")
  66. str := ""
  67. for _, v := range ex {
  68. if str == "" {
  69. str += "'" + v + "'"
  70. } else {
  71. str += ",'" + v + "'"
  72. }
  73. }
  74. where += " and app_id in(" + str + ")"
  75. }
  76. if req.AdType != "" {
  77. where += " and slot_id in('" + req.AdType + "')"
  78. }
  79. if req.StartDate != "" {
  80. where += " and date>='" + req.StartDate + "'"
  81. }
  82. if req.EndDate != "" {
  83. where += " and date<='" + req.EndDate + "'"
  84. }
  85. sql = fmt.Sprintf(sql, where)
  86. nativeString, _ := db.QueryNativeString(db.Db, sql)
  87. res := md.DataCenterTotalData{}
  88. for _, v := range nativeString {
  89. res = md.DataCenterTotalData{
  90. Date: "-",
  91. AppName: "-",
  92. PlatformName: "-",
  93. AdvName: "-",
  94. ExposureCount: v["exposure_count"],
  95. MediaRevenue: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100),
  96. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100),
  97. ClickRate: utils.GetPrec(v["click_rate"], "2") + "%",
  98. ClickCount: v["click_count"],
  99. }
  100. }
  101. return res
  102. }
  103. func DataCenterRecordList(c *gin.Context, req md.DataCenterRecordReq) md.DataCenterRecordRes {
  104. nativeString, total := comm(c, 1, req)
  105. list := make([]md.DataCenterTotalData, 0)
  106. for _, v := range nativeString {
  107. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  108. app, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(v["app_id"])
  109. platform := ""
  110. name := ""
  111. if app != nil {
  112. name = app.Name
  113. platform = app.Platform
  114. }
  115. NewAppletApplicationAdSpaceListDb := implement.NewAppletApplicationAdSpaceListDb(MasterDb(c))
  116. adData, _ := NewAppletApplicationAdSpaceListDb.GetAppletApplicationAdSpaceListByAdId(v["slot_id"])
  117. adName := enum.AdunitType(v["ad_slot"]).String()
  118. if adData != nil {
  119. adName = adData.Name
  120. }
  121. tmp := md.DataCenterTotalData{
  122. Date: v["date"],
  123. AppName: name,
  124. PlatformName: md.AppletPlatformMap[platform],
  125. AdvName: adName,
  126. ExposureCount: v["exposure_count"],
  127. MediaRevenue: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100),
  128. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100),
  129. ClickRate: v["click_rate"] + "%",
  130. ClickCount: v["click_count"],
  131. }
  132. list = append(list, tmp)
  133. }
  134. res := md.DataCenterRecordRes{
  135. List: list,
  136. Total: total,
  137. }
  138. return res
  139. }
  140. func DataCenterRecordOutPut(c *gin.Context, req md.DataCenterRecordOutPutReq) {
  141. var req1 md.DataCenterRecordReq
  142. copier.Copy(&req1, &req)
  143. req.Limit = "10000"
  144. nativeString, _ := comm(c, 0, req1)
  145. appId := make([]string, 0)
  146. adId := make([]string, 0)
  147. for _, v := range nativeString {
  148. appId = append(appId, v["app_id"])
  149. adId = append(adId, v["slot_id"])
  150. }
  151. var app []model2.AppletApplication
  152. appMap := make(map[string]model2.AppletApplication)
  153. MasterDb(c).In("app_id", appId).Find(&app)
  154. for _, v := range app {
  155. appMap[v.AppId] = v
  156. }
  157. var ad []model2.AppletApplicationAdSpaceList
  158. adMap := make(map[string]model2.AppletApplicationAdSpaceList)
  159. MasterDb(c).In("ad_id", adId).Find(&adMap)
  160. for _, v := range ad {
  161. adMap[v.AdId] = v
  162. }
  163. name := req.StartDate + "~" + req.EndDate + "(第" + req.Page + "页 " + utils.IntToStr(len(nativeString)) + "条)"
  164. //写入数据
  165. data := map[string]string{
  166. "A1": "日期",
  167. "B1": "应用名称",
  168. "C1": "平台类型",
  169. "D1": "广告位",
  170. "E1": "曝光量",
  171. "F1": "点击量",
  172. "G1": "ECPM",
  173. "H1": "预估收益",
  174. }
  175. for k, v := range nativeString {
  176. i := utils.IntToStr(k + 2)
  177. data["A"+i] = v["date"]
  178. data["B"+i] = appMap[v["app_id"]].Name
  179. data["C"+i] = md.AppletPlatformMap[appMap[v["app_id"]].Platform]
  180. data["D"+i] = adMap[v["slot_id"]].Name
  181. data["E"+i] = v["exposure_count"]
  182. data["F"+i] = v["click_count"]
  183. data["G"+i] = utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100)
  184. data["H"+i] = utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100)
  185. }
  186. file := utils.Output(c, name, data)
  187. filename := name + ".xlsx"
  188. r := map[string]string{
  189. "file": file,
  190. "filename": filename,
  191. }
  192. e.OutSuc(c, r, nil)
  193. return
  194. }
  195. func comm(c *gin.Context, isTotal int, req md.DataCenterRecordReq) ([]map[string]string, int64) {
  196. appId := GetAppletId(c, req.AppId, req.Platform)
  197. sql := `
  198. SELECT
  199. %s
  200. FROM generate_wx_ad_data
  201. where %s %s
  202. `
  203. user := GetUser(c)
  204. where := "is_generate_report=1 and uuid=" + c.GetString("mid") + " and medium_id=" + utils.IntToStr(user.MediumId)
  205. if req.AppId != "" || req.Platform != "" {
  206. ex := strings.Split(appId, ",")
  207. str := ""
  208. for _, v := range ex {
  209. if str == "" {
  210. str += "'" + v + "'"
  211. } else {
  212. str += ",'" + v + "'"
  213. }
  214. }
  215. where += " and app_id in(" + str + ")"
  216. }
  217. if req.AdType != "" {
  218. where += " and slot_id in('" + req.AdType + "')"
  219. }
  220. if req.StartDate != "" {
  221. where += " and date>='" + req.StartDate + "'"
  222. }
  223. if req.EndDate != "" {
  224. where += " and date<='" + req.EndDate + "'"
  225. }
  226. field := `*`
  227. start := (utils.StrToInt(req.Page) - 1) * utils.StrToInt(req.Limit)
  228. groupBy := " order by date desc,id desc"
  229. if req.Page != "" {
  230. groupBy += " limit " + utils.IntToStr(start) + "," + req.Limit
  231. } else {
  232. field = "SUM(ecpm) AS ecpm,SUM(media_revenue) AS media_revenue,SUM(exposure_count) AS exposure_count,date"
  233. groupBy = " group by date order by date asc,id asc"
  234. }
  235. sql1 := fmt.Sprintf(sql, field, where, groupBy)
  236. nativeString, _ := db.QueryNativeString(db.Db, sql1)
  237. var total int64 = 0
  238. if isTotal == 1 {
  239. sqlTotal := fmt.Sprintf(sql, "COUNT(*) as count ", where, "")
  240. nativeStringTotal, _ := db.QueryNativeString(db.Db, sqlTotal)
  241. for _, v := range nativeStringTotal {
  242. total = utils.StrToInt64(v["count"])
  243. }
  244. }
  245. return nativeString, total
  246. }
  247. func DataCenterSelectData(c *gin.Context) {
  248. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  249. appList, _ := NewAppletApplicationDb.FindAllAppletApplicationList()
  250. appMap := make(map[string][]map[string]interface{})
  251. user := GetUser(c)
  252. for _, v := range appList {
  253. if v.MediumId != user.MediumId {
  254. continue
  255. }
  256. _, ok := appMap[v.Platform]
  257. if ok == false {
  258. appMap[v.Platform] = make([]map[string]interface{}, 0)
  259. }
  260. NewAppletApplicationAdSpaceListDb := implement.NewAppletApplicationAdSpaceListDb(MasterDb(c))
  261. adList, _ := NewAppletApplicationAdSpaceListDb.FindAppletApplicationAdSpaceListByAppidIds(v.AppId)
  262. adData := make([]md.SelectData, 0)
  263. if adList != nil {
  264. for _, v1 := range *adList {
  265. tmp1 := md.SelectData{
  266. Name: v1.Name,
  267. Value: v1.AdId,
  268. }
  269. adData = append(adData, tmp1)
  270. }
  271. }
  272. tmp := map[string]interface{}{
  273. "name": v.Name,
  274. "app_id": v.AppId,
  275. "ad_type": adData,
  276. }
  277. appMap[v.Platform] = append(appMap[v.Platform], tmp)
  278. }
  279. platform := []map[string]interface{}{
  280. {
  281. "name": "微信小程序",
  282. "platform": "wx_applet",
  283. "app_list": appMap["wx_applet"],
  284. },
  285. }
  286. e.OutSuc(c, platform, nil)
  287. return
  288. }
  289. // 应用
  290. func GetAppletId(c *gin.Context, appId, platform string) string {
  291. mediumId := ""
  292. sess := MasterDb(c).Where("1=1")
  293. var ids = make([]string, 0)
  294. if appId != "" || platform != "" {
  295. ids = append(ids, "-1")
  296. }
  297. if platform != "" {
  298. var tmp []model2.AppletApplication
  299. if platform != "" {
  300. sess.And("platform = ? ", platform)
  301. }
  302. sess.Find(&tmp)
  303. for _, v := range tmp {
  304. ids = append(ids, utils.IntToStr(v.MediumId))
  305. }
  306. }
  307. if appId != "" {
  308. ids = []string{appId}
  309. }
  310. if appId != "" || platform != "" {
  311. mediumId = strings.Join(ids, ",")
  312. }
  313. return mediumId
  314. }
  315. func GetAppletInfo(c *gin.Context, id string) map[string]string {
  316. var res = map[string]string{
  317. "platform": "",
  318. "name": "",
  319. "logo": "",
  320. }
  321. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  322. data, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(id)
  323. if data != nil {
  324. res["platform"] = data.Platform
  325. res["name"] = data.Name
  326. res["logo"] = data.Logo
  327. }
  328. return res
  329. }