From 18d1f45193e34e00fc1b6f65b8596ddb29c5267d Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期二, 24 十二月 2024 15:32:43 +0800
Subject: [PATCH] Merge branch 'master' of http://115.159.85.185:8098/r/MeiRuiAn/HuaiAn

---
 代码管理/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderService.cs |  161 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 156 insertions(+), 5 deletions(-)

diff --git "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderService.cs" "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderService.cs"
index fa29aa5..c15e44e 100644
--- "a/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderService.cs"
+++ "b/\344\273\243\347\240\201\347\256\241\347\220\206/WMS/WIDESEA_WMSServer/WIDESEA_OutboundService/OutboundOrderService.cs"
@@ -1,6 +1,7 @@
 锘縰sing AutoMapper;
 using Newtonsoft.Json;
 using Newtonsoft.Json.Serialization;
+using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
 using System;
 using System.Collections.Generic;
 using System.Linq;
@@ -8,7 +9,10 @@
 using System.Text;
 using System.Threading.Tasks;
 using WIDESEA_Common.OrderEnum;
+using WIDESEA_Common.StockEnum;
+using WIDESEA_Common.WareHouseEnum;
 using WIDESEA_Core;
+using WIDESEA_Core.BaseRepository;
 using WIDESEA_Core.BaseServices;
 using WIDESEA_Core.Enums;
 using WIDESEA_Core.Helper;
@@ -16,9 +20,12 @@
 using WIDESEA_DTO.ERP;
 using WIDESEA_DTO.Inbound;
 using WIDESEA_DTO.Outbound;
+using WIDESEA_External.ERPService;
+using WIDESEA_External.Model;
 using WIDESEA_IBasicService;
 using WIDESEA_IOutboundRepository;
 using WIDESEA_IOutboundService;
+using WIDESEA_IStockRepository;
 using WIDESEA_IStockService;
 using WIDESEA_Model.Models;
 
@@ -27,16 +34,26 @@
     public class OutboundOrderService : ServiceBase<Dt_OutboundOrder, IOutboundOrderRepository>, IOutboundOrderService
     {
         private readonly IMapper _mapper;
+        private readonly IUnitOfWorkManage _unitOfWorkManage;
         private readonly IBasicService _basicService;
         private readonly IOutboundOrderDetailRepository _outboundOrderDetailRepository;
-
+        private readonly IStockInfoService _stockInfoService;
+        private readonly IStockInfoRepository _stockInfoRepository;
         public IOutboundOrderRepository Repository => BaseDal;
+        private readonly IInvokeERPService _invokeERPService;
+        private readonly IOutStockLockInfoService _outStockLockInfoService;
 
-        public OutboundOrderService(IOutboundOrderRepository BaseDal, IMapper mapper, IBasicService basicService, IOutboundOrderDetailRepository outboundOrderDetailRepository) : base(BaseDal)
+        public OutboundOrderService(IOutboundOrderRepository BaseDal, IMapper mapper,IUnitOfWorkManage unitOfWorkManage, IBasicService basicService, IOutboundOrderDetailRepository outboundOrderDetailRepository, IStockInfoService stockInfoService,
+          IStockInfoRepository stockInfoRepository, IInvokeERPService invokeERPService, IOutStockLockInfoService outStockLockInfoService) : base(BaseDal)
         {
             _mapper = mapper;
+            _unitOfWorkManage = unitOfWorkManage;
             _basicService = basicService;
             _outboundOrderDetailRepository = outboundOrderDetailRepository;
+            _stockInfoService = stockInfoService;
+            _stockInfoRepository = stockInfoRepository;
+            _invokeERPService = invokeERPService;
+            _outStockLockInfoService = outStockLockInfoService;
         }
 
         public WebResponseContent ReceiveOutOrder(ErpOutOrderDTO model)
@@ -81,8 +98,30 @@
                                 OrderQuantity = model.Qty,
                                 OrderId = oldOutboundOrder.Id
                             };
-
+                            List<Dt_StockInfo>? stockInfos = new List<Dt_StockInfo>();
+                            //ERP涓婁紶娴嬭瘯浠撻鏂欏崟鏇存柊瀵瑰簲搴撳瓨鐘舵��
+                            if (warehouse.WarehouseCode == WarehouseEnum.HA64.ToString())
+                            {
+                                WebResponseContent responseContent = TestOutStocksUpdate(warehouse, model);
+                                if (!responseContent.Status)
+                                {
+                                    return responseContent;
+                                }
+                                stockInfos = responseContent.Data as List<Dt_StockInfo>;
+                            }
+                            _unitOfWorkManage.BeginTran();
+                            if (stockInfos.Count>0)
+                            {
+                                _stockInfoRepository.UpdateData(stockInfos);
+                                //娴嬭瘯鏋跺簱缁橢RP涓婃姤鍑哄簱瀹屾垚
+                                if (!TestOutBack(stockInfos, warehouse, model).Status)
+                                {
+                                    _unitOfWorkManage.RollbackTran();
+                                    return WebResponseContent.Instance.Error($"娴嬭瘯鏋跺嚭搴撲笂鎶ュけ璐�");
+                                }
+                            }
                             _outboundOrderDetailRepository.AddData(outboundOrderDetail);
+                            _unitOfWorkManage.CommitTran();
                         }
                     }
                     else
@@ -106,8 +145,31 @@
                             WarehouseId = warehouse.WarehouseId,
                             Details = new List<Dt_OutboundOrderDetail> { outboundOrderDetail }
                         };
-
+                        List<Dt_StockInfo>? stockInfos = new List<Dt_StockInfo>();
+                        //ERP涓婁紶娴嬭瘯浠撻鏂欏崟鏇存柊瀵瑰簲搴撳瓨鐘舵��
+                        if (warehouse.WarehouseCode == WarehouseEnum.HA64.ToString())
+                        {
+                            WebResponseContent responseContent = TestOutStocksUpdate(warehouse, model);
+                            if (!responseContent.Status)
+                            {
+                                return responseContent;
+                            }
+                            stockInfos = responseContent.Data as List<Dt_StockInfo>;
+                        }
+                        _unitOfWorkManage.BeginTran();
+                        if (stockInfos.Count > 0)
+                        {
+                            _stockInfoRepository.UpdateData(stockInfos);
+                            //娴嬭瘯鏋跺簱缁橢RP涓婃姤鍑哄簱瀹屾垚
+                            if (!TestOutBack(stockInfos, warehouse, model).Status)
+                            {
+                                _unitOfWorkManage.RollbackTran();
+                                return WebResponseContent.Instance.Error($"娴嬭瘯鏋跺嚭搴撲笂鎶ュけ璐�");
+                            } 
+                        }
                         Db.InsertNav(outboundOrder).Include(x => x.Details).ExecuteCommand();
+                        _unitOfWorkManage.CommitTran();
+                        
                     }
                 }
                 else if (model.Way == 2)
@@ -121,7 +183,7 @@
                     {
                         return WebResponseContent.Instance.Error($"鏈壘鍒拌鏄庣粏琛屽彿淇℃伅");
                     }
-                    if(outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt())
+                    if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt())
                     {
                         return WebResponseContent.Instance.Error($"璇ユ槑缁嗕笉鍙慨鏀�");
                     }
@@ -137,13 +199,102 @@
 
                     _outboundOrderDetailRepository.UpdateData(outboundOrderDetail);
                 }
+                else if (model.Way == 3)
+                {
+                    if (oldOutboundOrder == null)
+                    {
+                        return WebResponseContent.Instance.Error($"鏈壘鍒拌鍑哄簱鍗�");
+                    }
+                }
 
                 return WebResponseContent.Instance.OK();
+            }
+            catch (Exception ex)
+            {
+                _unitOfWorkManage.RollbackTran();
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
+        /// <summary>
+        /// 鏇存敼鍑哄簱鍗曞簱瀛樼姸鎬�
+        /// </summary>
+        /// <param name="warehouse"></param>
+        /// <param name="model"></param>
+        /// <returns></returns>
+        public WebResponseContent TestOutStocksUpdate(Dt_Warehouse warehouse, ErpOutOrderDTO model)
+        {
+            try
+            {
+                //鑾峰彇鍑哄簱鍗曞簱瀛�
+                List<Dt_StockInfo> stockInfos = _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.WarehouseId == warehouse.WarehouseId).OrderBy(x => x.CreateDate).Includes(x => x.Details).Where(x => x.Details.Any(x => x.MaterielCode == model.MCode)).Take((int)(model.Qty)).ToList();
+                if (model.Qty > stockInfos.Count)
+                {
+                    return WebResponseContent.Instance.Error($"鐗╂枡:{model.MCode}搴撳瓨涓嶈冻");
+                }
+                //鏇存敼鐘舵��
+                stockInfos.ForEach(x =>
+                {
+                    x.StockStatus = StockStatusEmun.鍏ュ簱瀹屾垚.ObjToInt();
+                });
+                return WebResponseContent.Instance.OK("鎴愬姛", stockInfos);
             }
             catch (Exception ex)
             {
                 return WebResponseContent.Instance.Error(ex.Message);
             }
         }
+        ///鎺ㄩ�佹祴璇曟灦鍑哄簱
+        public WebResponseContent TestOutBack(List<Dt_StockInfo> stockInfos,Dt_Warehouse warehouse, ErpOutOrderDTO model)
+        {
+            try
+            {
+                //娴嬭瘯鏋跺簱缁橢RP涓婃姤鍑哄簱瀹屾垚
+                List<ERPOutPickDetail> pickDetails = stockInfos.Select(x => new ERPOutPickDetail()
+                {
+                    Lotno = x.Details[0].BatchNo,
+                    Qty = x.Details[0].StockQuantity
+                }).ToList();
+                ERPOutPick outPick = new ERPOutPick()
+                {
+                    Rowindex = model.RowNo,
+                    Material = model.MCode,
+                    Qty = model.Qty,
+                    Dataitem = pickDetails
+                };
+                ERPOutboundModel inboundModel = new ERPOutboundModel()
+                {
+                    Way = 1,
+                    UniqueTag = model.OrderNo,
+                    Code = model.OrderNo,
+                    WarehouseCode = warehouse.WarehouseCode,
+                    Docremark = "",
+                    Deptno = model.DepartmentCode,
+                    DeptName = model.DepartmentName,
+                    Createtime = DateTime.Now,
+                    Createuser = "WMS",
+                    Picklist = new List<ERPOutPick>() { outPick },
+                };
+                string response = _invokeERPService.InvokeOutboundOrderApi(inboundModel);
+                ErpRequestContent requestContent=response.DeserializeObject<ErpRequestContent>();
+                if (requestContent.res==1)
+                {
+                    
+                    return WebResponseContent.Instance.OK(requestContent.Data);
+                }
+                else
+                {
+                    return WebResponseContent.Instance.Error(requestContent.Data);
+                }
+            }
+            catch (Exception ex)
+            {
+                return WebResponseContent.Instance.Error(ex.Message);
+            }
+        }
+
+        //public WebResponseContent FeedbackOutbondIssue(List<Dt_OutStockLockInfo> outStockLockInfos)
+        //{
+        //    ERPPickItemModel
+        //}
     }
 }

--
Gitblit v1.9.3