LMES Cross-Day Shift Production Statistics Solution
Background
In the production process, it is common for a shift to span across two natural days (e.g., Night Shift: 20:00 to 08:00 the next day). Traditional statistics based on natural days cannot accurately reflect the production output of the shift. This document provides a solution for cross-day shift production statistics in LMES.
Solution Overview
1. Logical Day Definition
Introduce the concept of "Production Date" (WorkDate).
- If the shift does not span across days, Production Date = Natural Date.
- If the shift spans across days, the time period belonging to the next day is calculated into the Production Date of the previous day (start time).
2. Shift Configuration
Add the "Cross-day" flag and "Offset Time" to the shift configuration.
- Shift A: 08:00 - 20:00 (Non-cross-day)
- Shift B: 20:00 - 08:00 (+1 day) (Cross-day)
3. Data Storage
When storing production records (ProductionRecord), store both CreationTime (Physical Time) and WorkDate (Logical Date).
public class ProductionRecord : Entity<Guid>
{
public DateTime CreationTime { get; set; } // Actual time: 2023-10-02 01:00:00
public DateTime WorkDate { get; set; } // Logical date: 2023-10-01
public string ShiftId { get; set; } // Shift B
}
Implementation Logic
SQL Query Logic
When querying shift output, group by WorkDate and ShiftId instead of CreationTime.
SELECT WorkDate, ShiftId, COUNT(*) as Output
FROM ProductionRecords
WHERE WorkDate BETWEEN '2023-10-01' AND '2023-10-31'
GROUP BY WorkDate, ShiftId
Automatic Calculation of WorkDate
When inserting a record, automatically calculate WorkDate based on the current time and shift rules.
public DateTime CalculateWorkDate(DateTime now, Shift shift)
{
if (shift.IsCrossDay && now.TimeOfDay < shift.EndTime.TimeOfDay)
{
return now.Date.AddDays(-1);
}
return now.Date;
}