2026-01-06 22:59:58 +08:00

80 lines
4.5 KiB
Markdown

# Requirements Document
## Introduction
This specification addresses a critical bug in the `SecondaryCircuitInspectionResultAppService.FindDatas` method where data deduplication by day is not working correctly. The current implementation applies deduplication after pagination, which results in incomplete merging of records that should be consolidated when they share the same Year, Month, Day, SecondaryCircuitInspectionItemId, and Status values.
## Glossary
- **Inspection_Result**: A record in the SecondaryCircuitInspectionResult collection representing a single inspection execution
- **Daily_Deduplication**: The process of merging multiple inspection results into a single record when they occur on the same day for the same inspection item and status
- **Pagination**: The process of dividing query results into pages for display
- **MongoDB_Aggregation**: MongoDB's pipeline-based data processing framework
- **Collection_Sharding**: The practice of splitting data across multiple collections based on time periods (Year-Month)
## Requirements
### Requirement 1: Correct Deduplication Order
**User Story:** As a system user, I want inspection results to be properly deduplicated by day before pagination, so that I see only one record per day for each inspection item and status combination.
#### Acceptance Criteria
1. WHEN querying inspection results, THE System SHALL deduplicate records before applying pagination
2. WHEN multiple records exist with the same Year, Month, Day, SecondaryCircuitInspectionItemId, and Status, THE System SHALL retain only one record per group
3. WHEN deduplication is performed, THE System SHALL preserve the first record in each group based on the sort order
4. THE System SHALL apply pagination logic only after deduplication is complete
### Requirement 2: Accurate Total Count
**User Story:** As a system user, I want the total count to reflect the deduplicated number of records, so that pagination displays correctly.
#### Acceptance Criteria
1. WHEN calculating total count, THE System SHALL count deduplicated records
2. WHEN returning pagination metadata, THE System SHALL use the deduplicated count as TotalCount
3. THE System SHALL ensure PageIndex and PageSize calculations are based on deduplicated data
### Requirement 3: MongoDB Aggregation Pipeline Implementation
**User Story:** As a developer, I want the deduplication to be performed using MongoDB aggregation pipeline, so that the operation is efficient and scalable.
#### Acceptance Criteria
1. THE System SHALL use MongoDB's $group stage to deduplicate records
2. WHEN grouping records, THE System SHALL group by Year, Month, Day, SecondaryCircuitInspectionItemId, and Status fields
3. WHEN selecting a representative record from each group, THE System SHALL use $first operator to preserve the first record after sorting
4. THE System SHALL apply $skip and $limit stages after the $group stage
### Requirement 4: Cross-Collection Query Support
**User Story:** As a system administrator, I want deduplication to work correctly across multiple time-sharded collections, so that historical data is properly consolidated.
#### Acceptance Criteria
1. WHEN querying across multiple collections, THE System SHALL apply deduplication logic to the unified result set
2. THE System SHALL use $unionWith to combine data from multiple collections before deduplication
3. WHEN building the aggregation pipeline, THE System SHALL ensure consistent field projection across all collections
### Requirement 5: Performance Optimization
**User Story:** As a system user, I want query performance to remain acceptable after implementing proper deduplication, so that the application remains responsive.
#### Acceptance Criteria
1. THE System SHALL use appropriate MongoDB indexes to optimize grouping operations
2. WHEN performing deduplication, THE System SHALL minimize memory usage by using pipeline stages efficiently
3. THE System SHALL log performance metrics for deduplication operations
4. WHEN query time exceeds acceptable thresholds, THE System SHALL log warnings for monitoring
### Requirement 6: Backward Compatibility
**User Story:** As a developer, I want the API interface to remain unchanged, so that existing clients continue to work without modification.
#### Acceptance Criteria
1. THE FindDatas method SHALL maintain its current signature
2. THE System SHALL return results in the same format as before
3. WHEN processing search conditions, THE System SHALL support all existing filter parameters
4. THE System SHALL maintain the same sorting behavior for deduplicated results