track/src/SJZY.Track.Repository/Repositories/WarningRuleRepository.cs
hardstylewyl 7f938c5a7d perf:对于空值使用 -- 填充处理
fix:运输时效预警
 - 修复累计运输时间计算错误
 - 修复运输预警明细的已超时状态被忽略
2025-03-20 14:32:33 +08:00

285 lines
9.8 KiB
C#

using System.Linq.Expressions;
using SJZY.Track.Domain;
using SJZY.Track.Domain.Aggregates.WarningAggregate;
using SJZY.Track.Domain.Aggregates.WarningAggregate.Complete;
using SJZY.Track.Domain.Aggregates.WarningAggregate.Return;
using SJZY.Track.Domain.Aggregates.WarningAggregate.Transport;
using SJZY.Track.Domain.Enums;
using SJZY.Track.Domain.Extensions;
namespace SJZY.Track.Repository.Repositories;
public class WarningRuleRepository : BasicRepository<WarningRuleEntity>, IWarningRuleRepository, ITransientDependency
{
#region 规则主体
public async Task<bool> CheckNameDuplicateAsync(int id, int subsystemId, int customId, string name)
{
var entity = await Context.Queryable<WarningRuleEntity>()
.Where(x => x.SubSystemId == subsystemId &&
x.CustomId == customId
&& string.Equals(x.Name, name))
.FirstAsync();
// 不重复
if (entity == null)
return false;
// 如果是修改,则要求查出来的和要修改的是同一个,否则视为重复
return entity.Id != id;
}
public async Task<bool> CheckEnableDuplicateAsync(int warningId, int subsystemId, int customId, WarningRuleType warningType)
{
var entity = await Context.Queryable<WarningRuleEntity>()
.Where(x => x.SubSystemId == subsystemId
&& x.CustomId == customId
&& x.WarningType == (int) warningType
&& x.IsEnabled)
.FirstAsync();
// 没有【子系统+客户+类型】启用的规则
if (entity == null) return false;
// 已经启用和当前规则不同,视为启用重复,不允许操作
return entity.Id != warningId;
}
public async Task<int> UpsertReturnIdAsync(WarningRuleEntity entity)
{
if (entity.Id == 0)
{
return await Context.Insertable(entity)
.ExecuteReturnIdentityAsync();
}
var isUpdated = await Context.Updateable(entity)
.ExecuteCommandHasChangeAsync();
// 指定了id更新失败
if (!isUpdated)
{
throw new Exception($"{entity}更新失败");
}
return entity.Id;
}
public async Task<string> UpdateVersionAsync(WarningRuleEntity entity)
{
var version = VersionGenerate.GenVersion();
await Context.Updateable<WarningRuleEntity>()
.Where(x => x.Id == entity.Id)
.SetColumns(x => x.Version, version)
.ExecuteCommandHasChangeAsync();
return version;
}
public Task<List<WarningRuleEntity>> GetWarningRuleListAsync(int? subsystemId, string? keywords, bool? isEnabled, int? warningType)
{
return Context.Queryable<WarningRuleEntity>()
.WhereIF(subsystemId.HasValue, x => x.SubSystemId == subsystemId!.Value)
// todo: 客户/kpi名称 改成客户下拉
.WhereIF(!string.IsNullOrWhiteSpace(keywords), x => x.Name.Contains(keywords!))
.WhereIF(isEnabled.HasValue, x => x.IsEnabled == isEnabled!.Value)
.WhereIF(warningType.HasValue, x => x.WarningType == warningType!.Value)
.ToListAsync();
}
public async Task<WarningRuleEntity?> GetWarningRuleForEnabledAsync(int subSystemId, int customId, WarningRuleType warningType)
{
var warningRules = await Context.Queryable<WarningRuleEntity>()
.Where(x => x.CustomId == customId
&& x.SubSystemId == subSystemId
&& x.WarningType == (int) warningType
&& x.IsEnabled
)
.ToListAsync();
return warningRules.FirstOrDefault();
}
public async Task<WarningRuleEntity?> GetWarningRuleByIdAsync(int id, WarningRuleType? warningType = null)
{
var result = await Context.Queryable<WarningRuleEntity>()
.Where(x => x.Id == id)
.WhereIF(warningType.HasValue, x => x.WarningType == (int) warningType!.Value)
.ToListAsync();
return result.FirstOrDefault();
}
#endregion
#region 运输时效预警规则
public Task<List<TransportWarningSceneEntity>> GetTransportSceneAsync(int warningId)
{
return Context.Queryable<TransportWarningSceneEntity>()
.Where(x => x.WarningId == warningId)
.ToListAsync();
}
public Task<List<TransportWarningSceneEntity>> GetTransportSceneAsync(Expression<Func<TransportWarningSceneEntity, bool>> whereExpression)
{
return Context.Queryable<TransportWarningSceneEntity>()
.Where(whereExpression)
.ToListAsync();
}
public async Task<TransportWarningSceneEntity?> GetTransportSceneAsync(int warningId, int sceneId)
{
var result = await Context.Queryable<TransportWarningSceneEntity>()
.Where(x => x.WarningId == warningId && x.Id == sceneId)
.ToListAsync();
return result.FirstOrDefault();
}
public Task<List<TransportWarningSceneRuleItemEntity>> GetTransportSceneRuleItemsAsync(int warningId, int sceneId)
{
return Context.Queryable<TransportWarningSceneRuleItemEntity>()
.Where(x => x.WarningId == warningId && x.TransportWarningSceneId == sceneId)
.ToListAsync();
}
public Task<List<TResult>> GetTransportSceneRuleItemsAsync<TResult>(Expression<Func<TransportWarningSceneRuleItemEntity, bool>> whereExpression,
Expression<Func<TransportWarningSceneRuleItemEntity, TResult>> resultExpression)
{
return Context.Queryable<TransportWarningSceneRuleItemEntity>()
.Where(whereExpression)
.Select(resultExpression)
.ToListAsync();
}
public async Task<int> UpsertTransportSceneReturnIdAsync(TransportWarningSceneEntity entity)
{
if (entity.Id == 0)
{
return await Context.Insertable(entity)
.ExecuteReturnIdentityAsync();
}
var isUpdated = await Context.Updateable(entity)
.ExecuteCommandHasChangeAsync();
// 指定了id更新失败
if (!isUpdated)
{
throw new Exception($"{entity}更新失败");
}
return entity.Id;
}
public async Task<int> BulkUpdateTransportSceneRuleItemsAsync(List<TransportWarningSceneRuleItemEntity> entities)
{
return await Context.Updateable(entities)
.ExecuteCommandAsync();
}
public Task<int> BulkInsertTransportSceneRuleItemsAsync(List<TransportWarningSceneRuleItemEntity> entities)
{
return Context.Insertable(entities)
.ExecuteCommandAsync();
}
public Task<int> DeleteTransportSceneRuleItemsAsync(HashSet<int> ruleItemIds)
{
return Context.Deleteable<TransportWarningSceneRuleItemEntity>()
.Where(x => ruleItemIds.Contains(x.Id))
.ExecuteCommandAsync();
}
public async Task<bool> DeleteTransportSceneAsync(int warningId, int sceneId)
{
using var tran = TransactionHelper.GetReadCommitted();
await Context.Deleteable<TransportWarningSceneEntity>()
.Where(x => x.WarningId == warningId && x.Id == sceneId)
.ExecuteCommandAsync();
var r = await Context.Deleteable<TransportWarningSceneRuleItemEntity>()
.Where(x => x.TransportWarningSceneId == sceneId)
.ExecuteCommandHasChangeAsync();
tran.Complete();
return r;
}
#endregion
#region 回传时效预警规则
public Task<List<ReturnWarningRuleItemEntity>> GetReturnRuleItemsAsync(int warningId)
{
return Context.Queryable<ReturnWarningRuleItemEntity>()
.Where(x => x.WarningId == warningId)
.ToListAsync();
}
public Task<List<ReturnWarningRuleItemEntity>> GetReturnRuleItemsAsync(HashSet<int> ruleItemIds)
{
return Context.Queryable<ReturnWarningRuleItemEntity>()
.Where(x => ruleItemIds.Contains(x.Id))
.ToListAsync();
}
public Task<int> BulkInsertReturnRuleItemsAsync(List<ReturnWarningRuleItemEntity> entities)
{
return Context.Insertable(entities)
.ExecuteCommandAsync();
}
public async Task<int> BulkUpdateReturnRuleItemsAsync(List<ReturnWarningRuleItemEntity> entities)
{
return await Context.Updateable(entities)
.ExecuteCommandAsync();
}
public Task<int> DeleteReturnRuleItemsAsync(HashSet<int> ruleItemIds)
{
return Context.Deleteable<ReturnWarningRuleItemEntity>()
.Where(x => ruleItemIds.Contains(x.Id))
.ExecuteCommandAsync();
}
#endregion
#region 完整预警规则
public Task<List<CompleteWarningRuleItemEntity>> GetCompleteRuleItemsAsync(int warningId)
{
return Context.Queryable<CompleteWarningRuleItemEntity>()
.Where(x => x.WarningId == warningId)
.ToListAsync();
}
public Task<List<CompleteWarningRuleItemEntity>> GetCompleteRuleItemsAsync(HashSet<int> ruleItemIds)
{
return Context.Queryable<CompleteWarningRuleItemEntity>()
.Where(x => ruleItemIds.Contains(x.Id))
.ToListAsync();
}
public async Task<int> BulkUpdateCompleteRuleItemsAsync(List<CompleteWarningRuleItemEntity> entities)
{
return await Context.Updateable(entities)
.ExecuteCommandAsync();
}
public Task<int> BulkInsertCompleteRuleItemsAsync(List<CompleteWarningRuleItemEntity> entities)
{
return Context.Insertable(entities)
.ExecuteCommandAsync();
}
public Task<int> DeleteCompleteRuleItemsAsync(HashSet<int> ruleItemIds)
{
return Context.Deleteable<CompleteWarningRuleItemEntity>()
.Where(x => ruleItemIds.Contains(x.Id))
.ExecuteCommandAsync();
}
#endregion
}