pan
2025-12-03 9b6ad3e202d43f47729fa8e91ca5ccc871c8c4b9
ÏîÄ¿´úÂë/WMSÎÞ²Ö´¢°æ/WIDESEA_WMSServer/WIDESEA_AllocateService/AllocateService.cs
@@ -1,10 +1,13 @@
using MailKit.Search;
using Autofac.Core;
using MailKit.Search;
using Microsoft.Extensions.Logging;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.AllocateEnum;
@@ -15,6 +18,7 @@
using WIDESEA_Core.CodeConfigEnum;
using WIDESEA_Core.DB;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Seed;
using WIDESEA_IAllocateService;
using WIDESEA_IInboundService;
@@ -33,6 +37,8 @@
        public readonly IRepository<Dt_AllocateOrder> _allocateOrderRepository;
        public readonly IRepository<Dt_AllocateOrderDetail> _allocateOrderDetailRepository;
        public readonly IRepository<Dt_InboundOrderDetail> _inboundOrderDetailRepository;
        private readonly ILogger<AllocateService> _logger;
        public AllocateService(IRepository<Dt_AllocateOrder> BaseDal,
            IUnitOfWorkManage unitOfWorkManage,
@@ -40,6 +46,7 @@
            IOutboundService outboundService,
            IRepository<Dt_AllocateOrder> allocateOrderRepository,
            IRepository<Dt_AllocateOrderDetail> allocateOrderDetailRepository,
            IRepository<Dt_InboundOrderDetail> inboundOrderDetailRepository,
            ILogger<AllocateService> logger) : base(BaseDal)
        {
            _unitOfWorkManage = unitOfWorkManage;
@@ -48,6 +55,7 @@
            _allocateOrderRepository = allocateOrderRepository;
            _allocateOrderDetailRepository = allocateOrderDetailRepository;
            _logger = logger;
            _inboundOrderDetailRepository = inboundOrderDetailRepository;
        }
        public IRepository<Dt_AllocateOrder> Repository => BaseDal;
@@ -61,7 +69,7 @@
                {
                    1 => await AddAllocateOrder(allocateOrder),
                    2 => await UpdateAllocateOrder(allocateOrder),
                    3 => DeleteAllocateOrder(allocateOrder),
                    3 => await DeleteAllocateOrder(allocateOrder),
                    _ => WebResponseContent.Instance.OK(),
                };
@@ -82,33 +90,7 @@
                }
                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);
                        await _inboundService.InbounOrderService.ReceiveInboundOrder(inboundOrders, 1);
                    }
                    else if (businessType == BusinessTypeEnum.外部仓库调智仓 || businessType == BusinessTypeEnum.智仓调智仓)
                    {
                        if (businessType == BusinessTypeEnum.外部仓库调智仓)
                        {
                            allocateOrder.OrderType = InOrderTypeEnum.Allocat.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}");
                    }
                }
                await AddInOutData(allocateOrder);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
@@ -117,6 +99,39 @@
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
        private async Task AddInOutData(Dt_AllocateOrder allocateOrder)
        {
            if (Enum.TryParse<BusinessTypeEnum>(allocateOrder.BusinessType, out var businessType))
            {
                if (businessType == BusinessTypeEnum.外部仓库调智仓)
                {
                    allocateOrder.OrderType = (int)InOrderTypeEnum.AllocatInbound;
                    var inboundOrders = ConvertToInboundOrders(allocateOrder);
                    await _inboundService.InbounOrderService.ReceiveInboundOrder(inboundOrders, 1);
                }
                else if (businessType == BusinessTypeEnum.智仓调外部仓库 || businessType == BusinessTypeEnum.智仓调智仓)
                {
                    if (businessType == BusinessTypeEnum.智仓调外部仓库)
                    {
                        allocateOrder.OrderType = (int)InOrderTypeEnum.AllocatOutbound;
                    }
                    else if (businessType == BusinessTypeEnum.智仓调智仓)
                    {
                        allocateOrder.OrderType = (int)InOrderTypeEnum.InternalAllocat;
                    }
                    var outboundOrders = ConvertToOutboundOrders(allocateOrder);
                    await _outboundService.OutboundOrderService.ReceiveOutboundOrder(outboundOrders, 1);
                }
                else
                {
                    // å¤„理未定义的枚举值(如未来新增但未实现的类型)
                    throw new NotSupportedException($"不支持的业务类型枚举值: {businessType}");
                }
            }
        }
        public async Task<WebResponseContent> UpdateAllocateOrder(Dt_AllocateOrder model)
        {
            try
@@ -196,6 +211,10 @@
                _allocateOrderDetailRepository.AddData(allocateOrderDetails);
                BaseDal.UpdateData(allocateOrder);
                DeleteInOutData(model.UpperOrderNo, allocateOrder);
                await AddInOutData(allocateOrder);
                _unitOfWorkManage.CommitTran();
@@ -209,7 +228,7 @@
            }
        }
        public WebResponseContent DeleteAllocateOrder(Dt_AllocateOrder model)
        public async Task<WebResponseContent> DeleteAllocateOrder(Dt_AllocateOrder model)
        {
            try
            {
@@ -231,6 +250,9 @@
                    _allocateOrderDetailRepository.DeleteData(item);
                }
                BaseDal.DeleteData(allocateOrder);
                DeleteInOutData(model.UpperOrderNo, allocateOrder);
                _unitOfWorkManage.CommitTran();
                return WebResponseContent.Instance.OK();
            }
@@ -242,8 +264,33 @@
            }
        }
        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(){
@@ -258,9 +305,9 @@
                   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,
@@ -270,6 +317,7 @@
                       OrderDetailStatus=detail.OrderDetailStatus,
                       Unit=detail.Unit,
                       RowNo=0,
                       lineNo=detail.LineNo,
                       SupplyCode=detail.SupplyCode,
                       WarehouseCode=detail.WarehouseCode,
                       Barcode=detail.Barcode,
@@ -283,12 +331,24 @@
        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,
                OrderNo = allocateOrder.OrderNo,
                UpperOrderNo = allocateOrder.UpperOrderNo,
                OrderType = OutOrderTypeEnum.Allocate.ObjToInt(),
                OrderType = allocateOrder.OrderType,
                OrderStatus = allocateOrder.OrderStatus,
                CreateType = allocateOrder.CreateType,
                BusinessType = allocateOrder.BusinessType,
@@ -297,20 +357,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 =  item.Detail.BarcodeQty??0,
                    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 = detail.SupplyCode,
                    WarehouseCode = detail.WarehouseCode,
                    SupplyCode = item.Detail.SupplyCode,
                    WarehouseCode = item.Detail.WarehouseCode,
                }).ToList()
            };
@@ -375,5 +438,111 @@
                return code;
            }
        }
        public override PageGridData<Dt_AllocateOrder> GetPageData(PageDataOptions options)
        {
            string wheres = ValidatePageOptions(options);
            //获取排序字段
            Dictionary<string, SqlSugar.OrderByType> orderbyDic = GetPageDataSort(options, TProperties);
            List<OrderByModel> orderByModels = new List<OrderByModel>();
            foreach (var item in orderbyDic)
            {
                OrderByModel orderByModel = new()
                {
                    FieldName = item.Key,
                    OrderByType = item.Value
                };
                orderByModels.Add(orderByModel);
            }
            int totalCount = 0;
            List<SearchParameters> searchParametersList = new List<SearchParameters>();
            if (!string.IsNullOrEmpty(options.Wheres))
            {
                try
                {
                    searchParametersList = options.Wheres.DeserializeObject<List<SearchParameters>>();
                    options.Filter = searchParametersList;
                }
                catch { }
            }
            var data = BaseDal.Db.Queryable<Dt_AllocateOrder>()
                .WhereIF(!wheres.IsNullOrEmpty(), wheres)
                .OrderBy(orderByModels)
                .ToPageList(options.Page, options.Rows, ref totalCount);
            foreach (var item in data)
            {
                Dt_InboundOrder _InboundOrder = SqlSugarHelper.DbWMS.Queryable<Dt_InboundOrder>().Where(x => x.UpperOrderNo == item.UpperOrderNo).First();
                if (_InboundOrder != null)
                {
                    item.OrderStatus = _InboundOrder.OrderStatus;
                    item.OrderType = _InboundOrder.OrderType;
                }
            }
            return new PageGridData<Dt_AllocateOrder>(totalCount, data);
        }
        public override PageGridData<Dt_InboundOrderDetail> GetDetailPage(PageDataOptions options)
        {
            string wheres = ValidatePageOptions(options);
            //获取排序字段
            Dictionary<string, SqlSugar.OrderByType> orderbyDic = GetPageDataSort(options, TProperties);
            List<OrderByModel> orderByModels = new List<OrderByModel>();
            foreach (var item in orderbyDic)
            {
                OrderByModel orderByModel = new()
                {
                    FieldName = item.Key,
                    OrderByType = item.Value
                };
                orderByModels.Add(orderByModel);
            }
            int totalCount = 0;
            List<SearchParameters> searchParametersList = new List<SearchParameters>();
            if (!string.IsNullOrEmpty(options.Wheres))
            {
                try
                {
                    searchParametersList = options.Wheres.DeserializeObject<List<SearchParameters>>();
                    options.Filter = searchParametersList;
                }
                catch { }
            }
            //var data = BaseDal.Db.Queryable<Dt_AllocateOrderDetail>()
            //    .WhereIF(!wheres.IsNullOrEmpty(), wheres)
            //    .OrderBy(orderByModels)
            //    .ToPageList(options.Page, options.Rows, ref totalCount);
            //Dt_AllocateOrder allocateOrder = _allocateOrderRepository.QueryFirst(x => x.Id == (int)options.Value);
            //Dt_InboundOrder _InboundOrder = SqlSugarHelper.DbWMS.Queryable<Dt_InboundOrder>().Where(x => x.UpperOrderNo == allocateOrder.UpperOrderNo).First();
            //var details = _inboundOrderDetailRepository.QueryData(x => x.OrderId == _InboundOrder.Id );
            //foreach (var item in data)
            //{
            //    var detail = details.Where(x => x.MaterielCode == item.MaterielCode).FirstOrDefault();
            //    if (detail != null)
            //    {
            //        item.OrderQuantity = detail.OrderQuantity;
            //        item.ReceiptQuantity = detail.ReceiptQuantity;
            //        item.OverInQuantity = detail.OverInQuantity;
            //        item.OrderDetailStatus = detail.OrderDetailStatus;
            //    }
            //}
            //return new PageGridData<Dt_AllocateOrderDetail>(totalCount, data);
            Dt_AllocateOrder allocateOrder = _allocateOrderRepository.QueryFirst(x => x.Id == (int)options.Value);
            Dt_InboundOrder _InboundOrder = SqlSugarHelper.DbWMS.Queryable<Dt_InboundOrder>().Where(x => x.UpperOrderNo == allocateOrder.UpperOrderNo).First();
            var data = BaseDal.Db.Queryable<Dt_InboundOrderDetail>()
                .WhereIF(!wheres.IsNullOrEmpty(), wheres)
                .WhereIF(!_InboundOrder.IsNullOrEmpty(), x => x.OrderId == _InboundOrder.Id)
                .OrderBy(orderByModels)
                .ToPageList(options.Page, options.Rows, ref totalCount);
            return new PageGridData<Dt_InboundOrderDetail>(totalCount, data);
        }
    }
}