private void WorkCalendarContinuousTime(List<WorkCalendar> workCalendars) { List<WorkCalendar> addList = new List<WorkCalendar>(); if (workCalendars != null && workCalendars.Count > 0) { for (int i = 0; i < workCalendars.Count - 1; i++) { if (DateTime.Compare(workCalendars[i].EndTime, workCalendars[i + 1].StartTime) < 0) { if (workCalendars[i].Type == BusinessConstants.CODE_MASTER_WORKCALENDAR_TYPE_VALUE_WORK) { WorkCalendar workCalendar = new WorkCalendar(); DateTime startTime = workCalendars[i].EndTime; CloneHelper.CopyProperty(workCalendars[i], workCalendar); workCalendar.ShiftCode = null; workCalendar.ShiftName = null; workCalendar.Type = BusinessConstants.CODE_MASTER_WORKCALENDAR_TYPE_VALUE_REST; workCalendar.StartTime = workCalendars[i].EndTime; workCalendar.EndTime = workCalendars[i + 1].StartTime; addList.Add(workCalendar); } else { workCalendars[i].EndTime = workCalendars[i + 1].StartTime; } } } } if (addList.Count > 0) { workCalendars.AddRange(addList); } workCalendars.Sort(WorkCalendarTimeCompare); }
public List<WorkCalendar> GetWorkCalendar(DateTime date, string region, string workcenter) { List<WorkCalendar> workCalendars = new List<WorkCalendar>(); date = date.Date; string dayofweek = date.DayOfWeek.ToString(); IList<Workday> workdayList = WorkdayMgrE.GetWorkdayByDayofweekWizard(dayofweek, region, workcenter); if (workdayList == null || workdayList.Count == 0) return null; Workday workday = workdayList[0]; WorkCalendar workCalendar = new WorkCalendar(); if (workday != null) { IList<Shift> shifts = WorkdayShiftMgrE.GetShiftsByWorkdayId(workday.Id); if (shifts.Count > 0) { foreach (Shift s in shifts) { Shift shift = ShiftMgrE.LoadShift(s.Code, date); if (shift == null) continue; DateTime lastendtime = DateTime.MaxValue; int length = shift.ShiftTime.Split('|').GetLength(0); string[] times = shift.ShiftTime.Split('|'); foreach (string str in times) { string[] time = str.Split('-'); DateTime starttime = this.AssembleActualTime(date, time[0]); DateTime endtime = this.AssembleActualTime(date, time[1]); if (DateTime.Compare(starttime, endtime) >= 0) { endtime = endtime.AddDays(1); } if (DateTime.Compare(lastendtime, starttime) < 0) { workCalendar = new WorkCalendar(); workCalendar.Date = date; workCalendar.DayOfWeek = dayofweek; workCalendar.ShiftCode = shift.Code; workCalendar.ShiftName = shift.ShiftName; workCalendar.StartTime = lastendtime; workCalendar.EndTime = starttime; workCalendar.Type = BusinessConstants.CODE_MASTER_WORKCALENDAR_TYPE_VALUE_REST; workCalendars.Add(workCalendar); } workCalendar = new WorkCalendar(); workCalendar.Date = date; workCalendar.DayOfWeek = dayofweek; workCalendar.ShiftCode = shift.Code; workCalendar.ShiftName = shift.ShiftName; workCalendar.StartTime = starttime; workCalendar.EndTime = endtime; workCalendar.Type = workday.Type; workCalendars.Add(workCalendar); lastendtime = endtime; } } } else { workCalendar = new WorkCalendar(); workCalendar.Date = date; workCalendar.DayOfWeek = dayofweek; workCalendar.StartTime = date; workCalendar.EndTime = date.AddDays(1); workCalendar.Type = workday.Type; workCalendars.Add(workCalendar); } } else { workCalendar = new WorkCalendar(); workCalendar.Date = date; workCalendar.DayOfWeek = dayofweek; workCalendar.StartTime = date; workCalendar.EndTime = date.AddDays(1); workCalendar.Type = BusinessConstants.CODE_MASTER_WORKCALENDAR_TYPE_VALUE_WORK; workCalendars.Add(workCalendar); } workCalendars.Sort(WorkCalendarTimeCompare); workCalendars = this.SpecialTimeWizard(date, region, workcenter, workCalendars); workCalendars = this.WorkCalendarDataClean(workCalendars); return workCalendars; }
private static int WorkCalendarTimeCompare(WorkCalendar x, WorkCalendar y) { return DateTime.Compare(x.StartTime, y.StartTime); }
private List<WorkCalendar> SpecialTimeWizard(DateTime date, string region, string workcenter, List<WorkCalendar> workCalendars) { List<WorkCalendar> addList = new List<WorkCalendar>(); date = date.Date; string dayofweek = date.DayOfWeek.ToString(); DateTime dateShiftStart = date; DateTime dateShiftEnd = date.AddDays(1); if (workCalendars.Count > 0) { dateShiftStart = workCalendars[0].StartTime; dateShiftEnd = workCalendars[workCalendars.Count - 1].EndTime; } IList specialTimes = SpecialTimeMgrE.GetReferSpecialTimeWizard(dateShiftStart, dateShiftEnd, region, workcenter); if (specialTimes.Count > 0) { foreach (WorkCalendar workCalendar in workCalendars) { WorkCalendar newWorkCalendar = new WorkCalendar(); foreach (SpecialTime specialTime in specialTimes) { //SpecialTime ------ //WorkCalendar ------ if (DateTime.Compare(specialTime.EndTime, workCalendar.StartTime) <= 0) { continue; } //SpecialTime ------ //WorkCalendar ------ else if (DateTime.Compare(specialTime.StartTime, workCalendar.EndTime) >= 0) { continue; } else { if (specialTime.Type == workCalendar.Type) { continue; } else { if (DateTime.Compare(specialTime.StartTime, workCalendar.StartTime) <= 0) { //SpecialTime ---------- //WorkCalendar ------ if (DateTime.Compare(specialTime.EndTime, workCalendar.EndTime) >= 0) { workCalendar.Type = specialTime.Type; } //SpecialTime ------ //WorkCalendar ------ else { newWorkCalendar = new WorkCalendar(); CloneHelper.CopyProperty(workCalendar, newWorkCalendar); newWorkCalendar.EndTime = specialTime.EndTime; newWorkCalendar.Type = specialTime.Type; addList.Add(newWorkCalendar); workCalendar.StartTime = specialTime.EndTime; } } else { //SpecialTime ------ //WorkCalendar ------ if (DateTime.Compare(specialTime.EndTime, workCalendar.EndTime) >= 0) { newWorkCalendar = new WorkCalendar(); CloneHelper.CopyProperty(workCalendar, newWorkCalendar); newWorkCalendar.StartTime = specialTime.StartTime; newWorkCalendar.EndTime = workCalendar.EndTime; newWorkCalendar.Type = specialTime.Type; addList.Add(newWorkCalendar); workCalendar.EndTime = specialTime.StartTime; } //SpecialTime ------ //WorkCalendar ---------- else { newWorkCalendar = new WorkCalendar(); CloneHelper.CopyProperty(workCalendar, newWorkCalendar); newWorkCalendar.EndTime = specialTime.StartTime; newWorkCalendar.Type = workCalendar.Type; addList.Add(newWorkCalendar); newWorkCalendar = new WorkCalendar(); CloneHelper.CopyProperty(workCalendar, newWorkCalendar); newWorkCalendar.StartTime = specialTime.EndTime; newWorkCalendar.Type = workCalendar.Type; addList.Add(newWorkCalendar); workCalendar.StartTime = specialTime.StartTime; workCalendar.EndTime = specialTime.EndTime; workCalendar.Type = specialTime.Type; } } } } } } } else { return workCalendars; } workCalendars.AddRange(addList); workCalendars.Sort(WorkCalendarTimeCompare); return workCalendars; }