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>
149 lines
5.3 KiB
C#
149 lines
5.3 KiB
C#
using System.Threading.Tasks;
|
|
using WmsLite.Api.Common;
|
|
using WmsLite.Api.Dtos.Zone;
|
|
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 ZoneService : IZoneService
|
|
{
|
|
private readonly IZoneRepository _zoneRepository;
|
|
private readonly IWarehouseRepository _warehouseRepository;
|
|
private readonly IAreaRepository _areaRepository;
|
|
private readonly ICurrentUserContext _userContext;
|
|
|
|
public ZoneService(
|
|
IZoneRepository zoneRepository,
|
|
IWarehouseRepository warehouseRepository,
|
|
IAreaRepository areaRepository,
|
|
ICurrentUserContext userContext)
|
|
{
|
|
_zoneRepository = zoneRepository;
|
|
_warehouseRepository = warehouseRepository;
|
|
_areaRepository = areaRepository;
|
|
_userContext = userContext;
|
|
}
|
|
|
|
public async Task<PagedResult<ZoneViewDto>> GetPagedListAsync(ZonePagedQuery query)
|
|
{
|
|
query.TenantId = _userContext.TenantId.Trim();
|
|
query.OwnerCode = _userContext.OwnerCode.Trim();
|
|
var page = await _zoneRepository.GetPagedListAsync(query);
|
|
var items = page.Items.ConvertAll(MapToView);
|
|
return new PagedResult<ZoneViewDto>
|
|
{
|
|
PageIndex = page.PageIndex,
|
|
PageSize = page.PageSize,
|
|
TotalCount = page.TotalCount,
|
|
Items = items
|
|
};
|
|
}
|
|
|
|
public async Task<ZoneViewDto?> GetByIdAsync(long id, string tenantId, string ownerCode)
|
|
{
|
|
var entity = await _zoneRepository.GetByIdAsync(id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
|
|
return entity is null ? null : MapToView(entity);
|
|
}
|
|
|
|
public async Task<ZoneViewDto> CreateAsync(ZoneSaveRequest request)
|
|
{
|
|
var warehouseCode = request.WarehouseCode.Trim();
|
|
var areaCode = request.AreaCode.Trim();
|
|
var code = request.Code.Trim();
|
|
var name = request.Name.Trim();
|
|
|
|
if (await _warehouseRepository.GetByCodeAsync(_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), warehouseCode) is null)
|
|
{
|
|
throw new BusinessException($"仓库不存在: {warehouseCode}");
|
|
}
|
|
|
|
if (await _areaRepository.GetByWarehouseAndAreaCodeAsync(_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), warehouseCode, areaCode) is null)
|
|
{
|
|
throw new BusinessException($"库区不存在: {warehouseCode} / {areaCode}");
|
|
}
|
|
|
|
if (await _zoneRepository.GetByWarehouseAreaAndZoneCodeAsync(
|
|
_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), warehouseCode, areaCode, code) is not null)
|
|
{
|
|
throw new BusinessException($"该库区下分区编码已存在: {code}");
|
|
}
|
|
|
|
var entity = new Zone
|
|
{
|
|
TenantId = _userContext.TenantId.Trim(),
|
|
OwnerCode = _userContext.OwnerCode.Trim(),
|
|
WarehouseCode = warehouseCode,
|
|
AreaCode = areaCode,
|
|
Code = code,
|
|
Name = name,
|
|
Attrs = request.Attrs
|
|
};
|
|
|
|
await _zoneRepository.InsertAsync(entity);
|
|
return MapToView(entity);
|
|
}
|
|
|
|
public async Task<ZoneViewDto?> UpdateAsync(ZoneUpdateRequest request)
|
|
{
|
|
var entity = await _zoneRepository.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 code = request.Code.Trim();
|
|
var name = request.Name.Trim();
|
|
|
|
if (await _warehouseRepository.GetByCodeAsync(_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), warehouseCode) is null)
|
|
{
|
|
throw new BusinessException($"仓库不存在: {warehouseCode}");
|
|
}
|
|
|
|
if (await _areaRepository.GetByWarehouseAndAreaCodeAsync(_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), warehouseCode, areaCode) is null)
|
|
{
|
|
throw new BusinessException($"库区不存在: {warehouseCode} / {areaCode}");
|
|
}
|
|
|
|
var duplicate = await _zoneRepository.GetByWarehouseAreaAndZoneCodeAsync(
|
|
_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), warehouseCode, areaCode, code);
|
|
if (duplicate is not null && duplicate.Id != entity.Id)
|
|
{
|
|
throw new BusinessException($"该库区下分区编码已被占用: {code}");
|
|
}
|
|
|
|
entity.WarehouseCode = warehouseCode;
|
|
entity.AreaCode = areaCode;
|
|
entity.Code = code;
|
|
entity.Name = name;
|
|
entity.Attrs = request.Attrs;
|
|
|
|
await _zoneRepository.UpdateAsync(entity);
|
|
return MapToView(entity);
|
|
}
|
|
|
|
public Task<int> DeleteAsync(long id, string tenantId, string ownerCode)
|
|
{
|
|
return _zoneRepository.SoftDeleteAsync(id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
|
|
}
|
|
|
|
private static ZoneViewDto MapToView(Zone entity)
|
|
{
|
|
return new ZoneViewDto
|
|
{
|
|
Id = entity.Id,
|
|
TenantId = entity.TenantId,
|
|
OwnerCode = entity.OwnerCode,
|
|
WarehouseCode = entity.WarehouseCode,
|
|
AreaCode = entity.AreaCode,
|
|
Code = entity.Code,
|
|
Name = entity.Name,
|
|
Attrs = entity.Attrs
|
|
};
|
|
}
|
|
}
|