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>
This commit is contained in:
zhougfandCursor committed 2026-05-24 14:17:47 +08:00
commit fa1b286463
241 files changed
+31188

No files matched your search

+347
View File
@@ -0,0 +1,347 @@
using WmsLite.Api.Common;
using WmsLite.Api.Dtos.Location;
using WmsLite.Api.Enums;
using WmsLite.Api.Infrastructure;
using WmsLite.Api.Infrastructure.Auth;
using WmsLite.Api.Models;
using WmsLite.Api.Repositories.Interfaces;
using WmsLite.Api.Services.Interfaces;
namespace WmsLite.Api.Services;
public class LocationService : ILocationService
{
private readonly ITransactionRunner _tx;
private readonly ILocationRepository _locationRepository;
private readonly IZoneRepository _zoneRepository;
private readonly ICurrentUserContext _userContext;
public LocationService(
ITransactionRunner tx,
ILocationRepository locationRepository,
IZoneRepository zoneRepository,
ICurrentUserContext userContext)
{
_tx = tx;
_locationRepository = locationRepository;
_zoneRepository = zoneRepository;
_userContext = userContext;
}
public async Task<PagedResult<LocationViewDto>> GetPagedListAsync(LocationPagedQuery query)
{
query.TenantId = _userContext.TenantId.Trim();
query.OwnerCode = _userContext.OwnerCode.Trim();
var page = await _locationRepository.GetPagedListAsync(query);
var items = page.Items.ConvertAll(MapToView);
return new PagedResult<LocationViewDto>
{
PageIndex = page.PageIndex,
PageSize = page.PageSize,
TotalCount = page.TotalCount,
Items = items
};
}
public async Task<LocationViewDto?> GetByIdAsync(long id, string tenantId, string ownerCode)
{
var entity = await _locationRepository.GetByIdAsync(id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
return entity is null ? null : MapToView(entity);
}
public async Task<LocationViewDto> CreateAsync(LocationSaveRequest request)
{
var warehouseCode = request.WarehouseCode.Trim();
var areaCode = request.AreaCode.Trim();
var zoneCode = request.ZoneCode.Trim();
var code = request.Code.Trim();
if (await _zoneRepository.GetByWarehouseAreaAndZoneCodeAsync(
_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), warehouseCode, areaCode, zoneCode) is null)
{
throw new BusinessException($"分区不存在: {warehouseCode} / {areaCode} / {zoneCode}");
}
if (await _locationRepository.GetByWarehouseAreaZoneAndLocationCodeAsync(
_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), warehouseCode, areaCode, zoneCode, code) is not null)
{
throw new BusinessException($"储位编码已存在: {code}");
}
var entity = new Location
{
TenantId = _userContext.TenantId.Trim(),
OwnerCode = _userContext.OwnerCode.Trim(),
WarehouseCode = warehouseCode,
AreaCode = areaCode,
ZoneCode = zoneCode,
AisleCode = string.IsNullOrWhiteSpace(request.AisleCode) ? null : request.AisleCode.Trim(),
Code = code,
Name = request.Name?.Trim(),
RowNo = request.RowNo,
ColumnNo = request.ColumnNo,
LayerNo = request.LayerNo,
Height = request.Height,
LocationStatus = request.LocationStatus,
IsOuter = request.IsOuter,
ContainerCode = string.IsNullOrWhiteSpace(request.ContainerCode) ? null : request.ContainerCode.Trim(),
Attrs = request.Attrs
};
await _locationRepository.InsertAsync(entity);
return MapToView(entity);
}
public async Task<LocationViewDto?> UpdateAsync(LocationUpdateRequest request)
{
var entity = await _locationRepository.GetByIdAsync(request.Id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
if (entity is null)
{
return null;
}
var warehouseCode = request.WarehouseCode.Trim();
var areaCode = request.AreaCode.Trim();
var zoneCode = request.ZoneCode.Trim();
var code = request.Code.Trim();
if (await _zoneRepository.GetByWarehouseAreaAndZoneCodeAsync(
_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), warehouseCode, areaCode, zoneCode) is null)
{
throw new BusinessException($"分区不存在: {warehouseCode} / {areaCode} / {zoneCode}");
}
var duplicate = await _locationRepository.GetByWarehouseAreaZoneAndLocationCodeAsync(
_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), warehouseCode, areaCode, zoneCode, code);
if (duplicate is not null && duplicate.Id != entity.Id)
{
throw new BusinessException($"储位编码已被占用: {code}");
}
entity.WarehouseCode = warehouseCode;
entity.AreaCode = areaCode;
entity.ZoneCode = zoneCode;
entity.Code = code;
entity.Name = request.Name?.Trim();
entity.AisleCode = string.IsNullOrWhiteSpace(request.AisleCode) ? null : request.AisleCode.Trim();
entity.RowNo = request.RowNo;
entity.ColumnNo = request.ColumnNo;
entity.LayerNo = request.LayerNo;
entity.Height = request.Height;
entity.LocationStatus = request.LocationStatus;
entity.IsOuter = request.IsOuter;
entity.ContainerCode = string.IsNullOrWhiteSpace(request.ContainerCode) ? null : request.ContainerCode.Trim();
entity.Attrs = request.Attrs;
await _locationRepository.UpdateAsync(entity);
return MapToView(entity);
}
public Task<int> DeleteAsync(long id, string tenantId, string ownerCode)
{
return _locationRepository.SoftDeleteAsync(id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
}
/// <inheritdoc />
public async Task<LocationBatchCreateResultDto> BatchCreateAsync(LocationBatchCreateRequest request)
{
var tenant = _userContext.TenantId.Trim();
var owner = _userContext.OwnerCode.Trim();
var warehouseCode = request.WarehouseCode.Trim();
var areaCode = request.AreaCode.Trim();
var zoneCode = request.ZoneCode.Trim();
var aisleCode = request.AisleCode.Trim();
if (request.ColumnFrom > request.ColumnTo)
{
throw new BusinessException("列起始不能大于列结束。");
}
if (request.LayerFrom > request.LayerTo)
{
throw new BusinessException("层起始不能大于层结束。");
}
var rows = LocationShelfBatchHelper.RowsForShelf(request.ShelfDepthKind);
checked
{
long totalLength = rows.LongLength;
var colSpan = (long)(request.ColumnTo - request.ColumnFrom + 1);
var layerSpan = (long)(request.LayerTo - request.LayerFrom + 1);
var total = totalLength * colSpan * layerSpan;
if (total > LocationShelfBatchHelper.MaxGeneratedLocationsPerRequest)
{
throw new BusinessException(
$"本次生成数量为 {total},超过单次上限 {LocationShelfBatchHelper.MaxGeneratedLocationsPerRequest},请缩小列/层范围或分批生成。");
}
}
if (await _zoneRepository.GetByWarehouseAreaAndZoneCodeAsync(tenant, owner, warehouseCode, areaCode, zoneCode) is null)
{
throw new BusinessException($"分区不存在: {warehouseCode} / {areaCode} / {zoneCode}");
}
var toGenerate = BuildBatchLocations(
request, tenant, owner, warehouseCode, areaCode, zoneCode, aisleCode, rows);
var plannedCodes = new List<string>(toGenerate.Count);
var seenCodes = new HashSet<string>(StringComparer.Ordinal);
foreach (var loc in toGenerate)
{
if (!seenCodes.Add(loc.Code))
{
throw new BusinessException($"生成的库位编码在批次内重复:{loc.Code}。请检查模板是否包含行列层等区分字段。");
}
plannedCodes.Add(loc.Code);
}
var existingInDb =
await _locationRepository.GetExistingCodesAsync(tenant, owner, warehouseCode, areaCode, zoneCode, plannedCodes);
if (request.DuplicatePolicy == LocationBatchDuplicatePolicy.AbortOnDuplicate && existingInDb.Count > 0)
{
var sample = string.Join(", ", existingInDb.OrderBy(static x => x, StringComparer.Ordinal).Take(20));
var more = existingInDb.Count > 20 ? $" 等共 {existingInDb.Count} 条" : string.Empty;
throw new BusinessException($"存在已入库位编码(重复策略:整批中止)。示例:{sample}{more}");
}
List<Location> toInsert =
request.DuplicatePolicy == LocationBatchDuplicatePolicy.SkipExisting ?
toGenerate.Where(loc => !existingInDb.Contains(loc.Code)).ToList() :
toGenerate;
if (toInsert.Count == 0)
{
return new LocationBatchCreateResultDto
{
CreatedCount = 0,
SkippedCodes = existingInDb.OrderBy(static x => x, StringComparer.Ordinal).ToList()
};
}
return await _tx.ExecuteAsync(async () =>
{
await _locationRepository.InsertManyAsync(toInsert);
return new LocationBatchCreateResultDto
{
CreatedCount = toInsert.Count,
SkippedCodes =
request.DuplicatePolicy == LocationBatchDuplicatePolicy.SkipExisting ?
existingInDb.OrderBy(static x => x, StringComparer.Ordinal).ToList() :
new List<string>()
};
});
}
private static List<Location> BuildBatchLocations(
LocationBatchCreateRequest request,
string tenantId,
string ownerCode,
string warehouseCode,
string areaCode,
string zoneCode,
string aisleCode,
int[] rows)
{
var list = new List<Location>();
foreach (var rowNo in rows)
{
for (var column = request.ColumnFrom; column <= request.ColumnTo; column++)
{
for (var layer = request.LayerFrom; layer <= request.LayerTo; layer++)
{
var code = LocationShelfBatchHelper.FormatTemplate(
request.CodeTemplate.Trim(),
warehouseCode,
areaCode,
zoneCode,
aisleCode,
rowNo,
column,
layer);
code = code.Trim();
if (string.IsNullOrEmpty(code))
{
throw new BusinessException("模板生成的储位编码为空。");
}
if (code.Length > LocationShelfBatchHelper.MaxLocationCodeLength)
{
throw new BusinessException(
$"生成的储位编码长度 {code.Length} 超过上限 {LocationShelfBatchHelper.MaxLocationCodeLength}(示例:{code})");
}
string? name = null;
if (!string.IsNullOrWhiteSpace(request.NameTemplate))
{
name = LocationShelfBatchHelper.FormatTemplate(
request.NameTemplate!.Trim(),
warehouseCode,
areaCode,
zoneCode,
aisleCode,
rowNo,
column,
layer).Trim();
if (name.Length > LocationShelfBatchHelper.MaxLocationNameLength)
{
throw new BusinessException(
$"生成的库位名称长度 {name.Length} 超过上限 {LocationShelfBatchHelper.MaxLocationNameLength}(示例:{name})");
}
}
var isOuter = LocationShelfBatchHelper.ResolveIsOuter(request.ShelfDepthKind, rowNo);
list.Add(new Location
{
TenantId = tenantId,
OwnerCode = ownerCode,
WarehouseCode = warehouseCode,
AreaCode = areaCode,
ZoneCode = zoneCode,
AisleCode = aisleCode,
Code = code,
Name = string.IsNullOrEmpty(name) ? null : name,
RowNo = rowNo,
ColumnNo = column,
LayerNo = layer,
Height = request.Height,
LocationStatus = request.LocationStatus,
IsOuter = isOuter,
ContainerCode = null,
Attrs = null
});
}
}
}
return list;
}
private static LocationViewDto MapToView(Location entity)
{
return new LocationViewDto
{
Id = entity.Id,
TenantId = entity.TenantId,
OwnerCode = entity.OwnerCode,
WarehouseCode = entity.WarehouseCode,
AreaCode = entity.AreaCode,
ZoneCode = entity.ZoneCode,
AisleCode = entity.AisleCode,
Code = entity.Code,
Name = entity.Name,
RowNo = entity.RowNo,
ColumnNo = entity.ColumnNo,
LayerNo = entity.LayerNo,
Height = entity.Height,
LocationStatus = entity.LocationStatus,
IsOuter = entity.IsOuter,
ContainerCode = entity.ContainerCode,
Attrs = entity.Attrs
};
}
}