heshaofeng
7 天以前 ca3e4977395bc02c5d147dffdff7381333fdfbca
ÏîÄ¿´úÂë/WMSÎÞ²Ö´¢°æ/WIDESEA_WMSServer/WIDESEA_InboundService/InboundService.cs
@@ -3,6 +3,7 @@
using MailKit.Search;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Bcpg;
using SixLabors.ImageSharp;
using SqlSugar;
using System;
@@ -10,12 +11,15 @@
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.AllocateEnum;
using WIDESEA_Common.CommonEnum;
using WIDESEA_Common.OrderEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.Helper;
@@ -23,12 +27,14 @@
using WIDESEA_Core.Utilities;
using WIDESEA_DTO.Allocate;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Mes;
using WIDESEA_DTO.ReturnMES;
using WIDESEA_IBasicService;
using WIDESEA_IInboundService;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.Basic;
using WIDESEA_Model.Models.Check;
namespace WIDESEA_InboundService
{
@@ -50,7 +56,10 @@
        private readonly HttpClientHelper _httpClientHelper;
        private readonly IRepository<Dt_MesReturnRecord> _mesReturnRecord;
        private readonly ILocationInfoService _locationInfoService;
        public InboundService(IUnitOfWorkManage unitOfWorkManage, IInboundOrderDetailService inboundOrderDetailService, IInboundOrderService inbounOrderService, IRepository<Dt_InboundOrder> inboundOrderRepository, IRepository<Dt_WarehouseArea> warehouseAreaRepository, IRepository<Dt_LocationType> locationTypeRepository, IRepository<Dt_StockInfo> stockInfoRepository, IRepository<Dt_InboundOrderDetail> inboundOrderDetailRepository, IStockService stockService, IRepository<Dt_Task> taskRepository,IRepository<Dt_AllocateMaterialInfo> allocateMaterialInfo, HttpClientHelper httpClientHelper, IRepository<Dt_MesReturnRecord> mesReturnRecord,ILocationInfoService locationInfoService)
        private readonly IRepository<Dt_TakeStockOrder> _takeStockOrder;
        private readonly IRepository<Dt_StockInfoDetail> _stockInfoDetailRepository;
        private readonly IRepository<Dt_AllocateOrder> _allocateOrderRepository;
        public InboundService(IUnitOfWorkManage unitOfWorkManage, IInboundOrderDetailService inboundOrderDetailService, IInboundOrderService inbounOrderService, IRepository<Dt_InboundOrder> inboundOrderRepository, IRepository<Dt_WarehouseArea> warehouseAreaRepository, IRepository<Dt_LocationType> locationTypeRepository, IRepository<Dt_StockInfo> stockInfoRepository, IRepository<Dt_InboundOrderDetail> inboundOrderDetailRepository, IStockService stockService, IRepository<Dt_Task> taskRepository,IRepository<Dt_AllocateMaterialInfo> allocateMaterialInfo, HttpClientHelper httpClientHelper, IRepository<Dt_MesReturnRecord> mesReturnRecord,ILocationInfoService locationInfoService,IRepository<Dt_TakeStockOrder> takeStockOrder,IRepository<Dt_StockInfoDetail> stockInfoDetailRepository, IRepository<Dt_AllocateOrder> allocateOrderRepository)
        {
            _unitOfWorkManage = unitOfWorkManage;
            InboundOrderDetailService = inboundOrderDetailService;
@@ -66,9 +75,13 @@
            _httpClientHelper = httpClientHelper;
            _mesReturnRecord = mesReturnRecord;
            _locationInfoService = locationInfoService;
            _takeStockOrder = takeStockOrder;
            _stockInfoDetailRepository = stockInfoDetailRepository;
            _allocateOrderRepository = allocateOrderRepository;
        }
        public async Task<WebResponseContent> GroupPallet(GroupPalletDto palletDto)
        public async Task<WebResponseContent>
            GroupPallet(GroupPalletDto palletDto)
        {
            WebResponseContent content = new WebResponseContent();
            try
@@ -86,9 +99,41 @@
                
                var details = _inboundOrderDetailRepository.QueryData(x => (x.OutBoxbarcodes == palletDto.Barcode|| x.Barcode == palletDto.Barcode) && x.OrderDetailStatus == (int)InOrderStatusEnum.未开始);
                if (details.Count() <= 0)
                {
                    return content.Error("请确认是否已经组盘");
                    var inbounddetail = _inboundOrderDetailRepository.QueryFirst(x => x.Barcode == palletDto.Barcode || x.OutBoxbarcodes == palletDto.Barcode);
                    if (inbounddetail == null)
                    {
                        return content.Error($"条码{palletDto.Barcode}不存在");
                    }
                    var inbound = _inboundOrderRepository.Db.Queryable<Dt_InboundOrder>().Where(x => x.Id == inbounddetail.OrderId).First();
                    if (inbound == null)
                    {
                        return content.Error("单据不能为空");
                    }
                    var detail = _inboundOrderDetailRepository.Db.Queryable<Dt_InboundOrderDetail>().LeftJoin<Dt_InboundOrder>((d, o) => d.OrderId == o.Id).Where((d, o) => d.OrderId == inbound.Id
                      && d.ReceiptQuantity != 0
                      && d.OverInQuantity == 0).Select((d, o) => new
                      {
                          orderNo = o.InboundOrderNo,
                          d.Barcode,
                          d.MaterielCode,
                          d.BatchNo,
                          d.ReceiptQuantity,
                          d.Unit,
                          d.SupplyCode,
                          d.WarehouseCode
                      }).ToList();
                    var palletId = _stockInfoDetailRepository.QueryFirst(x => x.Barcode == palletDto.Barcode);
                    if (palletId == null)
                    {
                        return content.Error($"条码{palletDto.Barcode}已组盘");
                    }
                    var pallet = _stockInfoRepository.QueryFirst(x => x.Id == palletId.StockId);
                    return content.Error($"条码{palletDto.Barcode}已经组到{pallet.PalletCode}", detail);
                }
                inboundOrder = _inboundOrderRepository.Db.Queryable<Dt_InboundOrder>().Includes(x=>x.Details).Where(x => x.Id == details.First().OrderId).First();
@@ -106,10 +151,22 @@
                    return content.Error($"当前厂区不一致");
                }
                if(inboundOrder.BusinessType != "11"&& inboundOrder.Details.FirstOrDefault().WarehouseCode != palletDto.WarehouseType)
                if(inboundOrder.BusinessType != "11")
                {
                    return content.Error($"该条码所属仓库为{inboundOrder.Details.FirstOrDefault().WarehouseCode}与当前仓库{palletDto.WarehouseType}不一致,不允许组盘");
                    var warehouseType = _inboundOrderDetailRepository.Db.Queryable<Dt_InboundOrderDetail>().Where(x => (x.Barcode == palletDto.Barcode || x.OutBoxbarcodes == palletDto.Barcode)&& x.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt()).Select(x => x.WarehouseCode).First();
                    if (string.IsNullOrEmpty(warehouseType))
                    {
                        return content.Error($"未查询到条码[{palletDto.Barcode}]对应的仓库信息,不允许组盘");
                    }
                    if (!warehouseType.Equals(palletDto.WarehouseType))
                    {
                        return content.Error($"该条码所属仓库为{warehouseType}与当前仓库{palletDto.WarehouseType}不一致,不允许组盘");
                    }
                }
                Dt_StockInfo? stockInfo = await _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.PalletCode == palletDto.PalletCode).FirstAsync();
@@ -124,21 +181,62 @@
                    stockInfo = new Dt_StockInfo() { PalletType = (int)PalletTypeEnum.None, LocationType = Convert.ToInt32(palletDto.locationType) };
                    stockInfo.Details = new List<Dt_StockInfoDetail>();
                }
                //else
                //{
                //      var allowStatus = new[]
                //  {
                //      (int)StockStatusEmun.组盘暂存,
                //      (int)StockStatusEmun.智仓入智仓组盘暂存,
                //      (int)StockStatusEmun.手动组盘暂存
                //  };
                //    if (!allowStatus.Contains(stockInfo.StockStatus))
                //    {
                //        return content.Error($"该托盘{stockInfo.PalletCode}状态不允许组盘");
                //    }
                //}
                if (inboundOrder.BusinessType != MESDocumentType.PurchaseInbound.ToString() && stockInfo != null && stockInfo.Details.Count > 0 && stockInfo.Details.FirstOrDefault()?.WarehouseCode != palletDto.WarehouseType)
                {
                    return content.Error($"该托盘组盘仓库为{stockInfo.Details.FirstOrDefault()?.WarehouseCode}与当前仓库{palletDto.WarehouseType}不一致,不允许组盘");
                }
                var isWarehouse = _inboundOrderDetailRepository.QueryFirst(x => x.OrderId == inboundOrder.Id && !string.IsNullOrEmpty(x.WarehouseCode));
                if (isWarehouse != null && inboundOrder.BusinessType == "11" && isWarehouse.WarehouseCode != palletDto.WarehouseType)
                {
                    return content.Error($"一个采购单据里面不允许组盘两个仓库,请重新选择仓库,上一个组盘仓库为{isWarehouse.WarehouseCode}");
                }
                if (inboundOrder.BusinessType == "11" && inboundOrder.IsTestMaterials == 1 && palletDto.WarehouseType != "1076" && palletDto.WarehouseType != "5057")
                {
                    return content.Error($"该单据为试产物料,需要入库到试产仓,请重新根据厂区选择试产仓库进行组盘入库");
                }
                if (inboundOrder.IsTestMaterials == 0 && (palletDto.WarehouseType == "1076" || palletDto.WarehouseType == "5057"))
                {
                    return content.Error($"该单据为非试产物料,不能入到试产仓,请重新根据仓库进行组盘入库");
                }
                if (!_locationInfoService.QueryLocationCount(Convert.ToInt32(palletDto.locationType)))
                {
                    return content.Error($"该库区{palletDto.locationType}不存在空闲库位");
                }
                var nullpallet =_stockInfoRepository.QueryFirst(x => x.PalletType == -1 && x.PalletCode == palletDto.PalletCode);
                if (nullpallet != null)
                {
                    return content.Error($"该托盘{palletDto.PalletCode}已经进行空托组盘,不能在进行组盘");
                }
               var outbundPallet = _stockInfoRepository.QueryFirst(x => x.StockStatus == (int)StockStatusEmun.出库完成 && x.PalletCode == palletDto.PalletCode);
                if (outbundPallet != null)
                {
                    return content.Error($"该托盘{palletDto.PalletCode}未进行取空箱,不能进行组盘");
                }
                foreach (var item in details)
                {
                    var datevaliDate = _inboundOrderRepository.Db.Queryable<Dt_MaterialExpirationDate>().Where(x=>x.MaterialCode.Contains(item.MaterielCode.Substring(0,6))).First();
                    var datevaliDate = _inboundOrderRepository.Db.Queryable<Dt_MaterialExpirationDate>().Where(x => x.MaterialCode.Contains(item.MaterielCode.Substring(0, 6))).First();
                    if (datevaliDate == null)
                    {
                        return content.Error($"该物料{item.MaterielCode}未找到MES推送的有效期数据,请先添加该物料的有效期再组盘入库");
                    }
                    stockInfo.Details.Add(new Dt_StockInfoDetail
                    {
                        StockId = stockInfo == null ? 0 : stockInfo.Id,
@@ -223,7 +321,16 @@
                {
                    return content.Error("已上架的托盘不能再次组盘");
                }
                var nullpallet = _stockInfoRepository.QueryFirst(x => x.PalletType == -1 && x.PalletCode == palletDto.PalletCode);
                if (nullpallet != null)
                {
                    return content.Error($"该托盘{palletDto.PalletCode}已经进行空托组盘,不能在进行组盘");
                }
                var outbundPallet = _stockInfoRepository.QueryFirst(x => x.StockStatus == (int)StockStatusEmun.出库完成 && x.PalletCode == palletDto.PalletCode);
                if (outbundPallet != null)
                {
                    return content.Error($"该托盘{palletDto.PalletCode}未进行取空箱,不能进行组盘");
                }
                if (_stockService.StockInfoDetailService.ExistBarcodes(palletDto.Barcode))
                {
                    return content.Error($"{palletDto.Barcode} æ¡ç åœ¨åº“存中已存在");
@@ -244,7 +351,10 @@
                {
                    return content.Error($"未找到该条码{palletDto.Barcode}需调入智仓的信息");
                }
                if(allocateMaterialInfo.WarehouseCode != palletDto.WarehouseType)
                {
                    return content.Error($"该条码调入仓库为{allocateMaterialInfo.WarehouseCode},与选择的{palletDto.WarehouseType}仓库不一致");
                }
                stockInfo.Details.Add(new Dt_StockInfoDetail
                {
                    StockId = stockInfo == null ? 0 : stockInfo.Id,
@@ -366,9 +476,27 @@
            WebResponseContent content = new WebResponseContent();
            try
            {
                var inboundOrder = _inboundOrderRepository.Db.Queryable<Dt_InboundOrder>()
                Dt_InboundOrder inboundOrder = null;
                var allocateOrder = _allocateOrderRepository.Db.Queryable<Dt_AllocateOrder>().Where(x => x.Id == id).First();
                if (allocateOrder != null)
                {
                    inboundOrder = _inboundOrderRepository.Db.Queryable<Dt_InboundOrder>().Where(x => x.InboundOrderNo == allocateOrder.OrderNo).First();
                    if (inboundOrder.IsBatch == 0 && inboundOrder.OrderStatus == InOrderStatusEnum.入库中.ObjToInt())
                    {
                        return content = WebResponseContent.Instance.OK($"该单据属于不分批自动回传,不可手动分批回传");
                    }
                }else
                {
                    inboundOrder = _inboundOrderRepository.Db.Queryable<Dt_InboundOrder>()
                                    .Where(x => x.Id == id)
                                    .First();
                    if (inboundOrder.IsBatch == 0 && inboundOrder.OrderStatus == InOrderStatusEnum.入库中.ObjToInt())
                    {
                        return content = WebResponseContent.Instance.OK($"该单据属于不分批自动回传,不可手动分批回传");
                    }
                }
                List<Dt_MesReturnRecord> returnRecords = _mesReturnRecord.QueryData(x => x.OrderNo == inboundOrder.InboundOrderNo && x.OrderId == inboundOrder.Id && x.ReturnStatus == 2);
                foreach (var item in returnRecords)
@@ -389,13 +517,24 @@
                    //}
                }
                _mesReturnRecord.UpdateData(returnRecords);
                //重新回调完成的单据,更新库存状态
                foreach (var returnRecord in returnRecords)
                {
                    if (returnRecord.ReturnStatus == 1)
                    {
                        _stockInfoRepository.Db.Updateable<Dt_StockInfoDetail>().SetColumns(it => new Dt_StockInfoDetail
                        {
                            Status = StockStatusEmun.入库完成.ObjToInt()
                        }).Where(it => it.OrderNo == returnRecord.OrderNo && returnRecord.RequestData.Contains(it.Barcode)).ExecuteCommand();
                    }
                }
                var inboundOrderDetail = _inboundOrderRepository.Db.Queryable<Dt_InboundOrderDetail>()
                                        .Where(x => x.OrderId == inboundOrder.Id && x.OrderDetailStatus==(int)OrderDetailStatusEnum.Over && x.ReturnToMESStatus == 0)
                                        .Where(x => x.OrderId == inboundOrder.Id && x.OrderDetailStatus == (int)OrderDetailStatusEnum.Over && x.ReturnToMESStatus == 0)
                                        .ToList();
                var stocks = _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.StockStatus == (int)StockStatusEmun.入库完成)
                                            .Where(it => SqlFunc.Subqueryable<Dt_StockInfoDetail>().Where(s => s.StockId == it.Id && s.OrderNo == inboundOrder.InboundOrderNo).Any())
                                            .Where(it => SqlFunc.Subqueryable<Dt_StockInfoDetail>().Where(s => s.StockId == it.Id && s.OrderNo == inboundOrder.InboundOrderNo && s.Status == StockStatusEmun.入库确认.ObjToInt()).Any())
                                            .ToList();
                var stockIds = stocks.Select(s => s.Id).ToList();
@@ -416,6 +555,13 @@
                                    inbound => inbound.Barcode,
                                    stockdetail => stockdetail.Barcode,
                                    (inbound, stockdetail) => inbound.Id)
                                .ToList();
                var inbounddetailBarcode = inboundOrderDetail
                                .Join(allDetailsData,
                                    inbound => inbound.Barcode,
                                    stockdetail => stockdetail.Barcode,
                                    (inbound, stockdetail) => stockdetail.Barcode)
                                .ToList();
                var detail = allDetailsData.Where(x => matchedData.Contains(x.Barcode)).ToList();
@@ -439,7 +585,31 @@
                    if (allocatefeedmodel.Details.Count <= 0)
                    {
                        throw new Exception("未找到需要回传的数据");
                        if (returnRecords.Count() == returnRecords.Count(x => x.ReturnStatus == 1))
                        {
                            if (inboundOrder.OrderStatus == InOrderStatusEnum.入库完成.ObjToInt())
                            {
                                inboundOrder.ReturnToMESStatus = 1;
                            }
                            else if (inboundOrder.OrderStatus == InOrderStatusEnum.入库中.ObjToInt())
                            {
                                inboundOrder.ReturnToMESStatus = 3;
                            }
                            else
                            {
                                inboundOrder.ReturnToMESStatus = 0;
                            }
                        }
                        else if (returnRecords.Count(x => x.ReturnStatus == 2) > 0)
                        {
                            inboundOrder.ReturnToMESStatus = 4;
                        }
                        else if (returnRecords.Count() == returnRecords.Count(x => x.ReturnStatus == 2))
                        {
                            inboundOrder.ReturnToMESStatus = 2;
                        }
                        _inboundOrderRepository.UpdateData(inboundOrder);
                        return WebResponseContent.Instance.OK($"该单据没有需要回传明细,回传{returnRecords.Count()}条,回传成功{returnRecords.Count(x => x.ReturnStatus == 1)}条,回传失败{returnRecords.Count(x => x.ReturnStatus == 2)}条");
                    }
                    var response = responseModel(inboundOrder, 3, null, allocatefeedmodel);
@@ -448,11 +618,48 @@
                    {
                        _inboundOrderRepository.Db.Updateable<Dt_InboundOrderDetail>().SetColumns(it => new Dt_InboundOrderDetail { ReturnToMESStatus = 1 })
                        .Where(it => it.OrderId == inboundOrder.Id && inbounddetailID.Contains(it.Id)).ExecuteCommand();
                        if (inboundOrder.OrderStatus == InOrderStatusEnum.入库完成.ObjToInt())
                        {
                            _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 1 })
                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
                        }
                        else if (inboundOrder.OrderStatus == InOrderStatusEnum.入库中.ObjToInt())
                        {
                            _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 3 })
                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
                        }
                        else
                        {
                            _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 0 })
                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
                        }
                        _stockInfoRepository.Db.Updateable<Dt_StockInfoDetail>().SetColumns(it => new Dt_StockInfoDetail
                        {
                            Status = StockStatusEmun.入库完成.ObjToInt()
                        }).Where(it => it.OrderNo == inboundOrder.InboundOrderNo && inbounddetailBarcode.Contains(it.Barcode)).ExecuteCommand();
                    }
                    else
                    {
                        _inboundOrderRepository.Db.Updateable<Dt_InboundOrderDetail>().SetColumns(it => new Dt_InboundOrderDetail { ReturnToMESStatus = 2 })
                         .Where(it => inbounddetailID.Contains(it.Id)).ExecuteCommand();
                        if (inboundOrder.OrderStatus == InOrderStatusEnum.入库完成.ObjToInt())
                        {
                            _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 2 })
                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
                        }
                        else if (inboundOrder.OrderStatus == InOrderStatusEnum.入库中.ObjToInt())
                        {
                            _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 4 })
                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
                        }
                        else
                        {
                            _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 0 })
                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
                        }
                        return content.Error("回传MES失败");
                    }
                }
@@ -471,9 +678,33 @@
                        details = FeedbackInboundDetailsModelDto(detail)
                    };
                    if (feedmodel.details.Count<=0)
                    if (feedmodel.details.Count <= 0)
                    {
                        throw new Exception("未找到需要回传的数据");
                        if (returnRecords.Count() == returnRecords.Count(x => x.ReturnStatus == 1))
                        {
                            if (inboundOrder.OrderStatus == InOrderStatusEnum.入库完成.ObjToInt())
                            {
                                inboundOrder.ReturnToMESStatus = 1;
                            }
                            else if(inboundOrder.OrderStatus == InOrderStatusEnum.入库中.ObjToInt())
                            {
                                inboundOrder.ReturnToMESStatus = 3;
                            }
                            else
                            {
                                inboundOrder.ReturnToMESStatus = 0;
                            }
                        }
                        else if (returnRecords.Count(x => x.ReturnStatus == 2) > 0)
                        {
                            inboundOrder.ReturnToMESStatus = 4;
                        }
                        else if (returnRecords.Count() == returnRecords.Count(x => x.ReturnStatus == 2))
                        {
                            inboundOrder.ReturnToMESStatus = 2;
                        }
                        _inboundOrderRepository.UpdateData(inboundOrder);
                        return WebResponseContent.Instance.OK($"该单据没有需要回传明细,回传{returnRecords.Count()}条,回传成功{returnRecords.Count(x => x.ReturnStatus == 1)}条,回传失败{returnRecords.Count(x => x.ReturnStatus == 2)}条");
                    }
                    var response = responseModel(inboundOrder, 3, feedmodel);
@@ -481,11 +712,48 @@
                    {
                        _inboundOrderRepository.Db.Updateable<Dt_InboundOrderDetail>().SetColumns(it => new Dt_InboundOrderDetail { ReturnToMESStatus = 1 })
                        .Where(it => inbounddetailID.Contains(it.Id)).ExecuteCommand();
                        if(inboundOrder.OrderStatus == InOrderStatusEnum.入库完成.ObjToInt())
                        {
                            _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 1 })
                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
                        }
                        else if(inboundOrder.OrderStatus == InOrderStatusEnum.入库中.ObjToInt())
                        {
                            _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 3})
                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
                        }
                        else
                        {
                            _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 0 })
                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
                        }
                        _stockInfoRepository.Db.Updateable<Dt_StockInfoDetail>().SetColumns(it => new Dt_StockInfoDetail
                        {
                            Status = StockStatusEmun.入库完成.ObjToInt()
                        }).Where(it => it.OrderNo == inboundOrder.InboundOrderNo && inbounddetailBarcode.Contains(it.Barcode)).ExecuteCommand();
                    }
                    else
                    {
                        _inboundOrderRepository.Db.Updateable<Dt_InboundOrderDetail>().SetColumns(it => new Dt_InboundOrderDetail { ReturnToMESStatus = 2 })
                         .Where(it => inbounddetailID.Contains(it.Id)).ExecuteCommand();
                        if (inboundOrder.OrderStatus == InOrderStatusEnum.入库完成.ObjToInt())
                        {
                            _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 2 })
                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
                        }
                        else if (inboundOrder.OrderStatus == InOrderStatusEnum.入库中.ObjToInt())
                        {
                            _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 4 })
                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
                        }
                        else
                        {
                            _inboundOrderRepository.Db.Updateable<Dt_InboundOrder>().SetColumns(it => new Dt_InboundOrder { ReturnToMESStatus = 0 })
                                .Where(it => it.Id == inboundOrder.Id).ExecuteCommand();
                        }
                        return content.Error("回传MES失败");
                    }
                }
@@ -497,6 +765,7 @@
                return content.Error(ex.Message);
            }
        }
        public List<AllocateDtoDetail> GetAllocateDtoDetails(List<Dt_StockInfoDetail> stockInfoDetails)
        {
@@ -592,5 +861,245 @@
            return httpResponseResult;
        }
        public async Task<WebResponseContent> StockTakeGroupPallet(GroupPalletDto palletDto)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                (bool, string, object?) result2 = ModelValidate.ValidateModelData(palletDto);
                if (!result2.Item1) return content.Error(result2.Item2);
                // éªŒè¯ä»“库编号是否存在
                var code = _warehouseAreaRepository.Db.Queryable<Dt_WarehouseArea>()
                    .Where(x => x.Code == palletDto.WarehouseType)
                    .Select(x => x.Code)
                    .First();
                if (string.IsNullOrEmpty(code))
                {
                    return content.Error($"仓库中没有该{palletDto.WarehouseType}编号。");
                }
                // æŸ¥è¯¢å½“前托盘的库存信息
                Dt_StockInfo? stockInfo = await _stockInfoRepository.Db.Queryable<Dt_StockInfo>()
                    .Includes(x => x.Details)
                    .Where(x => x.PalletCode == palletDto.PalletCode)
                    .FirstAsync();
                // éªŒè¯æ‰˜ç›˜æ˜¯å¦å·²ç”Ÿæˆä»»åŠ¡
                if (_taskRepository.QueryFirst(x => x.PalletCode == palletDto.PalletCode) != null)
                {
                    return content.Error($"该托盘已生成任务");
                }
                // éªŒè¯æ‰˜ç›˜æ˜¯å¦å·²ä¸Šæž¶ï¼ˆå·²ä¸Šæž¶ä¸èƒ½ç»„盘)
                if (stockInfo != null && !string.IsNullOrEmpty(stockInfo.LocationCode) && stockInfo.StockStatus != (int)StockStatusEmun.组盘暂存)
                {
                    return content.Error("已上架的托盘不能再次组盘");
                }
                var nullpallet = _stockInfoRepository.QueryFirst(x => x.PalletType == -1 && x.PalletCode == palletDto.PalletCode);
                if (nullpallet != null)
                {
                    return content.Error($"该托盘{palletDto.PalletCode}已经进行空托组盘,不能在进行组盘");
                }
                var outbundPallet = _stockInfoRepository.QueryFirst(x => x.StockStatus == (int)StockStatusEmun.出库完成 && x.PalletCode == palletDto.PalletCode);
                if (outbundPallet != null)
                {
                    return content.Error($"该托盘{palletDto.PalletCode}未进行取空箱,不能进行组盘");
                }
                Dt_StockInfoDetail stockInfoDetail = _stockService.StockInfoDetailService.Db.Queryable<Dt_StockInfoDetail>()
                    .Where(x => x.Barcode == palletDto.Barcode && x.StockId == 0)
                    .First();
                if (stockInfoDetail == null)
                {
                    return content.Error($"{palletDto.Barcode} æ¡ç å·²å…³è”其他托盘,无法组盘");
                }
                Dt_TakeStockOrder takeStockOrder = _takeStockOrder.Db.Queryable<Dt_TakeStockOrder>()
                    .Where(x => x.OrderNo == stockInfoDetail.OrderNo)
                    .First();
                if (takeStockOrder == null)
                {
                    return content.Error($"{palletDto.Barcode} ä¸å±žäºŽç›˜ç‚¹å•据中的条码,不允许盘亏组盘");
                }
                if (stockInfo == null)
                {
                    stockInfo = new Dt_StockInfo()
                    {
                        PalletType = (int)PalletTypeEnum.None,
                        LocationType = Convert.ToInt32(palletDto.locationType),
                        PalletCode = palletDto.PalletCode,
                        StockStatus = (int)StockStatusEmun.组盘暂存,
                        Details = new List<Dt_StockInfoDetail>()
                    };
                }
                if (stockInfo.Details.Count > 0 && stockInfo.Details.FirstOrDefault()?.WarehouseCode != palletDto.WarehouseType)
                {
                    return content.Error($"该托盘组盘仓库为{stockInfo.Details.FirstOrDefault()?.WarehouseCode}与当前仓库{palletDto.WarehouseType}不一致,不允许组盘");
                }
                _unitOfWorkManage.BeginTran();
                try
                {
                    if (stockInfo.Id == 0)
                    {
                        int newStockId = await _stockInfoRepository.Db.Insertable(stockInfo).ExecuteReturnIdentityAsync();
                        stockInfo.Id = newStockId;
                    }
                    stockInfoDetail.StockId = stockInfo.Id;
                    await _stockService.StockInfoDetailService.Db.Updateable(stockInfoDetail)
                        .Where(x => x.Id == stockInfoDetail.Id)
                        .ExecuteCommandAsync();
                    if (stockInfo.Id != 0 && stockInfo.Details != null && !stockInfo.Details.Contains(stockInfoDetail))
                    {
                        stockInfo.Details.Add(stockInfoDetail);
                        await _stockInfoRepository.Db.Updateable(stockInfo)
                            .IgnoreColumns(x => x.Details)
                            .ExecuteCommandAsync();
                    }
                    // æäº¤äº‹åŠ¡
                    _unitOfWorkManage.CommitTran();
                }
                catch (Exception)
                {
                    // äº‹åŠ¡å›žæ»š
                    _unitOfWorkManage.RollbackTran();
                    throw; // æŠ›ç»™å¤–层catch处理日志
                }
                // æŸ¥è¯¢æœ€æ–°çš„库存信息(包含关联的明细)
                Dt_StockInfo? NewstockInfo = await _stockInfoRepository.Db.Queryable<Dt_StockInfo>()
                    .Includes(x => x.Details)
                    .Where(x => x.PalletCode == palletDto.PalletCode)
                    .FirstAsync();
                return WebResponseContent.Instance.OK(data: NewstockInfo.Details.OrderByDescending(x => x.Id));
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                LogFactory.GetLog($"组盘信息").Info(true, $"【异常】:【{ex.Message}】{Environment.NewLine}【{ex.StackTrace}】{Environment.NewLine}{Environment.NewLine}");
                return content.Error(ex.Message);
            }
        }
        public async Task<WebResponseContent> ReCheckGroupPallet(GroupPalletDto palletDto)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                (bool, string, object?) result2 = ModelValidate.ValidateModelData(palletDto);
                if (!result2.Item1) return content.Error(result2.Item2);
                var code = _warehouseAreaRepository.Db.Queryable<Dt_WarehouseArea>().Where(x => x.Code == palletDto.WarehouseType).Select(x => x.Code).First();
                if (string.IsNullOrEmpty(code))
                {
                    return content.Error($"仓库中没有该{palletDto.WarehouseType}编号。");
                }
                Dt_StockInfo? stockInfo = await _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.PalletCode == palletDto.PalletCode).FirstAsync();
                if (_taskRepository.QueryFirst(x => x.PalletCode == palletDto.PalletCode) != null)
                {
                    return content.Error($"该托盘已生成任务");
                }
                if (stockInfo != null && !string.IsNullOrEmpty(stockInfo.LocationCode) && stockInfo.StockStatus != (int)StockStatusEmun.组盘暂存)
                {
                    return content.Error("已上架的托盘不能再次组盘");
                }
                if (stockInfo == null)
                {
                    stockInfo = new Dt_StockInfo() { PalletType = (int)PalletTypeEnum.None, LocationType = Convert.ToInt32(palletDto.locationType) };
                    stockInfo.Details = new List<Dt_StockInfoDetail>();
                }
                if (stockInfo != null && stockInfo.Details.Count > 0 && stockInfo.Details.FirstOrDefault()?.WarehouseCode != palletDto.WarehouseType)
                {
                    return content.Error($"该托盘组盘仓库为{stockInfo.Details.FirstOrDefault()?.WarehouseCode}与当前仓库{palletDto.WarehouseType}不一致,不允许组盘");
                }
                var nullpallet = _stockInfoRepository.QueryFirst(x => x.PalletType == -1 && x.PalletCode == palletDto.PalletCode);
                if (nullpallet != null)
                {
                    return content.Error($"该托盘{palletDto.PalletCode}已经进行空托组盘,不能在进行组盘");
                }
                var outbundPallet = _stockInfoRepository.QueryFirst(x => x.StockStatus == (int)StockStatusEmun.出库完成 && x.PalletCode == palletDto.PalletCode);
                if (outbundPallet != null)
                {
                    return content.Error($"该托盘{palletDto.PalletCode}未进行取空箱,不能进行组盘");
                }
                Dt_StockInfoDetail stockInfoDetail = _stockInfoRepository.Db.Queryable<Dt_StockInfoDetail>().Where(x => x.Barcode == palletDto.Barcode && x.Status == StockStatusEmun.重检中.ObjToInt()).First();
                if(stockInfoDetail == null)
                {
                    return content.Error("未找到库存中重检冻结的条码");
                }
                Dt_ReCheckOrder reCheckOrder = _stockInfoRepository.Db.Queryable<Dt_ReCheckOrder>().Where(x => x.MaterielCode == stockInfoDetail.MaterielCode && x.BatchNo == stockInfoDetail.BatchNo && x.OrderNo == stockInfoDetail.OrderNo && (x.Result == 1 || x.Result == 2 )).First();
                if(reCheckOrder == null)
                {
                    return content.Error($"该重检条码的批次在重检单中未拿到重检结果,请检查重检单中物料{stockInfoDetail.MaterielCode}和{stockInfoDetail.BatchNo}物料批次状态");
                }
                int newStatus = reCheckOrder.Result == 1 ? StockStatusEmun.入库完成.ObjToInt(): StockStatusEmun.手动冻结.ObjToInt();
                stockInfo.Details.Add(new Dt_StockInfoDetail
                {
                    StockId = stockInfo == null ? 0 : stockInfo.Id,
                    Barcode = stockInfoDetail.Barcode,
                    MaterielCode = stockInfoDetail.MaterielCode,
                    MaterielName = stockInfoDetail.MaterielName,
                    BatchNo = stockInfoDetail.BatchNo,
                    Unit = stockInfoDetail.Unit,
                    SupplyCode = stockInfoDetail.SupplyCode,
                    WarehouseCode = stockInfoDetail.WarehouseCode,
                    StockQuantity = stockInfoDetail.StockQuantity,
                    BarcodeQty = stockInfoDetail.BarcodeQty,
                    BarcodeUnit = stockInfoDetail.BarcodeUnit,
                    FactoryArea = stockInfoDetail.FactoryArea,
                    Status = newStatus,
                    OrderNo = stockInfoDetail.OrderNo,
                    BusinessType = "30",
                    ValidDate = stockInfoDetail.ValidDate
                });
                _stockInfoRepository.Db.Deleteable<Dt_StockInfoDetail>().Where(x => x.Barcode == stockInfoDetail.Barcode).ExecuteCommand();
                if (stockInfo.Id == 0)
                {
                    stockInfo.PalletCode = palletDto.PalletCode;
                    stockInfo.StockStatus = (int)StockStatusEmun.送检库存完成;
                }
                stockInfo.PalletType = (int)PalletTypeEnum.None;
                _unitOfWorkManage.BeginTran();
                _stockService.StockInfoService.AddMaterielGroup(stockInfo);
                _unitOfWorkManage.CommitTran();
                Dt_StockInfo oldStockInfo = _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.Id == stockInfoDetail.StockId).Includes(x=>x.Details).First();
                if (oldStockInfo.Details.Count <= 0)
                {
                    oldStockInfo.PalletType = (int)PalletTypeEnum.Empty;
                }
                _stockInfoRepository.UpdateData(oldStockInfo);
                Dt_StockInfo? NewstockInfo = await _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.PalletCode == palletDto.PalletCode).FirstAsync();
                return WebResponseContent.Instance.OK(data: NewstockInfo.Details.OrderByDescending(x => x.Id));
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                LogFactory.GetLog($"组盘信息").Info(true, $"【异常】:【{ex.Message}】{Environment.NewLine}【{ex.StackTrace}】{Environment.NewLine}{Environment.NewLine}");
                return content.Error(ex.Message);
            }
        }
    }
}