调休计算
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
using RestSharp;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IRaCIS.Core.Application.Helper
|
||||
{
|
||||
|
||||
#region 对接模型
|
||||
public class HolidayCalendar
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Version { get; set; }
|
||||
public string Generated { get; set; }
|
||||
public string Timezone { get; set; }
|
||||
public string Author { get; set; }
|
||||
public string URL { get; set; }
|
||||
public Dictionary<string, List<HolidayYear>> Years { get; set; }
|
||||
}
|
||||
|
||||
public class HolidayYear
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public DateTime StartDate { get; set; }
|
||||
public DateTime EndDate { get; set; }
|
||||
public int Duration { get; set; }
|
||||
public List<DateTime> CompDays { get; set; }
|
||||
public string URL { get; set; }
|
||||
public string Memo { get; set; }
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
|
||||
public class HolidayHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// github 链接:https://github.com/lanceliao/china-holiday-calender?tab=readme-ov-file
|
||||
/// 接口请求参考文档:https://www.koudingke.cn/docs/zh-Hans/net-lib-docs/latest/RestSharp/Usage/Usage
|
||||
/// </summary>
|
||||
private static RestClient _client => new RestClient("https://www.shuyz.com/githubfiles/china-holiday-calender/master/holidayAPI.json");
|
||||
|
||||
private static HolidayCalendar _holidayCalendar { get; set; }
|
||||
|
||||
|
||||
private static List<DateTime> _holidays = new List<DateTime>();
|
||||
private static List<DateTime> _compensatedWorkdays = new List<DateTime>();
|
||||
|
||||
static HolidayHelper()
|
||||
{
|
||||
_holidayCalendar = _client.Get<HolidayCalendar>(new RestRequest()) ?? new HolidayCalendar();
|
||||
|
||||
foreach (var year in _holidayCalendar.Years.Values.SelectMany(x => x))
|
||||
{
|
||||
// 添加节假日到假日集合
|
||||
for (DateTime date = year.StartDate; date <= year.EndDate; date = date.AddDays(1))
|
||||
{
|
||||
_holidays.Add(date);
|
||||
}
|
||||
|
||||
// 添加补班日到补班日集合
|
||||
foreach (var compDay in year.CompDays)
|
||||
{
|
||||
_compensatedWorkdays.Add(compDay);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static TimeSpan GetChinaWorkTimeSpan(DateTime startDate, DateTime endDate)
|
||||
{
|
||||
if (startDate > endDate)
|
||||
{
|
||||
throw new ArgumentException("结束日期必须大于或等于开始日期");
|
||||
}
|
||||
|
||||
var diffTimeSpan = endDate - startDate;
|
||||
|
||||
// 初始化工作日数为0
|
||||
int workdays = 0;
|
||||
|
||||
// 找出在给定日期范围内的假期和补班日
|
||||
var filteredHolidays = _holidays.Where(d => d >= startDate && d <= endDate).ToList();
|
||||
var filteredCompensatedWorkdays = _compensatedWorkdays.Where(d => d >= startDate && d <= endDate).ToList();
|
||||
|
||||
// 遍历日期范围,计算工作日 注意 这里是小于
|
||||
for (DateTime date = startDate.Date; date < endDate.Date; date = date.AddDays(1))
|
||||
{
|
||||
// 判断是否为工作日
|
||||
bool isWeekend = date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday;
|
||||
bool isHoliday = filteredHolidays.Contains(date);
|
||||
bool isCompensatedWorkday = filteredCompensatedWorkdays.Contains(date);
|
||||
|
||||
// 如果是补班日,或是平日(非假期,非周末),则算作工作日
|
||||
if (isCompensatedWorkday || (!isWeekend && !isHoliday))
|
||||
{
|
||||
workdays++;
|
||||
}
|
||||
}
|
||||
|
||||
return new TimeSpan(workdays, diffTimeSpan.Hours, diffTimeSpan.Minutes, diffTimeSpan.Seconds);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user