初始化项目
This commit is contained in:
@@ -17,7 +17,7 @@ type StatReportService struct{}
|
||||
|
||||
var StatReport = &StatReportService{}
|
||||
|
||||
// 生成日报表
|
||||
// GenerateDailyReport 生成日报表(现在只用于手动触发,定时任务会自动生成)
|
||||
func (s *StatReportService) GenerateDailyReport(ctx context.Context, req *dto.ReportGenerateReq) (*dto.ReportGenerateResp, error) {
|
||||
// 获取统计日期
|
||||
reportDate := time.Now()
|
||||
@@ -28,6 +28,23 @@ func (s *StatReportService) GenerateDailyReport(ctx context.Context, req *dto.Re
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否已存在报表
|
||||
existingReport, err := dao.StatReport.GetByTenantAndDate(ctx, req.TenantID, "daily", reportDate.Format("2006-01-02"))
|
||||
if err == nil && existingReport != nil {
|
||||
// 返回已存在的报表
|
||||
var reportData map[string]interface{}
|
||||
if err := gconv.Struct(existingReport.ReportData, &reportData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &dto.ReportGenerateResp{
|
||||
ReportID: existingReport.Id,
|
||||
ReportType: "daily",
|
||||
ReportDate: reportDate.Format("2006-01-02"),
|
||||
Data: reportData,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 生成日报表数据
|
||||
reportData, err := s.generateReportData(ctx, req.TenantID, req.AppID, "daily", reportDate)
|
||||
if err != nil {
|
||||
@@ -58,7 +75,7 @@ func (s *StatReportService) GenerateDailyReport(ctx context.Context, req *dto.Re
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 生成月报表
|
||||
// GenerateMonthlyReport 生成月报表(现在优先使用预生成的报表)
|
||||
func (s *StatReportService) GenerateMonthlyReport(ctx context.Context, req *dto.ReportGenerateReq) (*dto.ReportGenerateResp, error) {
|
||||
reportDate := time.Now()
|
||||
if req.Date != "" {
|
||||
@@ -68,6 +85,22 @@ func (s *StatReportService) GenerateMonthlyReport(ctx context.Context, req *dto.
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否已存在报表
|
||||
existingReport, err := dao.StatReport.GetByTenantAndDate(ctx, req.TenantID, "monthly", reportDate.Format("2006-01"))
|
||||
if err == nil && existingReport != nil {
|
||||
var reportData map[string]interface{}
|
||||
if err := gconv.Struct(existingReport.ReportData, &reportData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &dto.ReportGenerateResp{
|
||||
ReportID: existingReport.Id,
|
||||
ReportType: "monthly",
|
||||
ReportDate: reportDate.Format("2006-01"),
|
||||
Data: reportData,
|
||||
}, nil
|
||||
}
|
||||
|
||||
reportData, err := s.generateReportData(ctx, req.TenantID, req.AppID, "monthly", reportDate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -96,6 +129,61 @@ func (s *StatReportService) GenerateMonthlyReport(ctx context.Context, req *dto.
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GenerateWeeklyReport 生成周报表(新增周报表支持)
|
||||
func (s *StatReportService) GenerateWeeklyReport(ctx context.Context, req *dto.ReportGenerateReq) (*dto.ReportGenerateResp, error) {
|
||||
reportDate := time.Now()
|
||||
if req.Date != "" {
|
||||
// 周报表格式:2024-W01
|
||||
parsedDate, err := time.Parse("2006-W01", req.Date)
|
||||
if err == nil {
|
||||
reportDate = parsedDate
|
||||
}
|
||||
}
|
||||
|
||||
// 检查是否已存在报表
|
||||
existingReport, err := dao.StatReport.GetByTenantAndDate(ctx, req.TenantID, "weekly", reportDate.Format("2006-W01"))
|
||||
if err == nil && existingReport != nil {
|
||||
var reportData map[string]interface{}
|
||||
if err := gconv.Struct(existingReport.ReportData, &reportData); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &dto.ReportGenerateResp{
|
||||
ReportID: existingReport.Id,
|
||||
ReportType: "weekly",
|
||||
ReportDate: reportDate.Format("2006-W01"),
|
||||
Data: reportData,
|
||||
}, nil
|
||||
}
|
||||
|
||||
reportData, err := s.generateReportData(ctx, req.TenantID, req.AppID, "weekly", reportDate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
report := &entity.StatReport{
|
||||
TenantId: req.TenantID,
|
||||
AppID: req.AppID,
|
||||
ReportType: "weekly",
|
||||
ReportDate: reportDate,
|
||||
ReportData: gconv.String(reportData),
|
||||
GeneratedAt: time.Now(),
|
||||
Status: "completed",
|
||||
}
|
||||
|
||||
_, err = dao.StatReport.Create(ctx, report)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &dto.ReportGenerateResp{
|
||||
ReportID: report.Id,
|
||||
ReportType: "weekly",
|
||||
ReportDate: reportDate.Format("2006-W01"),
|
||||
Data: reportData,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 生成季度报表
|
||||
func (s *StatReportService) GenerateQuarterlyReport(ctx context.Context, req *dto.ReportGenerateReq) (*dto.ReportGenerateResp, error) {
|
||||
reportDate := time.Now()
|
||||
|
||||
Reference in New Issue
Block a user