整理文件夹

This commit is contained in:
2024-10-22 15:30:38 +08:00
parent 38b4268c67
commit 06cb92b7e0
15 changed files with 19 additions and 27 deletions
@@ -0,0 +1,105 @@
using RestSharp;
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);
}
}
@@ -0,0 +1,60 @@
using FellowOakDicom.Imaging;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Processing;
namespace IRaCIS.Core.Application.Helper;
public static class ImageHelper
{
// 采用ImageSharp组件 跨平台,不依赖windows 平台图像处理组件,这里大坑(net 6 下,之前由于Dicom处理最新组件 依赖了这个库的一个旧版本,导致缩略图bug,单独升级依赖组件才解决)
public static void ResizeSave(string filePath, string? fileStorePath)
{
fileStorePath = fileStorePath ?? filePath + ".preview.jpeg";
using (var image = SixLabors.ImageSharp.Image.Load(filePath))
{
image.Mutate(x => x.Resize(500, 500));
image.Save(fileStorePath);
}
}
public static Stream RenderPreviewJpeg(string filePath)
{
string jpegPath = filePath + ".preview.jpg";
if (!File.Exists(jpegPath))
{
using (Stream stream = new FileStream(jpegPath, FileMode.Create))
{
DicomImage image = new DicomImage(filePath);
var sharpimage = image.RenderImage().AsSharpImage();
sharpimage.Save(stream, new JpegEncoder());
}
}
return new FileStream(jpegPath, FileMode.Open);
}
public static void RemovePreviewJpeg(string filePath)
{
string jpegPath = filePath + ".preview.jpg";
if (File.Exists(jpegPath)) File.Delete(jpegPath);
}
}