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> GetPagedListAsync(AreaPagedQuery query) { var page = await _areaRepository.GetPagedListAsync(query); var items = page.Items.ConvertAll(MapToView); return new PagedResult { PageIndex = page.PageIndex, PageSize = page.PageSize, TotalCount = page.TotalCount, Items = items }; } public async Task 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 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 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 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 }; } }