Files
WMSlite/WmsLite.Api/Services/AreaService.cs
T
zhougfandCursor 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

129 lines
4.4 KiB
C#

using System.Threading.Tasks;
using WmsLite.Api.Common;
using WmsLite.Api.Dtos.Area;
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 AreaService : IAreaService
{
private readonly IAreaRepository _areaRepository;
private readonly IWarehouseRepository _warehouseRepository;
private readonly ICurrentUserContext _userContext;
public AreaService(
IAreaRepository areaRepository,
IWarehouseRepository warehouseRepository,
ICurrentUserContext userContext)
{
_areaRepository = areaRepository;
_warehouseRepository = warehouseRepository;
_userContext = userContext;
}
public async Task<PagedResult<AreaViewDto>> GetPagedListAsync(AreaPagedQuery query)
{
query.TenantId = _userContext.TenantId.Trim();
query.OwnerCode = _userContext.OwnerCode.Trim();
var page = await _areaRepository.GetPagedListAsync(query);
var items = page.Items.ConvertAll(MapToView);
return new PagedResult<AreaViewDto>
{
PageIndex = page.PageIndex,
PageSize = page.PageSize,
TotalCount = page.TotalCount,
Items = items
};
}
public async Task<AreaViewDto?> GetByIdAsync(long id, string tenantId, string ownerCode)
{
var entity = await _areaRepository.GetByIdAsync(id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
return entity is null ? null : MapToView(entity);
}
public async Task<AreaViewDto> CreateAsync(AreaSaveRequest request)
{
var warehouseCode = request.WarehouseCode.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, code) is not null)
{
throw new BusinessException($"该仓库下库区编码已存在: {code}");
}
var entity = new Area
{
TenantId = _userContext.TenantId.Trim(),
OwnerCode = _userContext.OwnerCode.Trim(),
WarehouseCode = warehouseCode,
Code = code,
Name = name,
Attrs = request.Attrs
};
await _areaRepository.InsertAsync(entity);
return MapToView(entity);
}
public async Task<AreaViewDto?> UpdateAsync(AreaUpdateRequest request)
{
var entity = await _areaRepository.GetByIdAsync(request.Id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
if (entity is null)
{
return null;
}
var warehouseCode = request.WarehouseCode.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}");
}
var duplicate = await _areaRepository.GetByWarehouseAndAreaCodeAsync(_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), warehouseCode, code);
if (duplicate is not null && duplicate.Id != entity.Id)
{
throw new BusinessException($"该仓库下库区编码已被占用: {code}");
}
entity.WarehouseCode = warehouseCode;
entity.Code = code;
entity.Name = name;
entity.Attrs = request.Attrs;
await _areaRepository.UpdateAsync(entity);
return MapToView(entity);
}
public Task<int> DeleteAsync(long id, string tenantId, string ownerCode)
{
return _areaRepository.SoftDeleteAsync(id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
}
private static AreaViewDto MapToView(Area entity)
{
return new AreaViewDto
{
Id = entity.Id,
TenantId = entity.TenantId,
OwnerCode = entity.OwnerCode,
WarehouseCode = entity.WarehouseCode,
Code = entity.Code,
Name = entity.Name,
Attrs = entity.Attrs
};
}
}