using SJZY.Track.Domain.Aggregates.OrderAggregate; using SJZY.Track.Domain.Aggregates.TrackContentAggregate; using SJZY.Track.Domain.Aggregates.TrackFlagAggregate; using SJZY.Track.Domain.Aggregates.TrackStatusAggregate; using SJZY.Track.Domain.Aggregates.TrackTemplateAggregate; using SJZY.Track.Domain.Dtos.Order; using SJZY.Track.Domain.Dtos.TrackDetails; using SJZY.Track.Domain.Dtos.TrackTemplate; using SJZY.Track.Domain.Queries; namespace SJZY.Track.Repository.Queries; public class TrackQueries : ISugarRepository, ITrackQueries, ITransientDependency { public ISqlSugarClient Context { get; set; } public TrackQueries() { Context = SqlsugarScopeBasic.GetContextSafely("default"); } public async Task> GetOrderTemplateLinksAsync(int orderType, params string[] orderNumbers) { return await Context.Queryable() .LeftJoin((a, b) => a.Id == b.OrderId) .Where(a => a.OrderType == orderType && orderNumbers.Contains(a.OrderNo)) .Select((a, b) => new OrderTemplateLinkDto { OrderNo = a.OrderNo, TemplateId = b.TrackTemplateId }) .ToListAsync(); } public async Task> GetOrderTemplateLinksAsync(int orderType, List orderNumbers) { return await Context.Queryable() .LeftJoin((a, b) => a.Id == b.OrderId) .Where(a => a.OrderType == orderType && orderNumbers.Contains(a.OrderNo)) .Select((a, b) => new OrderTemplateLinkDto { OrderNo = a.OrderNo, TemplateId = b.TrackTemplateId }) .ToListAsync(); } public async Task> GetOrderedTrackCodesAsync(List templateIds) { return await Context.Queryable() .LeftJoin((a, b) => a.TrackContentId == b.Id) .Where(a => templateIds.Contains(a.TrackTemplateId)) .OrderBy(a => a.Sort) .Select((a, b) => new OrderedTrackCodeDto { TemplateId = a.Id, Code = b.Code }) .ToListAsync(); } public async Task MatchTrackContentAsync(int orderId, string code) { return await Context.Queryable() .LeftJoin((x, a) => x.TrackTemplateId == a.TrackTemplateId) .LeftJoin((x, a, b) => a.TrackContentId == b.Id) .LeftJoin((x, a, b, c) => b.TrackStatusId == c.Id) .LeftJoin((x, a, b, c, d) => b.TrackFlagId == d.Id) .Where((x, a, b) => x.OrderId == orderId && b.Code == code) .Select((x, a, b, c,d) => new MatchTrackContentDto { Content = b.Content, EnContent = b.EnContent, Status = c.Status, IsCompleted = b.IsCompleted }) .FirstAsync(); } public async Task MapTrackContentCodeAsync(int upSubSystemId, int subSystemId, string trackCode) { return await Context.Queryable() .Where(x => x.SubSystemId == upSubSystemId && x.MapSubSystemId == subSystemId && x.TrackContentCode == trackCode) .Select(x => x.MapTrackContentCode) .FirstAsync(); } }