From ca3e4977395bc02c5d147dffdff7381333fdfbca Mon Sep 17 00:00:00 2001
From: heshaofeng <heshaofeng@hnkhzn.com>
Date: 星期四, 09 四月 2026 14:39:37 +0800
Subject: [PATCH] 空箱跨区域移库

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_InboundService/InboundService.cs |  563 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 548 insertions(+), 15 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_InboundService/InboundService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_InboundService/InboundService.cs"
index 051e334..e984a4e 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/WIDESEA_WMSServer/WIDESEA_InboundService/InboundService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS\346\227\240\344\273\223\345\202\250\347\211\210/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,11 +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
 {
@@ -48,7 +55,11 @@
         private readonly IRepository<Dt_AllocateMaterialInfo> _allocateMaterialInfo;
         private readonly HttpClientHelper _httpClientHelper;
         private readonly IRepository<Dt_MesReturnRecord> _mesReturnRecord;
-        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)
+        private readonly 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;
@@ -63,9 +74,14 @@
             _allocateMaterialInfo = allocateMaterialInfo;
             _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
@@ -80,11 +96,44 @@
                 }
 
                 Dt_InboundOrder inboundOrder = new Dt_InboundOrder();
+                
                 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();
 
@@ -92,6 +141,33 @@
                 {
                     return content.Error("鏈壘鍒拌鏉$爜涓诲崟淇℃伅");
                 }
+                if(inboundOrder.OrderStatus == (int)InOrderStatusEnum.鎵嬪姩鍏抽棴)
+                {
+                    return content.Error("璇ュ崟鎹殑鐘舵�佷笉鑳界粍鐩�");
+                }
+                var warehouse =_warehouseAreaRepository.QueryFirst(x => x.Code == palletDto.WarehouseType);
+                if(inboundOrder.BusinessType=="11" && inboundOrder.FactoryArea != warehouse.FactoryArea)
+                {
+                    return content.Error($"褰撳墠鍘傚尯涓嶄竴鑷�");
+                }
+
+                if(inboundOrder.BusinessType != "11")
+                {
+                    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();
 
                 List<string?> materielCodes = details.GroupBy(x => x.Barcode).Select(x => x.Key).ToList();
@@ -105,16 +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 (stockInfo != null && stockInfo.Details.Count>0 && stockInfo.Details.FirstOrDefault()?.WarehouseCode != palletDto.WarehouseType)
+                //    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}鏈壘鍒癕ES鎺ㄩ�佺殑鏈夋晥鏈熸暟鎹紝璇峰厛娣诲姞璇ョ墿鏂欑殑鏈夋晥鏈熷啀缁勭洏鍏ュ簱");
+                    }
                     stockInfo.Details.Add(new Dt_StockInfoDetail
                     {
                         StockId = stockInfo == null ? 0 : stockInfo.Id,
@@ -199,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} 鏉$爜鍦ㄥ簱瀛樹腑宸插瓨鍦�");
@@ -220,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,
@@ -342,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)
@@ -365,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 == 6)
-                                            .Where(it => SqlFunc.Subqueryable<Dt_StockInfoDetail>().Where(s => s.StockId == it.Id && s.OrderNo == inboundOrder.InboundOrderNo).Any())
+                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 && s.Status == StockStatusEmun.鍏ュ簱纭.ObjToInt()).Any())
                                             .ToList();
 
                 var stockIds = stocks.Select(s => s.Id).ToList();
@@ -392,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();
@@ -415,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);
@@ -424,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澶辫触");
                     }
                 }
@@ -447,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);
 
@@ -457,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澶辫触");
                     }
                 }
@@ -473,6 +765,7 @@
                 return content.Error(ex.Message);
             }
         }
+
 
         public List<AllocateDtoDetail> GetAllocateDtoDetails(List<Dt_StockInfoDetail> stockInfoDetails)
         {
@@ -568,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);
+            }
+        }
+
+       
+        
     }
 }

--
Gitblit v1.9.3