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(); private ISalesProductRepository SalesProductRepository => LazyServiceProvider.GetRequiredService(); private IWarehouseAddressRepository WarehouseAddressRepository => LazyServiceProvider.GetRequiredService(); /// /// 新增入库记录。 /// /// 入参:入库记录创建参数。 /// 入库记录结果。 /// /// 核心业务逻辑: /// 1. 校验入参与关联对象、唯一性等前置条件。 /// 2. 组装目标实体及相关明细数据。 /// 3. 写入审计字段并持久化结果。 /// [HttpPost("/operation/inbound/create")] public async Task CreateAsync(CreateInboundRecordInput input) { var now = DateTime.Now; var operatorId = CurrentUser.GetId(); 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(savedEntity); } /// /// 查询最近入库记录列表。 /// /// 入参:返回记录条数上限。 /// 入参:warehouseId。 /// 入库记录列表结果。 /// /// 核心业务逻辑: /// 1. 根据查询条件构造筛选规则。 /// 2. 查询目标数据并补齐关联展示字段。 /// 3. 按接口约定返回列表结果。 /// [HttpGet("/operation/inbound/recent-list")] public async Task> 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 { [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(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 { Items = items }; } }