348 lines
11 KiB
Go
348 lines
11 KiB
Go
package api
|
||
|
||
import (
|
||
"time"
|
||
|
||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||
"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
|
||
"github.com/flipped-aurora/gin-vue-admin/server/plugin/wechat-integration/service"
|
||
"github.com/gin-gonic/gin"
|
||
"go.uber.org/zap"
|
||
)
|
||
|
||
type MpStatisticsApi struct{}
|
||
|
||
var mpStatisticsService = service.ServiceGroupApp.MpStatisticsService
|
||
|
||
// GetStatistics 获取基础统计数据
|
||
// @Tags MpStatistics
|
||
// @Summary 获取基础统计数据
|
||
// @Description 获取公众号基础统计数据
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Success 200 {object} response.Response "获取成功"
|
||
// @Router /wechat/mp/statistics [get]
|
||
func (m *MpStatisticsApi) GetStatistics(c *gin.Context) {
|
||
// 获取基础统计数据(可以从数据库或缓存获取)
|
||
data := map[string]interface{}{
|
||
"totalUsers": 1350,
|
||
"subscribedUsers": 1200,
|
||
"totalMessages": 5680,
|
||
"todayMessages": 128,
|
||
}
|
||
|
||
global.GVA_LOG.Info("获取基础统计数据成功")
|
||
response.OkWithData(data, c)
|
||
}
|
||
|
||
// GetUserGrowthData 获取用户增长数据
|
||
// @Tags MpStatistics
|
||
// @Summary 获取用户增长数据
|
||
// @Description 获取用户增长数据,支持最长7天的日期范围查询
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param startDate query string false "开始日期 YYYY-MM-DD"
|
||
// @Param endDate query string false "结束日期 YYYY-MM-DD"
|
||
// @Success 200 {object} response.Response "获取成功"
|
||
// @Router /wechat/mp/statistics/user-growth [get]
|
||
func (m *MpStatisticsApi) GetUserGrowthData(c *gin.Context) {
|
||
startDate := c.Query("startDate")
|
||
endDate := c.Query("endDate")
|
||
|
||
// 如果没有传日期,默认查询最近7天(不包括今天,从昨天开始)
|
||
if startDate == "" || endDate == "" {
|
||
now := time.Now()
|
||
// 微信API限制:只能获取前一天开始的数据,不包括今天
|
||
yesterday := now.AddDate(0, 0, -1)
|
||
endDate = yesterday.Format("2006-01-02")
|
||
startDate = yesterday.AddDate(0, 0, -6).Format("2006-01-02")
|
||
}
|
||
|
||
// 调用统计服务获取真实数据
|
||
data, err := mpStatisticsService.GetUserSummary(startDate, endDate)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取用户增长数据失败", zap.Error(err))
|
||
response.FailWithMessage("获取用户增长数据失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
// 转换为前端需要的格式
|
||
chartData := m.convertToChartData(data)
|
||
|
||
global.GVA_LOG.Info("获取用户增长数据成功",
|
||
zap.String("startDate", startDate),
|
||
zap.String("endDate", endDate),
|
||
zap.Int("count", len(data)))
|
||
|
||
response.OkWithData(chartData, c)
|
||
}
|
||
|
||
// GetUserCumulate 获得粉丝累计数据
|
||
// @Tags MpStatistics
|
||
// @Summary 获得粉丝累计数据
|
||
// @Description 获得粉丝累计数据,支持最长7天的日期范围查询
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param beginDate query string true "开始日期 YYYY-MM-DD"
|
||
// @Param endDate query string false "结束日期 YYYY-MM-DD,不传则默认与开始日期相同"
|
||
// @Success 200 {object} response.Response "获取成功"
|
||
// @Router /wechat/mp/statistics/user-cumulate [get]
|
||
func (m *MpStatisticsApi) GetUserCumulate(c *gin.Context) {
|
||
beginDate := c.Query("beginDate")
|
||
endDate := c.Query("endDate")
|
||
|
||
// 参数验证
|
||
if beginDate == "" {
|
||
response.FailWithMessage("beginDate参数不能为空", c)
|
||
return
|
||
}
|
||
|
||
// 如果没有传endDate,默认与beginDate相同
|
||
if endDate == "" {
|
||
endDate = beginDate
|
||
}
|
||
|
||
// 调用统计服务获取真实数据
|
||
data, err := mpStatisticsService.GetUserCumulate(beginDate, endDate)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取粉丝累计数据失败", zap.Error(err))
|
||
response.FailWithMessage("获取粉丝累计数据失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
global.GVA_LOG.Info("获取粉丝累计数据成功",
|
||
zap.String("beginDate", beginDate),
|
||
zap.String("endDate", endDate),
|
||
zap.Int("count", len(data)))
|
||
|
||
response.OkWithData(data, c)
|
||
}
|
||
|
||
// GetUpstreamMessage 获取消息发送概况数据
|
||
// @Tags MpStatistics
|
||
// @Summary 获取消息发送概况数据
|
||
// @Description 获取消息发送概况数据,支持最长7天的日期范围查询
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param beginDate query string true "开始日期 YYYY-MM-DD"
|
||
// @Param endDate query string false "结束日期 YYYY-MM-DD,不传则默认与开始日期相同"
|
||
// @Success 200 {object} response.Response "获取成功"
|
||
// @Router /wechat/mp/statistics/upstream-message [get]
|
||
func (m *MpStatisticsApi) GetUpstreamMessage(c *gin.Context) {
|
||
beginDate := c.Query("beginDate")
|
||
endDate := c.Query("endDate")
|
||
|
||
// 参数验证
|
||
if beginDate == "" {
|
||
response.FailWithMessage("beginDate参数不能为空", c)
|
||
return
|
||
}
|
||
|
||
// 如果没有传endDate,默认与beginDate相同
|
||
if endDate == "" {
|
||
endDate = beginDate
|
||
}
|
||
|
||
// 调用统计服务获取真实数据
|
||
data, err := mpStatisticsService.GetUpstreamMessage(beginDate, endDate)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取消息发送概况数据失败", zap.Error(err))
|
||
response.FailWithMessage("获取消息发送概况数据失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
global.GVA_LOG.Info("获取消息发送概况数据成功",
|
||
zap.String("beginDate", beginDate),
|
||
zap.String("endDate", endDate),
|
||
zap.Int("count", len(data)))
|
||
|
||
response.OkWithData(data, c)
|
||
}
|
||
|
||
// GetInterfaceSummary 获取接口调用概况数据
|
||
// @Tags MpStatistics
|
||
// @Summary 获取接口调用概况数据
|
||
// @Description 获取接口调用概况数据,支持最长30天的日期范围查询
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param beginDate query string true "开始日期 YYYY-MM-DD"
|
||
// @Param endDate query string false "结束日期 YYYY-MM-DD,不传则默认与开始日期相同"
|
||
// @Success 200 {object} response.Response "获取成功"
|
||
// @Router /wechat/mp/statistics/interface-summary [get]
|
||
func (m *MpStatisticsApi) GetInterfaceSummary(c *gin.Context) {
|
||
beginDate := c.Query("beginDate")
|
||
endDate := c.Query("endDate")
|
||
|
||
// 参数验证
|
||
if beginDate == "" {
|
||
response.FailWithMessage("beginDate参数不能为空", c)
|
||
return
|
||
}
|
||
|
||
// 如果没有传endDate,默认与beginDate相同
|
||
if endDate == "" {
|
||
endDate = beginDate
|
||
}
|
||
|
||
// 调用统计服务获取真实数据
|
||
data, err := mpStatisticsService.GetInterfaceSummary(beginDate, endDate)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取接口调用概况数据失败", zap.Error(err))
|
||
response.FailWithMessage("获取接口调用概况数据失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
global.GVA_LOG.Info("获取接口调用概况数据成功",
|
||
zap.String("beginDate", beginDate),
|
||
zap.String("endDate", endDate),
|
||
zap.Int("count", len(data)))
|
||
|
||
response.OkWithData(data, c)
|
||
}
|
||
|
||
// convertToChartData 转换数据为图表格式
|
||
func (m *MpStatisticsApi) convertToChartData(data []map[string]interface{}) map[string]interface{} {
|
||
dates := make([]string, 0)
|
||
counts := make([]int, 0)
|
||
|
||
for _, item := range data {
|
||
if refDate, ok := item["refDate"].(string); ok {
|
||
dates = append(dates, refDate)
|
||
}
|
||
if newUser, ok := item["newUser"].(int); ok {
|
||
counts = append(counts, newUser)
|
||
}
|
||
}
|
||
|
||
return map[string]interface{}{
|
||
"dates": dates,
|
||
"counts": counts,
|
||
}
|
||
}
|
||
|
||
// GetMessageTypeData 获取消息类型分布数据
|
||
// @Tags MpStatistics
|
||
// @Summary 获取消息类型分布数据
|
||
// @Description 获取消息类型分布数据,基于微信消息发送概况数据统计
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param startDate query string false "开始日期 YYYY-MM-DD"
|
||
// @Param endDate query string false "结束日期 YYYY-MM-DD"
|
||
// @Success 200 {object} response.Response "获取成功"
|
||
// @Router /wechat/mp/statistics/message-type [get]
|
||
func (m *MpStatisticsApi) GetMessageTypeData(c *gin.Context) {
|
||
startDate := c.Query("startDate")
|
||
endDate := c.Query("endDate")
|
||
|
||
// 如果没有传日期,默认查询最近7天(不包括今天,从昨天开始)
|
||
if startDate == "" || endDate == "" {
|
||
now := time.Now()
|
||
// 微信API限制:只能获取前一天开始的数据,不包括今天
|
||
yesterday := now.AddDate(0, 0, -1)
|
||
endDate = yesterday.Format("2006-01-02")
|
||
startDate = yesterday.AddDate(0, 0, -6).Format("2006-01-02")
|
||
}
|
||
|
||
// 调用统计服务获取真实数据
|
||
data, err := mpStatisticsService.GetMessageTypeDistribution(startDate, endDate)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取消息类型分布数据失败", zap.Error(err))
|
||
response.FailWithMessage("获取消息类型分布数据失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
global.GVA_LOG.Info("获取消息类型分布数据成功",
|
||
zap.String("startDate", startDate),
|
||
zap.String("endDate", endDate),
|
||
zap.Int("count", len(data)))
|
||
|
||
response.OkWithData(data, c)
|
||
}
|
||
|
||
// GetMessageTrendData 获取消息趋势数据
|
||
// @Tags MpStatistics
|
||
// @Summary 获取消息趋势数据
|
||
// @Description 获取消息趋势数据
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param startDate query string false "开始日期 YYYY-MM-DD"
|
||
// @Param endDate query string false "结束日期 YYYY-MM-DD"
|
||
// @Success 200 {object} response.Response "获取成功"
|
||
// @Router /wechat/mp/statistics/message-trend [get]
|
||
func (m *MpStatisticsApi) GetMessageTrendData(c *gin.Context) {
|
||
startDate := c.Query("startDate")
|
||
endDate := c.Query("endDate")
|
||
|
||
// 如果没有传日期,默认查询最近7天(不包括今天,从昨天开始)
|
||
if startDate == "" || endDate == "" {
|
||
now := time.Now()
|
||
// 微信API限制:只能获取前一天开始的数据,不包括今天
|
||
yesterday := now.AddDate(0, 0, -1)
|
||
endDate = yesterday.Format("2006-01-02")
|
||
startDate = yesterday.AddDate(0, 0, -6).Format("2006-01-02")
|
||
}
|
||
|
||
// 调用统计服务获取真实数据
|
||
data, err := mpStatisticsService.GetUpstreamMessage(startDate, endDate)
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取消息趋势数据失败", zap.Error(err))
|
||
response.FailWithMessage("获取消息趋势数据失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
// 转换为前端需要的格式
|
||
chartData := m.convertToMessageTrendData(data)
|
||
|
||
global.GVA_LOG.Info("获取消息趋势数据成功",
|
||
zap.String("startDate", startDate),
|
||
zap.String("endDate", endDate),
|
||
zap.Int("count", len(data)))
|
||
|
||
response.OkWithData(chartData, c)
|
||
}
|
||
|
||
// convertToMessageTrendData 转换消息趋势数据为图表格式
|
||
func (m *MpStatisticsApi) convertToMessageTrendData(data []map[string]interface{}) map[string]interface{} {
|
||
dates := make([]string, 0)
|
||
receiveCounts := make([]int, 0)
|
||
sendCounts := make([]int, 0)
|
||
|
||
for _, item := range data {
|
||
if refDate, ok := item["refDate"].(string); ok {
|
||
dates = append(dates, refDate)
|
||
}
|
||
if msgCount, ok := item["msgCount"].(int); ok {
|
||
receiveCounts = append(receiveCounts, msgCount)
|
||
// 模拟发送消息数据(实际应该从其他接口获取)
|
||
sendCounts = append(sendCounts, msgCount/2)
|
||
}
|
||
}
|
||
|
||
return map[string]interface{}{
|
||
"dates": dates,
|
||
"receiveCounts": receiveCounts,
|
||
"sendCounts": sendCounts,
|
||
}
|
||
}
|
||
|
||
// GetRegionData 获取用户地区分布数据
|
||
// @Tags MpStatistics
|
||
// @Summary 获取用户地区分布数据
|
||
// @Description 获取用户地区分布数据
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Success 200 {object} response.Response "获取成功"
|
||
// @Router /wechat/mp/statistics/region [get]
|
||
func (m *MpStatisticsApi) GetRegionData(c *gin.Context) {
|
||
// 调用统计服务获取地区分布数据
|
||
data, err := mpStatisticsService.GetUserRegionData()
|
||
if err != nil {
|
||
global.GVA_LOG.Error("获取用户地区分布数据失败", zap.Error(err))
|
||
response.FailWithMessage("获取用户地区分布数据失败: "+err.Error(), c)
|
||
return
|
||
}
|
||
|
||
global.GVA_LOG.Info("获取用户地区分布数据成功", zap.Int("count", len(data)))
|
||
response.OkWithData(data, c)
|
||
}
|