using System;
using System.Collections.Generic;
using System.Linq;
namespace LocalhostMES.Helpers
{
///
/// 年/月/日与单字符编码互转(用于本地 SN 等场景)。
///
public static class DateCodeConverter
{
private static readonly Dictionary yearToCode = new Dictionary
{
{2018, "1"}, {2019, "2"}, {2020, "3"}, {2021, "4"}, {2022, "5"},
{2023, "6"}, {2024, "7"}, {2025, "8"}, {2026, "9"}, {2027, "A"},
{2028, "B"}, {2029, "C"}, {2030, "D"}, {2031, "E"}, {2032, "F"},
{2033, "G"}, {2034, "H"}, {2035, "J"}, {2036, "K"}, {2037, "L"},
{2038, "M"}, {2039, "N"}, {2040, "P"}, {2041, "R"}, {2042, "S"},
{2043, "T"}, {2044, "V"}, {2045, "W"}, {2046, "X"}, {2047, "Y"},
{2048, "1"}, {2049, "2"}, {2050, "3"}, {2051, "4"}, {2052, "5"},
{2053, "6"}, {2054, "7"}, {2055, "8"}, {2056, "9"}, {2057, "A"}
};
private static readonly Dictionary monthToCode = new Dictionary
{
{1, "1"}, {2, "2"}, {3, "3"}, {4, "4"}, {5, "5"}, {6, "6"},
{7, "7"}, {8, "8"}, {9, "9"}, {10, "A"}, {11, "B"}, {12, "C"}
};
private static readonly Dictionary dayToCode = new Dictionary
{
{1, "1"}, {2, "2"}, {3, "3"}, {4, "4"}, {5, "5"}, {6, "6"}, {7, "7"}, {8, "8"}, {9, "9"},
{10, "A"}, {11, "B"}, {12, "C"}, {13, "D"}, {14, "E"}, {15, "F"}, {16, "G"},
{17, "H"}, {18, "J"}, {19, "K"}, {20, "L"}, {21, "M"}, {22, "N"}, {23, "P"}, {24, "R"},
{25, "S"}, {26, "T"}, {27, "U"}, {28, "V"}, {29, "W"}, {30, "X"}, {31, "Y"}
};
private static readonly Dictionary codeToYear;
private static readonly Dictionary codeToMonth;
private static readonly Dictionary codeToDay;
static DateCodeConverter()
{
codeToYear = yearToCode
.GroupBy(kv => kv.Value)
.ToDictionary(g => g.Key, g => g.Last().Key);
codeToMonth = monthToCode
.ToDictionary(kv => kv.Value, kv => kv.Key);
codeToDay = dayToCode
.ToDictionary(kv => kv.Value, kv => kv.Key);
}
public static string GetYearCode(int year)
{
return yearToCode.TryGetValue(year, out string code) ? code : "未知年份";
}
public static int? GetYearByCode(string code)
{
return codeToYear.TryGetValue(code, out int year) ? year : (int?)null;
}
public static List GetAllYearsByCode(string code)
{
return yearToCode
.Where(kv => kv.Value == code)
.Select(kv => kv.Key)
.ToList();
}
public static string GetMonthCode(int month)
{
if (month < 1 || month > 12)
return "无效月份";
return monthToCode.TryGetValue(month, out string code) ? code : "未知月份";
}
public static int? GetMonthByCode(string code)
{
return codeToMonth.TryGetValue(code, out int month) ? month : (int?)null;
}
public static string GetDayCode(int day)
{
if (day < 1 || day > 31)
return "无效日期";
return dayToCode.TryGetValue(day, out string code) ? code : "未知日期";
}
public static int? GetDayByCode(string code)
{
return codeToDay.TryGetValue(code, out int day) ? day : (int?)null;
}
public static string ConvertToDateCode(DateTime date)
{
string yearCode = GetYearCode(date.Year);
string monthCode = GetMonthCode(date.Month);
string dayCode = GetDayCode(date.Day);
if (yearCode == "未知年份" || monthCode == "无效月份" || dayCode == "无效日期" || dayCode == "未知日期")
{
return "无效日期";
}
return $"{yearCode}{monthCode}{dayCode}";
}
public static DateTime? ConvertFromDateCode(string dateCode)
{
if (string.IsNullOrEmpty(dateCode) || dateCode.Length != 3)
return null;
string yearCode = dateCode.Substring(0, 1);
string monthCode = dateCode.Substring(1, 1);
string dayCode = dateCode.Substring(2, 1);
var year = GetYearByCode(yearCode);
var month = GetMonthByCode(monthCode);
var day = GetDayByCode(dayCode);
if (!year.HasValue || !month.HasValue || !day.HasValue)
return null;
try
{
return new DateTime(year.Value, month.Value, day.Value);
}
catch (ArgumentOutOfRangeException)
{
return null;
}
}
public static List ConvertAllFromDateCode(string dateCode)
{
var result = new List();
if (string.IsNullOrEmpty(dateCode) || dateCode.Length != 3)
return result;
string yearCode = dateCode.Substring(0, 1);
string monthCode = dateCode.Substring(1, 1);
string dayCode = dateCode.Substring(2, 1);
var allYears = GetAllYearsByCode(yearCode);
var month = GetMonthByCode(monthCode);
var day = GetDayByCode(dayCode);
if (allYears.Count == 0 || !month.HasValue || !day.HasValue)
return result;
foreach (var year in allYears)
{
try
{
result.Add(new DateTime(year, month.Value, day.Value));
}
catch (ArgumentOutOfRangeException)
{
}
}
return result;
}
}
}