From 6c1dece09c289cc04375459d9a463be22d041086 Mon Sep 17 00:00:00 2001
From: tjx <t2856754968@163.com>
Date: 星期五, 21 十一月 2025 17:05:21 +0800
Subject: [PATCH] 1111

---
 StandardPda/MES.Service/service/Warehouse/MesInvItemStocksManager.cs |  245 ++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 233 insertions(+), 12 deletions(-)

diff --git a/StandardPda/MES.Service/service/Warehouse/MesInvItemStocksManager.cs b/StandardPda/MES.Service/service/Warehouse/MesInvItemStocksManager.cs
index 053c842..0e10efd 100644
--- a/StandardPda/MES.Service/service/Warehouse/MesInvItemStocksManager.cs
+++ b/StandardPda/MES.Service/service/Warehouse/MesInvItemStocksManager.cs
@@ -10,7 +10,228 @@
 public class MesInvItemStocksManager : Repository<MesInvItemStocks>
 {
     /// <summary>
-    ///     鏌ヨ鍙互閫�璐х殑鐗╂枡
+    ///     鏌ヨ鍙互閫�璐х殑鐗╂枡(甯﹀垎椤靛拰鎼滅储)
+    /// </summary>
+    /// <param name="searchDto">鎼滅储璇锋眰鍙傛暟</param>
+    /// <returns>鍒嗛〉缁撴灉</returns>
+    public PagedResult<ReturnableStockDto> GetReturnableStocks(ReturnableStockSearchDto searchDto)
+    {
+        // 鍙傛暟鏍¢獙
+        if (searchDto.PageIndex < 1)
+        {
+            throw new Exception("椤电爜蹇呴』澶т簬0");
+        }
+
+        if (!new[] { 10, 20, 50 }.Contains(searchDto.PageSize))
+        {
+            throw new Exception("姣忛〉鏉℃暟蹇呴』涓�10銆�20鎴�50");
+        }
+
+        // 1. 鏌ヨXB_RACKING_TASK_SYXT_LOG涓墍鏈夌殑鏉$爜骞跺幓閲�
+        var distinctBarcodes = Db.Queryable<XbRackingTaskSyxtLog>()
+            .Where(x => !string.IsNullOrEmpty(x.ItemBarcode))
+            .Select(x => x.ItemBarcode)
+            .Distinct()
+            .ToList();
+
+        if (distinctBarcodes == null || !distinctBarcodes.Any())
+        {
+            return new PagedResult<ReturnableStockDto>
+            {
+                TbBillList = new List<ReturnableStockDto>(),
+                Pagination = new PaginationInfo
+                {
+                    CurrentPage = searchDto.PageIndex,
+                    PageSize = searchDto.PageSize,
+                    TotalRecords = 0,
+                    TotalPages = 0
+                }
+            };
+        }
+
+        // 2. 鏋勫缓鏌ヨ鏉′欢
+        var query = Db.Queryable<MesInvItemStocks>()
+            .LeftJoin<MesItems>((stock, item) => stock.ItemId == item.Id)
+            .LeftJoin<MesDepots>((stock, item, depot) => stock.DepotsCode == depot.DepotCode)
+            .LeftJoin<Organize>((stock, item, depot, org) => item.UseOrg == org.Id.ToString())
+            .LeftJoin<MesUnit>((stock, item, depot, org, unit) => item.ItemUnit == unit.Id.ToString())
+            .Where((stock, item, depot, org, unit) => 
+                distinctBarcodes.Contains(stock.ItemBarcode) && 
+                stock.Quantity > 0);
+
+        // 3. 搴旂敤鎼滅储鏉′欢
+        var conditions = searchDto.Conditions;
+        if (conditions != null)
+        {
+            // 绮剧‘鍖归厤鏉′欢
+            if (!string.IsNullOrEmpty(conditions.IqcStatus))
+            {
+                query = query.Where((stock, item, depot, org, unit) => stock.IqcStatus == conditions.IqcStatus);
+            }
+
+            if (conditions.Quantity.HasValue)
+            {
+                query = query.Where((stock, item, depot, org, unit) => stock.Quantity == conditions.Quantity.Value);
+            }
+
+            // 妯$硦鍖归厤鏉′欢
+            if (!string.IsNullOrEmpty(conditions.StackCode))
+            {
+                query = query.Where((stock, item, depot, org, unit) => 
+                    stock.StackCode != null && stock.StackCode.Contains(conditions.StackCode));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.DepotName))
+            {
+                query = query.Where((stock, item, depot, org, unit) => 
+                    depot.DepotName != null && depot.DepotName.Contains(conditions.DepotName));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.DepotSectionsCode))
+            {
+                query = query.Where((stock, item, depot, org, unit) => 
+                    stock.DepotSectionsCode != null && stock.DepotSectionsCode.Contains(conditions.DepotSectionsCode));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.ItemNo))
+            {
+                query = query.Where((stock, item, depot, org, unit) => 
+                    item.ItemNo != null && item.ItemNo.Contains(conditions.ItemNo));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.ItemName))
+            {
+                query = query.Where((stock, item, depot, org, unit) => 
+                    item.ItemName != null && item.ItemName.Contains(conditions.ItemName));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.ItemModel))
+            {
+                query = query.Where((stock, item, depot, org, unit) => 
+                    item.ItemModel != null && item.ItemModel.Contains(conditions.ItemModel));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.ItemUnitName))
+            {
+                query = query.Where((stock, item, depot, org, unit) => 
+                    unit.Fname != null && unit.Fname.Contains(conditions.ItemUnitName));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.OrgCode))
+            {
+                query = query.Where((stock, item, depot, org, unit) => 
+                    org.Fnumber != null && org.Fnumber.Contains(conditions.OrgCode));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.OrgName))
+            {
+                query = query.Where((stock, item, depot, org, unit) => 
+                    org.Fname != null && org.Fname.Contains(conditions.OrgName));
+            }
+
+            if (!string.IsNullOrEmpty(conditions.ItemBarcode))
+            {
+                query = query.Where((stock, item, depot, org, unit) => 
+                    stock.ItemBarcode != null && stock.ItemBarcode.Contains(conditions.ItemBarcode));
+            }
+
+            // 鏃ユ湡鑼冨洿鏉′欢
+            if (!string.IsNullOrEmpty(conditions.IndepDateStart))
+            {
+                if (DateTime.TryParse(conditions.IndepDateStart, out var startDate))
+                {
+                    query = query.Where((stock, item, depot, org, unit) => stock.IndepDate >= startDate);
+                }
+            }
+
+            if (!string.IsNullOrEmpty(conditions.IndepDateEnd))
+            {
+                if (DateTime.TryParse(conditions.IndepDateEnd, out var endDate))
+                {
+                    query = query.Where((stock, item, depot, org, unit) => stock.IndepDate <= endDate);
+                }
+            }
+        }
+
+        // 4. 鏌ヨ鎬昏褰曟暟
+        var totalRecords = query.Count();
+
+        // 5. 璁$畻鍒嗛〉鍙傛暟
+        var totalPages = (int)Math.Ceiling((double)totalRecords / searchDto.PageSize);
+        var skip = (searchDto.PageIndex - 1) * searchDto.PageSize;
+
+        // 6. 鏌ヨ褰撳墠椤垫暟鎹�(鍏堟煡鍑轰腑闂存暟鎹�)
+        var queryResult = query
+            .OrderByDescending((stock, item, depot, org, unit) => stock.IndepDate)
+            .Select((stock, item, depot, org, unit) => new
+            {
+                stock.IqcStatus,
+                stock.StackCode,
+                DepotCode = stock.DepotsCode,
+                depot.DepotName,
+                stock.DepotSectionsCode,
+                item.ItemNo,
+                item.ItemName,
+                item.ItemModel,
+                stock.Quantity,
+                item.ItemUnit,
+                ItemUnitName = unit.Fname,
+                stock.IndepDate,
+                OrgCode = org.Fnumber,
+                OrgName = org.Fname,
+                stock.ItemBarcode
+            })
+            .Skip(skip)
+            .Take(searchDto.PageSize)
+            .ToList();
+
+        // 7. 鍦ㄥ唴瀛樹腑杞崲涓篋TO
+        var dataList = queryResult.Select(x => new ReturnableStockDto
+        {
+            IqcStatus = x.IqcStatus == "宸叉" ? "1" : "0",
+            ItemType = x.DepotName == "鍘熸潗鏂欎粨" ? "0" : "1",
+            StackCode = x.StackCode,
+            DepotCode = x.DepotCode,
+            DepotName = x.DepotName,
+            DepotSectionsCode = x.DepotSectionsCode,
+            ItemNo = x.ItemNo,
+            ItemName = x.ItemName,
+            ItemModel = x.ItemModel,
+            Quantity = x.Quantity,
+            ItemUnit = x.ItemUnit,
+            ItemUnitName = x.ItemUnitName,
+            IndepDate = x.IndepDate,
+            OrgCode = x.OrgCode,
+            OrgName = x.OrgName,
+            ItemBarcode = x.ItemBarcode
+        }).ToList();
+
+        // 8. 搴旂敤ItemType绛涢��(鍦ㄥ唴瀛樹腑杩囨护)
+        if (conditions?.ItemType != null)
+        {
+            dataList = dataList.Where(x => x.ItemType == conditions.ItemType).ToList();
+            // 閲嶆柊璁$畻鍒嗛〉淇℃伅
+            totalRecords = dataList.Count;
+            totalPages = (int)Math.Ceiling((double)totalRecords / searchDto.PageSize);
+            dataList = dataList.Skip(skip).Take(searchDto.PageSize).ToList();
+        }
+
+        // 9. 杩斿洖鍒嗛〉缁撴灉
+        return new PagedResult<ReturnableStockDto>
+        {
+            TbBillList = dataList,
+            Pagination = new PaginationInfo
+            {
+                CurrentPage = searchDto.PageIndex,
+                PageSize = searchDto.PageSize,
+                TotalRecords = totalRecords,
+                TotalPages = totalPages
+            }
+        };
+    }
+
+    /// <summary>
+    ///     鏌ヨ鍙互閫�璐х殑鐗╂枡(鏃х増鏈�,淇濈暀鍏煎)
     /// </summary>
     /// <returns>鍙��璐х墿鏂欏簱瀛樺垪琛�</returns>
     public List<ReturnableStockDto> GetReturnableStocks()
@@ -41,21 +262,21 @@
                 stock.Quantity > 0)
             .Select((stock, item, depot, org, unit) => new
             {
-                IqcStatus = stock.IqcStatus,
-                StackCode = stock.StackCode,
+                stock.IqcStatus,
+                stock.StackCode,
                 DepotCode = stock.DepotsCode,
-                DepotName = depot.DepotName,
-                DepotSectionsCode = stock.DepotSectionsCode,
-                ItemNo = item.ItemNo,
-                ItemName = item.ItemName,
-                ItemModel = item.ItemModel,
-                Quantity = stock.Quantity,
-                ItemUnit = item.ItemUnit,
+                depot.DepotName,
+                stock.DepotSectionsCode,
+                item.ItemNo,
+                item.ItemName,
+                item.ItemModel,
+                stock.Quantity,
+                item.ItemUnit,
                 ItemUnitName = unit.Fname,
-                IndepDate = stock.IndepDate,
+                stock.IndepDate,
                 OrgCode = org.Fnumber,
                 OrgName = org.Fname,
-                ItemBarcode = stock.ItemBarcode
+                stock.ItemBarcode
             })
             .ToList();
 

--
Gitblit v1.9.3