using Hotline.Caching.Interfaces;
using Hotline.Settings.TimeLimitDomain.ExpireTimeSupplier;
using Hotline.Settings.TimeLimits;
using Hotline.Share.Dtos.Settings;
using Hotline.Share.Enums.Settings;
using XF.Domain.Dependency;

namespace Hotline.Settings.TimeLimitDomain;
public class ExpireTimeHandler : IExpireTimeHandler, IScopeDependency
{
    private readonly ExpireTimeFactory _expireTimeFactory;
    private readonly IDaySettingCacheManager _daySettingCacheManager;

    public ExpireTimeHandler(ExpireTimeFactory expireTimeFactory, IDaySettingCacheManager daySettingCacheManager)
    {
        _expireTimeFactory = expireTimeFactory;
        _daySettingCacheManager = daySettingCacheManager;
    }

    /// <summary>
    /// 计算工作时间分钟数（已改返回秒）
    /// </summary>
    /// <param name="beginTime"></param>
    /// <param name="endTime"></param>
    /// <param name="workTime">工作日时间段</param>
    /// <param name="isCenter"></param>
    /// <returns></returns>
    public async Task<int> CalcWorkTimeAsync(DateTime beginTime, DateTime endTime, IList<string>? workTime, bool isCenter)
    {
        if (isCenter)
        {
            //如果是中心，采用中心计算方式
            //计算时间差
            TimeSpan minuteSpan = new TimeSpan(endTime.Ticks - beginTime.Ticks);
            //总时差分钟数
            int minutes = (int)minuteSpan.TotalMinutes;


            //计算开始时间是否计算在工作时间内
            if (await NotWorkDay(beginTime))
            {
                var newBeginTime = beginTime.AddDays(1).Date;
                TimeSpan beginTimeMinuteSpan = new TimeSpan(newBeginTime.Ticks - beginTime.Ticks);
                int beginTimeMinute = (int)beginTimeMinuteSpan.TotalMinutes;
                minutes = minutes - beginTimeMinute;
            }


            //计算结束时间是否计算在工作时间内
            if (await NotWorkDay(endTime))
            {
                var newEndTime = endTime.Date;
                TimeSpan endTimeMinuteSpan = new TimeSpan(endTime.Ticks - newEndTime.Ticks);
                int endTimeMinute = (int)endTimeMinuteSpan.TotalMinutes;
                minutes = minutes - endTimeMinute;
            }

            //计算整天时间是否在工作时间内
            int days = minuteSpan.Days;
            for (int i = 0;i < days;i++)
            {
                var newDay = beginTime.AddDays(i + 1);
                if (newDay.Date == endTime.Date)
                {
                    continue;
                }

                if (await NotWorkDay(beginTime.AddDays(i + 1)))
                {
                    minutes = minutes - 1440;
                }
            }
            return minutes == 0 ? 1 : minutes * 60;
        }
        else
        {
            //如果不是中心（则是部门计算方式）
            //
            if (workTime != null)
            {
                //DateTime WorkBeginTime = DateTime.Parse(DateTime.Now.ToShortDateString() + " " + workTime.SettingValue[0] + ":00");
                //DateTime WorkEndTime = DateTime.Parse(DateTime.Now.ToShortDateString() + " " + workTime.SettingValue[1] + ":00");
                DateTime WorkBeginTime = DateTime.Parse(beginTime.ToShortDateString() + " " + workTime[0] + ":00");
                DateTime WorkEndTime = DateTime.Parse(beginTime.ToShortDateString() + " " + workTime[1] + ":00");

                //处理开始时间
                if (beginTime.Date == WorkBeginTime.Date && beginTime < WorkBeginTime)
                {
                    beginTime = WorkBeginTime;
                }
                else
                {
                    if (beginTime > WorkEndTime)
                    {
                        beginTime = WorkBeginTime.AddDays(1);
                    }
                }
                if (beginTime >= endTime)
                {
                    return 1;
                }

                if (endTime.Date == WorkEndTime.Date && endTime < WorkBeginTime)
                {
                    endTime = WorkBeginTime;
                }
                else
                {
                    if (endTime > WorkEndTime && endTime < WorkBeginTime.AddDays(1))
                    {
                        endTime = WorkBeginTime.AddDays(1);
                    }
                }

                while (true)
                {
                    if (await NotWorkDay(endTime))
                    {
                        endTime = DateTime.Parse(endTime.AddDays(1).ToShortDateString() + " " + workTime[0] + ":00");
                    }
                    else
                    {
                        break;
                    }
                }

                int minutes = 0;
                int i = 0;
                while (true)
                {
                    i++;
                    //如果是工作日
                    if (await IsWorkDay(beginTime))
                    {
                        //最后一天
                        if (beginTime.Date == endTime.Date)
                        {
                            if (endTime > DateTime.Parse(endTime.ToShortDateString() + " " + workTime[1] + ":00"))
                            {
                                if (i != 1)
                                {
                                    TimeSpan sp = new TimeSpan(WorkEndTime.Ticks - WorkBeginTime.Ticks);
                                    minutes += (int)sp.TotalMinutes;
                                }
                                else
                                {
                                    TimeSpan sp = new TimeSpan(WorkEndTime.Ticks - beginTime.Ticks);
                                    minutes += (int)sp.TotalMinutes;
                                }
                            }
                            else
                            {
                                if (i != 1)
                                {
                                    TimeSpan sp = new TimeSpan(endTime.Ticks - WorkBeginTime.Ticks);
                                    minutes += (int)sp.TotalMinutes;
                                }
                                else
                                {
                                    TimeSpan sp = new TimeSpan(endTime.Ticks - beginTime.Ticks);
                                    minutes += (int)sp.TotalMinutes;
                                }
                            }
                            return minutes == 0 ? 1 : minutes * 60;
                        }
                        //如果不是当天完成
                        else
                        {
                            if (i == 1)
                            {
                                TimeSpan sp = new TimeSpan(WorkEndTime.Ticks - beginTime.Ticks);
                                minutes += (int)sp.TotalMinutes;
                            }
                            else
                            {
                                if (endTime.Date == WorkEndTime.Date)
                                {
                                    if (endTime > WorkEndTime)
                                    {
                                        TimeSpan sp = new TimeSpan(WorkEndTime.Ticks - WorkBeginTime.Ticks);
                                        minutes += (int)sp.TotalMinutes;
                                    }
                                    else
                                    {
                                        TimeSpan sp = new TimeSpan(endTime.Ticks - WorkBeginTime.Ticks);
                                        minutes += (int)sp.TotalMinutes;
                                    }
                                    return minutes == 0 ? 1 : minutes * 60;
                                }
                                else
                                {
                                    TimeSpan sp = new TimeSpan(WorkEndTime.Ticks - WorkBeginTime.Ticks);
                                    minutes += (int)sp.TotalMinutes;
                                }
                            }
                        }
                    }

                    beginTime = beginTime.AddDays(1);
                    WorkEndTime = WorkEndTime.AddDays(1);
                    WorkBeginTime = WorkBeginTime.AddDays(1);
                }
            }
            return 0;
        }
    }

    /// <summary>
    /// 计算工作时间分钟数(已改返回秒)
    /// </summary>
    /// <param name="beginTime"></param>
    /// <param name="endTime"></param>
    /// <param name="workTime">工作日时间段</param>
    /// <param name="isCenter"></param>
    /// <returns></returns>
    public async Task<int> CalcWorkTimeExAsync(DateTime beginTime, DateTime endTime, IList<string>? workTime, bool isCenter)
    {
        if (isCenter)
        {
            //如果是中心，采用中心计算方式
            //计算时间差
            TimeSpan minuteSpan = new TimeSpan(endTime.Ticks - beginTime.Ticks);
            //总时差分钟数
            int minutes = (int)minuteSpan.TotalMinutes;


            //计算开始时间是否计算在工作时间内
            if (await NotWorkDay(beginTime))
            {
                var newBeginTime = beginTime.AddDays(1).Date;
                TimeSpan beginTimeMinuteSpan = new TimeSpan(newBeginTime.Ticks - beginTime.Ticks);
                int beginTimeMinute = (int)beginTimeMinuteSpan.TotalMinutes;
                minutes = minutes - beginTimeMinute;
            }


            //计算结束时间是否计算在工作时间内
            if (await NotWorkDay(endTime))
            {
                var newEndTime = endTime.Date;
                TimeSpan endTimeMinuteSpan = new TimeSpan(endTime.Ticks - newEndTime.Ticks);
                int endTimeMinute = (int)endTimeMinuteSpan.TotalMinutes;
                minutes = minutes - endTimeMinute;
            }

            //计算整天时间是否在工作时间内
            int days = minuteSpan.Days;
            for (int i = 0;i < days;i++)
            {
                var newDay = beginTime.AddDays(i + 1);
                if (newDay.Date == endTime.Date)
                {
                    continue;
                }

                if (await NotWorkDay(beginTime.AddDays(i + 1)))
                {
                    minutes = minutes - 1440;
                }
            }
            return minutes == 0 ? 1 : minutes * 60;
        }
        else
        {
            //如果不是中心（则是部门计算方式）
            //
            if (workTime != null)
            {
                //DateTime WorkBeginTime = DateTime.Parse(DateTime.Now.ToShortDateString() + " " + workTime.SettingValue[0] + ":00");
                //DateTime WorkEndTime = DateTime.Parse(DateTime.Now.ToShortDateString() + " " + workTime.SettingValue[1] + ":00");
                DateTime MornWorkBeginTime = DateTime.Parse(beginTime.ToShortDateString() + " " + workTime[0] + ":00");
                DateTime MornWorkEndTime = DateTime.Parse(beginTime.ToShortDateString() + " " + workTime[1] + ":00");
                DateTime AfterWorkBeginTime = DateTime.Parse(beginTime.ToShortDateString() + " " + workTime[2] + ":00");
                DateTime AfterWorkEndTime = DateTime.Parse(beginTime.ToShortDateString() + " " + workTime[3] + ":00");

                //处理开始时间
                if (beginTime.Date == MornWorkBeginTime.Date && beginTime < MornWorkBeginTime)
                {
                    beginTime = MornWorkBeginTime;
                }
                else if (beginTime.Date == MornWorkBeginTime.Date && beginTime > MornWorkEndTime && beginTime < AfterWorkBeginTime)
                {
                    beginTime = AfterWorkBeginTime;
                }
                else
                {
                    if (beginTime > AfterWorkEndTime)
                    {
                        beginTime = MornWorkBeginTime.AddDays(1);
                        MornWorkBeginTime = MornWorkBeginTime.AddDays(1);
                        MornWorkEndTime = MornWorkEndTime.AddDays(1);
                        AfterWorkBeginTime = AfterWorkBeginTime.AddDays(1);
                        AfterWorkEndTime = AfterWorkEndTime.AddDays(1);
                    }
                }
                if (beginTime >= endTime)
                {
                    return 1;
                }

                if (endTime.Date == AfterWorkEndTime.Date && endTime < MornWorkBeginTime)
                {
                    endTime = MornWorkBeginTime;
                }
                else if (endTime.Date == AfterWorkEndTime.Date && endTime > MornWorkEndTime && endTime < AfterWorkBeginTime)
                {
                    endTime = MornWorkEndTime;
                }
                else
                {
                    if (endTime > AfterWorkEndTime && endTime < MornWorkBeginTime.AddDays(1))
                    {
                        endTime = AfterWorkEndTime;//MornWorkBeginTime.AddDays(1);
                    }
                }

                while (true)
                {
                    if (await NotWorkDay(endTime))
                    {
                        endTime = DateTime.Parse(endTime.AddDays(1).ToShortDateString() + " " + workTime[0] + ":00");
                    }
                    else
                    {
                        break;
                    }
                }

                //计算午休时差
                var restMinutes = 0;
                TimeSpan restSp = new TimeSpan(AfterWorkBeginTime.Ticks - MornWorkEndTime.Ticks);
                restMinutes = (int)restSp.TotalMinutes;


                int minutes = 0;
                int i = 0;
                while (true)
                {
                    i++;
                    //如果是工作日
                    if (await IsWorkDay(beginTime))
                    {
                        //最后一天
                        if (beginTime.Date == endTime.Date)
                        {
                            if (endTime > AfterWorkEndTime)
                            {
                                if (i != 1)
                                {
                                    TimeSpan sp = new TimeSpan(AfterWorkEndTime.Ticks - MornWorkBeginTime.Ticks);
                                    minutes += (int)sp.TotalMinutes - restMinutes;
                                }
                                //最后一天就是当天
                                else
                                {
                                    TimeSpan sp = new TimeSpan(AfterWorkEndTime.Ticks - beginTime.Ticks);
                                    minutes += (int)sp.TotalMinutes;
                                    if (beginTime > MornWorkEndTime)
                                    {
                                        minutes = minutes - restMinutes;
                                    }
                                }
                            }
                            else
                            {
                                if (i != 1)
                                {
                                    TimeSpan sp = new TimeSpan(endTime.Ticks - MornWorkBeginTime.Ticks);
                                    minutes += (int)sp.TotalMinutes;
                                    if (endTime > AfterWorkBeginTime)
                                    {
                                        minutes = minutes - restMinutes;
                                    }
                                }
                                else
                                {
                                    TimeSpan sp = new TimeSpan(endTime.Ticks - beginTime.Ticks);
                                    minutes += (int)sp.TotalMinutes;
                                    if (endTime > AfterWorkBeginTime && beginTime < MornWorkEndTime)
                                    {
                                        minutes = minutes - restMinutes;
                                    }
                                }
                            }
                            return minutes == 0 ? 1 : minutes * 60;
                        }
                        //如果不是当天完成
                        else
                        {
                            if (i == 1)
                            {
                                TimeSpan sp = new TimeSpan(AfterWorkEndTime.Ticks - beginTime.Ticks);
                                minutes += (int)sp.TotalMinutes;
                                if (beginTime > MornWorkEndTime)
                                {
                                    minutes = minutes - restMinutes;
                                }
                            }
                            else
                            {
                                if (endTime.Date == AfterWorkEndTime.Date)
                                {
                                    if (endTime > AfterWorkEndTime)
                                    {
                                        TimeSpan sp = new TimeSpan(AfterWorkEndTime.Ticks - MornWorkBeginTime.Ticks);
                                        minutes += (int)sp.TotalMinutes - restMinutes;
                                    }
                                    else
                                    {
                                        TimeSpan sp = new TimeSpan(endTime.Ticks - MornWorkBeginTime.Ticks);
                                        minutes += (int)sp.TotalMinutes;
                                        if (endTime > AfterWorkBeginTime)
                                        {
                                            minutes = minutes - restMinutes;
                                        }
                                    }
                                    return minutes == 0 ? 1 : minutes * 60;
                                }
                                else
                                {
                                    TimeSpan sp = new TimeSpan(AfterWorkEndTime.Ticks - MornWorkBeginTime.Ticks);
                                    minutes += (int)sp.TotalMinutes - restMinutes;
                                }
                            }
                        }
                    }

                    beginTime = beginTime.AddDays(1);
                    AfterWorkEndTime = AfterWorkEndTime.AddDays(1);
                    MornWorkBeginTime = MornWorkBeginTime.AddDays(1);
                    MornWorkEndTime = MornWorkEndTime.AddDays(1);
                    AfterWorkBeginTime = AfterWorkBeginTime.AddDays(1);
                }
            }
            return 0;
        }
    }

    /// <summary>
    /// 计算时间间隔
    /// </summary>
    /// <param name="beginTime"></param>
    /// <param name="endTime"></param>
    /// <param name="isCenter"></param>
    /// <returns></returns>
    public string CalcTimeInterval(DateTime beginTime, DateTime endTime, bool isCenter)
    {
        if (isCenter)
        {
            //如果是中心按24小时制
            TimeSpan centerIntervalSp = new TimeSpan(endTime.Ticks - beginTime.Ticks);
            double re = Math.Round(((double)centerIntervalSp.Microseconds / (24 * 60)), 1);
            if (re > 0)
            {
                return re + "天后超期";
            }
            else
            {
                return "已超期" + Math.Abs(re) + "天";
            }
        }
        else
        {
            //如果是部门按工作时间 8小时制
            TimeSpan centerIntervalSp = new TimeSpan(endTime.Ticks - beginTime.Ticks);
            double re = Math.Round(((double)centerIntervalSp.Microseconds / (8 * 60)), 1);
            if (re > 0)
            {
                return re + "天后超期";
            }
            else
            {
                return "已超期" + Math.Abs(re) + "天";
            }
            return "";
        }
    }

    /// <summary>
    /// 倒推工作日
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    public async Task<DateTime> CalcWorkTimeReduceAsync(DateTime date, int timeValue)
    {
        for (int i = 0;i < timeValue;i++)
        {
            if (await IsWorkDay(date))
            {
                date = date.AddDays(-1);
            }
            else
            {
                date = date.AddDays(-1);
                i--;
            }
        }
        return date;
    }

    public async Task<TimeResult> CalcEndTimeAsync(DateTime beginTime,DateTime startTime, TimeConfig timeConfig)
    { 
        return await _expireTimeFactory.GetSupplier(timeConfig.TimeType).CalcEndTimeNewAsync(beginTime,startTime, timeConfig);
    }

    public async Task<TimeResult> CalcEndTimeNewAsync(DateTime beginTime,DateTime startTime,ETimeType timeType,IList<string>? workTime,int timeValue,int Percentage,int PercentageOne,int? EndType)
    {
        return await _expireTimeFactory.GetSupplier(timeType).CalcEndTimeNewAsync(beginTime, startTime, new TimeConfig()
        {
            WorkTime = workTime,
            Count = timeValue,
            Percentage = Percentage,
            PercentageOne = PercentageOne,
            EndType = EndType
        });
    }


    /// <summary>
    /// 计算工作日
    /// </summary>
    /// <param name="beginTime"></param>
    /// <param name="timeType"></param>
    /// <param name="timeValue"></param>
    /// <param name="workTime">工作日时间段</param>
    /// <param name="Percentage">即将超期百分比</param>
    /// <param name="PercentageOne">超期百分比第一级</param>
    /// <returns></returns>
    public async Task<TimeResult> CalcEndTimeAsync(DateTime beginTime,DateTime startTime,  IList<string>? workTime,TimeLimitSetting setting)
    {
        return await _expireTimeFactory.GetSupplier(setting.TimeType).CalcEndTimeNewAsync(beginTime,startTime, new TimeConfig
        {
            WorkTime = workTime,
            Count = setting.TimeValue,
            Percentage = setting.Percentage,
            PercentageOne = setting.PercentageOne,
             EndType = setting.EndType ?? 1
        });

        //var startTime = beginTime;
        //var startTimeOne = beginTime;
        ////如果是部门，采用部门计算方式
        //switch (timeType)
        //{
        //    //新增对应小时
        //    //case ETimeType.Hour:
        //    //if (isCenter)
        //    //{
        //    //    //中心计算自然时间
        //    //    return new TimeResult { EndTime = beginTime.AddHours(timeValue), RuleStr = timeValue + "小时" };
        //    //}
        //    //else
        //    //{
        //    //    //部门计算工作时间
        //    //    //查询统一部门工作时间
        //    //    var workTime = _systemSettingRepository.Get(x => x.Code == "WorkTime");
        //    //    if (workTime != null)
        //    //    {
        //    //        //计算一天工作时间（分钟）
        //    //        DateTime WorkBeginTime = DateTime.Parse(DateTime.Now.ToShortDateString() + " " + workTime.SettingValue[0] + ":00");
        //    //        DateTime WorkEndTime = DateTime.Parse(DateTime.Now.ToShortDateString() + " " + workTime.SettingValue[1] + ":00");
        //    //        TimeSpan minuteSpan = new TimeSpan(WorkEndTime.Ticks - WorkBeginTime.Ticks);
        //    //        //一天工作时间（分钟）
        //    //        int minutes = (int)minuteSpan.TotalMinutes;
        //    //        //延期总时间（分钟）
        //    //        int timeValueMinutes = timeValue * 60;

        //    //        //判断开始时间不在工时间段修正时间复位至当天或者第二天的开始时间（如果大于结束时间则复位至第二天的开始时间）
        //    //        if (beginTime < WorkBeginTime)
        //    //        {
        //    //            beginTime = WorkBeginTime;
        //    //        }
        //    //        else
        //    //        {
        //    //            if (beginTime > WorkEndTime)
        //    //            {
        //    //                beginTime = WorkBeginTime.AddDays(1);
        //    //            }
        //    //        }

        //    //        //判断第一天时间是否足够
        //    //        TimeSpan firstMinuteSpan = new TimeSpan(WorkEndTime.Ticks - beginTime.Ticks);
        //    //        //计算开始时间的第一天时间
        //    //        int firstMinute = (int)firstMinuteSpan.TotalMinutes;
        //    //        //判断第一天时间是否足够
        //    //        if (firstMinute > timeValueMinutes)
        //    //        {
        //    //            //足够
        //    //            bool canWhile = true;
        //    //            while (canWhile)
        //    //            {
        //    //                //判断开始时间是否在工作日
        //    //                if (IsWorkDay(beginTime))
        //    //                {
        //    //                    return new TimeResult { EndTime = beginTime.AddHours(timeValue), RuleStr = timeValue + "小时" };
        //    //                }
        //    //                else
        //    //                {
        //    //                    //如果不是工作日就加一天
        //    //                    beginTime = beginTime.AddDays(1);
        //    //                }
        //    //            }
        //    //            return new TimeResult { EndTime = beginTime.AddHours(timeValue), RuleStr = timeValue + "小时" };
        //    //        }
        //    //        else
        //    //        {
        //    //            //不够
        //    //            bool canWhile = true;
        //    //            while (canWhile)
        //    //            {
        //    //                //判断开始时间是否在工作日
        //    //                if (IsWorkDay(beginTime))
        //    //                {
        //    //                    //在工作日
        //    //                    timeValueMinutes = timeValueMinutes - minutes;
        //    //                    if (timeValueMinutes > 0)
        //    //                    {
        //    //                        beginTime = beginTime.AddDays(1);
        //    //                    }
        //    //                    else
        //    //                    {
        //    //                        return new TimeResult { EndTime = beginTime.AddMinutes(timeValueMinutes), RuleStr = timeValue + "小时" };
        //    //                    }
        //    //                }
        //    //                else
        //    //                {
        //    //                    //如果不是工作日
        //    //                    beginTime = beginTime.AddDays(1);
        //    //                }
        //    //            }
        //    //        }

        //    //    }
        //    //    return new TimeResult { EndTime = beginTime.AddHours(timeValue), RuleStr = timeValue + "小时" };
        //    //}
        //    //   return null;
        //    //新增工作日
        //    case ETimeType.WorkDay:

        //        //检查时间段内是否存在休息日或者工作日
        //        //查询统一部门工作时间
        //        if (workTime != null)
        //        {
        //            //计算一天工作时间（分钟）
        //            DateTime WorkBeginTime = DateTime.Parse(beginTime.ToShortDateString() + " " + workTime[0] + ":00");
        //            DateTime WorkEndTime = DateTime.Parse(beginTime.ToShortDateString() + " " + workTime[1] + ":00");
        //            //计算一天多少个工作小时
        //            var duration = WorkEndTime - WorkBeginTime;
        //            double workMinutes = duration.TotalMinutes;
        //            double totalWorkMinutes = (workMinutes * timeValue) * (Percentage / 100.00);
        //            double totalWorkMinutesOne = (workMinutes * timeValue) * (PercentageOne / 100.00);

        //            //判断开始时间不在工时间段修正时间复位至当天或者第二天的开始时间（如果大于结束时间则复位至第二天的开始时间）
        //            if (beginTime < WorkBeginTime)
        //            {
        //                beginTime = WorkBeginTime;
        //            }
        //            else
        //            {
        //                if (beginTime > WorkEndTime)
        //                {
        //                    beginTime = WorkBeginTime.AddDays(1);
        //                }
        //            }

        //            int day = 1;
        //            for (int i = 1;i < timeValue + 1;i++)
        //            {
        //                if (await IsWorkDay(beginTime.AddDays(day)))
        //                {
        //                    beginTime = beginTime.AddDays(day);

        //                    if (totalWorkMinutes >= workMinutes)
        //                    {
        //                        startTime = startTime.AddDays(day);
        //                        totalWorkMinutes = totalWorkMinutes - workMinutes;
        //                    }
        //                    else if (totalWorkMinutes < workMinutes && totalWorkMinutes != 0)
        //                    {
        //                        if (startTime.AddMinutes(totalWorkMinutes) > DateTime.Parse(startTime.ToShortDateString() + " " + workTime[1] + ":00"))
        //                        {
        //                            totalWorkMinutes = totalWorkMinutes - ((DateTime.Parse(startTime.ToShortDateString() + " " + workTime[1] + ":00") - startTime).TotalMinutes);
        //                            startTime = startTime.AddDays(day);
        //                            startTime = DateTime.Parse(startTime.ToShortDateString() + " " + workTime[0] + ":00").AddMinutes(totalWorkMinutes);
        //                            totalWorkMinutes = 0;
        //                        }
        //                        else
        //                        {
        //                            startTime = startTime.AddMinutes(totalWorkMinutes);
        //                        }
        //                    }

        //                    if (totalWorkMinutesOne >= workMinutes)
        //                    {
        //                        startTimeOne = startTimeOne.AddDays(day);
        //                        totalWorkMinutesOne = totalWorkMinutesOne - workMinutes;
        //                    }
        //                    else if (totalWorkMinutesOne < workMinutes && totalWorkMinutesOne != 0)
        //                    {
        //                        if (startTimeOne.AddMinutes(totalWorkMinutesOne) > DateTime.Parse(startTimeOne.ToShortDateString() + " " + workTime[1] + ":00"))
        //                        {
        //                            totalWorkMinutesOne = totalWorkMinutesOne - ((DateTime.Parse(startTimeOne.ToShortDateString() + " " + workTime[1] + ":00") - startTimeOne).TotalMinutes);
        //                            startTimeOne = startTimeOne.AddDays(day);
        //                            startTimeOne = DateTime.Parse(startTimeOne.ToShortDateString() + " " + workTime[0] + ":00").AddMinutes(totalWorkMinutesOne);
        //                            totalWorkMinutesOne = 0;
        //                        }
        //                        else
        //                        {
        //                            startTimeOne = startTimeOne.AddMinutes(totalWorkMinutesOne);
        //                        }
        //                    }


        //                    day = 1;
        //                }
        //                else
        //                {
        //                    i--;
        //                    day++;
        //                }
        //            }
        //        }


        //        //TimeSpan duration = beginTime - startTime;
        //        //double percentage = 0.8 * duration.TotalHours;
        //        //DateTime resultTime = startTime + TimeSpan.FromHours((int)(percentage));
        //        //bool isWhile = true;
        //        //while (isWhile)
        //        //{
        //        //    if (IsWorkDay(resultTime))
        //        //    {
        //        //        isWhile = false;
        //        //    }
        //        //    else
        //        //    {
        //        //        resultTime = resultTime.AddDays(1);
        //        //    }
        //        //}


        //        return new TimeResult { EndTime = beginTime, RuleStr = timeValue + "个工作日", NearlyExpiredTime = startTime, NearlyExpiredTimeOne = startTimeOne };
        //    //新增自然日
        //    case ETimeType.Day:
        //        return new TimeResult { EndTime = beginTime.AddDays(timeValue), RuleStr = timeValue + "个自然日", NearlyExpiredTime = beginTime };
        //    default:
        //        return null;
        //}
    }

    /// <summary>
    /// 是否是工作日
    /// 根据数据库的工作日和周末时间判断
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    private async Task<bool> IsWorkDay(DateTime date)
    {
        return _daySettingCacheManager.IsWorkDay(date);
    }

    public async Task<bool> NotWorkDay(DateTime date)
    {
        return !await IsWorkDay(date);
    }
}

