From 300ca9810420efbf8468c9d6f47bd364c9c72d5f Mon Sep 17 00:00:00 2001
From: 647556386 <647556386@qq.com>
Date: 星期一, 12 一月 2026 11:09:20 +0800
Subject: [PATCH] 盘点更改

---
 项目代码/WMS无仓储版/WIDESEA_WMSServer/WIDESEA_InboundService/InboundService.cs |  162 +++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 155 insertions(+), 7 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 9721aff..4a3c19c 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"
@@ -10,6 +10,7 @@
 using System.Collections.Generic;
 using System.Data;
 using System.Linq;
+using System.Net;
 using System.Text;
 using System.Threading.Tasks;
 using WIDESEA_Common.AllocateEnum;
@@ -23,7 +24,9 @@
 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;
@@ -48,7 +51,9 @@
         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;
+        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)
         {
             _unitOfWorkManage = unitOfWorkManage;
             InboundOrderDetailService = inboundOrderDetailService;
@@ -63,6 +68,8 @@
             _allocateMaterialInfo = allocateMaterialInfo;
             _httpClientHelper = httpClientHelper;
             _mesReturnRecord = mesReturnRecord;
+            _locationInfoService = locationInfoService;
+            _takeStockOrder = takeStockOrder;
         }
 
         public async Task<WebResponseContent> GroupPallet(GroupPalletDto palletDto)
@@ -80,11 +87,12 @@
                 }
 
                 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("鏈壘鍒拌鏉$爜鍗曟嵁淇℃伅璇风‘璁ゆ槸鍚﹀凡缁忕粍鐩樺畬鎴�");
+                    return content.Error("璇风‘璁ゆ槸鍚﹀凡缁忕粍鐩�");
                 }
                 inboundOrder = _inboundOrderRepository.Db.Queryable<Dt_InboundOrder>().Includes(x=>x.Details).Where(x => x.Id == details.First().OrderId).First();
 
@@ -92,6 +100,21 @@
                 {
                     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"&& inboundOrder.Details.FirstOrDefault().WarehouseCode != palletDto.WarehouseType)
+                {
+                    return content.Error($"璇ユ潯鐮佹墍灞炰粨搴撲负{inboundOrder.Details.FirstOrDefault().WarehouseCode}涓庡綋鍓嶄粨搴搟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();
@@ -106,9 +129,14 @@
                     stockInfo.Details = new List<Dt_StockInfoDetail>();
                 }
 
-                if (stockInfo != null && stockInfo.Details.Count>0 && stockInfo.Details.FirstOrDefault()?.WarehouseCode != palletDto.WarehouseType)
+                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}涓嶄竴鑷达紝涓嶅厑璁哥粍鐩�");
+                }
+
+                if (!_locationInfoService.QueryLocationCount(Convert.ToInt32(palletDto.locationType)))
+                {
+                    return content.Error($"璇ュ簱鍖簕palletDto.locationType}涓嶅瓨鍦ㄧ┖闂插簱浣�");
                 }
 
                 foreach (var item in details)
@@ -120,6 +148,7 @@
                         StockId = stockInfo == null ? 0 : stockInfo.Id,
                         Barcode = item.Barcode,
                         MaterielCode = item.MaterielCode,
+                        MaterielName = item.MaterielName,
                         BatchNo = item.BatchNo,
                         Unit = item.Unit,
                         InboundOrderRowNo = item.lineNo,
@@ -133,7 +162,6 @@
                         OrderNo = inboundOrder.InboundOrderNo,
                         BusinessType = inboundOrder.BusinessType,
                         ValidDate = inboundOrder.BusinessType == BusinessTypeEnum.澶栭儴浠撳簱璋冩櫤浠�.ToString() ? item.ValidDate : datevaliDate == null ? null : Convert.ToDateTime(DateTime.Now).AddDays(Convert.ToDouble(datevaliDate.ValidityDays)),
-                        //ValidDate = datevaliDate == null ? null : DateTime.Now.AddDays(datevaliDate.ValidityDays),
                     });
 
                     item.ReceiptQuantity = item.BarcodeQty;
@@ -370,7 +398,7 @@
                                         .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)
+                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())
                                             .ToList();
 
@@ -415,7 +443,7 @@
 
                     if (allocatefeedmodel.Details.Count <= 0)
                     {
-                        throw new Exception("鏈壘鍒伴渶瑕佸洖浼犵殑鏁版嵁");
+                        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);
@@ -449,7 +477,7 @@
 
                     if (feedmodel.details.Count<=0)
                     {
-                        throw new Exception("鏈壘鍒伴渶瑕佸洖浼犵殑鏁版嵁");
+                        return WebResponseContent.Instance.OK($"璇ュ崟鎹病鏈夐渶瑕佸洖浼犳槑缁嗭紝澶辫触鏁版嵁鍥炰紶{returnRecords.Count()}鏉★紝鍥炰紶鎴愬姛{returnRecords.Count(x => x.ReturnStatus == 1)}鏉★紝鍥炰紶澶辫触{returnRecords.Count(x => x.ReturnStatus == 2)}鏉�");
                     }
                     var response = responseModel(inboundOrder, 3, feedmodel);
 
@@ -568,5 +596,125 @@
 
             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("宸蹭笂鏋剁殑鎵樼洏涓嶈兘鍐嶆缁勭洏");
+                }
+
+                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);
+            }
+        }
     }
 }

--
Gitblit v1.9.3