116 lines
3.8 KiB
C#
116 lines
3.8 KiB
C#
using System;
|
|
using System.Threading.Tasks;
|
|
using WmsLite.Api.Common;
|
|
using WmsLite.Api.Dtos.Material;
|
|
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 MaterialService : IMaterialService
|
|
{
|
|
private readonly IMaterialRepository _materialRepository;
|
|
private readonly ICurrentUserContext _userContext;
|
|
|
|
public MaterialService(IMaterialRepository materialRepository, ICurrentUserContext userContext)
|
|
{
|
|
_materialRepository = materialRepository;
|
|
_userContext = userContext;
|
|
}
|
|
|
|
public async Task<PagedResult<MaterialViewDto>> GetPagedListAsync(MaterialPagedQuery query)
|
|
{
|
|
var page = await _materialRepository.GetPagedListAsync(query);
|
|
var items = page.Items.ConvertAll(MapToView);
|
|
return new PagedResult<MaterialViewDto>
|
|
{
|
|
PageIndex = page.PageIndex,
|
|
PageSize = page.PageSize,
|
|
TotalCount = page.TotalCount,
|
|
Items = items
|
|
};
|
|
}
|
|
|
|
public async Task<MaterialViewDto?> GetByIdAsync(long id, string tenantId, string ownerCode)
|
|
{
|
|
var entity = await _materialRepository.GetByIdAsync(id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
|
|
return entity is null ? null : MapToView(entity);
|
|
}
|
|
|
|
public async Task<MaterialViewDto> CreateAsync(MaterialSaveRequest request)
|
|
{
|
|
var code = request.Code.Trim();
|
|
var name = request.Name.Trim();
|
|
var unit = request.Unit.Trim();
|
|
|
|
if (await _materialRepository.GetByCodeAsync(_userContext.TenantId.Trim(), _userContext.OwnerCode.Trim(), code) is not null)
|
|
{
|
|
throw new BusinessException($"物料编码已存在: {code}");
|
|
}
|
|
|
|
var entity = new Material
|
|
{
|
|
TenantId = _userContext.TenantId.Trim(),
|
|
OwnerCode = _userContext.OwnerCode.Trim(),
|
|
Code = code,
|
|
Name = name,
|
|
Spec = string.IsNullOrWhiteSpace(request.Spec) ? null : request.Spec.Trim(),
|
|
Unit = unit,
|
|
Attrs = request.Attrs
|
|
};
|
|
|
|
await _materialRepository.InsertAsync(entity);
|
|
return MapToView(entity);
|
|
}
|
|
|
|
public async Task<MaterialViewDto?> UpdateAsync(MaterialUpdateRequest request)
|
|
{
|
|
var entity = await _materialRepository.GetByIdAsync(request.Id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
|
|
if (entity is null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var code = request.Code.Trim();
|
|
var name = request.Name.Trim();
|
|
var unit = request.Unit.Trim();
|
|
|
|
var existing = await _materialRepository.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 = name;
|
|
entity.Spec = string.IsNullOrWhiteSpace(request.Spec) ? null : request.Spec.Trim();
|
|
entity.Unit = unit;
|
|
entity.Attrs = request.Attrs;
|
|
|
|
await _materialRepository.UpdateAsync(entity);
|
|
return MapToView(entity);
|
|
}
|
|
|
|
public Task<int> DeleteAsync(long id, string tenantId, string ownerCode)
|
|
{
|
|
return _materialRepository.SoftDeleteAsync(id, _userContext.TenantId.Trim(), _userContext.OwnerCode.Trim());
|
|
}
|
|
|
|
private static MaterialViewDto MapToView(Material entity)
|
|
{
|
|
return new MaterialViewDto
|
|
{
|
|
Id = entity.Id,
|
|
TenantId = entity.TenantId,
|
|
OwnerCode = entity.OwnerCode,
|
|
Code = entity.Code,
|
|
Name = entity.Name,
|
|
Spec = entity.Spec,
|
|
Unit = entity.Unit,
|
|
Attrs = entity.Attrs
|
|
};
|
|
}
|
|
}
|