예제 #1
0
        public void CalculateAttendRecordWithExtrawork(DateTime datetime, List <string> shiftCodes = null, StaffSearchModel searchModel = null)
        {
            // 是否是休息日
            IWorkAndRestService wars = new WorkAndRestService(this.DbString);
            WorkAndRest         wr   = wars.FindByDate(datetime);
            bool          isRestDay  = wars.IsRestDay(wr);
            SystemSetting setting    = new SystemSettingService(this.DbString).Find();


            DataContext dc = new DataContext(this.DbString);



            // 计算在职人员的考勤
            List <Staff> staffs = dc.Context.GetTable <Staff>().Where(s => (s.workStatus == (int)WorkStatus.OnWork) || (s.workStatus == (int)WorkStatus.OffWork && s.resignAt >= datetime)).ToList();

            foreach (Staff staff in staffs)
            {
                double workdayHour = 0;

                double extraHour = 0;
                // 异常情况
                List <AttendanceExceptionType> exceptions = new List <AttendanceExceptionType>();
                if (staff.nr.Equals("201471"))
                {
                    string s = "201471";
                }
                // 获取已经结束的排班
                List <ShiftScheduleView> shifts = dc.Context.GetTable <ShiftScheduleView>().Where(s => s.staffNr.Equals(staff.nr) && s.fullStartAt.Value.Date == datetime.Date && s.fullEndAt <= DateTime.Now).ToList();
                if (shifts.Count > 0)
                {
                    foreach (var shift in shifts)
                    {
                        DateTime validSQ = shift.fullStartAt.Value.AddMinutes(0 - setting.validAttendanceRecordTime.Value);
                        DateTime validEQ = shift.fullEndAt.Value.AddMinutes(0 + setting.validAttendanceRecordTime.Value);
                        DateTime sq      = validSQ;
                        DateTime eq      = shift.shiftType == (int)ShiftType.Today ? datetime.Date.AddDays(1).AddHours(8) : datetime.Date.AddDays(1).AddHours(23);

                        ShiftScheduleView nextShift = new ShiftSheduleService(this.DbString).GetNextShiftScheduleView(staff.nr, shift.fullEndAt.Value);

                        if (nextShift != null)
                        {
                            if ((nextShift.fullStartAt.Value.Date - shift.fullStartAt.Value.Date).TotalDays < 2)
                            {
                                eq = nextShift.fullStartAt.Value.AddMinutes(0 - setting.validAttendanceRecordTime.Value);
                            }
                        }

                        List <AttendanceRecordDetail> records = new AttendanceRecordDetailService(this.DbString).GetByStaffAndTimespan(staff.nr, sq, eq);
                        if (records.Count == 0)
                        {
                            // 旷工,可能他有请假。。。
                            exceptions.Add(AttendanceExceptionType.Absence);
                        }
                        else
                        {
                            /// 清洗数据
                            MarkRepeatRecordByMinute(records, (float)setting.repeatAttendanceRecordTime.Value);
                            records = records.Where(s => s.isRepeatedData == false).ToList();

                            // 如果考勤记录不配对,如 进-出 的偶数被,则是打开记录不完整
                            // 但是这不影响计算
                            if (records.Count % 2 != 0)
                            {
                                exceptions.Add(AttendanceExceptionType.MessRecord);
                            }

                            /// 开始计算
                            /// 早到的使用排班开始时间,晚到的算迟到,提早走的算早退
                            /// 此处不涉及到多次进出-计算目前只能算出大概,很难精确
                            /// 打卡迟了肯定算迟到
                            /// 如果早退了一般不会来加班了
                            // 是否有早打卡?
                            // int hasEarlyCount= records.Where(s => s.recordAt < shift.fullStartAt.Value).Count();

                            if (records.Count == 1)
                            {
                                exceptions.Add(AttendanceExceptionType.MessRecord);
                                /// 以下都是推测,都是要人为的调整的
                                DateTime recordAt = records[0].recordAt;
                                // 如果是早来,迟走,可能是忘记打卡了
                                if (recordAt < shift.fullStartAt.Value || (records.Last().recordAt >= shift.fullEndAt.Value && recordAt <= validEQ))
                                {
                                    /// 如果早来超过一个小时,可能是加班
                                    if (recordAt <= shift.fullStartAt.Value.AddHours(-1))
                                    {
                                        extraHour = (shift.fullStartAt.Value - recordAt).TotalHours;
                                    }
                                    workdayHour = (shift.fullEndAt.Value - shift.fullStartAt.Value).TotalHours;
                                }// 如果迟走超过一个小时,可能是加班,即使此处误判,也需要加班单存在的
                                else if (recordAt >= shift.fullEndAt.Value.AddHours(1))
                                {
                                    exceptions.Add(AttendanceExceptionType.ExtraWork);
                                    workdayHour = (shift.fullEndAt.Value - shift.fullStartAt.Value).TotalHours;
                                    extraHour   = (recordAt - shift.fullEndAt.Value).TotalHours;
                                }
                                else
                                {
                                    // 如果只有一次打卡记录在应该的工作区间内,则需要人工手动调整了!
                                }
                            }
                            else
                            {
                                DateTime firstD = records.First().recordAt;
                                DateTime lastD  = records.Last().recordAt;
                                // 正常上班,无加班
                                // 1. 如果走时间在排班结束范围外内,则算整个排班时间
                                if (lastD >= shift.fullEndAt.Value &&
                                    lastD <= validEQ)
                                {
                                    if (firstD <= shift.fullStartAt)
                                    {
                                        /// 如果早来超过一个小时,可能是加班
                                        if (firstD <= shift.fullStartAt.Value.AddHours(-1))
                                        {
                                            extraHour = (shift.fullStartAt.Value - firstD).TotalHours;
                                        }
                                        // 正常
                                        workdayHour = (shift.fullEndAt.Value - shift.fullStartAt.Value).TotalHours;
                                    }
                                    else
                                    {
                                        // 迟到
                                        exceptions.Add(AttendanceExceptionType.Late);
                                        workdayHour = (shift.fullEndAt.Value - firstD).TotalHours;
                                    }
                                }
                                else
                                {
                                    if (lastD < shift.fullEndAt.Value)
                                    {
                                        // 早退
                                        exceptions.Add(AttendanceExceptionType.EarlyLeave);
                                        if (firstD < shift.fullStartAt)
                                        {
                                            /// 如果早来超过一个小时,可能是加班
                                            if (firstD <= shift.fullStartAt.Value.AddHours(-1))
                                            {
                                                extraHour = (shift.fullStartAt.Value - firstD).TotalHours;
                                            }
                                            workdayHour = (lastD - shift.fullStartAt.Value).TotalHours;
                                        }
                                        else
                                        {
                                            // 迟到+早退
                                            exceptions.Add(AttendanceExceptionType.Late);
                                            workdayHour = (lastD - firstD).TotalHours;
                                        }
                                    }
                                    else
                                    {
                                        if (lastD >= shift.fullEndAt.Value.AddHours(1))
                                        {
                                            exceptions.Add(AttendanceExceptionType.ExtraWork);

                                            workdayHour = (shift.fullEndAt.Value - shift.fullStartAt.Value).TotalHours;
                                            extraHour   = (lastD - shift.fullEndAt.Value).TotalHours;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    // 如果没有排班怎么计算?
                    // 如果没有排班,应该都是加班
                    // TODO, 根据目前和德晋HR了解情况,会排班,所以这种情况暂时不考虑
                }
                /// 暂时对没有排班的不处理
                if (shifts.Count > 0)
                {
                    // 基本工时都是 8 小时,不分部门和职称
                    if (workdayHour > 8)
                    {
                        workdayHour = 8;
                    }

                    /// 如果是双休或节假日都算加班!无论是否有排班
                    if (isRestDay)
                    {
                        extraHour  += workdayHour;
                        workdayHour = 0;
                    }
                    extraHour   = Math.Round(extraHour, 1);
                    workdayHour = Math.Round(workdayHour, 1);

                    // 如果是成型课 或者 行政课的司机,则加班不减0.5h,其它的都减
                    if (extraHour > 0)
                    {
                        exceptions.Add(AttendanceExceptionType.ExtraWork);
                        if (staff.IsMinusExtraWorkHour)
                        {
                            extraHour -= 0.5;
                            if (extraHour < 0)
                            {
                                extraHour = 0;
                            }
                        }
                        ExtraWorkRecord extraRecord = new ExtraWorkRecordService(this.DbString).FindByStaffNrAndDete(staff.nr, datetime.Date);
                        if (extraRecord == null)
                        {
                            exceptions.Add(AttendanceExceptionType.ExtraWorkNoRecord);
                        }
                        else
                        {
                            if (extraHour != extraRecord.duration)
                            {
                                exceptions.Add(AttendanceExceptionType.ExtraWorkHourNotMatch);
                            }

                            if (wr.dateType != extraRecord.ExtraWorkType.systemCode)
                            {
                                exceptions.Add(AttendanceExceptionType.ExtraWorkTypeNotMatch);
                            }
                        }
                    }

                    DataContext         comitDC   = new DataContext(this.DbString);
                    AttendanceRecordCal calRecord = comitDC.Context.GetTable <AttendanceRecordCal>().FirstOrDefault(s => s.staffNr.Equals(staff.nr) && s.attendanceDate.Equals(datetime.Date));

                    exceptions = exceptions.Distinct().ToList();
                    int?extraType = extraHour == 0 ? null : wr.dateType;
                    if (calRecord == null)
                    {
                        comitDC.Context.GetTable <AttendanceRecordCal>().InsertOnSubmit(new AttendanceRecordCal()
                        {
                            staffNr              = staff.nr,
                            attendanceDate       = datetime.Date,
                            oriWorkingHour       = workdayHour,
                            actWorkingHour       = workdayHour,
                            oriExtraWorkingHour  = extraHour,
                            actExtraWorkingHour  = extraHour,
                            extraworkType        = extraType,
                            attendanceExceptions = exceptions.Distinct().ToList(),
                            createdAt            = DateTime.Now
                        });
                    }
                    else
                    {
                        calRecord.extraworkType        = extraType;
                        calRecord.oriWorkingHour       = calRecord.actWorkingHour = workdayHour;
                        calRecord.oriExtraWorkingHour  = calRecord.actExtraWorkingHour = extraHour;
                        calRecord.createdAt            = DateTime.Now;
                        calRecord.attendanceExceptions = exceptions;
                    }
                    comitDC.Context.SubmitChanges();
                }
            }
        }
예제 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="searchModel"></param>
        /// <param name="maxExtraTotalHour"></param>
        /// <returns></returns>
        public List <WorkSumExcelModel> GetSumExcelModels(DateTime startDate, DateTime endDate, StaffSearchModel searchModel, double maxExtraTotalHour = 36)
        {
            List <WorkSumExcelModel> records = new List <WorkSumExcelModel>();
            DataContext dc = new DataContext(this.DbString);

            IQueryable <StaffView> staffQ             = dc.Context.GetTable <StaffView>();
            IQueryable <AttendanceRecordCalView> attQ = dc.Context.GetTable <AttendanceRecordCalView>();
            IQueryable <AbsenceRecordView>       abQ  = dc.Context.GetTable <AbsenceRecordView>();

            staffQ = staffQ.Where(s => (s.workStatus == (int)WorkStatus.OnWork) ||
                                  (s.workStatus == (int)WorkStatus.OffWork && (s.resignAt == null || s.resignAt >= startDate)));
            attQ = attQ.Where(s => (s.workStatus == (int)WorkStatus.OnWork) ||
                              (s.workStatus == (int)WorkStatus.OffWork && (s.resignAt == null || s.resignAt >= startDate)));
            abQ = abQ.Where(s => (s.workStatus == (int)WorkStatus.OnWork) ||
                            (s.workStatus == (int)WorkStatus.OffWork && (s.resignAt == null || s.resignAt >= startDate)));

            if (searchModel.companyId.HasValue)
            {
                staffQ = staffQ.Where(s => s.companyId == searchModel.companyId.Value);
                attQ   = attQ.Where(s => s.companyId == searchModel.companyId.Value);
                abQ    = abQ.Where(s => s.companyId == searchModel.companyId.Value);
            }

            if (searchModel.departmentId.HasValue)
            {
                staffQ = staffQ.Where(s => s.departmentId == searchModel.departmentId.Value);
                attQ   = attQ.Where(s => s.departmentId == searchModel.departmentId.Value);
                abQ    = abQ.Where(s => s.departmentId == searchModel.departmentId.Value);
            }

            if (searchModel.StaffNrs != null && searchModel.StaffNrs.Count > 0)
            {
                staffQ = staffQ.Where(s => searchModel.StaffNrs.Contains(s.nr));
                attQ   = attQ.Where(s => searchModel.StaffNrs.Contains(s.nr));
                abQ    = abQ.Where(s => searchModel.StaffNrs.Contains(s.nr));
            }

            attQ = attQ.Where(s => s.attendanceDate >= startDate && s.attendanceDate <= endDate);
            abQ  = abQ.Where(s => s.absenceDate >= startDate && s.absenceDate <= endDate);

            List <StaffView> staffs = staffQ.ToList();
            List <AttendanceRecordCalView> attends = attQ.ToList();
            List <AbsenceRecordView>       abs     = abQ.ToList();
            List <WorkAndRest>             wrs     = new WorkAndRestService(this.DbString).GetByDateSpan(startDate, endDate);

            int    OriginAttendDays  = wrs.Where(s => s.IsWorkDay).Count();
            double OriginAttendHours = OriginAttendDays * WORK_DAY_HOUR;

            // 初始化系统预定义缺勤类型
            List <AbsenceType> abTypes = new AbsenceTypeService(this.DbString).GetSystemTypes();
            Dictionary <SystemAbsenceType, int> abTypesDic = new Dictionary <SystemAbsenceType, int>();

            foreach (var abT in abTypes)
            {
                abTypesDic.Add((SystemAbsenceType)abT.systemCode.Value, abT.id);
            }

            foreach (var staff in staffs)
            {
                WorkSumExcelModel sumModel = new WorkSumExcelModel()
                {
                    Staff = staff, Items = new List <WorkSumExcelItemModel>()
                };
                for (var date = startDate; date <= endDate; date = date.AddDays(1))
                {
                    AttendanceRecordCalView at = attends.FirstOrDefault(s => s.staffNr.Equals(staff.nr) && s.attendanceDate == date);
                    AbsenceRecordView       ab = abs.FirstOrDefault(s => s.staffNr.Equals(staff.nr) && s.absenceDate == date);
                    WorkAndRest             wr = wrs.FirstOrDefault(s => s.dateAt == date);
                    sumModel.Items.Add(new WorkSumExcelItemModel()
                    {
                        Id = Guid.NewGuid().ToString(), Staff = staff, DateTime = date, WorkAndRest = wr, AttendanceRecordCal = at, AbsenceRecord = ab
                    });
                }

                IEnumerable <AttendanceRecordCalView> baseATQ = attends.Where(s => s.staffNr.Equals(staff.nr));
                IEnumerable <AbsenceRecordView>       baseABQ = abs.Where(s => s.staffNr.Equals(staff.nr));

                // 节假日总时间的统计
                #region 节假日总时间的统计

                // 放班- -
                double?HolidayWork = sumModel.Items.Where(s => s.AbsenceRecord != null && s.AbsenceRecord.absenceTypeId == abTypesDic[SystemAbsenceType.HolidayWork]).Sum(s => s.AbsenceRecord.duration);
                sumModel.HolidayWork = HolidayWork.HasValue ? HolidayWork.Value : 0;

                double?HolidayAbsence = sumModel.Items.Where(s => s.AbsenceRecord != null && s.AbsenceRecord.absenceTypeId == abTypesDic[SystemAbsenceType.HolidayAbsence]).Sum(s => s.AbsenceRecord.duration);
                sumModel.HolidayAbsence = HolidayAbsence.HasValue ? HolidayAbsence.Value : 0;

                double?SickAbsence = sumModel.Items.Where(s => s.AbsenceRecord != null && s.AbsenceRecord.absenceTypeId == abTypesDic[SystemAbsenceType.SickAbsence]).Sum(s => s.AbsenceRecord.duration);
                sumModel.SickAbsence = SickAbsence.HasValue ? SickAbsence.Value : 0;

                double?MaternityAbsence = sumModel.Items.Where(s => s.AbsenceRecord != null && s.AbsenceRecord.absenceTypeId == abTypesDic[SystemAbsenceType.MaternityAbsence]).Sum(s => s.AbsenceRecord.duration);
                sumModel.MaternityAbsence = MaternityAbsence.HasValue ? MaternityAbsence.Value : 0;

                double?FuneralAbsence = sumModel.Items.Where(s => s.AbsenceRecord != null && s.AbsenceRecord.absenceTypeId == abTypesDic[SystemAbsenceType.FuneralAbsence]).Sum(s => s.AbsenceRecord.duration);
                sumModel.FuneralAbsence = FuneralAbsence.HasValue ? FuneralAbsence.Value : 0;

                double?TurnAbsence = sumModel.Items.Where(s => s.AbsenceRecord != null && s.AbsenceRecord.absenceTypeId == abTypesDic[SystemAbsenceType.TurnAbsence]).Sum(s => s.AbsenceRecord.duration);
                sumModel.TurnAbsence = TurnAbsence.HasValue ? TurnAbsence.Value : 0;


                double?PublicAbsence = sumModel.Items.Where(s => s.AbsenceRecord != null && s.AbsenceRecord.absenceTypeId == abTypesDic[SystemAbsenceType.PublicAbsence]).Sum(s => s.AbsenceRecord.duration);
                sumModel.PublicAbsence = PublicAbsence.HasValue ? PublicAbsence.Value : 0;

                double?YearAbsence = sumModel.Items.Where(s => s.AbsenceRecord != null && s.AbsenceRecord.absenceTypeId == abTypesDic[SystemAbsenceType.YearAbsence]).Sum(s => s.AbsenceRecord.duration);
                sumModel.YearAbsence = YearAbsence.HasValue ? YearAbsence.Value : 0;

                double?WorkAbsence = sumModel.Items.Where(s => s.AbsenceRecord != null && s.AbsenceRecord.absenceTypeId == abTypesDic[SystemAbsenceType.WorkAbsence]).Sum(s => s.AbsenceRecord.duration);
                sumModel.WorkAbsence = WorkAbsence.HasValue ? WorkAbsence.Value : 0;

                // 新进
                sumModel.NewAbsence = sumModel.Items.Where(s => s.IsNew == true && s.WorkAndRest.IsWorkDay == true).Count() * WORK_DAY_HOUR;
                // 离职
                sumModel.ResignAbsence = sumModel.Items.Where(s => s.IsResign == true && s.WorkAndRest.IsWorkDay == true).Count() * WORK_DAY_HOUR;
                #endregion

                // 出勤工时
                #region 出勤工时
                sumModel.AttendHour = OriginAttendHours - sumModel.Items.Where(s => s.AbsenceRecord != null && abTypesDic.Where(ss => ss.Key != SystemAbsenceType.HolidayWork).Select(ss => ss.Value).Contains(s.AbsenceRecord.absenceTypeId)).Sum(s => s.AbsenceRecord.duration) - sumModel.NewAbsence - sumModel.ResignAbsence;
                #endregion

                // 原始记录的加班统计
                #region 原始记录的加班统计
                double?WorkExtraHour = baseATQ.Where(s => s.extraworkType == (int)SystemExtraType.WorkExtra).Sum(s => s.actExtraWorkingHour);
                sumModel.OriWorkExtraHour = WorkExtraHour.HasValue ? WorkExtraHour.Value : 0;


                double?WeekendExtraHour = baseATQ.Where(s => s.extraworkType == (int)SystemExtraType.WeekendExtra).Sum(s => s.actExtraWorkingHour);
                sumModel.OriWeekendExtraHour = WeekendExtraHour.HasValue ? WeekendExtraHour.Value : 0;


                double?HolidayExtraHour = baseATQ.Where(s => s.extraworkType == (int)SystemExtraType.HolidayExtra).Sum(s => s.actExtraWorkingHour);
                sumModel.OriHolidayExtraHour = HolidayExtraHour.HasValue ? HolidayExtraHour.Value : 0;
                #endregion

                // 计算统计!!!!
                #region
                // 计算减去放班的加班总计
                // 计算A->B,减去放班的统计 PlanA, 可以根据其他的总计,舍弃这个这个方式
                //#region 计算A->B,减去放班的统计
                //double workHoliday = sumModel.HolidayWork;
                //if (workHoliday >= 0)
                //{
                //    if (sumModel.OriHolidayExtraHour >= workHoliday)
                //    {
                //        sumModel.MinusedHolidayWorkHolidayLeftExtraHour = sumModel.OriHolidayExtraHour - workHoliday;
                //        workHoliday = 0;
                //    }
                //    else
                //    {
                //        sumModel.MinusedHolidayWorkHolidayLeftExtraHour = 0;
                //        workHoliday = workHoliday - sumModel.OriHolidayExtraHour;
                //    }
                //    if (workHoliday >= 0)
                //    {
                //        if (sumModel.OriWeekendExtraHour >= workHoliday)
                //        {
                //            sumModel.MinusedHolidayWorkWeekendLeftExtraHour = sumModel.OriWeekendExtraHour - workHoliday;
                //            workHoliday = 0;
                //        }
                //        else
                //        {
                //            sumModel.MinusedHolidayWorkWeekendLeftExtraHour = 0;
                //            workHoliday = workHoliday - sumModel.OriWeekendExtraHour;
                //        }

                //        if (workHoliday >= 0)
                //        {
                //            if (sumModel.OriWorkExtraHour >= workHoliday)
                //            {
                //                sumModel.MinusedHolidayWorkWorkLeftExtraHour = sumModel.OriWorkExtraHour - workHoliday;
                //                workHoliday = 0;
                //            }
                //            else
                //            {
                //                sumModel.MinusedHolidayWorkWorkLeftExtraHour = 0;
                //                workHoliday = workHoliday - sumModel.OriWorkExtraHour;
                //            }
                //        }
                //    }

                //}
                //#endregion

                // 计算

                // 分别计算单个的 去掉放班的时间
                #region 分别计算单个的 去掉放班 的时间
                if (sumModel.HolidayWork > 0)
                {
                    double factor = sumModel.HolidayWork;

                    // 先将 Items 排序,
                    // 按 type 降序,即 节假日、双休、平时,
                    // 按 加班量 降序
                    var q = sumModel.Items.Where(s => s.AttendanceRecordCal != null)
                            .OrderByDescending(s => s.DateTime)
                            //  .OrderByDescending(s => s.AttendanceRecordCal.actExtraWorkingHour)
                            .OrderByDescending(s => s.AttendanceRecordCal.extraworkType);
                    List <WorkSumExcelItemModel> sortedItems = q.ToList();
                    foreach (var item in sortedItems)
                    {
                        if (factor <= 0)
                        {
                            sumModel.Items.FirstOrDefault(s => s.Id == item.Id).MinusedHolidayWorkHour = 0;
                        }
                        else
                        {
                            if (factor <= item.AttendanceRecordCal.actExtraWorkingHour)
                            {
                                double xfactor = item.AttendanceRecordCal.actExtraWorkingHour.Value - factor;
                                if (xfactor >= 4)
                                {
                                    sumModel.Items.FirstOrDefault(s => s.Id == item.Id).MinusedHolidayWorkHour = factor;
                                }
                                else
                                {
                                    sumModel.Items.FirstOrDefault(s => s.Id == item.Id).MinusedHolidayWorkHour = item.AttendanceRecordCal.actExtraWorkingHour.Value;
                                }
                                factor = 0;
                            }
                            else
                            {
                                sumModel.Items.FirstOrDefault(s => s.Id == item.Id).MinusedHolidayWorkHour = item.AttendanceRecordCal.actExtraWorkingHour.Value;
                                factor = factor - item.AttendanceRecordCal.actExtraWorkingHour.Value;
                            }
                        }
                    }
                }
                #endregion

                // 计算减去放班的加班总计
                // 计算A->B,减去放班的统计 PlanB
                #region 计算A->B,减去放班的统计
                double?MinusedHolidayWorkWorkLeftExtraHour = sumModel.Items.Where(s => s.AttendanceRecordCal != null && s.AttendanceRecordCal.extraworkType == (int)SystemExtraType.WorkExtra).Sum(s => s.MinuseHolidayWorkLeftExtraHour);
                sumModel.MinusedHolidayWorkWorkLeftExtraHour = MinusedHolidayWorkWorkLeftExtraHour.HasValue ? MinusedHolidayWorkWorkLeftExtraHour.Value : 0;


                double?MinusedHolidayWorkWeekendLeftExtraHour = sumModel.Items.Where(s => s.AttendanceRecordCal != null && s.AttendanceRecordCal.extraworkType == (int)SystemExtraType.WeekendExtra).Sum(s => s.MinuseHolidayWorkLeftExtraHour);
                sumModel.MinusedHolidayWorkWeekendLeftExtraHour = MinusedHolidayWorkWeekendLeftExtraHour.HasValue ? MinusedHolidayWorkWeekendLeftExtraHour.Value : 0;

                double?MinusedHolidayWorkHolidayLeftExtraHour = sumModel.Items.Where(s => s.AttendanceRecordCal != null && s.AttendanceRecordCal.extraworkType == (int)SystemExtraType.HolidayExtra).Sum(s => s.MinuseHolidayWorkLeftExtraHour);
                sumModel.MinusedHolidayWorkHolidayLeftExtraHour = MinusedHolidayWorkHolidayLeftExtraHour.HasValue ? MinusedHolidayWorkHolidayLeftExtraHour.Value : 0;

                #endregion


                // 分别计算单个的 去掉放班的时间
                #region 分别计算单个的 打于maxExtraTotalHour 的时间
                if (sumModel.MinusedHolidayLeftTotalExtraHour > maxExtraTotalHour)
                {
                    double factor = sumModel.MinusedHolidayLeftTotalExtraHour - maxExtraTotalHour;
                    // 先将 Items 排序,
                    // 按 type 降序,即 节假日、双休、平时,
                    // 按 加班量减掉放班的剩余量 降序
                    var q = sumModel.Items.Where(s => s.AttendanceRecordCal != null)
                            //.OrderBy(s => s.DateTime)
                            .OrderByDescending(s => s.MinuseHolidayWorkLeftExtraHour)
                            .OrderByDescending(s => s.AttendanceRecordCal.extraworkType);
                    List <WorkSumExcelItemModel> sortedItems = q.ToList();

                    foreach (var item in sortedItems)
                    {
                        if (factor <= 0)
                        {
                            sumModel.Items.FirstOrDefault(s => s.Id == item.Id).MinusedThresholdHour = 0;
                        }
                        else
                        {
                            if (factor <= item.MinuseHolidayWorkLeftExtraHour)
                            {
                                //sumModel.Items.FirstOrDefault(s => s.Id == item.Id).MinusedThresholdHour = factor;
                                //factor = 0;
                                double xfactor = item.MinuseHolidayWorkLeftExtraHour - factor;
                                if (xfactor >= 4)
                                {
                                    sumModel.Items.FirstOrDefault(s => s.Id == item.Id).MinusedThresholdHour = factor;
                                }
                                else
                                {
                                    sumModel.Items.FirstOrDefault(s => s.Id == item.Id).MinusedThresholdHour = item.MinuseHolidayWorkLeftExtraHour;
                                }
                                factor = 0;
                            }
                            else
                            {
                                sumModel.Items.FirstOrDefault(s => s.Id == item.Id).MinusedThresholdHour = item.MinuseHolidayWorkLeftExtraHour;
                                factor = factor - item.MinuseHolidayWorkLeftExtraHour;
                            }
                        }
                    }
                }
                #endregion


                #region 统计加班



                // 计算B->C,减去多余加班的统计
                #region 计算B->C,减去多余加班的统计
                double?MinusedThresholdWorkLeftExtraHour = sumModel.Items.Where(s => s.AttendanceRecordCal != null && s.AttendanceRecordCal.extraworkType == (int)SystemExtraType.WorkExtra).Sum(s => s.MinuseHolidayWorkAndThresHoldLeftExtraHour);
                sumModel.MinusedThresholdWorkLeftExtraHour = MinusedThresholdWorkLeftExtraHour.HasValue ? MinusedThresholdWorkLeftExtraHour.Value : 0;


                double?MinusedThresholdWeekendLeftExtraHour = sumModel.Items.Where(s => s.AttendanceRecordCal != null && s.AttendanceRecordCal.extraworkType == (int)SystemExtraType.WeekendExtra).Sum(s => s.MinuseHolidayWorkAndThresHoldLeftExtraHour);
                sumModel.MinusedThresholdWeekendLeftExtraHour = MinusedThresholdWeekendLeftExtraHour.HasValue ? MinusedThresholdWeekendLeftExtraHour.Value : 0;

                double?MinusedThresholdHolidayLeftExtraHour = sumModel.Items.Where(s => s.AttendanceRecordCal != null && s.AttendanceRecordCal.extraworkType == (int)SystemExtraType.HolidayExtra).Sum(s => s.MinuseHolidayWorkAndThresHoldLeftExtraHour);
                sumModel.MinusedThresholdHolidayLeftExtraHour = MinusedThresholdHolidayLeftExtraHour.HasValue ? MinusedThresholdHolidayLeftExtraHour.Value : 0;

                #endregion


                // 计算C->D,被减去多余加班的统计
                #region 计算C->D,被减去多余加班的统计
                double?MinusedThresholdWorkExtraHour = sumModel.Items.Where(s => s.AttendanceRecordCal != null && s.AttendanceRecordCal.extraworkType == (int)SystemExtraType.WorkExtra).Sum(s => s.MinusedThresholdHour);
                sumModel.MinusedThresholdWorkExtraHour = MinusedThresholdWorkExtraHour.HasValue ? MinusedThresholdWorkExtraHour.Value : 0;


                double?MinusedThresholdWeekendExtraHour = sumModel.Items.Where(s => s.AttendanceRecordCal != null && s.AttendanceRecordCal.extraworkType == (int)SystemExtraType.WeekendExtra).Sum(s => s.MinusedThresholdHour);
                sumModel.MinusedThresholdWeekendExtraHour = MinusedThresholdWeekendExtraHour.HasValue ? MinusedThresholdWeekendExtraHour.Value : 0;

                double?MinusedThresholdHolidayExtraHour = sumModel.Items.Where(s => s.AttendanceRecordCal != null && s.AttendanceRecordCal.extraworkType == (int)SystemExtraType.HolidayExtra).Sum(s => s.MinusedThresholdHour);
                sumModel.MinusedThresholdHolidayExtraHour = MinusedThresholdHolidayExtraHour.HasValue ? MinusedThresholdHolidayExtraHour.Value : 0;

                #endregion


                #endregion

                #endregion

                records.Add(sumModel);
            }



            return(records);
        }