蛋蛋星球 后台端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

159 lines
5.2 KiB

  1. package hdl
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/utils"
  6. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  7. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "strings"
  11. "time"
  12. )
  13. // GetTotalData
  14. // @Summary 首页-首页-数据总览
  15. // @Tags 首页
  16. // @Description 数据总览
  17. // @Accept json
  18. // @Produce json
  19. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  20. // @Param year query string true "年"
  21. // @Param month query string true "月"
  22. // @Success 200 {object} model.PlatformTotalData "具体数据"
  23. // @Failure 400 {object} md.Response "具体错误"
  24. // @Router /api/homePage/totalData [GET]
  25. func GetTotalData(c *gin.Context) {
  26. year := c.Query("year")
  27. month := c.Query("month")
  28. if year == "" || month == "" {
  29. nowStr := time.Now().Format("2006-01-02")
  30. year = strings.Split(nowStr, "-")[0]
  31. month = strings.Split(nowStr, "-")[1]
  32. }
  33. dataDb := implement.NewPlatformTotalDataDb(db.Db)
  34. data, err := dataDb.PlatformTotalDataGetOneByTime(year, month)
  35. if err != nil {
  36. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  37. return
  38. }
  39. e.OutSuc(c, data, nil)
  40. }
  41. type GetActiveDataResp struct {
  42. Today *model.PlatformActiveData `json:"today"` // 今日数据
  43. Yesterday *model.PlatformActiveData `json:"yesterday"` // 昨日数据
  44. }
  45. // GetActiveData
  46. // @Summary 首页-首页-活跃数据
  47. // @Tags 首页
  48. // @Description 活跃数据
  49. // @Accept json
  50. // @Produce json
  51. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  52. // @Success 200 {object} GetActiveDataResp "具体数据"
  53. // @Failure 400 {object} md.Response "具体错误"
  54. // @Router /api/homePage/activeData [GET]
  55. func GetActiveData(c *gin.Context) {
  56. nowStr := time.Now().Format("2006-01-02")
  57. yesterdayStr := time.Now().AddDate(0, 0, -1).Format("2006-01-02")
  58. activityDb := implement.NewPlatformActiveDataDb(db.Db)
  59. today, err := activityDb.PlatformActiveDataGetOneByParams(map[string]interface{}{"key": "date", "value": nowStr})
  60. if err != nil {
  61. e.OutErr(c, e.ERR_DB_ORM, err)
  62. }
  63. yesterday, err := activityDb.PlatformActiveDataGetOneByParams(map[string]interface{}{"key": "date", "value": yesterdayStr})
  64. if err != nil {
  65. e.OutErr(c, e.ERR_DB_ORM, err)
  66. }
  67. resp := GetActiveDataResp{
  68. Today: today,
  69. Yesterday: yesterday,
  70. }
  71. e.OutSuc(c, resp, nil)
  72. }
  73. // GetGrowData
  74. // @Summary 首页-首页-用户增长曲线
  75. // @Tags 首页
  76. // @Description 用户增长曲线
  77. // @Accept json
  78. // @Produce json
  79. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  80. // @Param kind query string false "1:按天 2:按周 3:按小时"
  81. // @Success 200 {object} md.GetPriceCurveResp "具体数据"
  82. // @Failure 400 {object} md.Response "具体错误"
  83. // @Router /api/homePage/growData [GET]
  84. func GetGrowData(c *gin.Context) {
  85. kind := c.DefaultQuery("kind", "1")
  86. now := time.Now()
  87. dataDb := implement.NewPlatformGrowDataDb(db.Db)
  88. m, has, err := dataDb.PlatformGrowDataGetLastOne()
  89. if err != nil {
  90. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  91. return
  92. }
  93. if !has {
  94. e.OutErr(c, e.ERR_NO_DATA, "未查询到数据")
  95. return
  96. }
  97. var yData []interface{}
  98. var xData []interface{}
  99. switch kind {
  100. case "1":
  101. // 日
  102. var date = now.AddDate(0, 0, -30).Format("2006-01-02")
  103. var sql = fmt.Sprintf("SELECT user_grow_count,date FROM `platform_grow_data` WHERE DATE >= \"%s\" AND DATE != \"%s\" ORDER BY DATE ASC ", date, now.Format("2006-01-02"))
  104. results, _ := db.Db.QueryString(sql)
  105. for _, v := range results {
  106. tmpDate := utils.TimeParseStd(v["date"])
  107. yData = append(yData, v["user_grow_count"])
  108. xData = append(xData, tmpDate.Format("2006-01-02"))
  109. }
  110. yData = append(yData, m.UserGrowCount)
  111. tmpDate := utils.TimeParseStd(m.Date)
  112. xData = append(xData, tmpDate.Format("2006-01-02"))
  113. break
  114. case "2":
  115. // 周
  116. var nums = 29
  117. for i := nums; i >= 1; i-- {
  118. var date = now.AddDate(0, 0, -7*i).Format("2006-01-02")
  119. var sql = "SELECT user_grow_count,date FROM `platform_grow_data` WHERE DATE = \"%s\" "
  120. sql = fmt.Sprintf(sql, date)
  121. results, _ := db.Db.QueryString(sql)
  122. if results != nil {
  123. year, week, _, _ := utils.GetWeekInfo(results[0]["date"])
  124. yData = append(yData, results[0]["user_grow_count"])
  125. xData = append(xData, fmt.Sprintf("%s年%s周", year, week))
  126. }
  127. }
  128. yData = append(yData, m.UserGrowCount)
  129. year, week, _, _ := utils.GetWeekInfo(m.Date)
  130. xData = append(xData, fmt.Sprintf("%s年%s周", year, week))
  131. break
  132. case "3":
  133. // 月
  134. var nums = 29
  135. for i := nums; i >= 1; i-- {
  136. var date = now.AddDate(0, -i, 0).Format("2006-01-02")
  137. var sql = "SELECT user_grow_count,date FROM `platform_grow_data` WHERE DATE = \"%s\" "
  138. sql = fmt.Sprintf(sql, date)
  139. results, _ := db.Db.QueryString(sql)
  140. if results != nil {
  141. tmpDate := utils.TimeParseStd(results[0]["date"])
  142. yData = append(yData, results[0]["user_grow_count"])
  143. xData = append(xData, tmpDate.Format("2006-01"))
  144. }
  145. }
  146. yData = append(yData, m.UserGrowCount)
  147. tmpDate := utils.TimeParseStd(m.Date)
  148. xData = append(xData, tmpDate.Format("2006-01"))
  149. break
  150. }
  151. }