WMSlite/WmsLite.Api/Common/LocationShelfBatchHelper.cs
zhougf fa1b286463 Add WmsLite monorepo with JWT auth, tenant context, and project READMEs.
Implement API security baseline (sys_user, JWT, audit fields, transactions,
unique constraints) and Web real login with 401 handling. Document setup
and development workflows in root, WmsLite.Api, and WmsLite.Web README files.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-24 14:17:47 +08:00

107 lines
3.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Globalization;
using System.Text.RegularExpressions;
using WmsLite.Api.Enums;
namespace WmsLite.Api.Common;
public static class LocationShelfBatchHelper
{
public const int MaxGeneratedLocationsPerRequest = 10_000;
public const int MaxLocationCodeLength = 64;
public const int MaxLocationNameLength = 256;
/// <summary>占位符正则:Label 或 Label:格式(如 Row:D2)。</summary>
private static readonly Regex PlaceholderRegex = new(
@"\{(\w+)(?::([^}]+))?\}",
RegexOptions.Compiled | RegexOptions.CultureInvariant);
public static int[] RowsForShelf(ShelfDepthKind kind) =>
kind switch
{
ShelfDepthKind.SingleShallowTwoRows => new[] { 1, 2 },
ShelfDepthKind.SingleSideDoubleTwoRows => new[] { 1, 2 },
ShelfDepthKind.DoubleDeepFourRows => new[] { 1, 2, 3, 4 },
_ => throw new BusinessException($"无效的伸位类型:{kind}。")
};
public static bool ResolveIsOuter(ShelfDepthKind kind, int rowNo) =>
kind switch
{
ShelfDepthKind.SingleShallowTwoRows => false,
ShelfDepthKind.SingleSideDoubleTwoRows => rowNo != 1,
ShelfDepthKind.DoubleDeepFourRows => rowNo is not (1 or 4),
_ => false
};
public static string FormatTemplate(
string template,
string warehouseCode,
string areaCode,
string zoneCode,
string aisleCode,
int row,
int column,
int layer)
{
return PlaceholderRegex.Replace(template, m =>
{
var token = m.Groups[1].Value;
var format = m.Groups[2].Success ? m.Groups[2].Value : null;
return token.ToUpperInvariant() switch
{
"WAREHOUSE" when format is null => warehouseCode,
"WAREHOUSE" => string.IsNullOrEmpty(format)
? warehouseCode
: FormatStringWithOptionalFormat(warehouseCode, format),
"AREA" when format is null => areaCode,
"AREA" => FormatStringWithOptionalFormat(areaCode, format!),
"ZONE" when format is null => zoneCode,
"ZONE" => FormatStringWithOptionalFormat(zoneCode, format),
"AISLE" when format is null => aisleCode,
"AISLE" => FormatStringWithOptionalFormat(aisleCode, format!),
"ROW" => FormatIntegerToken(row, format),
"COL" => FormatIntegerToken(column, format),
"LAYER" => FormatIntegerToken(layer, format),
_ => throw new BusinessException($"模板中存在未知占位符:{{{token}}}。")
};
});
}
private static string FormatIntegerToken(int value, string? format)
{
if (string.IsNullOrEmpty(format))
{
return value.ToString(CultureInfo.InvariantCulture);
}
try
{
return value.ToString(format, CultureInfo.InvariantCulture);
}
catch (FormatException ex)
{
throw new BusinessException($"占位符的数字格式无效:{format}。{ex.Message}");
}
}
private static string FormatStringWithOptionalFormat(string value, string format)
{
try
{
return string.Format(CultureInfo.InvariantCulture, $"{{0:{format}}}", value);
}
catch (FormatException)
{
return value;
}
}
}