103 lines
4.6 KiB
C#
103 lines
4.6 KiB
C#
using Fake.Application;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Oms.Application.Dtos.Operations;
|
||
using Oms.Application.Dtos.Shared;
|
||
using Oms.Domain.ChannelsAggregate.Repositories;
|
||
using Oms.Domain.OperationsAggregate.Entities;
|
||
using Oms.Domain.OperationsAggregate.Repositories;
|
||
using Oms.Domain.SystemAggregate.Repositories;
|
||
|
||
namespace Oms.Application.Services.Operations;
|
||
|
||
[ApiExplorerSettings(GroupName = "OMS-Operation")]
|
||
public class InboundRecordService : ApplicationService
|
||
{
|
||
private IInboundRecordRepository InboundRecordRepository => LazyServiceProvider.GetRequiredService<IInboundRecordRepository>();
|
||
private ISalesProductRepository SalesProductRepository => LazyServiceProvider.GetRequiredService<ISalesProductRepository>();
|
||
private IWarehouseAddressRepository WarehouseAddressRepository => LazyServiceProvider.GetRequiredService<IWarehouseAddressRepository>();
|
||
|
||
/// <summary>
|
||
/// 新增入库记录。
|
||
/// </summary>
|
||
/// <param name="input">入参:入库记录创建参数。</param>
|
||
/// <returns>入库记录结果。</returns>
|
||
/// <remarks>
|
||
/// 核心业务逻辑:
|
||
/// 1. 校验入参与关联对象、唯一性等前置条件。
|
||
/// 2. 组装目标实体及相关明细数据。
|
||
/// 3. 写入审计字段并持久化结果。
|
||
/// </remarks>
|
||
[HttpPost("/operation/inbound/create")]
|
||
public async Task<InboundRecordOutput> CreateAsync(CreateInboundRecordInput input)
|
||
{
|
||
var now = DateTime.Now;
|
||
var operatorId = CurrentUser.GetId<long>();
|
||
var entity = new InboundRecord
|
||
{
|
||
WaybillNo = input.WaybillNo,
|
||
Weight = input.Weight,
|
||
Length = input.Length,
|
||
Width = input.Width,
|
||
Height = input.Height,
|
||
SalesProductId = input.SalesProductId,
|
||
DestinationCountry = input.DestinationCountry,
|
||
WarehouseId = input.WarehouseId,
|
||
OperateTime = now,
|
||
OperatorId = operatorId
|
||
};
|
||
|
||
var savedEntity = await InboundRecordRepository.InsertAsync(entity);
|
||
return ObjectMapper.Map<InboundRecord, InboundRecordOutput>(savedEntity);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 查询最近入库记录列表。
|
||
/// </summary>
|
||
/// <param name="limit">入参:返回记录条数上限。</param>
|
||
/// <param name="warehouseId">入参:warehouseId。</param>
|
||
/// <returns>入库记录列表结果。</returns>
|
||
/// <remarks>
|
||
/// 核心业务逻辑:
|
||
/// 1. 根据查询条件构造筛选规则。
|
||
/// 2. 查询目标数据并补齐关联展示字段。
|
||
/// 3. 按接口约定返回列表结果。
|
||
/// </remarks>
|
||
[HttpGet("/operation/inbound/recent-list")]
|
||
public async Task<OmsItemsResult<InboundRecordOutput>> GetRecentListAsync([FromQuery] int limit = 20, [FromQuery] long? warehouseId = null)
|
||
{
|
||
var hasWarehouseFilter = warehouseId.HasValue;
|
||
var warehouseIdValue = warehouseId.GetValueOrDefault();
|
||
var entities = await InboundRecordRepository.GetPagedListAsync(
|
||
x => !hasWarehouseFilter || x.WarehouseId == warehouseIdValue,
|
||
1,
|
||
limit <= 0 ? 20 : limit,
|
||
new Dictionary<string, bool> { [nameof(InboundRecord.OperateTime)] = false, [nameof(InboundRecord.Id)] = false });
|
||
|
||
var salesProductIds = entities.Where(x => x.SalesProductId.HasValue).Select(x => x.SalesProductId!.Value).Distinct().ToArray();
|
||
var warehouseIds = entities.Where(x => x.WarehouseId.HasValue).Select(x => x.WarehouseId!.Value).Distinct().ToArray();
|
||
var products = salesProductIds.Length == 0
|
||
? []
|
||
: await SalesProductRepository.GetListAsync(x => salesProductIds.Contains(x.Id));
|
||
var warehouses = warehouseIds.Length == 0
|
||
? []
|
||
: await WarehouseAddressRepository.GetListAsync(x => warehouseIds.Contains(x.Id));
|
||
var productMap = products.ToDictionary(x => x.Id);
|
||
var warehouseMap = warehouses.ToDictionary(x => x.Id);
|
||
|
||
var items = entities.Select(entity =>
|
||
{
|
||
var output = ObjectMapper.Map<InboundRecord, InboundRecordOutput>(entity);
|
||
output.SalesProductName = entity.SalesProductId.HasValue && productMap.TryGetValue(entity.SalesProductId.Value, out var product)
|
||
? product.ProductName
|
||
: null;
|
||
output.WarehouseName = entity.WarehouseId.HasValue && warehouseMap.TryGetValue(entity.WarehouseId.Value, out var warehouse)
|
||
? warehouse.WarehouseName
|
||
: null;
|
||
output.OperatorName = entity.OperatorId?.ToString();
|
||
return output;
|
||
}).ToList();
|
||
|
||
return new OmsItemsResult<InboundRecordOutput> { Items = items };
|
||
}
|
||
}
|