feat: complete workbench split admin routes

This commit is contained in:
richjohnrambo
2026-09-17 23:37:16 +08:00
parent 7099a33cde
commit 5c805c7b09
6 changed files with 334 additions and 26 deletions
+82
View File
@@ -783,6 +783,34 @@ func (s *Store) SubmitDaily(ctx context.Context, userID int64, reportID string,
return s.DailyReport(ctx, userID, reportID)
}
func (s *Store) DailyReportStats(ctx context.Context, date time.Time, departmentID string) (*ReportStats, error) {
stats := &ReportStats{DepartmentID: strings.TrimSpace(departmentID)}
memberWhere, memberArgs := reportMemberScope(stats.DepartmentID)
if err := s.db.GetContext(ctx, &stats.ExpectedCount,
`SELECT COUNT(DISTINCT user_id) FROM im_org_members WHERE `+strings.Join(memberWhere, " AND "), memberArgs...); err != nil {
if isMissingTable(err) {
return stats, nil
}
return nil, err
}
args := append([]any{date.Format("2006-01-02")}, memberArgs...)
if err := s.db.GetContext(ctx, &stats.SubmittedCount,
`SELECT COUNT(DISTINCT r.author_id)
FROM im_daily_reports AS r
INNER JOIN im_org_members AS m ON m.user_id=r.author_id
WHERE r.report_date=? AND r.status='submitted' AND `+strings.Join(prefixReportMemberScope(memberWhere), " AND "), args...); err != nil {
if isMissingTable(err) {
return stats, nil
}
return nil, err
}
stats.MissingCount = stats.ExpectedCount - stats.SubmittedCount
if stats.MissingCount < 0 {
stats.MissingCount = 0
}
return stats, nil
}
func (s *Store) MonthlyReports(ctx context.Context, userID int64, cursor string, limit int) ([]MonthlyReport, string, error) {
var items []MonthlyReport
query := `SELECT report_id,author_id,report_month,work_summary,next_plan,attachments,status,read_state,version,submitted_at,created_at,updated_at FROM im_monthly_reports WHERE author_id=?`
@@ -869,6 +897,34 @@ func (s *Store) ReopenMonthly(ctx context.Context, userID int64, reportID string
return s.MonthlyReport(ctx, userID, reportID)
}
func (s *Store) MonthlyReportStats(ctx context.Context, month, departmentID string) (*ReportStats, error) {
stats := &ReportStats{DepartmentID: strings.TrimSpace(departmentID)}
memberWhere, memberArgs := reportMemberScope(stats.DepartmentID)
if err := s.db.GetContext(ctx, &stats.ExpectedCount,
`SELECT COUNT(DISTINCT user_id) FROM im_org_members WHERE `+strings.Join(memberWhere, " AND "), memberArgs...); err != nil {
if isMissingTable(err) {
return stats, nil
}
return nil, err
}
args := append([]any{strings.TrimSpace(month)}, memberArgs...)
if err := s.db.GetContext(ctx, &stats.SubmittedCount,
`SELECT COUNT(DISTINCT r.author_id)
FROM im_monthly_reports AS r
INNER JOIN im_org_members AS m ON m.user_id=r.author_id
WHERE r.report_month=? AND r.status='submitted' AND `+strings.Join(prefixReportMemberScope(memberWhere), " AND "), args...); err != nil {
if isMissingTable(err) {
return stats, nil
}
return nil, err
}
stats.MissingCount = stats.ExpectedCount - stats.SubmittedCount
if stats.MissingCount < 0 {
stats.MissingCount = 0
}
return stats, nil
}
func (s *Store) MailboxConfig(ctx context.Context) (*MailboxConfig, error) {
var cfg MailboxConfig
if err := s.db.GetContext(ctx, &cfg, `SELECT enabled,allow_anonymous,required_fields,max_length,attachment_policy,notice_title,notice_content,CAST(COALESCE(UNIX_TIMESTAMP(notice_updated_at),1) AS UNSIGNED) AS version FROM im_mailbox_config WHERE id=1`); err != nil {
@@ -1138,6 +1194,32 @@ func trimMemberPage(items []OrganizationMember, limit int) ([]OrganizationMember
return items, next, nil
}
func reportMemberScope(departmentID string) ([]string, []any) {
where := []string{"employment_state='active'"}
args := []any{}
if departmentID = strings.TrimSpace(departmentID); departmentID != "" {
where = append(where, "department_id=?")
args = append(args, departmentID)
}
return where, args
}
func prefixReportMemberScope(where []string) []string {
out := make([]string, 0, len(where))
for _, condition := range where {
condition = strings.TrimSpace(condition)
switch {
case strings.HasPrefix(condition, "employment_state"):
out = append(out, "m."+condition)
case strings.HasPrefix(condition, "department_id"):
out = append(out, "m."+condition)
default:
out = append(out, condition)
}
}
return out
}
func isDuplicate(err error) bool {
return err != nil && strings.Contains(err.Error(), "Duplicate entry")
}