From 314076388f664f8ce1a1d19c4c9717fe54634e05 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期六, 18 四月 2026 15:51:51 +0800
Subject: [PATCH] Merge branch 'dev' of http://115.159.85.185:8098/r/SuZhouGuanHong/ShanMeiXinNengYuan into dev

---
 Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_AGV.cs |  149 ++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 127 insertions(+), 22 deletions(-)

diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_AGV.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_AGV.cs
index b054e73..74dfd61 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_AGV.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_TaskInfoService/TaskService_AGV.cs
@@ -1,5 +1,6 @@
 using Mapster;
 using System.Text.Json;
+using WIDESEA_Common.Constants;
 using WIDESEA_Common.LocationEnum;
 using WIDESEA_Common.StockEnum;
 using WIDESEA_Common.TaskEnum;
@@ -8,6 +9,7 @@
 using WIDESEA_Core.Enums;
 using WIDESEA_Core.Helper;
 using WIDESEA_DTO.Task;
+using WIDESEA_IStockService;
 using WIDESEA_Model.Models;
 
 namespace WIDESEA_TaskInfoService
@@ -168,7 +170,7 @@
                     return response.OK();
 
                 if (task.TaskStatus == (int)TaskInStatusEnum.InNew)
-                    return CancelAgvInboundTask(task);
+                    return await CancelAgvInboundTask(task);
 
                 if (task.TaskStatus == (int)TaskOutStatusEnum.OutNew)
                     return await CancelAgvOutboundTaskAsync(task);
@@ -300,35 +302,117 @@
                 return response.Error($"褰撳墠鎵樼洏{dto.TrayNumber}宸茬粡鍏ュ簱浜�");
 
             // 鍒涘缓搴撳瓨鏄庣粏
+            var details = new Dt_StockInfoDetail
+            {
+                MaterielCode = dto.ProductNo,
+                MaterielName = dto.ProductName,
+                StockQuantity = int.TryParse(dto.Quantity, out int quantity) ? quantity : 0,
+                Unit = dto.UomCode,
+                OrderNo = dto.ProductNo,
+                ProductionDate =dto.ProductionDate,
+                EffectiveDate = dto.LowerLimitTime,
+                SerialNumber = dto.TrayNumber,
+                Status = (int)StockStatusEmun.鍏ュ簱纭,
+                InboundOrderRowNo = 1,
+                Creater = StockConstants.AGV_USER,
+                CreateDate = DateTime.Now,
+                Remark = $"AGV鍏ュ簱浠诲姟鍒涘缓锛屼换鍔″彿锛歿dto.TaskId}"
+            };
+
+            // 鍒涘缓搴撳瓨涓昏褰�
+            var stock = new Dt_StockInfo
+            {
+                PalletCode = dto.TrayNumber,
+                PalletType = dto.Floor,
+                WarehouseId = dto.YinYang == 1 ? (int)WarehouseEnum.FJ1 : (int)WarehouseEnum.ZJ1,
+                StockStatus = (int)StockStatusEmun.鍏ュ簱纭,
+                Creater = StockConstants.AGV_USER,
+                CreateDate = DateTime.Now,
+                Remark = $"AGV鍏ュ簱浠诲姟鍒涘缓锛屼换鍔″彿锛歿dto.TaskId}",
+                Details = new List<Dt_StockInfoDetail> { details }
+            };
 
             task.TaskType = (int)TaskInboundTypeEnum.Inbound;
             task.TaskStatus = (int)TaskInStatusEnum.InNew;
             task.CurrentAddress = task.SourceAddress;
 
-            var addResult = await BaseDal.AddDataAsync(task) > 0;
-            return addResult ? null : response.Error("鍏ュ簱浠诲姟鍒涘缓澶辫触");
+            _unitOfWorkManage.BeginTran();
+            try
+            {
+                // 鍏堝垱寤轰换鍔�
+                var taskResult = await BaseDal.AddDataAsync(task) > 0;
+                if (!taskResult)
+                {
+                    _unitOfWorkManage.RollbackTran();
+                    return response.Error("鍏ュ簱浠诲姟鍒涘缓澶辫触");
+                }
+                var result = _stockInfoService.Repository.AddData(stock, x => x.Details);
+                if (result)
+                {
+                    _unitOfWorkManage.CommitTran();
+                    return null;
+                }
+                else
+                {
+                    _unitOfWorkManage.RollbackTran();
+                    return response.Error("搴撳瓨淇℃伅鍒涘缓澶辫触");
+                }
+                // 浣跨敤搴撳瓨鏈嶅姟娣诲姞搴撳瓨涓昏褰曞拰鏄庣粏
+                //var stockResult = await _stockInfoService.AddStockWithDetailsUsingTransactionAsync(stock);
+
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                return response.Error($"鍏ュ簱浠诲姟鍒涘缓寮傚父锛歿ex.Message}");
+            }
         }
 
         // 鍑哄簱鍒涘缓
         private async Task<AGVResponse?> CreateAgvOutboundTaskAsync(Dt_Task task, ApplyInOutDto dto)
         {
             AGVResponse response = new AGVResponse();
+            
+            // 妫�鏌ュ簱瀛樻槸鍚﹀瓨鍦�
             var stockInfo = await _stockInfoService.GetStockInfoAsync(dto.TrayNumber);
             if (stockInfo == null)
                 return response.Error($"鏈壘鍒版墭鐩榹dto.TrayNumber}鐨勫簱瀛樹俊鎭�");
 
-            //if (stockInfo.WarehouseId != dto.YinYang)
-            //    return response.Error($"鎵樼洏{dto.TrayNumber}涓嶅睘浜庡綋鍓峽(dto.YinYang == 1 ? "闃存瀬" : "闃虫瀬")}");
+            // 妫�鏌ュ簱瀛樻槸鍚︽湁鏄庣粏锛堝嵆鏄惁鐪熺殑鏈夊簱瀛橈級
+            if (stockInfo.Details == null || !stockInfo.Details.Any())
+                return response.Error($"鎵樼洏{dto.TrayNumber}娌℃湁搴撳瓨鏄庣粏锛屾棤娉曞嚭搴�");
 
+            // 妫�鏌ュ簱瀛樻�绘暟閲忔槸鍚﹀ぇ浜�0
+            var totalQuantity = stockInfo.Details.Sum(d => d.StockQuantity);
+            if (totalQuantity <= 0)
+                return response.Error($"鎵樼洏{dto.TrayNumber}搴撳瓨鏁伴噺涓嶈冻锛屾棤娉曞嚭搴�");
+
+            // 鏍规嵁dto鍙傛暟杩涗竴姝ラ獙璇佸簱瀛樹俊鎭�
+            if (!string.IsNullOrEmpty(dto.ProductNo))
+            {
+                // 妫�鏌ュ簱瀛樻槑缁嗕腑鏄惁鍖呭惈鎸囧畾鐨勭墿鏂欑紪鐮�
+                var hasMatchingMaterial = stockInfo.Details.Any(d => d.MaterielCode == dto.ProductNo);
+                if (!hasMatchingMaterial)
+                    return response.Error($"鎵樼洏{dto.TrayNumber}涓病鏈夌墿鏂欑紪鐮佷负{dto.ProductNo}鐨勫簱瀛橈紝鏃犳硶鍑哄簱");
+            }
+
+            // 妫�鏌ュ簱瀛樼姸鎬佹槸鍚﹀厑璁稿嚭搴�
             if (stockInfo.StockStatus != (int)StockStatusEmun.鍏ュ簱瀹屾垚)
                 return response.Error($"鎵樼洏{dto.TrayNumber}姝e湪绉诲姩涓紝璇风◢鍚庯紒");
 
+            // 妫�鏌ヨ揣浣嶄俊鎭�
             var locationInfo = await _locationInfoService.GetLocationInfo(stockInfo.LocationCode);
             if (locationInfo == null)
                 return response.Error($"鏈壘鍒版墭鐩榹stockInfo.LocationCode}鐨勮揣浣嶄俊鎭�");
 
+            // 妫�鏌ヨ揣浣嶇姸鎬佹槸鍚﹀厑璁稿嚭搴�
             if (locationInfo.LocationStatus != (int)LocationStatusEnum.InStock)
                 return response.Error($"褰撳墠璐т綅{locationInfo.LocationStatus}鐘舵�佷俊鎭敊璇�");
+
+            // 楠岃瘉浠撳簱ID鏄惁鍖归厤锛堟牴鎹甦to鐨勯槾闃虫瀬鍙傛暟锛�
+            var expectedWarehouseId = dto.YinYang == 1 ? (int)WarehouseEnum.FJ1 : (int)WarehouseEnum.ZJ1;
+            if (stockInfo.WarehouseId != expectedWarehouseId)
+                return response.Error($"鎵樼洏{dto.TrayNumber}涓嶅湪棰勬湡鐨勪粨搴撲腑锛屾棤娉曞嚭搴�");
 
             task.TaskType = (int)TaskOutboundTypeEnum.Outbound;
             task.TaskStatus = (int)TaskOutStatusEnum.OutNew;
@@ -454,7 +538,8 @@
             task.TaskStatus = (int)TaskOutStatusEnum.OutFinish;
 
             _unitOfWorkManage.BeginTran();
-            var deleteStockResult = _stockInfoService.DeleteData(stockInfo);
+            //var deleteStockResult = _stockInfoService.DeleteData(stockInfo)
+            var deleteStockResult = await _stockInfoService.DeleteStockWithDetailsAsync(stockInfo.Id);
             var updateLocationResult = _locationInfoService.UpdateData(locationInfo);
             BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateTypeEnum.鑷姩瀹屾垚 : OperateTypeEnum.浜哄伐瀹屾垚);
             if (!deleteStockResult.Status || !updateLocationResult.Status)
@@ -499,23 +584,23 @@
             if (existingStock != null)
                 return response.Error($"鎵樼洏{task.PalletCode}鐨勫簱瀛樹俊鎭凡瀛樺湪锛岃鍕块噸澶嶅叆搴�");
 
-            Dt_StockInfo stockInfo = new Dt_StockInfo
-            {
-                PalletCode = task.PalletCode,
-                StockStatus = (int)StockStatusEmun.鍏ュ簱纭,
-                LocationCode = locationInfo.LocationCode,
-                WarehouseId = task.WarehouseId,
-                Creater = "AGV",
-                CreateDate = DateTime.Now
-            };
+            //Dt_StockInfo stockInfo = new Dt_StockInfo
+            //{
+            //    PalletCode = task.PalletCode,
+            //    StockStatus = (int)StockStatusEmun.鍏ュ簱纭,
+            //    LocationCode = locationInfo.LocationCode,
+            //    WarehouseId = task.WarehouseId,
+            //    Creater = "AGV",
+            //    CreateDate = DateTime.Now
+            //};
 
             locationInfo.LocationStatus = (int)LocationStatusEnum.FreeLock;
 
             _unitOfWorkManage.BeginTran();
             var updateTaskResult = BaseDal.UpdateData(task);
             var updateLocationResult = _locationInfoService.UpdateData(locationInfo);
-            var addStockResult = _stockInfoService.AddData(stockInfo);
-            if (!updateTaskResult || !updateLocationResult.Status || !addStockResult.Status)
+            //var addStockResult = _stockInfoService.AddData(stockInfo);
+            if (!updateTaskResult || !updateLocationResult.Status /*|| !addStockResult.Status*/)
             {
                 _unitOfWorkManage.RollbackTran();
                 return response.Error("鍏ュ簱瀹屾垚鍚庯紝鏈湴浠诲姟銆佸簱瀛樻垨璐т綅鏇存柊澶辫触");
@@ -526,15 +611,35 @@
         }
 
         // AGV鍏ュ簱鍙栨秷
-        private AGVResponse CancelAgvInboundTask(Dt_Task task)
+        private async Task<AGVResponse> CancelAgvInboundTask(Dt_Task task)
         {
             AGVResponse response = new AGVResponse();
             task.TaskStatus = (int)TaskInStatusEnum.InCancel;
-
+            
             _unitOfWorkManage.BeginTran();
-            BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateTypeEnum.鑷姩瀹屾垚 : OperateTypeEnum.浜哄伐瀹屾垚);
-            _unitOfWorkManage.CommitTran();
-            return response.OK();
+            try
+            {
+                var stockInfo = await _stockInfoService.GetStockInfoAsync(task.PalletCode);
+                if (stockInfo != null)
+                {
+                    var deleteResult = await _stockInfoService.DeleteStockWithDetailsAsync(stockInfo.Id);
+                    if (!deleteResult.Status)
+                    {
+                        _unitOfWorkManage.RollbackTran();
+                        return response.Error($"鍒犻櫎搴撳瓨澶辫触: {deleteResult.Message}");
+                    }
+                }
+
+                BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId == 0 ? OperateTypeEnum.鑷姩瀹屾垚 : OperateTypeEnum.浜哄伐瀹屾垚);
+
+                _unitOfWorkManage.CommitTran();
+                return response.OK();
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                return response.Error($"鍙栨秷鍏ュ簱浠诲姟鏃跺彂鐢熷紓甯�: {ex.Message}");
+            }
         }
 
 

--
Gitblit v1.9.3