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, IWarningRuleRepository, ITransientDependency { #region 规则主体 public async Task CheckNameDuplicateAsync(int id, int subsystemId, int customId, string name) { var entity = await Context.Queryable() .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 CheckEnableDuplicateAsync(int warningId, int subsystemId, int customId, WarningRuleType warningType) { var entity = await Context.Queryable() .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 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 UpdateVersionAsync(WarningRuleEntity entity) { var version = VersionGenerate.GenVersion(); await Context.Updateable() .Where(x => x.Id == entity.Id) .SetColumns(x => x.Version, version) .ExecuteCommandHasChangeAsync(); return version; } public Task> GetWarningRuleListAsync(int? subsystemId, string? keywords, bool? isEnabled, int? warningType) { return Context.Queryable() .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 GetWarningRuleForEnabledAsync(int subSystemId, int customId, WarningRuleType warningType) { var warningRules = await Context.Queryable() .Where(x => x.CustomId == customId && x.SubSystemId == subSystemId && x.WarningType == (int) warningType && x.IsEnabled ) .ToListAsync(); return warningRules.FirstOrDefault(); } public async Task GetWarningRuleByIdAsync(int id, WarningRuleType? warningType = null) { var result = await Context.Queryable() .Where(x => x.Id == id) .WhereIF(warningType.HasValue, x => x.WarningType == (int) warningType!.Value) .ToListAsync(); return result.FirstOrDefault(); } #endregion #region 运输时效预警规则 public Task> GetTransportSceneAsync(int warningId) { return Context.Queryable() .Where(x => x.WarningId == warningId) .ToListAsync(); } public Task> GetTransportSceneAsync(Expression> whereExpression) { return Context.Queryable() .Where(whereExpression) .ToListAsync(); } public async Task GetTransportSceneAsync(int warningId, int sceneId) { var result = await Context.Queryable() .Where(x => x.WarningId == warningId && x.Id == sceneId) .ToListAsync(); return result.FirstOrDefault(); } public Task> GetTransportSceneRuleItemsAsync(int warningId, int sceneId) { return Context.Queryable() .Where(x => x.WarningId == warningId && x.TransportWarningSceneId == sceneId) .ToListAsync(); } public Task> GetTransportSceneRuleItemsAsync(Expression> whereExpression, Expression> resultExpression) { return Context.Queryable() .Where(whereExpression) .Select(resultExpression) .ToListAsync(); } public async Task 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 BulkUpdateTransportSceneRuleItemsAsync(List entities) { return await Context.Updateable(entities) .ExecuteCommandAsync(); } public Task BulkInsertTransportSceneRuleItemsAsync(List entities) { return Context.Insertable(entities) .ExecuteCommandAsync(); } public Task DeleteTransportSceneRuleItemsAsync(HashSet ruleItemIds) { return Context.Deleteable() .Where(x => ruleItemIds.Contains(x.Id)) .ExecuteCommandAsync(); } public async Task DeleteTransportSceneAsync(int warningId, int sceneId) { using var tran = TransactionHelper.GetReadCommitted(); await Context.Deleteable() .Where(x => x.WarningId == warningId && x.Id == sceneId) .ExecuteCommandAsync(); var r = await Context.Deleteable() .Where(x => x.TransportWarningSceneId == sceneId) .ExecuteCommandHasChangeAsync(); tran.Complete(); return r; } #endregion #region 回传时效预警规则 public Task> GetReturnRuleItemsAsync(int warningId) { return Context.Queryable() .Where(x => x.WarningId == warningId) .ToListAsync(); } public Task> GetReturnRuleItemsAsync(HashSet ruleItemIds) { return Context.Queryable() .Where(x => ruleItemIds.Contains(x.Id)) .ToListAsync(); } public Task BulkInsertReturnRuleItemsAsync(List entities) { return Context.Insertable(entities) .ExecuteCommandAsync(); } public async Task BulkUpdateReturnRuleItemsAsync(List entities) { return await Context.Updateable(entities) .ExecuteCommandAsync(); } public Task DeleteReturnRuleItemsAsync(HashSet ruleItemIds) { return Context.Deleteable() .Where(x => ruleItemIds.Contains(x.Id)) .ExecuteCommandAsync(); } #endregion #region 完整预警规则 public Task> GetCompleteRuleItemsAsync(int warningId) { return Context.Queryable() .Where(x => x.WarningId == warningId) .ToListAsync(); } public Task> GetCompleteRuleItemsAsync(HashSet ruleItemIds) { return Context.Queryable() .Where(x => ruleItemIds.Contains(x.Id)) .ToListAsync(); } public async Task BulkUpdateCompleteRuleItemsAsync(List entities) { return await Context.Updateable(entities) .ExecuteCommandAsync(); } public Task BulkInsertCompleteRuleItemsAsync(List entities) { return Context.Insertable(entities) .ExecuteCommandAsync(); } public Task DeleteCompleteRuleItemsAsync(HashSet ruleItemIds) { return Context.Deleteable() .Where(x => ruleItemIds.Contains(x.Id)) .ExecuteCommandAsync(); } #endregion }