列表 predicate 改用 hasWarehouseFilter/hasStoreFilter 等本地变量,避免 ORM 生成非法 SQL。 Co-authored-by: Cursor <cursoragent@cursor.com>
106 lines
3.8 KiB
C#
106 lines
3.8 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 查询库位列表。
|
|
/// </summary>
|
|
/// <param name="input">入参:仓库 ID 与筛选条件。</param>
|
|
/// <returns>库位列表。</returns>
|
|
[HttpPost("/warehouse/bin-location/list")]
|
|
public async Task<OmsItemsResult<WarehouseBinLocationOutput>> 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<WarehouseBinLocationOutput>
|
|
{
|
|
Items = items.Select(MapBinLocation).ToList()
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存库位(新增或编辑)。
|
|
/// </summary>
|
|
/// <param name="input">入参:库位保存数据。</param>
|
|
/// <returns>保存后的库位。</returns>
|
|
[HttpPost("/warehouse/bin-location/save")]
|
|
public async Task<WarehouseBinLocationOutput> 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
|
|
};
|
|
}
|