From 4eafe9c975b88483da2a16f20c45285db0a3e791 Mon Sep 17 00:00:00 2001
From: zs <zhousong@weben-smart.com>
Date: 周一, 05 5月 2025 21:33:05 +0800
Subject: [PATCH] 库存及库存明细

---
 HIAWms/server/src/CMS.Plugin.HIAWms.EntityFrameworkCore/Repositories/EfCoreWmsMaterialStockRepository.cs |  119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 111 insertions(+), 8 deletions(-)

diff --git a/HIAWms/server/src/CMS.Plugin.HIAWms.EntityFrameworkCore/Repositories/EfCoreWmsMaterialStockRepository.cs b/HIAWms/server/src/CMS.Plugin.HIAWms.EntityFrameworkCore/Repositories/EfCoreWmsMaterialStockRepository.cs
index 4e491d3..86b12d4 100644
--- a/HIAWms/server/src/CMS.Plugin.HIAWms.EntityFrameworkCore/Repositories/EfCoreWmsMaterialStockRepository.cs
+++ b/HIAWms/server/src/CMS.Plugin.HIAWms.EntityFrameworkCore/Repositories/EfCoreWmsMaterialStockRepository.cs
@@ -2,6 +2,7 @@
 using CMS.Plugin.HIAWms.Domain.WmsMaterialStocks;
 using CMS.Plugin.HIAWms.EntityFrameworkCore.Extensions;
 using Microsoft.EntityFrameworkCore;
+using SqlKata;
 using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
 using Volo.Abp.EntityFrameworkCore;
 using Volo.Abp.Specifications;
@@ -25,6 +26,7 @@
     {
         return await (await GetDbSetAsync())
             .IncludeDetails()
+        .Where(u => !u.IsDeleted)
             .OrderBy(t => t.Sort)
             .FirstOrDefaultAsync(t => t.MaterialNo == materialNo, GetCancellationToken(cancellationToken));
     }
@@ -32,19 +34,22 @@
     /// <inheritdoc />
     public async Task<bool> NameExistAsync(string materialNo, Guid? id = null)
     {
-        return await (await GetDbSetAsync()).WhereIf(id.HasValue, p => p.Id != id).AnyAsync(x => x.MaterialNo == materialNo);
+        return await (await GetDbSetAsync()).WhereIf(id.HasValue, p => p.Id != id)
+        .Where(u => !u.IsDeleted).AnyAsync(x => x.MaterialNo == materialNo);
     }
 
     /// <inheritdoc />
     public async Task<int> GetMaxSortAsync()
     {
-        var hasAny = await (await GetQueryableAsync()).AnyAsync();
+        var hasAny = await (await GetQueryableAsync())
+        .Where(u => !u.IsDeleted).AnyAsync();
         if (!hasAny)
         {
             return 1;
         }
 
-        var sort = await (await GetQueryableAsync()).MaxAsync(x => x.Sort);
+        var sort = await (await GetQueryableAsync())
+        .Where(u => !u.IsDeleted).MaxAsync(x => x.Sort);
         return sort + 1;
     }
 
@@ -52,7 +57,7 @@
     public async Task<List<WmsMaterialStock>> GetListAsync(WmsMaterialStock? stock, DateTime? startTime = null, DateTime? endTime = null, string sorting = null, int maxResultCount = int.MaxValue, int skipCount = 0, string filter = null, Specification<WmsMaterialStock> specification = null, bool includeDetails = false, CancellationToken cancellationToken = default)
     {
         specification ??= new WmsMaterialStockSpecification();
-        return await (await GetDbSetAsync())
+        var baseQuery = (await GetDbSetAsync())
         .IncludeDetails(includeDetails)
         .WhereIf(!filter.IsNullOrWhiteSpace(),
             u => u.MaterialNo.Contains(filter) ||
@@ -76,16 +81,66 @@
         .WhereIf(stock.IsLock > 0, u => u.IsLock == stock.IsLock)
         .WhereIf(startTime.HasValue, u => u.InStockTime >= startTime.Value)
         .WhereIf(endTime.HasValue, u => u.InStockTime <= endTime.Value)
+        .Where(u => !u.IsDeleted)
+        .AsQueryable();
+
+        var materialList = await baseQuery
+        .Select(x => new
+        {
+            x.MaterialNo,
+            x.MaterialName,
+            x.PlaceNo,
+            x.ContainerNo,
+            x.ContainerStatus,
+            x.PlaceStatus,
+            x.StorageTypeNo,
+            x.MaterialBatch,
+            x.MaterialModel,
+            x.AreaCode,
+            x.AreaName,
+            x.CheckStatus,
+            x.IsLock,
+            x.EmptyContainer,
+            x.InStockTime
+        })
+        .ToListAsync(GetCancellationToken(cancellationToken));
+
+        var groupedData = materialList
+       .GroupBy(x => new { x.MaterialNo, x.PlaceNo, x.ContainerNo, x.MaterialName })
+       .Select(g => new WmsMaterialStock
+       {
+           MaterialNo = g.Key.MaterialNo,
+           MaterialName = g.Key.MaterialName,
+           PlaceNo = g.Key.PlaceNo,
+           ContainerNo = g.Key.ContainerNo,
+           ContainerStatus = g.First().ContainerStatus,
+           PlaceStatus = g.First().PlaceStatus,
+           StorageTypeNo = g.First().StorageTypeNo,
+           MaterialBatch = g.First().MaterialBatch,
+           MaterialModel = g.First().MaterialModel,
+           AreaCode = g.First().AreaCode,
+           AreaName = g.First().AreaName,
+           CheckStatus = g.First().CheckStatus,
+           IsLock = g.First().IsLock,
+           EmptyContainer = g.First().EmptyContainer,
+           InStockTime = g.First().InStockTime,
+           StockNumber = g.Count()
+       })
+       .AsQueryable(); // 转换回IQueryable以支持后续操作
+
+        var result = groupedData
         .OrderBy(sorting.IsNullOrEmpty() ? nameof(WmsMaterialStock.Sort) : sorting)
         .PageBy(skipCount, maxResultCount)
-        .ToListAsync(GetCancellationToken(cancellationToken));
+        .ToList();
+
+        return result;
     }
 
     /// <inheritdoc />
     public async Task<long> GetCountAsync(WmsMaterialStock? stock, DateTime? startTime = null, DateTime? endTime = null, string filter = null, Specification<WmsMaterialStock> specification = null, CancellationToken cancellationToken = default)
     {
         specification ??= new WmsMaterialStockSpecification();
-        return await (await GetQueryableAsync())
+        var baseQuery = (await GetQueryableAsync())
             .WhereIf(!filter.IsNullOrWhiteSpace(),
             u => u.MaterialNo.Contains(filter) ||
                  u.MaterialName.Contains(filter) ||
@@ -108,12 +163,60 @@
             .WhereIf(stock.IsLock > 0, u => u.IsLock == stock.IsLock)
             .WhereIf(startTime.HasValue, u => u.InStockTime >= startTime.Value)
             .WhereIf(endTime.HasValue, u => u.InStockTime <= endTime.Value)
-            .CountAsync(cancellationToken: GetCancellationToken(cancellationToken));
+            .Where(u => !u.IsDeleted)
+            .AsQueryable();
+
+        var materialList = await baseQuery
+       .Select(x => new
+       {
+           x.MaterialNo,
+           x.MaterialName,
+           x.PlaceNo,
+           x.ContainerNo,
+           x.ContainerStatus,
+           x.PlaceStatus,
+           x.StorageTypeNo,
+           x.MaterialBatch,
+           x.MaterialModel,
+           x.AreaCode,
+           x.AreaName,
+           x.CheckStatus,
+           x.IsLock,
+           x.EmptyContainer,
+           x.InStockTime
+       })
+       .ToListAsync(GetCancellationToken(cancellationToken));
+
+        var groupedData = materialList
+       .GroupBy(x => new { x.MaterialNo, x.PlaceNo, x.ContainerNo, x.MaterialName })
+       .Select(g => new WmsMaterialStock
+       {
+           MaterialNo = g.Key.MaterialNo,
+           MaterialName = g.Key.MaterialName,
+           PlaceNo = g.Key.PlaceNo,
+           ContainerNo = g.Key.ContainerNo,
+           ContainerStatus = g.First().ContainerStatus,
+           PlaceStatus = g.First().PlaceStatus,
+           StorageTypeNo = g.First().StorageTypeNo,
+           MaterialBatch = g.First().MaterialBatch,
+           MaterialModel = g.First().MaterialModel,
+           AreaCode = g.First().AreaCode,
+           AreaName = g.First().AreaName,
+           CheckStatus = g.First().CheckStatus,
+           IsLock = g.First().IsLock,
+           EmptyContainer = g.First().EmptyContainer,
+           InStockTime = g.First().InStockTime,
+           StockNumber = g.Count()
+       })
+       .AsQueryable(); // 转换回IQueryable以支持后续操作
+
+        return groupedData.Count();
     }
 
     /// <inheritdoc />
     public override async Task<IQueryable<WmsMaterialStock>> WithDetailsAsync()
     {
-        return (await GetQueryableAsync()).IncludeDetails();
+        return (await GetQueryableAsync()).IncludeDetails()
+        .Where(u => !u.IsDeleted);
     }
 }

--
Gitblit v1.9.3