222
schangxiang@126.com
2025-05-11 847e7a89974cb6f5bcfbf7fc8fbe84f085502af6
PipeLineLems/server/src/CMS.Plugin.PipeLineLems.EntityFrameworkCore/Repositories/EfCoreWorkPlanRepository.cs
@@ -1,73 +1,90 @@
using CMS.Plugin.PipeLineLems.Domain.WorkPlan;
using CMS.Plugin.PipeLineLems.EntityFrameworkCore.Extensions;
using CmsQueryExtensions.Extension;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
namespace CMS.Plugin.PipeLineLems.EntityFrameworkCore.Repositories;
using CMS.Plugin.PipeLineLems.Domain.WorkPlan;
using CMS.Plugin.PipeLineLems.EntityFrameworkCore.Extensions;
using CmsQueryExtensions.Extension;
using Microsoft.EntityFrameworkCore;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
namespace CMS.Plugin.PipeLineLems.EntityFrameworkCore.Repositories;
/// <summary> 
/// 作业计划表仓储实现 
/// </summary> 
public class EfCoreWorkPlanRepository : EfCoreRepository<ICMSPluginDbContext, WorkPlan, Guid>, IWorkPlanRepository
{
public class EfCoreWorkPlanRepository : EfCoreRepository<ICMSPluginDbContext, WorkPlan, Guid>, IWorkPlanRepository
{
    /// <summary> 
    /// Initializes a new instance of the <see cref="EfCoreWorkPlanRepository"/> class. 
    /// </summary> 
    /// <param name="dbContextProvider">The database context provider.</param> 
    public EfCoreWorkPlanRepository(IDbContextProvider<ICMSPluginDbContext> dbContextProvider)
        : base(dbContextProvider)
    {
    }
    public EfCoreWorkPlanRepository(IDbContextProvider<ICMSPluginDbContext> dbContextProvider)
        : base(dbContextProvider)
    {
    }
    /// <summary> 
    /// 按照名称查找作业计划表 
    /// </summary> 
    /// <param name="name"></param> 
    /// <param name="cancellationToken"></param> 
    /// <returns></returns> 
    public virtual async Task<WorkPlan> FindByNameAsync(string name, CancellationToken cancellationToken = default)
    {
        return await (await GetDbSetAsync())
            .IncludeDetails()
            .Where(x => !x.IsDeleted)
            .OrderByDescending(x=>x.CreationTime)
            .FirstOrDefaultAsync(t => t.TaskCode == name, GetCancellationToken(cancellationToken));
    }
    public virtual async Task<WorkPlan> FindByNameAsync(string name, CancellationToken cancellationToken = default)
    {
        return await (await GetDbSetAsync())
            .IncludeDetails()
            .Where(x => !x.IsDeleted)
            .OrderByDescending(x => x.CreationTime)
            .FirstOrDefaultAsync(t => t.TaskCode == name, GetCancellationToken(cancellationToken));
    }
    /// <summary>
    /// 按照 原料标记 查找作业计划表
    /// </summary>
    /// <param name="name"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    public virtual async Task<List<WorkPlan>> FindByDataIdentifierAsync(string dataIdentifier, CancellationToken cancellationToken = default)
    {
        return await (await GetDbSetAsync())
            .IncludeDetails()
            .Where(x => !x.IsDeleted)
            .Where(t => t.DataIdentifier == dataIdentifier)
            .OrderByDescending(x => x.CreationTime)
             .ToListAsync(GetCancellationToken(cancellationToken));
    }
    /// <summary> 
    /// 验证名称是否存在作业计划表 
    /// </summary> 
    /// <param name="name">校验值</param> 
    /// <param name="id"></param> 
    /// <returns></returns> 
    public async Task<bool> NameExistAsync(string name, Guid? id = null)
    {
        return await (await GetDbSetAsync()).WhereIf(id.HasValue, p => p.Id != id).Where(x => !x.IsDeleted).AnyAsync(x => x.TaskCode == name);
    }
    public async Task<bool> NameExistAsync(string name, Guid? id = null)
    {
        return await (await GetDbSetAsync()).WhereIf(id.HasValue, p => p.Id != id).Where(x => !x.IsDeleted).AnyAsync(x => x.TaskCode == name);
    }
    /// <summary> 
    /// 获取最大排序作业计划表 
    /// </summary> 
    /// <returns></returns> 
    public async Task<int> GetMaxSortAsync()
    {
        var hasAny = await (await GetQueryableAsync())
            .Where(x => !x.IsDeleted).AnyAsync();
        if (!hasAny)
        {
            return 1;
        }
        var sort = await (await GetQueryableAsync())
            .Where(x => !x.IsDeleted).MaxAsync(x => x.Sort);
        return sort + 1;
    }
    public async Task<int> GetMaxSortAsync()
    {
        var hasAny = await (await GetQueryableAsync())
            .Where(x => !x.IsDeleted).AnyAsync();
        if (!hasAny)
        {
            return 1;
        }
        var sort = await (await GetQueryableAsync())
            .Where(x => !x.IsDeleted).MaxAsync(x => x.Sort);
        return sort + 1;
    }
    /// <summary> 
    /// 获取分页列表作业计划表 
    /// </summary> 
@@ -78,36 +95,36 @@
    /// <param name="includeDetails"></param> 
    /// <param name="cancellationToken"></param> 
    /// <returns></returns> 
    public async Task<List<WorkPlan>> GetListAsync(FunReturnResultModel<Expression<Func<WorkPlan, 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)
            .OrderByDescending(x=>x.CreationTime)
            .PageBy(skipCount, maxResultCount)
            .ToListAsync(GetCancellationToken(cancellationToken));
    }
    public async Task<List<WorkPlan>> GetListAsync(FunReturnResultModel<Expression<Func<WorkPlan, 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)
            .OrderByDescending(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> GetCountAsync(FunReturnResultModel<Expression<Func<WorkPlan, bool>>> whereConditions, CancellationToken cancellationToken = default)
    {
        return await (await GetQueryableAsync())
            .WhereIf(whereConditions != null, whereConditions.data)
            .Where(x => !x.IsDeleted)
            .CountAsync(cancellationToken: GetCancellationToken(cancellationToken));
    }
    public async Task<long> GetCountAsync(FunReturnResultModel<Expression<Func<WorkPlan, 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<WorkPlan>> WithDetailsAsync()
    {
        return (await GetQueryableAsync())
            .Where(x => !x.IsDeleted).IncludeDetails();
    }
}
    public override async Task<IQueryable<WorkPlan>> WithDetailsAsync()
    {
        return (await GetQueryableAsync())
            .Where(x => !x.IsDeleted).IncludeDetails();
    }
}