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>
111 lines
3.9 KiB
C#
111 lines
3.9 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using WmsLite.Api.Common;
|
|
using WmsLite.Api.Dtos.Container;
|
|
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 ContainerService : IContainerService
|
|
{
|
|
private readonly IContainerRepository _containerRepository;
|
|
private readonly ICurrentUserContext _userContext;
|
|
|
|
public ContainerService(IContainerRepository containerRepository, ICurrentUserContext userContext)
|
|
{
|
|
_containerRepository = containerRepository;
|
|
_userContext = userContext;
|
|
}
|
|
|
|
public async Task<PagedResult<ContainerViewDto>> GetPagedListAsync(ContainerPagedQuery query)
|
|
{
|
|
query.TenantId = _userContext.TenantId.Trim();
|
|
query.OwnerCode = _userContext.OwnerCode.Trim();
|
|
var page = await _containerRepository.GetPagedListAsync(query);
|
|
var items = page.Items.ConvertAll(MapToView);
|
|
return new PagedResult<ContainerViewDto>
|
|
{
|
|
PageIndex = page.PageIndex,
|
|
PageSize = page.PageSize,
|
|
TotalCount = page.TotalCount,
|
|
Items = items
|
|
};
|
|
}
|
|
|
|
public async Task<ContainerViewDto?> GetByIdAsync(long id, string tenantId, string ownerCode)
|
|
{
|
|
var entity = await _containerRepository.GetByIdAsync(id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
|
|
return entity is null ? null : MapToView(entity);
|
|
}
|
|
|
|
public async Task<ContainerViewDto> CreateAsync(ContainerSaveRequest request)
|
|
{
|
|
var code = request.Code.Trim();
|
|
|
|
if (await _containerRepository.GetByCodeAsync(_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), code) is not null)
|
|
{
|
|
throw new BusinessException($"容器编码已存在: {code}");
|
|
}
|
|
|
|
var entity = new Container
|
|
{
|
|
TenantId = _userContext.TenantId.Trim(),
|
|
OwnerCode = _userContext.OwnerCode.Trim(),
|
|
Code = code,
|
|
Name = string.IsNullOrWhiteSpace(request.Name) ? null : request.Name.Trim(),
|
|
ContainerType = string.IsNullOrWhiteSpace(request.ContainerType) ? null : request.ContainerType.Trim(),
|
|
Attrs = request.Attrs
|
|
};
|
|
|
|
await _containerRepository.InsertAsync(entity);
|
|
return MapToView(entity);
|
|
}
|
|
|
|
public async Task<ContainerViewDto?> UpdateAsync(ContainerUpdateRequest request)
|
|
{
|
|
var entity = await _containerRepository.GetByIdAsync(request.Id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
|
|
if (entity is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var code = request.Code.Trim();
|
|
|
|
var existing = await _containerRepository.GetByCodeAsync(_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), code);
|
|
if (existing is not null && existing.Id != entity.Id)
|
|
{
|
|
throw new BusinessException($"容器编码已被占用: {code}");
|
|
}
|
|
|
|
entity.Code = code;
|
|
entity.Name = string.IsNullOrWhiteSpace(request.Name) ? null : request.Name.Trim();
|
|
entity.ContainerType = string.IsNullOrWhiteSpace(request.ContainerType) ? null : request.ContainerType.Trim();
|
|
entity.Attrs = request.Attrs;
|
|
|
|
await _containerRepository.UpdateAsync(entity);
|
|
return MapToView(entity);
|
|
}
|
|
|
|
public Task<int> DeleteAsync(long id, string tenantId, string ownerCode)
|
|
{
|
|
return _containerRepository.SoftDeleteAsync(id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
|
|
}
|
|
|
|
private static ContainerViewDto MapToView(Container entity)
|
|
{
|
|
return new ContainerViewDto
|
|
{
|
|
Id = entity.Id,
|
|
TenantId = entity.TenantId,
|
|
OwnerCode = entity.OwnerCode,
|
|
Code = entity.Code,
|
|
Name = entity.Name,
|
|
ContainerType = entity.ContainerType,
|
|
Attrs = entity.Attrs
|
|
};
|
|
}
|
|
}
|