From 18d2f40bbf40469b033de6f07ca8a8e3bc191132 Mon Sep 17 00:00:00 2001
From: dengjunjie <dengjunjie@hnkhzn.com>
Date: 星期六, 26 四月 2025 19:21:40 +0800
Subject: [PATCH] 优化代码

---
 项目代码/WMS/WIDESEA_WMSServer/WIDESEA_StockService/Base/StockInfoDetailService.cs |   74 +++++++++++++++++++++++++++++++++++-
 1 files changed, 71 insertions(+), 3 deletions(-)

diff --git "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_StockService/Base/StockInfoDetailService.cs" "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_StockService/Base/StockInfoDetailService.cs"
index 555e3a5..82bd331 100644
--- "a/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_StockService/Base/StockInfoDetailService.cs"
+++ "b/\351\241\271\347\233\256\344\273\243\347\240\201/WMS/WIDESEA_WMSServer/WIDESEA_StockService/Base/StockInfoDetailService.cs"
@@ -1,9 +1,15 @@
-锘縰sing System;
+锘縰sing SqlSugar.Extensions;
+using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Reflection.Metadata;
 using System.Text;
 using System.Threading.Tasks;
+using System.Transactions;
+using WIDESEA_Core;
 using WIDESEA_Core.BaseServices;
+using WIDESEA_Core.Enums;
+using WIDESEA_DTO.Inbound;
 using WIDESEA_IStockRepository;
 using WIDESEA_IStockService;
 using WIDESEA_Model.Models;
@@ -12,11 +18,73 @@
 {
     public partial class StockInfoDetailService : ServiceBase<Dt_StockInfoDetail, IStockInfoDetailRepository>, IStockInfoDetailService
     {
-        public StockInfoDetailService(IStockInfoDetailRepository BaseDal) : base(BaseDal)
+        private readonly IStockInfoDetailRepository _stockInfoDetailRepository;
+        private readonly IStockInfoService _stockInfoService;
+        public StockInfoDetailService(IStockInfoDetailRepository BaseDal, IStockInfoDetailRepository stockInfoDetailRepository, IStockInfoService stockInfoService) : base(BaseDal)
         {
+            _stockInfoDetailRepository = stockInfoDetailRepository;
+            _stockInfoService = stockInfoService;
         }
 
         public IStockInfoDetailRepository Repository => BaseDal;
-
+        public override WebResponseContent DeleteData(object[] keys)
+        {
+            WebResponseContent content = new WebResponseContent();
+            try
+            {
+                var DelStockInfoDetails = BaseDal.QueryData(x => keys.Contains(x.Id)).ToList();
+                if (DelStockInfoDetails == null || DelStockInfoDetails.Count < 1) throw new Exception($"鏈壘鍒颁俊鎭�");
+                if (DelStockInfoDetails.FirstOrDefault(x => x.Status > StockStatusEmun.缁勭洏鏆傚瓨.ObjToInt()) != null)
+                    throw new Exception($"搴撳瓨鏄庣粏鐘舵�佷笉涓虹粍鐩樻殏瀛樻殏涓嶈兘鍒犻櫎");
+                var StockIds = DelStockInfoDetails.Select(x => x.StockId).ToList();
+                var StockInfoDetails = BaseDal.QueryData(x => StockIds.Contains(x.StockId)).Select(x => x.StockId).GroupBy(x => x).ToList();
+                List<Dt_StockInfo> StockInfos = new List<Dt_StockInfo>();
+                foreach (var item in StockInfoDetails)
+                {
+                    if (item.Count() == DelStockInfoDetails.Where(x => x.StockId == item.Key).Count())
+                    {
+                        var StockInfo = Db.Queryable<Dt_StockInfo>().Where(x => item.Key == x.Id).First();
+                        StockInfos.Add(StockInfo);
+                    }
+                }
+                List<Dt_InboundOrder> inboundOrders = new List<Dt_InboundOrder>();
+                List<Dt_InboundOrderDetail> inboundOrderDetails = new List<Dt_InboundOrderDetail>();
+                foreach (var DelStockInfoDetail in DelStockInfoDetails.GroupBy(x => x.OrderNo))
+                {
+                    var inboundOrder = BaseDal.Db.Queryable<Dt_InboundOrder>().Includes(x => x.Details).First(x => x.OrderNo == DelStockInfoDetail.Key);
+                    if (inboundOrder != null)
+                    {
+                        foreach (var item in DelStockInfoDetail)
+                        {
+                            var inboundOrderDetail = inboundOrder.Details.FirstOrDefault(x => x.BatchNo == item.BatchNo);
+                            if (inboundOrderDetail != null)
+                            {
+                                inboundOrderDetail.ReceiptQuantity -= 1;// item.StockQuantity;
+                                if (inboundOrderDetail.ReceiptQuantity == 0) inboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt();
+                            }
+                        }
+                        inboundOrderDetails.AddRange(inboundOrder.Details);
+                        if (inboundOrder.Details.FirstOrDefault(x => x.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt()) == null)
+                        {
+                            inboundOrder.OrderStatus = InboundStatusEnum.鏈紑濮�.ObjToInt();
+                            inboundOrders.Add(inboundOrder);
+                        }
+                    }
+                }
+                Db.Ado.BeginTran();
+                BaseDal.Db.Updateable(inboundOrderDetails).ExecuteCommand();
+                BaseDal.Db.Updateable(inboundOrders).ExecuteCommand();
+                _stockInfoService.DeleteData(StockInfos);
+                base.DeleteData(keys);
+                Db.Ado.CommitTran();
+                content.OK();
+            }
+            catch (Exception ex)
+            {
+                Db.Ado.RollbackTran();
+                content.Error(ex.Message);
+            }
+            return content;
+        }
     }
 }

--
Gitblit v1.9.3