using Newtonsoft.Json; using SJZY.Track.Domain.Aggregates.ForewarningAggregate; using SJZY.Track.Domain.Aggregates.KPIAreaAggregate; using SJZY.Track.Domain.Aggregates.OrderAggregate; using SJZY.Track.Domain.Dtos.Common; using SJZY.Track.Domain.Dtos.Forewarning; using SJZY.Track.Domain.Queries; using SJZY.Track.Domain.VEntities; namespace SJZY.Track.Repository.Queries; public class WarningQueries : ISugarRepository, IWarningQueries, ITransientDependency { public ISqlSugarClient Context { get; set; } = SqlsugarScopeBasic.GetContextSafely("default"); public async Task> GetWarningNotifyRulesAsync(List ruleIds) { return await Context.Queryable() .LeftJoin((w, wr) => w.Id == wr.WarningId) .LeftJoin((w, wr, a) => wr.AreaId == a.Id) .LeftJoin((w, wr, a, wn) => wr.WarningId == wn.WarningId && wr.StartTrackStatusId == wn.StartTrackStatusId && wr.EndTrackStatusId == wn.EndTrackStatusId) .LeftJoin((w, wr, a, wn, cus) => w.CustomerId == cus.Id) .LeftJoin((w, wr, a, wn, cus, system) => w.SubSystemId == system.Id) .Filter(null, true) // 支持历史规则 .Where((w, wr) => ruleIds.Contains(wr.Id)) .Select((w, wr, a, wn, cus, system) => new GetWarningNotifiesVo { SubSystemId = w.SubSystemId, SubSystemName = system.CnName, RuleId = wr.Id, WarningName = w.Name, CustomId = w.CustomerId, CustomName = cus.Name, TotalTimeSpan = wr.TotalTimeSpan, WarnTimeSpan = wr.WarnTimeSpan, WarnCount = wr.WarnCount, StartTrackStatus = wr.StartTrackStatus, EndTrackStatus = wr.EndTrackStatus, CargoType = wr.CargoType, Months = wr.Months, AreaName = a.Name, IsTransit = wr.IsTransit, LeavePort = wr.LeavePort, ProductCode = wr.ProductCode, NotifyMode = wn.NotifyMode, NotifyUser = wn.NotifyUser, FactorTypeSort = w.FactorTypeSort }) .Mapper(rule => { rule.NotifyUsers = JsonConvert.DeserializeObject>(rule.NotifyUser) ?? new List(); }) .ToListAsync(); } public Task?> GetWarningRule(int systemId, int customerId) { return Context.Queryable() .InnerJoin((a, b) => a.WarningId == b.Id) .Where((a, b) => b.IsEnable && b.SubSystemId == systemId && b.CustomerId == customerId) .Select((a, b) => new WarningRuleCacheDto { Id = a.Id, WarningId = a.WarningId, TotalTimeSpan = a.TotalTimeSpan, WarnTimeSpan = a.WarnTimeSpan, WarnCount = a.WarnCount, StartTrackStatusId = a.StartTrackStatusId, EndTrackStatusId = a.EndTrackStatusId, CargoType = a.CargoType, Months = a.Months, AreaId = a.AreaId, IsTransit = a.IsTransit, LeavePort = a.LeavePort, ProductCode = a.ProductCode, CustomerId = b.CustomerId, SystemId = b.SubSystemId }) .ToListAsync(); } public Task> GetWarningRuleByAreaId(int areaId) { return Context.Queryable() .InnerJoin((a, b) => a.WarningId == b.Id) .Where((a, b) => b.IsEnable && a.AreaId == areaId) .Select((a, b) => new RuleAreaBaseInfoVo { CustomerId = b.CustomerId, SystemId = b.SubSystemId }) .ToListAsync(); } public async Task> GetOrderNoByIdsAsync(int[] orderIds) { return await Context.Queryable() .Where(x => orderIds.Contains(x.Id)) .Select(x => new IdWithNameDto { Id = x.Id, Name = x.OrderNo }) .Filter(null, true) .ToListAsync(); } }