type ProjectCountByMonth struct {
    Month int `json:"month"`
    Count int `json:"count"`
}

// ProjectCountByMonths 统计某年每个月的新增项目数量
func (a *Invoice0) ProjectCountByMonths(ctx context.Context, niandu int) ([]ProjectCountByMonth, error) {
    results := []ProjectCountByMonth{}
    err := entity.GetInvoiceDB(ctx, a.DB).
        Table("project").
        Select("MONTH(created_at) as month, COUNT(*) as count").
        Where("YEAR(created_at) = ?", niandu).
        Where("deleted_at IS NULL"). // 忽略软删除项目
        Group("MONTH(created_at)").
        Order("month").
        Scan(&results).Error

    if err != nil {
        return nil, err
    }

    // 补全12个月
    counts := make([]ProjectCountByMonth, 12)
    for i := 1; i <= 12; i++ {
        counts[i-1] = ProjectCountByMonth{Month: i, Count: 0}
    }
    for _, r := range results {
        if r.Month >= 1 && r.Month <= 12 {
            counts[r.Month-1].Count = r.Count
        }
    }

    return counts, nil
}

这段代码是Service 层的方法,用于统计指定年份每个月的新增项目数量,并确保返回完整的 1-12 月数据