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

svc_data_center.go 9.1 KiB

3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
1ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
2ヶ月前
3ヶ月前
1ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
3ヶ月前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. 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 ad_slot='" + 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 ad_slot='" + 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. groupBy = " order by date asc,id asc"
  233. }
  234. sql1 := fmt.Sprintf(sql, field, where, groupBy)
  235. nativeString, _ := db.QueryNativeString(db.Db, sql1)
  236. var total int64 = 0
  237. if isTotal == 1 {
  238. sqlTotal := fmt.Sprintf(sql, "COUNT(*) as count ", where, "")
  239. nativeStringTotal, _ := db.QueryNativeString(db.Db, sqlTotal)
  240. for _, v := range nativeStringTotal {
  241. total = utils.StrToInt64(v["count"])
  242. }
  243. }
  244. return nativeString, total
  245. }
  246. func DataCenterSelectData(c *gin.Context) {
  247. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  248. appList, _ := NewAppletApplicationDb.FindAllAppletApplicationList()
  249. appMap := make(map[string][]map[string]interface{})
  250. user := GetUser(c)
  251. for _, v := range appList {
  252. if v.MediumId != user.MediumId {
  253. continue
  254. }
  255. _, ok := appMap[v.Platform]
  256. if ok == false {
  257. appMap[v.Platform] = make([]map[string]interface{}, 0)
  258. }
  259. tmp := map[string]interface{}{
  260. "name": v.Name,
  261. "app_id": v.AppId,
  262. "ad_type": enum.AdTypeList,
  263. }
  264. appMap[v.Platform] = append(appMap[v.Platform], tmp)
  265. }
  266. platform := []map[string]interface{}{
  267. {
  268. "name": "微信小程序",
  269. "platform": "wx_applet",
  270. "app_list": appMap["wx_applet"],
  271. },
  272. }
  273. e.OutSuc(c, platform, nil)
  274. return
  275. }
  276. // 应用
  277. func GetAppletId(c *gin.Context, appId, platform string) string {
  278. mediumId := ""
  279. sess := MasterDb(c).Where("1=1")
  280. var ids = make([]string, 0)
  281. if appId != "" || platform != "" {
  282. ids = append(ids, "-1")
  283. }
  284. if platform != "" {
  285. var tmp []model2.AppletApplication
  286. if platform != "" {
  287. sess.And("platform = ? ", platform)
  288. }
  289. sess.Find(&tmp)
  290. for _, v := range tmp {
  291. ids = append(ids, utils.IntToStr(v.MediumId))
  292. }
  293. }
  294. if appId != "" {
  295. ids = []string{appId}
  296. }
  297. if appId != "" || platform != "" {
  298. mediumId = strings.Join(ids, ",")
  299. }
  300. return mediumId
  301. }
  302. func GetAppletInfo(c *gin.Context, id string) map[string]string {
  303. var res = map[string]string{
  304. "platform": "",
  305. "name": "",
  306. "logo": "",
  307. }
  308. NewAppletApplicationDb := implement.NewAppletApplicationDb(MasterDb(c))
  309. data, _ := NewAppletApplicationDb.GetAppletApplicationListByAppid(id)
  310. if data != nil {
  311. res["platform"] = data.Platform
  312. res["name"] = data.Name
  313. res["logo"] = data.Logo
  314. }
  315. return res
  316. }