75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package statistics
|
||
|
||
import (
|
||
"dd_fiber_api/internal/camp/dao"
|
||
question_dao "dd_fiber_api/internal/question/dao"
|
||
"dd_fiber_api/pkg/database"
|
||
"fmt"
|
||
)
|
||
|
||
// Service 统计服务
|
||
type Service struct {
|
||
mysqlClient *database.MySQLClient
|
||
campDAO *dao.CampDAO
|
||
questionDAO question_dao.QuestionDAOInterface
|
||
paperDAO question_dao.PaperDAOInterface
|
||
}
|
||
|
||
// NewService 创建统计服务
|
||
func NewService(
|
||
mysqlClient *database.MySQLClient,
|
||
campDAO *dao.CampDAO,
|
||
questionDAO question_dao.QuestionDAOInterface,
|
||
paperDAO question_dao.PaperDAOInterface,
|
||
) *Service {
|
||
return &Service{
|
||
mysqlClient: mysqlClient,
|
||
campDAO: campDAO,
|
||
questionDAO: questionDAO,
|
||
paperDAO: paperDAO,
|
||
}
|
||
}
|
||
|
||
// Statistics 统计数据
|
||
type Statistics struct {
|
||
UserCount int `json:"user_count"` // 用户总数
|
||
CampCount int `json:"camp_count"` // 打卡营数量
|
||
QuestionCount int `json:"question_count"` // 题目数量
|
||
PaperCount int `json:"paper_count"` // 试卷数量
|
||
}
|
||
|
||
// GetStatistics 获取统计数据
|
||
func (s *Service) GetStatistics() (*Statistics, error) {
|
||
stats := &Statistics{}
|
||
|
||
// 统计用户总数(从 camp_user_camps 表中统计不重复的 user_id)
|
||
userCountQuery := `SELECT COUNT(DISTINCT user_id) FROM camp_user_camps`
|
||
err := s.mysqlClient.DB.QueryRow(userCountQuery).Scan(&stats.UserCount)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("统计用户总数失败: %v", err)
|
||
}
|
||
|
||
// 统计打卡营数量(排除已删除的)
|
||
campCountQuery := `SELECT COUNT(*) FROM camp_camps WHERE deleted_at IS NULL`
|
||
err = s.mysqlClient.DB.QueryRow(campCountQuery).Scan(&stats.CampCount)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("统计打卡营数量失败: %v", err)
|
||
}
|
||
|
||
// 统计题目数量(使用 MongoDB 接口)
|
||
_, total, err := s.questionDAO.Search("", 0, nil, 1, 1)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("统计题目数量失败: %v", err)
|
||
}
|
||
stats.QuestionCount = int(total)
|
||
|
||
// 统计试卷数量(使用 MongoDB 接口)
|
||
_, total, err = s.paperDAO.Search("", 1, 1)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("统计试卷数量失败: %v", err)
|
||
}
|
||
stats.PaperCount = int(total)
|
||
|
||
return stats, nil
|
||
}
|