广告平台(媒体使用)
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

305 rindas
8.8 KiB

  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: v["ecpm"],
  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. where += " and app_id in(" + appId + ")"
  66. }
  67. if req.AdType != "" {
  68. where += " and ad_slot='" + req.AdType + "'"
  69. }
  70. if req.StartDate != "" {
  71. where += " and date>='" + req.StartDate + "'"
  72. }
  73. if req.EndDate != "" {
  74. where += " and date<='" + req.EndDate + "'"
  75. }
  76. sql = fmt.Sprintf(sql, where)
  77. nativeString, _ := db.QueryNativeString(db.Db, sql)
  78. res := md.DataCenterTotalData{}
  79. for _, v := range nativeString {
  80. res = md.DataCenterTotalData{
  81. Date: "-",
  82. AppName: "-",
  83. PlatformName: "-",
  84. AdvName: "-",
  85. ExposureCount: v["exposure_count"],
  86. MediaRevenue: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100),
  87. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100),
  88. ClickRate: utils.GetPrec(v["click_rate"], "2") + "%",
  89. ClickCount: v["click_count"],
  90. }
  91. }
  92. return res
  93. }
  94. func DataCenterRecordList(c *gin.Context, req md.DataCenterRecordReq) md.DataCenterRecordRes {
  95. nativeString, total := comm(c, 1, req)
  96. list := make([]md.DataCenterTotalData, 0)
  97. for _, v := range nativeString {
  98. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  99. app, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(v["app_id"])
  100. platform := ""
  101. name := ""
  102. if app != nil {
  103. name = app.Name
  104. platform = app.Platform
  105. }
  106. NewAppletApplicationAdSpaceListDb := implement.NewAppletApplicationAdSpaceListDb(MasterDb(c))
  107. adData, _ := NewAppletApplicationAdSpaceListDb.GetAppletApplicationAdSpaceListByAdId(v["slot_id"])
  108. adName := enum.AdunitType(v["ad_slot"]).String()
  109. if adData != nil {
  110. adName = adData.Name
  111. }
  112. tmp := md.DataCenterTotalData{
  113. Date: v["date"],
  114. AppName: name,
  115. PlatformName: md.AppletPlatformMap[platform],
  116. AdvName: adName,
  117. ExposureCount: v["exposure_count"],
  118. MediaRevenue: utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100),
  119. Ecpm: utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100),
  120. ClickRate: v["click_rate"] + "%",
  121. ClickCount: v["click_count"],
  122. }
  123. list = append(list, tmp)
  124. }
  125. res := md.DataCenterRecordRes{
  126. List: list,
  127. Total: total,
  128. }
  129. return res
  130. }
  131. func DataCenterRecordOutPut(c *gin.Context, req md.DataCenterRecordOutPutReq) {
  132. var req1 md.DataCenterRecordReq
  133. copier.Copy(&req1, &req)
  134. req.Limit = "10000"
  135. nativeString, _ := comm(c, 0, req1)
  136. appId := make([]string, 0)
  137. adId := make([]string, 0)
  138. for _, v := range nativeString {
  139. appId = append(appId, v["app_id"])
  140. adId = append(adId, v["slot_id"])
  141. }
  142. var app []model2.AppletApplication
  143. appMap := make(map[string]model2.AppletApplication)
  144. MasterDb(c).In("app_id=?", appId).Find(&app)
  145. for _, v := range app {
  146. appMap[v.AppId] = v
  147. }
  148. var ad []model2.AppletApplicationAdSpaceList
  149. adMap := make(map[string]model2.AppletApplicationAdSpaceList)
  150. MasterDb(c).In("ad_id=?", adId).Find(&adMap)
  151. for _, v := range ad {
  152. adMap[v.AdId] = v
  153. }
  154. name := req.StartDate + "~" + req.EndDate + "(第" + req.Page + "页 " + utils.IntToStr(len(nativeString)) + "条)"
  155. //写入数据
  156. data := map[string]string{
  157. "A1": "日期",
  158. "B1": "应用名称",
  159. "C1": "平台类型",
  160. "D1": "广告位",
  161. "E1": "曝光量",
  162. "F1": "点击量",
  163. "G1": "ECPM",
  164. "H1": "预估收益",
  165. }
  166. for k, v := range nativeString {
  167. i := utils.IntToStr(k + 2)
  168. data["A"+i] = v["date"]
  169. data["B"+i] = appMap[v["app_id"]].Name
  170. data["C"+i] = md.AppletPlatformMap[appMap[v["app_id"]].Platform]
  171. data["D"+i] = adMap[v["slot_id"]].Name
  172. data["E"+i] = v["exposure_count"]
  173. data["F"+i] = v["click_count"]
  174. data["G"+i] = utils.Float64ToStr(utils.StrToFloat64(v["ecpm"]) / 100)
  175. data["H"+i] = utils.Float64ToStr(utils.StrToFloat64(v["media_revenue"]) / 100)
  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 comm(c *gin.Context, isTotal int, req md.DataCenterRecordReq) ([]map[string]string, int64) {
  187. appId := GetAppletId(c, req.AppId, req.Platform)
  188. sql := `
  189. SELECT
  190. %s
  191. FROM generate_wx_ad_data
  192. where %s %s
  193. `
  194. user := GetUser(c)
  195. where := "is_generate_report=1 and uuid=" + c.GetString("mid") + " and medium_id=" + utils.IntToStr(user.MediumId)
  196. if req.AppId != "" || req.Platform != "" {
  197. where += " and app_id in(" + appId + ")"
  198. }
  199. if req.AdType != "" {
  200. where += " and ad_slot='" + req.AdType + "'"
  201. }
  202. if req.StartDate != "" {
  203. where += " and date>='" + req.StartDate + "'"
  204. }
  205. if req.EndDate != "" {
  206. where += " and date<='" + req.EndDate + "'"
  207. }
  208. field := `*`
  209. start := (utils.StrToInt(req.Page) - 1) * utils.StrToInt(req.Limit)
  210. groupBy := " order by date desc,id desc"
  211. if req.Page != "" {
  212. groupBy += " limit " + utils.IntToStr(start) + "," + req.Limit
  213. } else {
  214. groupBy = " order by date asc,id asc"
  215. }
  216. sql1 := fmt.Sprintf(sql, field, where, groupBy)
  217. nativeString, _ := db.QueryNativeString(db.Db, sql1)
  218. var total int64 = 0
  219. if isTotal == 1 {
  220. sqlTotal := fmt.Sprintf(sql, "COUNT(*) as count ", where, "")
  221. nativeStringTotal, _ := db.QueryNativeString(db.Db, sqlTotal)
  222. for _, v := range nativeStringTotal {
  223. total = utils.StrToInt64(v["count"])
  224. }
  225. }
  226. return nativeString, total
  227. }
  228. func DataCenterSelectData(c *gin.Context) {
  229. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  230. appList, _ := NewAppletApplicationDb.FindAllAppletApplicationList()
  231. appMap := make(map[string][]map[string]interface{})
  232. user := GetUser(c)
  233. for _, v := range appList {
  234. if v.MediumId != user.MediumId {
  235. continue
  236. }
  237. _, ok := appMap[v.Platform]
  238. if ok == false {
  239. appMap[v.Platform] = make([]map[string]interface{}, 0)
  240. }
  241. tmp := map[string]interface{}{
  242. "name": v.Name,
  243. "app_id": v.AppId,
  244. "ad_type": enum.AdTypeList,
  245. }
  246. appMap[v.Platform] = append(appMap[v.Platform], tmp)
  247. }
  248. platform := []map[string]interface{}{
  249. {
  250. "name": "微信小程序",
  251. "platform": "wx_applet",
  252. "app_list": appMap["wx_applet"],
  253. },
  254. }
  255. e.OutSuc(c, platform, nil)
  256. return
  257. }
  258. // 应用
  259. func GetAppletId(c *gin.Context, appId, platform string) string {
  260. mediumId := ""
  261. sess := MasterDb(c).Where("1=1")
  262. var ids = make([]string, 0)
  263. if appId != "" || platform != "" {
  264. ids = append(ids, "-1")
  265. }
  266. if platform != "" {
  267. var tmp []model2.AppletApplication
  268. if platform != "" {
  269. sess.And("platform = ? ", platform)
  270. }
  271. sess.Find(&tmp)
  272. for _, v := range tmp {
  273. ids = append(ids, utils.IntToStr(v.MediumId))
  274. }
  275. }
  276. if appId != "" {
  277. ids = []string{appId}
  278. }
  279. if appId != "" || platform != "" {
  280. mediumId = strings.Join(ids, ",")
  281. }
  282. return mediumId
  283. }
  284. func GetAppletInfo(c *gin.Context, id string) map[string]string {
  285. var res = map[string]string{
  286. "platform": "",
  287. "name": "",
  288. "logo": "",
  289. }
  290. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  291. data, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(id)
  292. if data != nil {
  293. res["platform"] = data.Platform
  294. res["name"] = data.Name
  295. res["logo"] = data.Logo
  296. }
  297. return res
  298. }