RepotTool.cs 6.03 KB
using HHECS.BllModel;
using HHECS.WorkHourStatistics.Dtos;
using HHECS.WorkHourStatistics.Models;

namespace HHECS.WorkHourStatistics.Utils
{
    public static class RepotTool
    {
        public static BllResult<List<ReportModel>> GetReport(IEnumerable<WorkRecord> workRecords)
        {
            try
            {
                var data = workRecords.Where(x => x.WorkCard != null && x.OffDutyCard != null).DistinctBy(x => new { x.Number, x.WorkCard, x.OffDutyCard }).ToList();

                var reportData = data.GroupBy(x => x.Number).Select(x => new ReportModel
                {
                    WorkNo = x.Key!,
                    Name = x.First(t => t.Number == x.Key)?.PersonName!,
                    SysOrgCode = x.First(t => t.Number == x.Key)?.DeptName!,
                    Values = x.ToLookup(y => $"{y.Number}#{y.WorkCard!.Value:yyyy-MM-dd}({GetWeek((DateTime)y.WorkCard)})", z => GetWT((DateTime)z.WorkCard!, (DateTime)z.OffDutyCard!)).ToDictionary(x => x.Key, y => y.Max())
                }).OrderBy(x => x.SysOrgCode).ToList();
                return BllResultFactory.Success(reportData);
            }
            catch (Exception ex)
            {
                return BllResultFactory.Error<List<ReportModel>>(ex.Message);
            }
        }

        /// <summary>
        /// 计算WT值
        /// <para>公式:WT = WH * 0.1 + WNH * 0.1</para>
        /// </summary>
        /// <param name="workingHours">上班时间</param>
        /// <param name="offDutyHours"><下班时间/param>
        /// <returns></returns>
        private static double GetWT(DateTime workingHours, DateTime offDutyHours)
        {
            double wtValue = 0;
            //上班时间大于下班时间,属于异常数据
            if (workingHours >= offDutyHours)
            {
                return 0;
            }
            var week = GetWeek(workingHours);
            var wnh = new List<string>() { "六", "日" };
            //总时长
            var totalTime = TimeSpan.Zero;
            //周末*0.1
            if (wnh.Contains(week))
            {
                //上班打卡时间:当天8:30
                var startAMTime = workingHours.Date.AddHours(8).AddMinutes(30);
                //上午结束时间:当天12:00
                var endAMTime = workingHours.Date.AddHours(12);
                //下午开始时间:当天13:30
                var startPMTime = offDutyHours.Date.AddHours(13).AddMinutes(30);
                //下班打卡时间:当天17:30
                var endPMTime = offDutyHours.Date.AddHours(17).AddMinutes(30);
                //上午工作时长
                var amTime = TimeSpan.Zero;
                //下午工作时长
                var pmTime = TimeSpan.Zero;

                //8:30之前打上班卡
                if (workingHours <= startAMTime && offDutyHours > startAMTime)
                {
                    amTime = endAMTime - startAMTime;
                }
                //上午迟到
                else if (workingHours > startAMTime && workingHours <= endAMTime)
                {
                    amTime = endAMTime - workingHours;
                }

                //上午早退12:00之前打下班卡
                if (offDutyHours <= endAMTime)
                {
                    if (workingHours <= startAMTime)
                    {
                        amTime = offDutyHours - startAMTime;
                    }
                    else
                    {
                        amTime = offDutyHours - workingHours;
                    }
                }
                //上午早退12:00 - 13:30之间打下班卡
                else if (offDutyHours <= startPMTime && offDutyHours >= endAMTime)
                {
                    if (workingHours <= startAMTime)
                    {
                        amTime = endAMTime - startAMTime;
                    }
                    else
                    {
                        amTime = endAMTime - workingHours;
                    }
                }

                //17:30之后打下班卡
                if (offDutyHours >= endPMTime)
                {
                    pmTime = endPMTime - startPMTime;
                }
                //下午早退
                else if (offDutyHours >= startPMTime && offDutyHours <= endPMTime)
                {
                    if (workingHours < startPMTime)
                    {
                        pmTime = offDutyHours - startPMTime;
                    }
                    else
                    {
                        //下午打上班卡和下班卡
                        pmTime = offDutyHours - workingHours;
                    }
                }

                totalTime = amTime + pmTime;
                wtValue = totalTime.TotalHours * 0.1;
            }
            //周一至周五*0.1
            else if (offDutyHours > offDutyHours.Date.AddHours(18))
            {
                //只统计18:30之后的时长
                var endTime = offDutyHours.Date.AddHours(18).AddMinutes(30);
                if (workingHours > endTime)
                {
                    totalTime = offDutyHours - workingHours;
                }
                else if (offDutyHours >= endTime)
                {
                    totalTime = offDutyHours - endTime;
                }
                wtValue = totalTime.TotalHours * 0.1;
            }
            return Math.Round(wtValue, 2, MidpointRounding.AwayFromZero);
        }

        private static string GetWeek(DateTime dateTime)
        {
            string[] weekdays = { "日", "一", "二", "三", "四", "五", "六" };
            return weekdays[Convert.ToInt32(dateTime.DayOfWeek)];
        }

        public static List<string> GetHeadDays(DateTime startTime, DateTime endTime)
        {
            var li = new List<string>();
            while (startTime <= endTime)
            {
                li.Add($"{startTime:yyyy-MM-dd}({GetWeek(startTime)})");
                startTime = startTime.AddDays(1);
            }
            return li.ToList();
        }
    }
}