From 6db89a97e81e6c26bb2d2ccb3db79ed8858462a2 Mon Sep 17 00:00:00 2001
From: huangxiaoqiang <huangxiaoqiang@hnkhzn.com>
Date: 星期四, 17 七月 2025 20:38:16 +0800
Subject: [PATCH] 优化异常库位出库功能

---
 Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageBasicServices/Location/LocationInfoService.cs |  230 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 224 insertions(+), 6 deletions(-)

diff --git a/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageBasicServices/Location/LocationInfoService.cs b/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageBasicServices/Location/LocationInfoService.cs
index 5ccc088..d39fe99 100644
--- a/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageBasicServices/Location/LocationInfoService.cs
+++ b/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_StorageBasicServices/Location/LocationInfoService.cs
@@ -1,4 +1,13 @@
-锘縰sing WIDESEA_DTO;
+锘縰sing AngleSharp.Dom;
+using Mapster;
+using System.Threading.Tasks;
+using WIDESEA_Common;
+using WIDESEA_Core;
+using WIDESEA_Core.Const;
+using WIDESEA_DTO;
+using WIDESEA_DTO.WMS;
+using WIDESEA_IServices;
+using WIDESEA_Model.Models;
 
 namespace WIDESEA_StorageBasicService;
 
@@ -7,11 +16,16 @@
     private readonly LogFactory LogFactory = new LogFactory();
     private readonly IUnitOfWorkManage _unitOfWorkManage;
     private readonly IDt_TaskRepository _taskRepository;
+    private readonly IDt_TaskService _taskService;
     private readonly IStockInfoRepository _stockInfoRepository;
+    private readonly IStockInfoDetailRepository _stockInfoDetailRepository;
     private readonly IDt_WareAreaInfoRepository _wareAreaInfoRepository;
     private readonly IPointStackerRelationRepository _pointStackerRelationRepository;
     private readonly ITaskExecuteDetailRepository _taskExecuteDetailRepository;
+    private readonly IDt_AreaInfoRepository _AreaInfoRepository;
     private readonly ILocationStatusChangeRecordRepository _locationStatusChangeRecordRepository;
+    private readonly ISys_ConfigService _configService;
+    private readonly IMapper _mapper;
 
     public LocationInfoService(ILocationInfoRepository BaseDal,
                                     IUnitOfWorkManage unitOfWorkManage,
@@ -20,7 +34,12 @@
                                     IDt_WareAreaInfoRepository wareAreaInfoRepository,
                                     IPointStackerRelationRepository pointStackerRelationRepository,
                                     ITaskExecuteDetailRepository taskExecuteDetailRepository,
-                                    ILocationStatusChangeRecordRepository locationStatusChangeRecordRepository) : base(BaseDal)
+                                    ILocationStatusChangeRecordRepository locationStatusChangeRecordRepository,
+                                    IStockInfoDetailRepository stockInfoDetailRepository,
+                                    IMapper mapper,
+                                    IDt_TaskService taskService, 
+                                    IDt_AreaInfoRepository AreaInfoRepository,
+                                    ISys_ConfigService configService) : base(BaseDal)
     {
         _unitOfWorkManage = unitOfWorkManage;
         _taskRepository = taskRepository;
@@ -29,6 +48,11 @@
         _pointStackerRelationRepository = pointStackerRelationRepository;
         _taskExecuteDetailRepository = taskExecuteDetailRepository;
         _locationStatusChangeRecordRepository = locationStatusChangeRecordRepository;
+        _stockInfoDetailRepository = stockInfoDetailRepository;
+        _mapper = mapper;
+        _taskService = taskService;
+        _AreaInfoRepository = AreaInfoRepository;
+        _configService= configService;
     }
 
     /// <summary>
@@ -82,7 +106,163 @@
         }
     }
 
-    
+    public override WebResponseContent UpdateData(SaveModel saveModel)
+    {
+        int id = saveModel.MainData["id"].ObjToInt();
+        int status = saveModel.MainData["locationStatus"].ObjToInt();
+        var location = BaseDal.QueryFirst(x => x.Id == id);
+
+        LocationChangeRecordDto changeRecordDto = new LocationChangeRecordDto()
+        {
+            AfterStatus = status,
+            BeforeStatus = location.LocationStatus,
+            TaskNum = 0,
+            LocationId = id,
+            LocationCode = location.LocationCode,
+            ChangeType = (int)StatusChangeTypeEnum.ManualOperation
+        };
+
+        _locationStatusChangeRecordRepository.AddStatusChangeRecord(changeRecordDto);
+
+        return base.UpdateData(saveModel);
+    }
+
+    public WebResponseContent HandOutTask(int locationID)
+    {
+        LogFactory.GetLog("鎵嬪姩鍑哄簱浠诲姟").InfoFormat(true, "鎵嬪姩鍑哄簱浠诲姟", JsonConvert.SerializeObject(locationID), App.User.UserName);
+        WebResponseContent content=new WebResponseContent();
+        try
+        {
+            Dt_AreaInfo areaInfo = _AreaInfoRepository.QueryFirst(x => x.AreaCode == "CWSC1");
+            DtLocationInfo location = BaseDal.QueryFirst(x => x.Id == locationID);
+            DtStockInfo stock = _stockInfoRepository.QueryFirst(x => x.LocationId == location.Id && x.LocationCode == location.LocationCode);
+            if (location.AreaId == areaInfo.AreaID)
+            {
+                Dt_Task task = new Dt_Task
+                {
+                    Grade = 3,
+                    Roadway = location.RoadwayNo,
+                    TargetAddress = "001-035-001",
+                    Dispatchertime = DateTime.Now,
+                    MaterialNo = "",
+                    NextAddress = "001-035-001",
+                    OrderNo = null,
+                    PalletCode = stock == null ? "M" + DateTime.Now.ToString("MMddHHmmss") + "-" + new Random().Next(100, 1000) : stock.PalletCode,
+                    SourceAddress = location.LocationCode,
+                    CurrentAddress = location.LocationCode,
+                    TaskState = (int)TaskOutStatusEnum.OutNew,
+                    TaskType = (int)TaskOutboundTypeEnum.InToOut,
+                    TaskNum = _taskRepository.GetTaskNo().Result,
+                    Creater = "System", // 淇鎷煎啓閿欒
+                    CreateDate = DateTime.Now,
+                    TaskId = 0,
+                    ProductionLine = stock == null ? "" : stock.ProductionLine,
+                    ProcessCode = stock == null ? "" : stock.ProcessCode,
+                };
+
+                var taskDTO = new WMSTaskDTO
+                {
+                    TaskNum = task.TaskNum.Value,
+                    Grade = task.Grade.Value,
+                    PalletCode = task.PalletCode,
+                    RoadWay = task.Roadway,
+                    SourceAddress = task.SourceAddress,
+                    TargetAddress = task.TargetAddress,
+                    TaskState = task.TaskState.Value,
+                    Id = 0,
+                    TaskType = task.TaskType,
+                    ProductionLine = task.ProductionLine,
+                };
+                var configs = _configService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
+                var ipAddress = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.WCSIPAddress)?.ConfigValue;
+                var ReceiveByWMSTask = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.ReceiveByWMSTask)?.ConfigValue;
+                if (ReceiveByWMSTask == null || ipAddress == null)
+                {
+                    throw new Exception("WMS IP 鏈厤缃�");
+                }
+                var wmsIpAddrss = ipAddress + ReceiveByWMSTask;
+
+                var respon = HttpHelper.Post(wmsIpAddrss, JsonConvert.SerializeObject(taskDTO));   //http://localhost:9291/api/Task/ReceiveTask,
+                if (respon != null)
+                {
+                    WebResponseContent respone = JsonConvert.DeserializeObject<WebResponseContent>(respon.ToString());
+                    if (respone.Status)
+                    {
+                        var taskId = _taskRepository.AddData(task);
+                        location.LocationStatus = (int)LocationEnum.InStockDisable;
+                        BaseDal.UpdateData(location);
+                    }
+                    else
+                    {
+                        throw new Exception("WCS澶勭悊澶辫触:" + respone.Message);
+                    }
+                }
+                else
+                {
+                    throw new Exception("WCS澶勭悊澶辫触");
+                }
+                return content.OK("浠诲姟娣诲姞鎴愬姛锛�");
+            }
+            else
+            {
+                return content.Error("涓嶆槸甯告俯璐т綅锛屼笉鍏佽鍑哄簱锛侊紒");
+            }
+        }
+        catch (Exception ex)
+        {
+            return content.Error(ex.Message);
+        }
+    }
+
+    #region 鍒濆鍖栧簱浣�
+    public async Task<WebResponseContent> initializeLocation(int locationID)
+    {
+        WebResponseContent content = new WebResponseContent();
+        try
+        {
+            DtLocationInfo? location = BaseDal.QueryData(x => x.Id == locationID).FirstOrDefault();
+            int LastStatus = location.LocationStatus;
+            if (location == null)
+            {
+                return content.Error("鏈壘鍒拌揣浣嶄俊鎭�!");
+            }
+            DtStockInfo stock = _stockInfoRepository.QueryFirst(x => x.LocationId == location.Id);
+            if(stock == null)
+            {
+                location.LocationStatus= (int)LocationEnum.Free;
+                BaseDal.UpdateData(location);
+            }
+            else
+            {
+                _unitOfWorkManage.BeginTran();
+                DtStockInfo_Hty stockInfo_Hty = stock.Adapt<DtStockInfo_Hty>();
+                stockInfo_Hty.ModifyDate = DateTime.Now;
+                await DeleteStockInfoAsync(stock.Id);
+                List<DtStockInfoDetail> detail = _stockInfoDetailRepository.QueryData(x => x.StockId == stock.Id).ToList();
+                if (detail != null && detail.Count() > 0)
+                {
+                    List<DtStockInfoDetail_Hty> details = detail.Adapt<List<DtStockInfoDetail_Hty>>();
+                    await DeleteStockInfoDetailsAsync(detail);
+                    AddStockInfoDetailHty(details);
+                }
+                await AddStockInfoHtyAsync(stockInfo_Hty);
+                
+
+                location.LocationStatus = (int)LocationEnum.Free;
+                BaseDal.UpdateData(location);
+
+                _locationStatusChangeRecordRepository.AddLocationStatusChangeRecord(location, LastStatus, (int)StatusChangeTypeEnum.ManualOperation, 0);
+                _unitOfWorkManage.CommitTran();
+            }
+            return content.OK();
+        }
+        catch (Exception ex)
+        {
+            _unitOfWorkManage.RollbackTran();
+            return content.Error(ex.Message);
+        }
+    }
+    #endregion
 
     #region 鍒涘缓鍘熷璐т綅鏁版嵁
 
@@ -115,10 +295,10 @@
                         LocationType = locType,
                         Remark = "",
                         Depth = locType > 1 ? (((line - 1) % 4) + 1) == 2 || (((line - 1) % 4) + 1) == 3 ? 1 : 2 : 1,
-                        RoadwayNo = locType > 1 ? $"SC{((line - 1) / 4) + 1}" : $"SC{((line - 1) / 2) + 1}",
+                        RoadwayNo = locType > 1 ? $"CHSC{((line - 1) / 4) + 1}" : $"CHSC{((line - 1) / 2) + 1}",
                         LocationStatus = LocationEnum.Free.ObjToInt(),
                         AreaId = areaId,
-                        Creater = "systeam",
+                        Creater = "System",
                         EnalbeStatus = 2,
                     });
                 }
@@ -225,7 +405,7 @@
                 SourceAddress = originalLocationID,
                 TaskNum = taskNo,
                 TargetAddress = emptyLocation.LocationCode,
-                TaskState = InTaskStatusEnum.RelocationNew.ObjToInt(),
+                //TaskState = TaskInStatusEnum.RelocationNew.ObjToInt(),
                 TaskType = TaskTypeEnum.RelocationIn.ObjToInt(),
             };
 
@@ -305,7 +485,45 @@
 
     #endregion 鍒涘缓鍒濆璐т綅鏂规硶
 
+    #region 搴撳瓨绉诲叆鍘嗗彶
+    private async Task DeleteStockInfoAsync(int stockId)
+    {
+        var isStockUpdated = await _stockInfoRepository.DeleteDataByIdAsync(stockId);
+        if (!isStockUpdated)
+        {
+            throw new Exception("搴撳瓨淇℃伅鏇存柊澶辫触");
+        }
+    }
+
+    private async Task AddStockInfoHtyAsync(DtStockInfo_Hty dtStock)
+    {
+        var isStockAdd = await SqlSugarHelper.DbWMS.InsertNav(dtStock).IncludesAllFirstLayer().ExecuteCommandAsync();
+        if (!isStockAdd)
+        {
+            throw new Exception("搴撳瓨鍘嗗彶淇℃伅娣诲姞澶辫触");
+        }
+    }
+
+    private async Task DeleteStockInfoDetailsAsync(IEnumerable<DtStockInfoDetail> details)
+    {
+        var ids = details.Select(x => (object)x.Id).ToArray();
+        var isStockDetailUpdated = await _stockInfoDetailRepository.DeleteDataByIdsAsync(ids);
+        if (!isStockDetailUpdated)
+        {
+            throw new Exception("搴撳瓨璇︽儏淇℃伅鏇存柊澶辫触");
+        }
+    }
+    private void AddStockInfoDetailHty(List<DtStockInfoDetail_Hty> details)
+    {
+
+        var isStockAdd = SqlSugarHelper.DbWMS.Insertable(details).ExecuteCommand();
+        if (isStockAdd==0)
+        {
+            throw new Exception("搴撳瓨鏄庣粏鍘嗗彶淇℃伅娣诲姞澶辫触");
+        }
+    }
     
+    #endregion
 
     #endregion 鍐呴儴鏂规硶
 }
\ No newline at end of file

--
Gitblit v1.9.3