using System.Linq.Expressions; using TrackCenter.Domain; using TrackCenter.Domain.Aggregates.WarningAggregate; using TrackCenter.Domain.Aggregates.WarningAggregate.Complete; using TrackCenter.Domain.Aggregates.WarningAggregate.Return; using TrackCenter.Domain.Aggregates.WarningAggregate.Transport; using TrackCenter.Domain.Enums; using TrackCenter.Domain.Extensions; namespace TrackCenter.Repository.Repositories; public class WarningRuleRepository : BasicRepository, IWarningRuleRepository, ITransientDependency { #region 规则主体 public async Task CheckNameDuplicateAsync(int id, string appKey, string customer, string name, WarningRuleType warningType) { // 运输时效:AppKey + Name + Type(方案级,不按客户) var query = Context.Queryable() .Where(x => x.AppKey == appKey && x.WarningType == (int) warningType && string.Equals(x.Name, name)); if (warningType != WarningRuleType.运输时效预警) query = query.Where(x => x.Customer == customer); var entity = await query.FirstAsync(); // 不重复 if (entity == null) return false; // 如果是修改,则要求查出来的和要修改的是同一个,否则视为重复 return entity.Id != id; } public async Task CheckEnableDuplicateAsync(int warningId, string appKey, string customer, WarningRuleType warningType) { // 运输时效:同一 AppKey + 类型仅允许一个启用方案 var query = Context.Queryable() .Where(x => x.AppKey == appKey && x.WarningType == (int) warningType && x.IsEnabled); if (warningType != WarningRuleType.运输时效预警) query = query.Where(x => x.Customer == customer); var entity = await query.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(string? appKey, string? customer, string? kpiname, bool? isEnabled, int? warningType) { return Context.Queryable() .WhereIF(!customer.IsNullOrWhiteSpace(), x => x.Customer.Contains(customer!)) .WhereIF(!appKey.IsNullOrWhiteSpace(), x => x.AppKey == appKey) .WhereIF(warningType.HasValue, x => x.WarningType == warningType!.Value) .WhereIF(isEnabled.HasValue, x => x.IsEnabled == isEnabled!.Value) .WhereIF(!string.IsNullOrWhiteSpace(kpiname), x => x.Name.Contains(kpiname!)) .ToListAsync(); } public async Task GetWarningRuleForEnabledAsync(string appKey, string customer, WarningRuleType warningType) { var warningRules = await Context.Queryable() .Where(x => x.Customer == customer && x.AppKey == appKey && x.WarningType == (int) warningType && x.IsEnabled ) .ToListAsync(); return warningRules.FirstOrDefault(); } public async Task> GetWarningRuleForEnabledAsync(HashSet<(string AppKey, string Customer)> conditions, WarningRuleType warningType) { Expression> baseExp = x => x.WarningType == (int) warningType && x.IsEnabled; var exp = Expressionable.Create(); foreach (var condition in conditions) exp.Or(x => x.Customer == condition.Customer && x.AppKey == condition.AppKey); var list = await Context.Queryable() .Where(baseExp.And(exp.ToExpression())) .ToListAsync(); return list.Count == 0 ? Empty.Dict : list.ToDictionary(x => x.Id); } public async Task> GetWarningRuleForEnabledByAppKeysAsync(HashSet appKeys, WarningRuleType warningType) { if (appKeys.Count == 0) return Empty.Dict; var list = await Context.Queryable() .Where(x => x.WarningType == (int) warningType && x.IsEnabled && appKeys.Contains(x.AppKey)) .ToListAsync(); return list.Count == 0 ? Empty.Dict : list.ToDictionary(x => x.Id); } 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) { return Context.Queryable() .Where(whereExpression) .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 Task> GetCompleteRuleItemsAsync(Expression> whereExpression) { return Context.Queryable() .Where(whereExpression) .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 }