heshaofeng
2025-11-28 27ac9d45281117d277ace08338ad1425b1d4dcd6
ÏîÄ¿´úÂë/WMSÎÞ²Ö´¢°æ/WIDESEA_WMSServer/WIDESEA_AllocateService/AllocateService.cs
@@ -1,10 +1,15 @@
using SqlSugar;
using Autofac.Core;
using MailKit.Search;
using Microsoft.Extensions.Logging;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.AllocateEnum;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
@@ -28,32 +33,36 @@
        public readonly IOutboundService _outboundService;
        public readonly IRepository<Dt_AllocateOrder> _allocateOrderRepository;
        public readonly IRepository<Dt_AllocateOrderDetail> _allocateOrderDetailRepository;
        private readonly ILogger<AllocateService> _logger;
        public AllocateService(IRepository<Dt_AllocateOrder> BaseDal,
            IUnitOfWorkManage unitOfWorkManage,
            IInboundService inboundService,
            IOutboundService outboundService,
            IRepository<Dt_AllocateOrder> allocateOrderRepository,
            IRepository<Dt_AllocateOrderDetail> allocateOrderDetailRepository) : base(BaseDal)
            IRepository<Dt_AllocateOrderDetail> allocateOrderDetailRepository,
            ILogger<AllocateService> logger) : base(BaseDal)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _inboundService = inboundService;
            _outboundService = outboundService;
            _allocateOrderRepository = allocateOrderRepository;
            _allocateOrderDetailRepository = allocateOrderDetailRepository;
            _logger = logger;
        }
        public IRepository<Dt_AllocateOrder> Repository => BaseDal;
        public WebResponseContent ReceiveAllocateOrder(Dt_AllocateOrder allocateOrder, int operateType)
        public async Task<WebResponseContent> ReceiveAllocateOrder(Dt_AllocateOrder allocateOrder, int operateType)
        {
            try
            {
                return operateType switch
                {
                    1 => AddInboundOrder(allocateOrder),
                    2 => UpdateInboundOrder(allocateOrder),
                    3 => DeleteInboundOrder(allocateOrder),
                    1 => await AddAllocateOrder(allocateOrder),
                    2 => await UpdateAllocateOrder(allocateOrder),
                    3 => await DeleteAllocateOrder(allocateOrder),
                    _ => WebResponseContent.Instance.OK(),
                };
@@ -64,7 +73,7 @@
            }
        }
        public WebResponseContent AddInboundOrder(Dt_AllocateOrder allocateOrder)
        public async Task<WebResponseContent> AddAllocateOrder(Dt_AllocateOrder allocateOrder)
        {
            try
            {
@@ -74,32 +83,49 @@
                }
                allocateOrder.OrderNo = CreateCodeByRule(nameof(RuleCodeEnum.AllocateOrderCodeRule));
                Db.InsertNav(allocateOrder).Include(x => x.Details).ExecuteCommand();
                if (Enum.TryParse<BusinessTypeEnum>(allocateOrder.BusinessType, out var businessType))
                {
                    if (businessType == BusinessTypeEnum.外部仓库调智仓)
                    {
                        var inboundOrders = ConvertToInboundOrders(allocateOrder);
                        _inboundService.InbounOrderService.ReceiveInboundOrder(inboundOrders, 1);
                    }
                    else if (businessType == BusinessTypeEnum.智仓调外部仓库)
                    {
                        var outboundOrders = ConvertToOutboundOrders(allocateOrder);
                        _outboundService.OutboundOrderService.ReceiveOutboundOrder(outboundOrders, 1);
                    }
                    else
                    {
                        // å¤„理未定义的枚举值(如未来新增但未实现的类型)
                        throw new NotSupportedException($"不支持的业务类型枚举值: {businessType}");
                    }
                }
                await AddInOutData(allocateOrder);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _logger.LogInformation("AllocateService AddAllocateOrder  err:  " + ex.Message);
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent UpdateInboundOrder(Dt_AllocateOrder model)
        private async Task AddInOutData(Dt_AllocateOrder allocateOrder)
        {
            if (Enum.TryParse<BusinessTypeEnum>(allocateOrder.BusinessType, out var businessType))
            {
                if (businessType == BusinessTypeEnum.外部仓库调智仓)
                {
                    allocateOrder.OrderType = InOrderTypeEnum.AllocatInbound.ObjToInt();
                    var inboundOrders = ConvertToInboundOrders(allocateOrder);
                    await _inboundService.InbounOrderService.ReceiveInboundOrder(inboundOrders, 1);
                }
                else if (businessType == BusinessTypeEnum.智仓调外部仓库 || businessType == BusinessTypeEnum.智仓调智仓)
                {
                    if (businessType == BusinessTypeEnum.智仓调外部仓库)
                    {
                        allocateOrder.OrderType = InOrderTypeEnum.AllocatOutbound.ObjToInt();
                    }
                    else if (businessType == BusinessTypeEnum.智仓调智仓)
                    {
                        allocateOrder.OrderType = InOrderTypeEnum.InternalAllocat.ObjToInt();
                    }
                    var outboundOrders = ConvertToOutboundOrders(allocateOrder);
                    await _outboundService.OutboundOrderService.ReceiveOutboundOrder(outboundOrders, 1);
                }
                else
                {
                    // å¤„理未定义的枚举值(如未来新增但未实现的类型)
                    throw new NotSupportedException($"不支持的业务类型枚举值: {businessType}");
                }
            }
        }
        public async Task<WebResponseContent> UpdateAllocateOrder(Dt_AllocateOrder model)
        {
            try
            {
@@ -170,13 +196,18 @@
                _unitOfWorkManage.BeginTran();
                foreach (var item in deletePurchaseOrderDetails)
                {
                    _allocateOrderDetailRepository.DeleteAndMoveIntoHty(item, OperateTypeEnum.自动删除);
                    // _allocateOrderDetailRepository.DeleteAndMoveIntoHty(item, OperateTypeEnum.自动删除);
                    _allocateOrderDetailRepository.DeleteData(item);
                }
                _allocateOrderDetailRepository.UpdateData(updateAllocateOrderDetails);
                _allocateOrderDetailRepository.AddData(allocateOrderDetails);
                BaseDal.UpdateData(allocateOrder);
                DeleteInOutData(model.UpperOrderNo, allocateOrder);
                await AddInOutData(allocateOrder);
                _unitOfWorkManage.CommitTran();
@@ -185,11 +216,12 @@
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                _logger.LogInformation("AllocateService UpdateAllocateOrder  err:  " + ex.Message);
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        public WebResponseContent DeleteInboundOrder(Dt_AllocateOrder model)
        public async Task<WebResponseContent> DeleteAllocateOrder(Dt_AllocateOrder model)
        {
            try
            {
@@ -202,7 +234,7 @@
                {
                    return WebResponseContent.Instance.Error($"未找到调拨单明细信息");
                }
                //Db.DeleteNav(inboundOrder).Include(x => x.Details).ExecuteCommand();
                //Db.DeleteNav(Allocate).Include(x => x.Details).ExecuteCommand();
                _unitOfWorkManage.BeginTran();
                // BaseDal.DeleteAndMoveIntoHty(allocateOrder, OperateTypeEnum.自动删除);
                foreach (var item in allocateOrder.Details)
@@ -211,18 +243,47 @@
                    _allocateOrderDetailRepository.DeleteData(item);
                }
                BaseDal.DeleteData(allocateOrder);
                DeleteInOutData(model.UpperOrderNo, allocateOrder);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                _logger.LogInformation("AllocateService DeleteAllocateOrder  err:  " + ex.Message);
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        private void DeleteInOutData(string upperOrderNo, Dt_AllocateOrder allocateOrder)
        {
            if (Enum.TryParse<BusinessTypeEnum>(allocateOrder.BusinessType, out var businessType))
            {
                if (businessType == BusinessTypeEnum.外部仓库调智仓)
                {
                    _inboundService.InbounOrderService.Db.Deleteable<Dt_InboundOrder>().Where(x => x.UpperOrderNo == upperOrderNo).ExecuteCommand();
                    _inboundService.InboundOrderDetailService.Db.Deleteable<Dt_InboundOrderDetail>()
                     .Where(p => SqlFunc.Subqueryable<Dt_InboundOrder>().Where(s => s.Id == p.OrderId && s.UpperOrderNo == upperOrderNo).Any()).ExecuteCommand();
                }
                else if (businessType == BusinessTypeEnum.智仓调外部仓库 || businessType == BusinessTypeEnum.智仓调智仓)
                {
                    _outboundService.OutboundOrderService.Db.Deleteable<Dt_OutboundOrder>().Where(x => x.UpperOrderNo == upperOrderNo).ExecuteCommand();
                    _outboundService.OutboundOrderDetailService.Db.Deleteable<Dt_OutboundOrderDetail>()
                     .Where(p => SqlFunc.Subqueryable<Dt_OutboundOrder>().Where(s => s.Id == p.OrderId && s.UpperOrderNo == upperOrderNo).Any()).ExecuteCommand();
                }
            }
        }
        public List<Dt_InboundOrder> ConvertToInboundOrders(Dt_AllocateOrder allocateOrder)
        {
            var distinctDetails = allocateOrder.Details
                .GroupBy(d => d.Barcode)
                .Select(g => g.First())
                .ToList();
            return new List<Dt_InboundOrder>()
            {
                new Dt_InboundOrder(){
@@ -230,16 +291,16 @@
                   InboundOrderNo=allocateOrder.OrderNo,
                   UpperOrderNo=allocateOrder.UpperOrderNo,
                   SupplierId=allocateOrder.SupplierId,
                   OrderType=allocateOrder.OrderType,
                   OrderType= allocateOrder.OrderType ,
                   OrderStatus=allocateOrder.OrderStatus,
                   CreateType=allocateOrder.CreateType,
                   BusinessType=allocateOrder.BusinessType,
                   IsBatch=allocateOrder.IsBatch,
                   FactoryArea=allocateOrder.FactoryArea,
                   Remark=allocateOrder.Remark,
                   Details=allocateOrder.Details.Select(detail=>new Dt_InboundOrderDetail
                   Details=distinctDetails.Select(detail=>new Dt_InboundOrderDetail
                   {
                       OrderId= detail.OrderId,
                       OrderId= 0,
                       MaterielCode=detail.MaterielCode,
                       MaterielName="",
                       BatchNo=detail.BatchNo,
@@ -249,7 +310,8 @@
                       OrderDetailStatus=detail.OrderDetailStatus,
                       Unit=detail.Unit,
                       RowNo=0,
                       SupplyCode="",
                       lineNo=detail.LineNo,
                       SupplyCode=detail.SupplyCode,
                       WarehouseCode=detail.WarehouseCode,
                       Barcode=detail.Barcode,
                       OutBoxbarcodes="",
@@ -262,6 +324,18 @@
        public Dt_OutboundOrder ConvertToOutboundOrders(Dt_AllocateOrder allocateOrder)
        {
            var distinctDetails = allocateOrder.Details
    .GroupBy(d => string.IsNullOrEmpty(d.Barcode)
        ? $"{d.MaterielCode}_{d.BatchNo}_{d.SupplyCode}_{d.WarehouseCode}"
        : d.Barcode)
    .Select(g => new
    {
        Detail = g.First(),
        //汇总分组内的数量
        Qty = g.Sum(x => x.BarcodeQty ?? x.OrderQuantity)
    }).ToList();
            return new Dt_OutboundOrder()
            {
                WarehouseId = allocateOrder.WarehouseId,
@@ -276,20 +350,23 @@
                Remark = allocateOrder.Remark,
                DepartmentCode = "",
                DepartmentName = "",
                Details = allocateOrder.Details.Select(detail => new Dt_OutboundOrderDetail
                Details = distinctDetails.Select(item => new Dt_OutboundOrderDetail
                {
                    OrderId = detail.OrderId,
                    MaterielCode = detail.MaterielCode,
                    OrderId = 0,
                    MaterielCode = item.Detail.MaterielCode,
                    MaterielName = "",
                    BatchNo = detail.BatchNo,
                    OrderQuantity = detail.OrderQuantity,
                    BatchNo = item.Detail.BatchNo,
                    OrderQuantity = item.Detail.OrderQuantity,
                    BarcodeQty = (decimal)item.Detail.BarcodeQty,
                    BarcodeUnit = item.Detail.BarcodeUnit,
                    LockQuantity = 0,
                    lineNo = item.Detail.LineNo,
                    OverOutQuantity = 0,
                    OrderDetailStatus = detail.OrderDetailStatus,
                    Unit = detail.Unit,
                    OrderDetailStatus = item.Detail.OrderDetailStatus,
                    Unit = item.Detail.Unit,
                    RowNo = 0,
                    SupplyCode = "",
                    WarehouseCode = detail.WarehouseCode,
                    SupplyCode = item.Detail.SupplyCode,
                    WarehouseCode = item.Detail.WarehouseCode,
                }).ToList()
            };