From f5851974b861a20182c06d6f891fd427b1414209 Mon Sep 17 00:00:00 2001
From: schangxiang@126.com <schangxiang@126.com>
Date: 周二, 20 5月 2025 15:21:19 +0800
Subject: [PATCH] 优化
---
HIAWms/server/src/CMS.Plugin.HIAWms.EntityFrameworkCore/Repositories/EfCoreWmsMaterialStockRepository.cs | 229 +++++++++++++++++++++++++++++++++++++++++++--------------
1 files changed, 173 insertions(+), 56 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 f7ad367..f86df38 100644
--- a/HIAWms/server/src/CMS.Plugin.HIAWms.EntityFrameworkCore/Repositories/EfCoreWmsMaterialStockRepository.cs
+++ b/HIAWms/server/src/CMS.Plugin.HIAWms.EntityFrameworkCore/Repositories/EfCoreWmsMaterialStockRepository.cs
@@ -1,9 +1,13 @@
-using System.Linq.Dynamic.Core;
+锘縰sing System.Linq.Dynamic.Core;
+using System.Linq.Expressions;
using CMS.Plugin.HIAWms.Domain.WmsMaterialStocks;
+using CMS.Plugin.HIAWms.Domain.WmsTask;
using CMS.Plugin.HIAWms.EntityFrameworkCore.Extensions;
+using CmsQueryExtensions.Extension;
using DatabaseSchemaReader.Filters;
using Microsoft.EntityFrameworkCore;
using SqlKata;
+using Volo.Abp;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Specifications;
@@ -32,6 +36,21 @@
.FirstOrDefaultAsync(t => t.MaterialNo == materialNo, GetCancellationToken(cancellationToken));
}
+ /// <summary>
+ /// 鏌ユ壘鍨嬪彿
+ /// </summary>
+ /// <param name="materiaModel"></param>
+ /// <param name="cancellationToken"></param>
+ /// <returns></returns>
+ public virtual async Task<WmsMaterialStock> FindByModelAsync(string materiaModel, CancellationToken cancellationToken = default)
+ {
+ return await (await GetDbSetAsync())
+ .IncludeDetails()
+ .Where(u => !u.IsDeleted)
+ .OrderBy(t => t.Sort)
+ .FirstOrDefaultAsync(t => t.MaterialModel == materiaModel, GetCancellationToken(cancellationToken));
+ }
+
/// <inheritdoc />
public async Task<bool> NameExistAsync(string materialNo, Guid? id = null)
{
@@ -55,33 +74,10 @@
}
/// <inheritdoc />
- 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)
+ public async Task<List<WmsMaterialStock>> GetListAsync(FunReturnResultModel<Expression<Func<WmsMaterialStock, bool>>> whereConditions, string sorting = null, int maxResultCount = int.MaxValue, int skipCount = 0, bool includeDetails = false, CancellationToken cancellationToken = default)
{
- specification ??= new WmsMaterialStockSpecification();
var baseQuery = (await GetDbSetAsync())
- .IncludeDetails(includeDetails)
- .WhereIf(!filter.IsNullOrWhiteSpace(),
- u => u.MaterialNo.Contains(filter) ||
- u.MaterialName.Contains(filter) ||
- u.MaterialBatch.Contains(filter) ||
- u.PlaceNo.Contains(filter) ||
- u.AreaName.Contains(filter))
- .WhereIf(!string.IsNullOrEmpty(stock?.MaterialNo), u => u.MaterialNo.Contains(stock.MaterialNo))
- .WhereIf(!string.IsNullOrEmpty(stock?.MaterialName), u => u.MaterialName.Contains(stock.MaterialName))
- .WhereIf(!string.IsNullOrEmpty(stock?.ContainerNo), u => u.ContainerNo == stock.ContainerNo)
- .WhereIf(stock?.ContainerStatus > 0, u => u.ContainerStatus == stock.ContainerStatus)
- .WhereIf(stock?.ContainerType > 0, u => u.ContainerType == stock.ContainerType)
- .WhereIf(!string.IsNullOrEmpty(stock?.MaterialBatch), u => u.MaterialBatch == stock.MaterialBatch)
- .WhereIf(!string.IsNullOrEmpty(stock?.SupplierCode), u => u.SupplierCode == stock.SupplierCode)
- .WhereIf(!string.IsNullOrEmpty(stock?.MaterialModel), u => u.MaterialModel.Contains(stock.MaterialModel))
- .WhereIf(!string.IsNullOrEmpty(stock?.PlaceNo), u => u.PlaceNo == stock.PlaceNo)
- .WhereIf(stock.PlaceStatus > 0, u => u.PlaceStatus == stock.PlaceStatus)
- .WhereIf(stock?.StorageTypeNo > 0, u => u.StorageTypeNo == stock.StorageTypeNo)
- .WhereIf(!string.IsNullOrEmpty(stock?.AreaCode), u => u.AreaCode == stock.AreaCode)
- .WhereIf(!string.IsNullOrEmpty(stock?.AreaName), u => u.AreaName.Contains(stock.AreaName))
- .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)
+ .IncludeDetails(includeDetails).WhereIf(whereConditions != null, whereConditions.data)
.Where(u => !u.IsDeleted)
.AsQueryable();
@@ -108,7 +104,7 @@
.ToListAsync(GetCancellationToken(cancellationToken));
var groupedData = materialList
- .GroupBy(x => new { x.MaterialNo, x.PlaceNo, x.ContainerNo})
+ .GroupBy(x => new { x.MaterialNo, x.PlaceNo, x.ContainerNo,x.MaterialModel})
.Select(g => new WmsMaterialStock
{
MaterialNo = g.Key.MaterialNo,
@@ -119,7 +115,7 @@
PlaceStatus = g.First().PlaceStatus,
StorageTypeNo = g.First().StorageTypeNo,
MaterialBatch = g.First().MaterialBatch,
- MaterialModel = g.First().MaterialModel,
+ MaterialModel = g.Key.MaterialModel,
AreaCode = g.First().AreaCode,
AreaName = g.First().AreaName,
CheckStatus = g.First().CheckStatus,
@@ -128,10 +124,10 @@
InStockTime = g.First().InStockTime,
StockNumber = g.Sum(x=>x.StockNumber)
})
- .AsQueryable(); // 转换回IQueryable以支持后续操作
+ .AsQueryable(); // 杞崲鍥濱Queryable浠ユ敮鎸佸悗缁搷浣�
var result = groupedData
- .OrderBy(sorting.IsNullOrEmpty() ? nameof(WmsMaterialStock.Sort) : sorting)
+ .OrderBy(x=>x.StockNumber)
.PageBy(skipCount, maxResultCount)
.ToList();
@@ -139,32 +135,10 @@
}
/// <inheritdoc />
- public async Task<long> GetCountAsync(WmsMaterialStock? stock, DateTime? startTime = null, DateTime? endTime = null, string filter = null, Specification<WmsMaterialStock> specification = null, CancellationToken cancellationToken = default)
+ public async Task<long> GetCountAsync(FunReturnResultModel<Expression<Func<WmsMaterialStock, bool>>> whereConditions, CancellationToken cancellationToken = default)
{
- specification ??= new WmsMaterialStockSpecification();
var baseQuery = (await GetQueryableAsync())
- .WhereIf(!filter.IsNullOrWhiteSpace(),
- u => u.MaterialNo.Contains(filter) ||
- u.MaterialName.Contains(filter) ||
- u.MaterialBatch.Contains(filter) ||
- u.PlaceNo.Contains(filter) ||
- u.AreaName.Contains(filter))
- .WhereIf(!string.IsNullOrEmpty(stock?.MaterialNo), u => u.MaterialNo.Contains(stock.MaterialNo))
- .WhereIf(!string.IsNullOrEmpty(stock?.MaterialName), u => u.MaterialName.Contains(stock.MaterialName))
- .WhereIf(!string.IsNullOrEmpty(stock?.ContainerNo), u => u.ContainerNo == stock.ContainerNo)
- .WhereIf(stock?.ContainerStatus > 0, u => u.ContainerStatus == stock.ContainerStatus)
- .WhereIf(stock?.ContainerType > 0, u => u.ContainerType == stock.ContainerType)
- .WhereIf(!string.IsNullOrEmpty(stock?.MaterialBatch), u => u.MaterialBatch == stock.MaterialBatch)
- .WhereIf(!string.IsNullOrEmpty(stock?.SupplierCode), u => u.SupplierCode == stock.SupplierCode)
- .WhereIf(!string.IsNullOrEmpty(stock?.MaterialModel), u => u.MaterialModel.Contains(stock.MaterialModel))
- .WhereIf(!string.IsNullOrEmpty(stock?.PlaceNo), u => u.PlaceNo == stock.PlaceNo)
- .WhereIf(stock.PlaceStatus > 0, u => u.PlaceStatus == stock.PlaceStatus)
- .WhereIf(stock?.StorageTypeNo > 0, u => u.StorageTypeNo == stock.StorageTypeNo)
- .WhereIf(!string.IsNullOrEmpty(stock?.AreaCode), u => u.AreaCode == stock.AreaCode)
- .WhereIf(!string.IsNullOrEmpty(stock?.AreaName), u => u.AreaName.Contains(stock.AreaName))
- .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)
+ .WhereIf(whereConditions != null, whereConditions.data)
.Where(u => !u.IsDeleted)
.AsQueryable();
@@ -210,13 +184,13 @@
InStockTime = g.First().InStockTime,
StockNumber = g.Count()
})
- .AsQueryable(); // 转换回IQueryable以支持后续操作
+ .AsQueryable(); // 杞崲鍥濱Queryable浠ユ敮鎸佸悗缁搷浣�
return groupedData.Count();
}
/// <summary>
- /// 查询库存列表
+ /// 鏌ヨ搴撳瓨鍒楄〃
/// </summary>
/// <param name="stock"></param>
/// <returns></returns>
@@ -238,13 +212,156 @@
.WhereIf(!string.IsNullOrEmpty(stock?.AreaName), u => u.AreaName.Contains(stock.AreaName))
.WhereIf(stock.IsLock > 0, u => u.IsLock == stock.IsLock)
.Where(u => !u.IsDeleted)
+ .OrderByDescending(x=>x.InStockTime)
.ToListAsync();
return baseQuery;
}
+
+ /// <summary>
+ /// 鏌ヨ搴撳瓨鏄庣粏
+ /// </summary>
+ /// <param name="whereConditions"></param>
+ /// <param name="sorting"></param>
+ /// <param name="maxResultCount"></param>
+ /// <param name="skipCount"></param>
+ /// <param name="includeDetails"></param>
+ /// <param name="cancellationToken"></param>
+ /// <returns></returns>
+ public async Task<List<WmsMaterialStock>> GetStockDetailListAsync(FunReturnResultModel<Expression<Func<WmsMaterialStock, bool>>> whereConditions, string sorting = null, int maxResultCount = int.MaxValue, int skipCount = 0, bool includeDetails = false, CancellationToken cancellationToken = default)
+ {
+ return await (await GetDbSetAsync())
+ .IncludeDetails(includeDetails)
+ .WhereIf(whereConditions != null, whereConditions.data)
+ .Where(x => !x.IsDeleted)
+ .OrderBy(x=>x.PlaceNo)
+ .ThenByDescending(x => x.CreationTime)
+ .PageBy(skipCount, maxResultCount)
+ .ToListAsync(GetCancellationToken(cancellationToken));
+ }
+
+ /// <summary>
+ /// 鑾峰彇鎬绘暟搴撳瓨绠$悊
+ /// </summary>
+ /// <param name="whereConditions"></param>
+ /// <param name="cancellationToken"></param>
+ /// <returns></returns>
+ public async Task<long> GetDetailCountAsync(FunReturnResultModel<Expression<Func<WmsMaterialStock, bool>>> whereConditions, CancellationToken cancellationToken = default)
+ {
+ return await (await GetQueryableAsync())
+ .WhereIf(whereConditions != null, whereConditions.data)
+ .Where(x => !x.IsDeleted)
+ .CountAsync(cancellationToken: GetCancellationToken(cancellationToken));
+ }
+
+
/// <inheritdoc />
public override async Task<IQueryable<WmsMaterialStock>> WithDetailsAsync()
{
return (await GetQueryableAsync()).IncludeDetails()
.Where(u => !u.IsDeleted);
}
+
+ /// <summary>
+ /// 鐗╃悊鍒犻櫎鍑哄叆搴撳崟鎹鎯�
+ /// </summary>
+ /// <param name="id">涓婚敭ID</param>
+ /// <param name="cancellationToken"></param>
+ /// <returns></returns>
+ public virtual async Task DeletePermanentlyAsync(Guid id, CancellationToken cancellationToken = default)
+ {
+ var entity = await (await GetDbSetAsync())
+ .FirstOrDefaultAsync(x => x.Id == id && !x.IsDeleted, GetCancellationToken(cancellationToken));
+
+ if (entity == null)
+ {
+ throw new Volo.Abp.Domain.Entities.EntityNotFoundException(typeof(WmsMaterialStock), id);
+ }
+
+ // 2. 鑾峰彇 DbContext 骞舵墽琛屽垹闄�
+ var dbContext = await GetDbContextAsync();
+
+ // 鐩存帴鎵ц SQL 鍒犻櫎
+ var sql = $"DELETE FROM scms_wmsmaterialstocks WHERE Id ='{entity.Id.ToString()}'";
+ await dbContext.Database.ExecuteSqlRawAsync(sql, cancellationToken);
+ }
+
+ /// <summary>
+ /// 鎵归噺鐗╃悊鍒犻櫎鍑哄叆搴撳崟鎹鎯咃紙鐩存帴鍒犻櫎锛屼笉杞垹闄わ級
+ /// </summary>
+ /// <param name="ids">瑕佸垹闄ょ殑涓婚敭ID鍒楄〃</param>
+ /// <param name="cancellationToken"></param>
+ /// <returns></returns>
+ public virtual async Task BatchDeletePermanentlyAsync(IEnumerable<Guid> ids, CancellationToken cancellationToken = default)
+ {
+ // 1. 鏌ヨ绗﹀悎鏉′欢鐨勫疄浣擄紙鏈蒋鍒犻櫎鐨勮褰曪級
+ var entities = await (await GetDbSetAsync())
+ .Where(x => ids.Contains(x.Id) && !x.IsDeleted)
+ .ToListAsync(GetCancellationToken(cancellationToken));
+
+ if (!entities.Any())
+ {
+ // 濡傛灉娌℃湁闇�瑕佸垹闄ょ殑璁板綍锛岀洿鎺ヨ繑鍥烇紙閬垮厤涓嶅繀瑕佺殑鏁版嵁搴撴搷浣滐級
+ return;
+ }
+
+ // 2. 鑾峰彇 DbContext 骞舵墽琛屾壒閲忓垹闄�
+ var dbContext = await GetDbContextAsync();
+
+ var idsToDelete = entities.Select(e => e.Id).ToList();
+
+ // 鐩存帴鎵ц SQL 鍒犻櫎
+ var sql = $"DELETE FROM scms_wmsmaterialstocks WHERE Id IN ({string.Join(",", idsToDelete.Select(id => $"'{id}'"))})";
+ await dbContext.Database.ExecuteSqlRawAsync(sql, cancellationToken);
+ }
+
+ /// <summary>
+ /// 鏍规嵁鏉′欢鑾峰彇鍑哄叆搴撳崟鎹鎯呭垪琛�
+ /// </summary>
+ /// <param name="whereConditions"></param>
+ /// <param name="cancellationToken"></param>
+ /// <returns></returns>
+ public async Task<List<WmsMaterialStock>> GetListByFilterAsync(Expression<Func<WmsMaterialStock, bool>> whereConditions, CancellationToken cancellationToken = default)
+ {
+ return await (await GetDbSetAsync())
+ .IncludeDetails()
+ .WhereIf(whereConditions != null, whereConditions)
+ .Where(x => !x.IsDeleted)
+ .OrderByDescending(x => x.CreationTime)
+ .ToListAsync(GetCancellationToken(cancellationToken));
+ }
+
+ /// <summary>
+ /// 鏍规嵁鏉′欢鑾峰彇鍗曚釜鍑哄叆搴撳崟鎹鎯�
+ /// </summary>
+ /// <param name="whereConditions"></param>
+ /// <param name="isMultipleThrowException">鏄惁鏌ヨ鍑哄鏉″氨鎶ラ敊</param>
+ /// <param name="cancellationToken"></param>
+ /// <returns></returns>
+ /// <exception cref="UserFriendlyException"></exception>
+ public async Task<WmsMaterialStock> GetSingleByFilterAsync(Expression<Func<WmsMaterialStock, bool>> whereConditions, bool is鈥婱ultipleThrowException = false, CancellationToken cancellationToken = default)
+ {
+ if (is鈥婱ultipleThrowException)
+ {
+ var entitys = await (await GetDbSetAsync())
+ .IncludeDetails()
+ .WhereIf(whereConditions != null, whereConditions)
+ .Where(x => !x.IsDeleted)
+ .OrderByDescending(x => x.CreationTime)
+ .ToListAsync(GetCancellationToken(cancellationToken));
+ if (entitys?.Count > 1)
+ {
+ throw new UserFriendlyException("鏌ヨ鍒板鏉¤褰�");
+ }
+ return entitys?.FirstOrDefault();
+ }
+ else
+ {
+ return await (await GetDbSetAsync())
+ .IncludeDetails()
+ .WhereIf(whereConditions != null, whereConditions)
+ .Where(x => !x.IsDeleted)
+ .OrderByDescending(x => x.CreationTime)
+ .FirstOrDefaultAsync(GetCancellationToken(cancellationToken));
+ }
+ }
}
--
Gitblit v1.9.3