using System.Linq.Expressions; using Fake.Domain.Entities.Auditing; using TrackCenter.Domain.Aggregates.TrackContentAggregate; using TrackCenter.Domain.Aggregates.TrackPullAggregate; using TrackCenter.Domain.Aggregates.TrackStatusAggregate; using TrackCenter.Domain.Dtos.Common; using TrackCenter.Domain.Dtos.TrackPull; using TrackCenter.Domain.VEntities; namespace TrackCenter.Repository.Repositories; public class TrackPullConfigRepository : BasicRepository, ITrackPullConfigRepository, IScopedDependency { public async Task> GetPageAsync(KeywordsInputVO input) { return await Context.Queryable() .LeftJoin((trackPull, system) => trackPull.AppKey == system.AppKey) .LeftJoin((trackPull, system, service) => trackPull.ServiceProviderCode == service.Code) .WhereIF(!input.AppKey.IsNullOrWhiteSpace(), trackPull => trackPull.AppKey == input.AppKey) .WhereIF(!input.Keywords.IsNullOrWhiteSpace(), (trackPull, system, service) => service.Name.Contains(input.Keywords!)) .OrderByDescending(trackPull => trackPull.CreateTime) .Select((trackPull, system, service) => new GetTrackPullConfigPageOutputVO { Id = trackPull.Id, AppKey = system.AppKey, App = system.Name, ServiceProvider = service.Name, CreateTime = trackPull.CreateTime }) .ToPagedListAsync(input.PageIndex, input.PageSize); } public async Task> GetContentPageAsync( GetTrackPullContentPageInputVO input) { return await Context.Queryable() .InnerJoin((pullContent, trackContent) => pullContent.TrackContentId == trackContent.Id) .Where(pullContent => pullContent.TrackPullConfigId == input.TrackPullConfigId) .OrderBy(pullContent => pullContent.CreateTime) .Select( (pullContent, trackContent) => new GetTrackPullContentPageOutputVO { Id = pullContent.Id, TrackPlaceValue = pullContent.TrackPlaceValue }, true) .ToPagedListAsync(input.PageIndex, input.PageSize); } public async Task> GetContentListCacheAsync(int trackPullConfigId) { return await Context.Queryable() .InnerJoin((pullContent, trackContent) => pullContent.TrackContentId == trackContent.Id) .LeftJoin((pullContent, trackContent, trackStutas) => trackContent.TrackStatusId == trackStutas.Id) .Where(pullContent => pullContent.TrackPullConfigId == trackPullConfigId) .Select( (pullContent, trackContent, trackStutas) => new GetTrackPullContentListCacheVO { TrackContentCode = trackContent.Code, }, true) .Mapper(x => { x.TrackKeywords = x.TrackKeyword.IsNullOrWhiteSpace() ? new List() : x.TrackKeyword!.Split(',').ToList(); x.TrackKeywordReverses = x.TrackKeywordReverse.IsNullOrWhiteSpace() ? new List() : x.TrackKeywordReverse!.Split(',').ToList(); x.TrackStatusValues = x.TrackStatusValue.IsNullOrWhiteSpace() ? new List() : x.TrackStatusValue!.Split(',').ToList(); }) .ToListAsync(); } public async Task GetDetailAsync(int id) { return await AsQueryable() .LeftJoin((trackPull, system) => trackPull.AppKey == system.AppKey) .LeftJoin((trackPull, system, service) => trackPull.ServiceProviderCode == service.Code) .Where(trackPull => trackPull.Id == id) .Select( (trackPull, system, service) => new GetTrackPullConfigDetailOutputVO { Id = trackPull.Id, App = system.Name, AppKey = system.AppKey, ServiceProviderId = service.Id, ServiceProviderCode = service.Code, OrderNoName = trackPull.OrderNoName, ParseMethod = (int)trackPull.ParseMethod, TrackIsoTimeName = trackPull.TrackIsoTimeName, TrackTimeName = trackPull.TrackTimeName, TrackTimeZoneName = trackPull.TrackTimeZoneName, TrackContentCodeName = trackPull.TrackContentCodeName, TrackContentName = trackPull.TrackContentName, TrackPlaceName = trackPull.TrackPlaceName, TrackStatusName = trackPull.TrackStatusName, OrderPath = trackPull.OrderPath, TrackDetailsPath = trackPull.TrackDetailsPath, StartTrackCode = trackPull.StartTrackCode, EndTrackCode = trackPull.EndTrackCode, SilenceBeforeStart = trackPull.SilenceBeforeStart, SilenceAfterEnd = trackPull.SilenceAfterEnd }, true) .FirstAsync(); } public async Task GetContentDetailAsync(int id) { return await Context.Queryable() .InnerJoin((pullContent, trackContent) => pullContent.TrackContentId == trackContent.Id) .Where(pullContent => pullContent.Id == id) .Select( (pullContent, trackContent) => new GetTrackPullContentDetailOutputVO { Id = pullContent.Id, Code = trackContent.Code + "-" + trackContent.Content, TrackTimePlaceValue = pullContent.TrackZonePlaceValue, }, true) .Mapper(x => { x.TrackKeywords = x.TrackKeyword.IsNullOrWhiteSpace() ? new List() : x.TrackKeyword!.Split(',').ToList(); x.TrackKeywordReverses = x.TrackKeywordReverse.IsNullOrWhiteSpace() ? new List() : x.TrackKeywordReverse!.Split(',').ToList(); x.TrackStatusValues = x.TrackStatusValue.IsNullOrWhiteSpace() ? new List() : x.TrackStatusValue!.Split(',').ToList(); }) .SingleAsync(); } public async Task> GetPriorityPageAsync(GetTrackPullPriorityPageInputVO input) { return await Context.Queryable() .LeftJoin((x, creator) => x.CreateUserId == creator.UserId) .LeftJoin((x, creator, modifier) => x.UpdateUserId == modifier.UserId) .Where(x => x.TrackPullConfigId == input.TrackPullConfigId) .OrderByDescending(x => x.Priority) .Select((x, creator, modifier) => new GetTrackPullPriorityPageOutputVO { Id = x.Id, Customer = x.Customer, OrderType = x.OrderType, Priority = x.Priority, PullInterval = x.PullInterval, CreateTime = x.CreateTime, CreateUser = creator.Name, UpdateUser = modifier.Name, UpdateTime = x.UpdateTime }) .ToPagedListAsync(input.PageIndex, input.PageSize); } public async Task GetPriorityDetailAsync(int id) { return await Context.Queryable() .Where(x => x.Id == id) .Select(x => new GetTrackPullPriorityDetailOutputVO { Id = x.Id, TrackPullConfigId = x.TrackPullConfigId, Customer = x.Customer, OrderType = x.OrderType, Priority = x.Priority, PullInterval = x.PullInterval }) .SingleAsync(); } public async Task InsertContentAsync(TrackPullContentConfigEntity insertObj) { return await Context.Insertable(insertObj).ExecuteCommandAsync() > 0; } public async Task InsertPriorityAsync(TrackPullPriorityEntity insertObj) { return await Context.Insertable(insertObj).ExecuteCommandAsync() > 0; } public async Task SoftDeleteContentAsync(Expression> whereExpression) { return await Context.Updateable() .Where(whereExpression) .SetColumns(nameof(ISoftDelete.IsDeleted), true) .ExecuteCommandHasChangeAsync(); } public async Task SoftDeletePriorityAsync(Expression> whereExpression) { return await Context.Updateable() .Where(whereExpression) .SetColumns(nameof(ISoftDelete.IsDeleted), true) .ExecuteCommandHasChangeAsync(); } public async Task GetContentByIdAsync(int id) { return await Context.Queryable().InSingleAsync(id); } public async Task GetPriorityByIdAsync(int id) { return await Context.Queryable().InSingleAsync(id); } public async Task UpdateContentAsync(TrackPullContentConfigEntity updateObj) { return await Context.Updateable(updateObj).ExecuteCommandHasChangeAsync(); } public async Task UpdatePriorityAsync(TrackPullPriorityEntity updateObj) { return await Context.Updateable(updateObj).ExecuteCommandHasChangeAsync(); } public async Task IsContentAnyAsync(Expression> whereExpression) { return await Context.Queryable().Where(whereExpression).AnyAsync(); } public async Task IsPriorityAnyAsync(Expression> whereExpression) { return await Context.Queryable().Where(whereExpression).AnyAsync(); } public async Task FindIdAsync(string appKey, string serviceProviderCode) { return await AsQueryable() .Where(trackPull => trackPull.AppKey == appKey && trackPull.ServiceProviderCode == serviceProviderCode) .Select(trackPull => trackPull.Id) .FirstAsync(); } }