using Fake.Application;
using Microsoft.AspNetCore.Mvc;
using Oms.Application.Dtos.Shared;
using Oms.Application.Dtos.Warehouse;
using Oms.Domain.WarehouseAggregate.Entities;
namespace Oms.Application.Services.Warehouse;
public partial class WarehouseService
{
///
/// 查询库位列表。
///
/// 入参:仓库 ID 与筛选条件。
/// 库位列表。
[HttpPost("/warehouse/bin-location/list")]
public async Task> GetBinLocationListAsync(WarehouseBinLocationListInput input)
{
if (input.WarehouseId <= 0)
{
throw new BusinessException("仓库ID不能为空");
}
var keyword = input.Keyword?.Trim();
var hasKeyword = !string.IsNullOrWhiteSpace(keyword);
var hasEnabledFilter = input.IsEnabled.HasValue;
var isEnabledValue = input.IsEnabled.GetValueOrDefault();
var items = await BinLocationRepository.GetListAsync(x =>
x.WarehouseId == input.WarehouseId
&& (!hasEnabledFilter || x.IsEnabled == isEnabledValue)
&& (!hasKeyword || x.BinCode.Contains(keyword!) || (x.BinName != null && x.BinName.Contains(keyword!))));
items = items.OrderBy(x => x.BinCode).ThenBy(x => x.Id).ToList();
return new OmsItemsResult
{
Items = items.Select(MapBinLocation).ToList()
};
}
///
/// 保存库位(新增或编辑)。
///
/// 入参:库位保存数据。
/// 保存后的库位。
[HttpPost("/warehouse/bin-location/save")]
public async Task SaveBinLocationAsync(WarehouseBinLocationSaveInput input)
{
var binCode = input.BinCode.Trim();
if (string.IsNullOrWhiteSpace(binCode))
{
throw new BusinessException("库位编码不能为空");
}
CBinLocation entity;
if (input.Id > 0)
{
entity = await BinLocationRepository.FirstOrDefaultAsync(x => x.Id == input.Id)
?? throw new BusinessException("库位不存在");
if (entity.WarehouseId != input.WarehouseId)
{
throw new BusinessException("库位仓库不匹配");
}
}
else
{
entity = new CBinLocation { WarehouseId = input.WarehouseId };
}
var duplicate = await BinLocationRepository.FirstOrDefaultAsync(x =>
x.WarehouseId == input.WarehouseId && x.BinCode == binCode && x.Id != entity.Id);
if (duplicate != null)
{
throw new BusinessException("库位编码已存在");
}
entity.BinCode = binCode;
entity.BinName = string.IsNullOrWhiteSpace(input.BinName) ? null : input.BinName.Trim();
entity.ZoneCode = string.IsNullOrWhiteSpace(input.ZoneCode) ? null : input.ZoneCode.Trim();
entity.IsEnabled = input.IsEnabled;
entity.Remark = string.IsNullOrWhiteSpace(input.Remark) ? null : input.Remark.Trim();
if (input.Id > 0)
{
await BinLocationRepository.UpdateAsync(entity);
}
else
{
await BinLocationRepository.InsertAsync(entity);
}
return MapBinLocation(entity);
}
private static WarehouseBinLocationOutput MapBinLocation(CBinLocation entity) =>
new()
{
Id = entity.Id,
WarehouseId = entity.WarehouseId,
BinCode = entity.BinCode,
BinName = entity.BinName,
ZoneCode = entity.ZoneCode,
IsEnabled = entity.IsEnabled,
Remark = entity.Remark
};
}