| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- namespace Admin.NET.Plugin.AiDOP.Supply;
- public static class NumberRuleFormatter
- {
- public static int ResolveSerialWidth(int? maxValue)
- {
- if (!maxValue.HasValue || maxValue.Value <= 0) return 4;
- return Math.Max(1, maxValue.Value.ToString().Length);
- }
- public static string BuildNumber(string? prefix1, string? prefix2, string? prefix3, string? dateType, bool? isDateType, int serial, int? maxValue, DateTime now)
- {
- var prefix = BuildPrefix(prefix1, prefix2, prefix3, dateType, isDateType, now);
- return $"{prefix}{serial.ToString().PadLeft(ResolveSerialWidth(maxValue), '0')}";
- }
- public static string BuildPrefix(string? prefix1, string? prefix2, string? prefix3, string? dateType, bool? isDateType, DateTime now)
- {
- var prefix = string.Concat(
- prefix1?.Trim(),
- prefix2?.Trim(),
- prefix3?.Trim());
- if (isDateType == true || !string.IsNullOrWhiteSpace(dateType))
- prefix += FormatDatePart(dateType, now);
- return prefix;
- }
- public static string FormatDatePart(string? dateType, DateTime now)
- {
- var normalized = (dateType ?? string.Empty).Trim().ToUpperInvariant();
- return normalized switch
- {
- "YYYYMMDD" => now.ToString("yyyyMMdd"),
- "YYMMDD" => now.ToString("yyMMdd"),
- "YYYYMM" => now.ToString("yyyyMM"),
- "YYMM" => now.ToString("yyMM"),
- "YYYY" => now.ToString("yyyy"),
- "MMDD" => now.ToString("MMdd"),
- _ => normalized
- .Replace("YYYY", now.ToString("yyyy"))
- .Replace("YY", now.ToString("yy"))
- .Replace("MM", now.ToString("MM"))
- .Replace("DD", now.ToString("dd"))
- };
- }
- public static DateTime? ResolveSequenceDate(string? dateType, bool? isDateType, DateTime now)
- {
- var normalized = (dateType ?? string.Empty).Trim().ToUpperInvariant();
- if (isDateType != true && string.IsNullOrWhiteSpace(normalized)) return null;
- return normalized switch
- {
- "YY" or "YYYY" => new DateTime(now.Year, 1, 1),
- "YYMM" or "YYYYMM" => new DateTime(now.Year, now.Month, 1),
- _ => now.Date
- };
- }
- }
|