using System.Linq.Expressions; 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.TrackContents; using SJZY.Track.Domain.VEntities; namespace SJZY.Track.Repository.Repositories; public class TrackContentRepository : BasicRepository, ITrackContentRepository, IScopedDependency { /// /// 获取轨迹内容明细 /// /// /// public async Task GetDetailAsync(int id) { var detail = await Context.Queryable() .LeftJoin((a, b) => a.TrackStatusId == b.Id && b.IsDeleted == false) .LeftJoin((a, b, d) => a.SubSystemId == d.Id && d.IsDeleted == false) .Where((a, b, d) => a.Id == id) .Select((a, b, d) => new GetTrackContentDetailVO { Id = a.Id, SubSystemId = a.SubSystemId, SubSystem = d.CnName, TrackStatusId = a.TrackStatusId, TrackStatus = b.Status, Code = a.Code, Content = a.Content, EnContent = a.EnContent, TrackFlagId = a.TrackFlagId, IsExternal = a.IsExternal, IsRepeatable = a.IsRepeatable, IsAllowManuallyUpdate = a.IsAllowManuallyUpdate, IsAllowManuallyCreate = a.IsAllowManuallyCreate }) .Mapper(x => { x.TrackFlagIds = x.TrackFlagId?.Split(',').Select(int.Parse).ToList(); }) .FirstAsync(); return detail; } /// /// 分页获取轨迹内容 /// /// /// public async Task> GetPageTrackContent(GetPageTrackContentVO input) { var pagedList = await Context.Queryable() .LeftJoin((a, b) => a.TrackStatusId == b.Id && b.IsDeleted == false) .LeftJoin((a, b, d) => a.SubSystemId == d.Id && d.IsDeleted == false) .LeftJoin((a, b, d, e) => a.CreateUserId == e.Id && e.IsDeleted == false) .LeftJoin((a, b, d, e, f) => a.UpdateUserId == f.Id && f.IsDeleted == false) .WhereIF(input.SubSystemId > 0, (a, b, d, e, f) => a.SubSystemId == input.SubSystemId) .WhereIF(!input.Keywords.IsNullOrWhiteSpace(), (a, b, d, e, f) => a.Code.Contains(input.Keywords) || a.Content.Contains(input.Keywords) || a.EnContent.Contains(input.Keywords)) .WhereIF(input.TrackStatusId > 0, (a, b, d, e, f) => a.TrackStatusId == input.TrackStatusId) .OrderByIF(input.SelectIds.IsNullOrEmpty(), (a, b, d, e, f) => a.CreateTime, OrderByType.Desc) .OrderByIF(!input.SelectIds.IsNullOrEmpty(), (a, b, d, e, f) => SqlFunc.IIF(input.SelectIds.Contains(a.Id), SqlFunc.DateAdd(SqlFunc.GetDate(), 1), a.CreateTime), OrderByType.Desc) .Select((a, b, d, e, f) => new GetTrackContentListVO { Id = a.Id, SubSystem = d.CnName, Code = a.Code, Content = a.Content, EnContent = a.EnContent, TrackStatus = b.Status, IsExternal = a.IsExternal, Node = SqlFunc.IF(b.Node == 1).Return("揽收").ElseIF(b.Node == 2).Return("清关").ElseIF(b.Node == 3) .Return("干线") .ElseIF(b.Node == 4).Return("派送").ElseIF(b.Node == 5).Return("其他") .End(""), TrackFlag = SqlFunc.Subqueryable() .Where(x => SqlFunc.SplitIn(a.TrackFlagId, x.Id.ToString())) .SelectStringJoin(x => x.Name, ","), CreateTime = a.CreateTime, CreateUser = e.Name, UpdateTime = a.UpdateTime, UpdateUser = f.Name, IsRepeatable = a.IsRepeatable, IsAllowManuallyUpdate = a.IsAllowManuallyUpdate, IsAllowManuallyCreate = a.IsAllowManuallyCreate }) .ToPagedListAsync(input.PageIndex, input.PageSize); return pagedList; } /// /// 获取轨迹模版是否存在 /// /// /// public async Task GetTemplateIsExist(int trackContentId) { //var trackTemplateContents = await _trackTemplateContentBasicQuery.GetListAsync(a => a.TrackContentId == input.Id); var trackContentEntities = await Context.Queryable() .LeftJoin((a, b) => a.Id == b.TrackContentId && b.IsDeleted == false) .LeftJoin((a, b, c) => b.TrackTemplateId == c.Id && c.IsDeleted == false) .Where((a, b, c) => a.Id == trackContentId) .Select((a, b, c) => new { TrackContentId = a.Id, TemplateId = c.Id }) .ToListAsync(); if (trackContentEntities.Any(a => a.TemplateId > 0)) { return true; } return false; } public async Task> GetMapPageAsync( GetTrackContentMapPageInputVO input) { return await Context.Queryable() .InnerJoin((contentMap, sourceContent) => contentMap.TrackContentId == sourceContent.Id && !sourceContent.IsDeleted) .InnerJoin((contentMap, sourceContent, targetContent) => contentMap.MapTrackContentId == targetContent.Id && !targetContent.IsDeleted) .LeftJoin((contentMap, sourceContent, targetContent, sourceSystem) => contentMap.SubSystemId == sourceSystem.Id && !sourceSystem.IsDeleted) .LeftJoin((contentMap, sourceContent, targetContent, sourceSystem, targetSystem) => contentMap.MapSubSystemId == targetSystem.Id && !targetSystem.IsDeleted) .LeftJoin( (contentMap, sourceContent, targetContent, sourceSystem, targetSystem, sourceStatus) => sourceContent.TrackStatusId == sourceStatus.Id && !sourceStatus.IsDeleted) .LeftJoin( (contentMap, sourceContent, targetContent, sourceSystem, targetSystem, sourceStatus, targetStatus) => targetContent.TrackStatusId == targetStatus.Id && !targetStatus.IsDeleted) .Filter(null, true) .Where(contentMap => !contentMap.IsDeleted) .WhereIF(input.SubSystemId.HasValue, contentMap => contentMap.SubSystemId == input.SubSystemId) .WhereIF(input.MapSubSystemId.HasValue, contentMap => contentMap.MapSubSystemId == input.MapSubSystemId) .WhereIF(!input.Keywords.IsNullOrWhiteSpace(), (contentMap, sourceContent) => sourceContent.Code.Contains(input.Keywords) || sourceContent.Content.Contains(input.Keywords) || sourceContent.EnContent.Contains(input.Keywords)) .OrderByDescending(contentMap => contentMap.CreateTime) .Select( (contentMap, sourceContent, targetContent, sourceSystem, targetSystem, sourceStatus, targetStatus) => new GetTrackContentMapPageOutputVO { Id = contentMap.Id, SubSystem = sourceSystem.CnName, Code = contentMap.TrackContentCode, TrackStatus = sourceStatus.Status, Content = sourceContent.Content, EnContent = sourceContent.EnContent, MapSubSystem = targetSystem.CnName, MapCode = contentMap.MapTrackContentCode, MapTrackStatus = targetStatus.Status, MapContent = targetContent.Content, MapEnContent = targetContent.EnContent }) .ToPagedListAsync(input.PageIndex, input.PageSize); } public async Task GetMapDetailAsync(int id) { return await Context.Queryable() .InnerJoin((contentMap, sourceContent) => contentMap.TrackContentId == sourceContent.Id && !sourceContent.IsDeleted) .InnerJoin((contentMap, sourceContent, targetContent) => contentMap.MapTrackContentId == targetContent.Id && !targetContent.IsDeleted) .LeftJoin((contentMap, sourceContent, targetContent, sourceSystem) => contentMap.SubSystemId == sourceSystem.Id && !sourceSystem.IsDeleted) .LeftJoin((contentMap, sourceContent, targetContent, sourceSystem, targetSystem) => contentMap.MapSubSystemId == targetSystem.Id && !targetSystem.IsDeleted) .Filter(null, true) .Where(contentMap => contentMap.Id == id && !contentMap.IsDeleted) .Select((contentMap, sourceContent, targetContent, sourceSystem, targetSystem) => new GetTrackContentMapDetailOutputVO { Id = contentMap.Id, SubSystemId = contentMap.SubSystemId, SubSystem = sourceSystem.CnName, TrackContent = contentMap.TrackContentCode + "-" + sourceContent.Content, MapSubSystem = targetSystem.CnName, MapTrackContent = contentMap.MapTrackContentCode + "-" + targetContent.Content }, true) .SingleAsync(); } public async Task IsMapAnyAsync(Expression> whereExpression) { return await Context.Queryable().Where(whereExpression).AnyAsync(); } public async Task InsertMapAsync(TrackContentMapEntity insertObj) { return await Context.Insertable(insertObj).ExecuteCommandAsync() > 0; } public async Task GetMapByIdAsync(int id) { return await Context.Queryable().InSingleAsync(id); } public async Task UpdateMapAsync(TrackContentMapEntity updateObj) { return await Context.Updateable(updateObj).ExecuteCommandHasChangeAsync(); } public async Task SoftDeleteMapAsync(Expression> whereExpression) { return await Context.Updateable() .Where(whereExpression) .SetColumns(nameof(ISoftDeleteBasicEntity.IsDeleted), true) .ExecuteCommandHasChangeAsync(); } public async Task UpdateMapAsync(Expression> columns, Expression> whereExpression) { return await Context.Updateable().SetColumns(columns).Where(whereExpression) .ExecuteCommandAsync() > 0; } public Task GetByCodeAsync(int systemId, string code) { return Context.Queryable().FirstAsync(m => m.SubSystemId == systemId && m.Code == code); } public Task> GetListBySystemIdAsync(int systemId) { return Context.Queryable() .Where(content => content.SubSystemId == systemId && !content.IsDeleted) .Select((content) => new TrackContentEntity { Id = content.Id, Code = content.Code, IsExternal = content.IsExternal, EnContent = content.EnContent, Content = content.Content, IsRepeatable = content.IsRepeatable, IsAllowManuallyCreate = content.IsAllowManuallyCreate, IsAllowManuallyUpdate = content.IsAllowManuallyUpdate, }) .ToListAsync(); } public Task> GetTrackContentBySyetemIdAsync(int subSystemId) { return Context.Queryable() .LeftJoin((x, y) => x.TrackStatusId == y.Id) .LeftJoin((x, y, z) => x.SubSystemId == z.Id) .Where(x => x.SubSystemId == subSystemId) .Select((x, y, z) => new TrackContentVO { SubSystemCode = z.Key, TrackStatusId = x.TrackStatusId, TrackStatus = y.Status, TrackStatusCode = y.Code, TrackContentId = x.Id, TrackContent = x.Content, TrackContentCode = x.Code, TrackEnContent = x.EnContent, IsCompleted = y.IsCompleted, IsRepeatable = x.IsRepeatable, IsAllowManuallyUpdate = x.IsAllowManuallyUpdate, IsAllowManuallyCreate = x.IsAllowManuallyCreate, IsToMq = y.IsToMq, IsExternal = x.IsExternal }).ToListAsync(); } }