| 
									
										
										
										
											2025-07-13 16:36:20 +08:00
										 |  |  |  | package queries | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-21 15:13:26 +08:00
										 |  |  |  | import ( | 
					
						
							|  |  |  |  | 	"fmt" | 
					
						
							|  |  |  |  | 	"time" | 
					
						
							| 
									
										
										
										
											2025-07-13 16:36:20 +08:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-21 15:13:26 +08:00
										 |  |  |  | 	"tyapi-server/internal/domains/certification/enums" | 
					
						
							|  |  |  |  | ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // GetCertificationQuery 获取单个认证查询 | 
					
						
							|  |  |  |  | type GetCertificationQuery struct { | 
					
						
							|  |  |  |  | 	ID     string `json:"id" validate:"required"` | 
					
						
							|  |  |  |  | 	UserID string `json:"user_id,omitempty"` // 可选的用户ID,用于权限验证 | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // ListCertificationsQuery 认证列表查询 | 
					
						
							| 
									
										
										
										
											2025-07-13 16:36:20 +08:00
										 |  |  |  | type ListCertificationsQuery struct { | 
					
						
							| 
									
										
										
										
											2025-07-21 15:13:26 +08:00
										 |  |  |  | 	// 分页参数 | 
					
						
							|  |  |  |  | 	Page     int `json:"page" validate:"min=1"` | 
					
						
							|  |  |  |  | 	PageSize int `json:"page_size" validate:"min=1,max=100"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 排序参数 | 
					
						
							|  |  |  |  | 	SortBy    string `json:"sort_by"`    // 排序字段: created_at, updated_at, status, progress | 
					
						
							|  |  |  |  | 	SortOrder string `json:"sort_order"` // 排序方向: asc, desc | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 过滤条件 | 
					
						
							|  |  |  |  | 	UserID           string                       `json:"user_id,omitempty"` | 
					
						
							|  |  |  |  | 	Status           enums.CertificationStatus   `json:"status,omitempty"` | 
					
						
							|  |  |  |  | 	Statuses         []enums.CertificationStatus `json:"statuses,omitempty"` | 
					
						
							|  |  |  |  | 	FailureReason    enums.FailureReason         `json:"failure_reason,omitempty"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 时间范围过滤 | 
					
						
							|  |  |  |  | 	CreatedAfter     *time.Time `json:"created_after,omitempty"` | 
					
						
							|  |  |  |  | 	CreatedBefore    *time.Time `json:"created_before,omitempty"` | 
					
						
							|  |  |  |  | 	UpdatedAfter     *time.Time `json:"updated_after,omitempty"` | 
					
						
							|  |  |  |  | 	UpdatedBefore    *time.Time `json:"updated_before,omitempty"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 企业信息过滤 | 
					
						
							|  |  |  |  | 	CompanyName      string `json:"company_name,omitempty"` | 
					
						
							|  |  |  |  | 	LegalPersonName  string `json:"legal_person_name,omitempty"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 业务状态过滤 | 
					
						
							|  |  |  |  | 	IsCompleted      *bool `json:"is_completed,omitempty"` | 
					
						
							|  |  |  |  | 	IsFailed         *bool `json:"is_failed,omitempty"` | 
					
						
							|  |  |  |  | 	IsUserActionRequired *bool `json:"is_user_action_required,omitempty"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 高级过滤 | 
					
						
							|  |  |  |  | 	MinRetryCount    *int    `json:"min_retry_count,omitempty"` | 
					
						
							|  |  |  |  | 	MaxRetryCount    *int    `json:"max_retry_count,omitempty"` | 
					
						
							|  |  |  |  | 	MinProgress      *int    `json:"min_progress,omitempty"` | 
					
						
							|  |  |  |  | 	MaxProgress      *int    `json:"max_progress,omitempty"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 搜索参数 | 
					
						
							|  |  |  |  | 	SearchKeyword    string `json:"search_keyword,omitempty"` // 通用搜索关键词 | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 包含关联数据 | 
					
						
							|  |  |  |  | 	IncludeMetadata  bool `json:"include_metadata,omitempty"` | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // DefaultValues 设置默认值 | 
					
						
							|  |  |  |  | func (q *ListCertificationsQuery) DefaultValues() { | 
					
						
							|  |  |  |  | 	if q.Page <= 0 { | 
					
						
							|  |  |  |  | 		q.Page = 1 | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	if q.PageSize <= 0 { | 
					
						
							|  |  |  |  | 		q.PageSize = 20 | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	if q.SortBy == "" { | 
					
						
							|  |  |  |  | 		q.SortBy = "created_at" | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	if q.SortOrder == "" { | 
					
						
							|  |  |  |  | 		q.SortOrder = "desc" | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // GetOffset 计算分页偏移量 | 
					
						
							|  |  |  |  | func (q *ListCertificationsQuery) GetOffset() int { | 
					
						
							|  |  |  |  | 	return (q.Page - 1) * q.PageSize | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // GetLimit 获取查询限制数量 | 
					
						
							|  |  |  |  | func (q *ListCertificationsQuery) GetLimit() int { | 
					
						
							|  |  |  |  | 	return q.PageSize | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // HasTimeFilter 检查是否有时间过滤条件 | 
					
						
							|  |  |  |  | func (q *ListCertificationsQuery) HasTimeFilter() bool { | 
					
						
							|  |  |  |  | 	return q.CreatedAfter != nil || q.CreatedBefore != nil || | 
					
						
							|  |  |  |  | 		   q.UpdatedAfter != nil || q.UpdatedBefore != nil | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // HasStatusFilter 检查是否有状态过滤条件 | 
					
						
							|  |  |  |  | func (q *ListCertificationsQuery) HasStatusFilter() bool { | 
					
						
							|  |  |  |  | 	return q.Status != "" || len(q.Statuses) > 0 | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // HasSearchFilter 检查是否有搜索过滤条件 | 
					
						
							|  |  |  |  | func (q *ListCertificationsQuery) HasSearchFilter() bool { | 
					
						
							|  |  |  |  | 	return q.CompanyName != "" || q.LegalPersonName != "" || q.SearchKeyword != "" | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // GetSearchFields 获取搜索字段映射 | 
					
						
							|  |  |  |  | func (q *ListCertificationsQuery) GetSearchFields() map[string]string { | 
					
						
							|  |  |  |  | 	fields := make(map[string]string) | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	if q.CompanyName != "" { | 
					
						
							|  |  |  |  | 		fields["company_name"] = q.CompanyName | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	if q.LegalPersonName != "" { | 
					
						
							|  |  |  |  | 		fields["legal_person_name"] = q.LegalPersonName | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	if q.SearchKeyword != "" { | 
					
						
							|  |  |  |  | 		fields["keyword"] = q.SearchKeyword | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	return fields | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // CertificationStatisticsQuery 认证统计查询 | 
					
						
							|  |  |  |  | type CertificationStatisticsQuery struct { | 
					
						
							|  |  |  |  | 	// 时间范围 | 
					
						
							|  |  |  |  | 	StartDate    time.Time `json:"start_date" validate:"required"` | 
					
						
							|  |  |  |  | 	EndDate      time.Time `json:"end_date" validate:"required"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 统计周期 | 
					
						
							|  |  |  |  | 	Period       string    `json:"period" validate:"oneof=daily weekly monthly yearly"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 分组维度 | 
					
						
							|  |  |  |  | 	GroupBy      []string  `json:"group_by,omitempty"` // status, failure_reason, user_type, date | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 过滤条件 | 
					
						
							|  |  |  |  | 	UserIDs      []string  `json:"user_ids,omitempty"` | 
					
						
							|  |  |  |  | 	Statuses     []enums.CertificationStatus `json:"statuses,omitempty"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 统计类型 | 
					
						
							|  |  |  |  | 	IncludeProgressStats bool `json:"include_progress_stats,omitempty"` | 
					
						
							|  |  |  |  | 	IncludeRetryStats    bool `json:"include_retry_stats,omitempty"` | 
					
						
							|  |  |  |  | 	IncludeTimeStats     bool `json:"include_time_stats,omitempty"` | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // Validate 验证统计查询参数 | 
					
						
							|  |  |  |  | func (q *CertificationStatisticsQuery) Validate() error { | 
					
						
							|  |  |  |  | 	if q.EndDate.Before(q.StartDate) { | 
					
						
							|  |  |  |  | 		return fmt.Errorf("结束时间不能早于开始时间") | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 检查时间范围是否合理(不超过1年) | 
					
						
							|  |  |  |  | 	if q.EndDate.Sub(q.StartDate) > 365*24*time.Hour { | 
					
						
							|  |  |  |  | 		return fmt.Errorf("查询时间范围不能超过1年") | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	return nil | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // GetTimeRange 获取时间范围描述 | 
					
						
							|  |  |  |  | func (q *CertificationStatisticsQuery) GetTimeRange() string { | 
					
						
							|  |  |  |  | 	return fmt.Sprintf("%s 到 %s",  | 
					
						
							|  |  |  |  | 		q.StartDate.Format("2006-01-02"),  | 
					
						
							|  |  |  |  | 		q.EndDate.Format("2006-01-02")) | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // SearchCertificationsQuery 搜索认证查询 | 
					
						
							|  |  |  |  | type SearchCertificationsQuery struct { | 
					
						
							|  |  |  |  | 	// 搜索关键词 | 
					
						
							|  |  |  |  | 	Keyword      string `json:"keyword" validate:"required,min=2"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 搜索字段 | 
					
						
							|  |  |  |  | 	SearchFields []string `json:"search_fields,omitempty"` // company_name, legal_person_name, unified_social_code | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 过滤条件 | 
					
						
							|  |  |  |  | 	Statuses     []enums.CertificationStatus `json:"statuses,omitempty"` | 
					
						
							|  |  |  |  | 	UserID       string `json:"user_id,omitempty"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 分页参数 | 
					
						
							|  |  |  |  | 	Page         int `json:"page" validate:"min=1"` | 
					
						
							|  |  |  |  | 	PageSize     int `json:"page_size" validate:"min=1,max=50"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 排序参数 | 
					
						
							|  |  |  |  | 	SortBy       string `json:"sort_by"` | 
					
						
							|  |  |  |  | 	SortOrder    string `json:"sort_order"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 搜索选项 | 
					
						
							|  |  |  |  | 	ExactMatch   bool `json:"exact_match,omitempty"` // 是否精确匹配 | 
					
						
							|  |  |  |  | 	IgnoreCase   bool `json:"ignore_case,omitempty"` // 是否忽略大小写 | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // DefaultValues 设置搜索查询默认值 | 
					
						
							|  |  |  |  | func (q *SearchCertificationsQuery) DefaultValues() { | 
					
						
							|  |  |  |  | 	if q.Page <= 0 { | 
					
						
							|  |  |  |  | 		q.Page = 1 | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	if q.PageSize <= 0 { | 
					
						
							|  |  |  |  | 		q.PageSize = 10 | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	if q.SortBy == "" { | 
					
						
							|  |  |  |  | 		q.SortBy = "created_at" | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	if q.SortOrder == "" { | 
					
						
							|  |  |  |  | 		q.SortOrder = "desc" | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	if len(q.SearchFields) == 0 { | 
					
						
							|  |  |  |  | 		q.SearchFields = []string{"company_name", "legal_person_name"} | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 默认忽略大小写 | 
					
						
							|  |  |  |  | 	q.IgnoreCase = true | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // GetLimit 获取查询限制数量 | 
					
						
							|  |  |  |  | func (q *SearchCertificationsQuery) GetLimit() int { | 
					
						
							|  |  |  |  | 	return q.PageSize | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // GetSearchPattern 获取搜索模式 | 
					
						
							|  |  |  |  | func (q *SearchCertificationsQuery) GetSearchPattern() string { | 
					
						
							|  |  |  |  | 	if q.ExactMatch { | 
					
						
							|  |  |  |  | 		return q.Keyword | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 模糊搜索,添加通配符 | 
					
						
							|  |  |  |  | 	return "%" + q.Keyword + "%" | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // UserCertificationsQuery 用户认证查询 | 
					
						
							|  |  |  |  | type UserCertificationsQuery struct { | 
					
						
							|  |  |  |  | 	UserID       string `json:"user_id" validate:"required"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 状态过滤 | 
					
						
							|  |  |  |  | 	Status       enums.CertificationStatus `json:"status,omitempty"` | 
					
						
							|  |  |  |  | 	IncludeCompleted bool `json:"include_completed,omitempty"` | 
					
						
							|  |  |  |  | 	IncludeFailed    bool `json:"include_failed,omitempty"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 时间过滤 | 
					
						
							|  |  |  |  | 	After        *time.Time `json:"after,omitempty"` | 
					
						
							|  |  |  |  | 	Before       *time.Time `json:"before,omitempty"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 分页 | 
					
						
							|  |  |  |  | 	Page         int `json:"page"` | 
					
						
							|  |  |  |  | 	PageSize     int `json:"page_size"` | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 排序 | 
					
						
							|  |  |  |  | 	SortBy       string `json:"sort_by"` | 
					
						
							|  |  |  |  | 	SortOrder    string `json:"sort_order"` | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // DefaultValues 设置用户认证查询默认值 | 
					
						
							|  |  |  |  | func (q *UserCertificationsQuery) DefaultValues() { | 
					
						
							|  |  |  |  | 	if q.Page <= 0 { | 
					
						
							|  |  |  |  | 		q.Page = 1 | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	if q.PageSize <= 0 { | 
					
						
							|  |  |  |  | 		q.PageSize = 10 | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	if q.SortBy == "" { | 
					
						
							|  |  |  |  | 		q.SortBy = "created_at" | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	if q.SortOrder == "" { | 
					
						
							|  |  |  |  | 		q.SortOrder = "desc" | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // ShouldIncludeStatus 检查是否应该包含指定状态 | 
					
						
							|  |  |  |  | func (q *UserCertificationsQuery) ShouldIncludeStatus(status enums.CertificationStatus) bool { | 
					
						
							|  |  |  |  | 	// 如果指定了特定状态,只返回该状态 | 
					
						
							|  |  |  |  | 	if q.Status != "" { | 
					
						
							|  |  |  |  | 		return status == q.Status | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	// 根据包含选项决定 | 
					
						
							|  |  |  |  | 	if enums.IsFinalStatus(status) && !q.IncludeCompleted { | 
					
						
							|  |  |  |  | 		return false | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	if enums.IsFailureStatus(status) && !q.IncludeFailed { | 
					
						
							|  |  |  |  | 		return false | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 	 | 
					
						
							|  |  |  |  | 	return true | 
					
						
							| 
									
										
										
										
											2025-07-13 16:36:20 +08:00
										 |  |  |  | } |