track/src/SJZY.Track.Repository/Repositories/TrackContentRepository.cs
2024-08-20 09:07:19 +08:00

252 lines
13 KiB
C#

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.Dtos.TrackTemplate;
using SJZY.Track.Domain.VEntities;
namespace SJZY.Track.Repository.Repositories;
public class TrackContentRepository : BasicRepository<TrackContentEntity>, ITrackContentRepository, IScopedDependency
{
public TrackContentRepository()
{
}
/// <summary>
/// 获取轨迹内容明细
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public async Task<GetTrackContentDetailVO?> GetDetailAsync(int id)
{
var detail = await Context.Queryable<TrackContentEntity>()
.LeftJoin<TrackStatusEntity>((a, b) => a.TrackStatusId == b.Id && b.IsDeleted == false)
.LeftJoin<TrackFlagEntity>((a, b, c) => a.TrackFlagId == c.Id && c.IsDeleted == false)
.LeftJoin<VSystemEntity>((a, b, c, d) => a.SubSystemId == d.Id && d.IsDeleted == false)
.Where((a, b, c, d) => a.Id == id)
.Select((a, b, c, 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,
TrackFlag = c.Name
})
.FirstAsync();
return detail;
}
/// <summary>
/// 分页获取轨迹内容
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task<UnifyPageResultDto<GetTrackContentListVO>> GetPageTrackContent(GetPageTrackContentVO input)
{
UnifyPageResultDto<GetTrackContentListVO> pagedList = new UnifyPageResultDto<GetTrackContentListVO>();
pagedList = await Context.Queryable<TrackContentEntity>()
.LeftJoin<TrackStatusEntity>((a, b) => a.TrackStatusId == b.Id && b.IsDeleted == false)
.LeftJoin<TrackFlagEntity>((a, b, c) => a.TrackFlagId == c.Id && c.IsDeleted == false)
.LeftJoin<VSystemEntity>((a, b, c, d) => a.SubSystemId == d.Id && d.IsDeleted == false)
.LeftJoin<VUserEntity>((a, b, c, d, e) => a.CreateUserId == e.Id && e.IsDeleted == false)
.LeftJoin<VUserEntity>((a, b, c, d, e, f) => a.UpdateUserId == f.Id && f.IsDeleted == false)
.WhereIF(input.SubSystemId > 0, (a, b, c, d, e, f) => a.SubSystemId == input.SubSystemId)
.WhereIF(!input.Keywords.IsNullOrWhiteSpace(),
(a, b, c, d, e, f) => a.Code.Contains(input.Keywords) || a.Content.Contains(input.Keywords)
|| a.EnContent.Contains(input.Keywords))
.WhereIF(input.TrackStatusId > 0, (a, b, c, d, e, f) => a.TrackStatusId == input.TrackStatusId)
.OrderByIF(input.SelectIds.IsNullOrEmpty(), (a, b, c, d, e, f) => a.CreateTime, OrderByType.Desc)
.OrderByIF(!input.SelectIds.IsNullOrEmpty(),
(a, b, c, d, e, f) => SqlFunc.IIF(input.SelectIds.Contains(a.Id), SqlFunc.DateAdd(SqlFunc.GetDate(), 1),
a.CreateTime), OrderByType.Desc)
.Select((a, b, c, d, e, f) => new GetTrackContentListVO
{
Id = a.Id,
SubSystem = d.CnName,
Code = a.Code,
Content = a.Content,
EnContent = a.EnContent,
TrackStatus = b.Status,
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 = c.Name,
CreateTime = a.CreateTime,
CreateUser = e.Name,
UpdateTime = a.UpdateTime,
UpdateUser = f.Name,
})
.ToPagedListAsync(input.PageIndex, input.PageSize);
return pagedList;
}
/// <summary>
/// 获取轨迹内容属性值
/// </summary>
/// <returns></returns>
public async Task<List<GetTemplateContentAttributeValueVO>> GetTrackContentAttributeValue(
GetTemplateContentAttributeValueInputVO input)
{
var list = await Context.Queryable<TrackContentEntity>()
.LeftJoin<TrackTemplateContentEntity>((a, b) =>
a.Id == b.TrackContentId && b.TrackTemplateId > 0 && b.IsDeleted == false)
.LeftJoin<TrackTemplateEntity>((a, b, c) => b.TrackTemplateId == c.Id && c.IsDeleted == false)
.WhereIF(!input.TrackCodes.IsNullOrEmpty(), (a, b, c) => input.TrackCodes!.Contains(a.Code))
.WhereIF(input.SubSystemId > 0, (a, b, c) => a.SubSystemId == input.SubSystemId)
.WhereIF(!input.TrackTemplateIds.IsNullOrEmpty(), (a, b, c) => input.TrackTemplateIds!.Contains(c.Id))
.Where((a, b, c) => c.Id > 0)
//.OrderByIF(!input.TrackTemplateIds.IsNullOrEmpty(), (a, b, c) => SqlFunc.IIF(input.TrackTemplateIds.Contains(a.Id), SqlFunc.DateAdd(SqlFunc.GetDate(), 1), c.CreateTime), OrderByType.Desc)
.Select((a, b, c) => new GetTemplateContentAttributeValueVO
{
IsCanDelete = b.IsCanDelete,
IsExternal = b.IsExternal,
SubSystemId = a.SubSystemId,
TrackCode = a.Code,
TrackContenId = a.Id,
TrackTemplateId = c.Id
}).ToListAsync();
return list;
}
/// <summary>
/// 获取轨迹模版是否存在
/// </summary>
/// <param name="trackContentId"></param>
/// <returns></returns>
public async Task<bool> GetTemplateIsExist(int trackContentId)
{
//var trackTemplateContents = await _trackTemplateContentBasicQuery.GetListAsync(a => a.TrackContentId == input.Id);
var trackContentEntities = await Context.Queryable<TrackContentEntity>()
.LeftJoin<TrackTemplateContentEntity>((a, b) => a.Id == b.TrackContentId && b.IsDeleted == false)
.LeftJoin<TrackTemplateEntity>((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<UnifyPageResultDto<GetTrackContentMapPageOutputVO>> GetMapPageAsync(
GetTrackContentMapPageInputVO input)
{
return await Context.Queryable<TrackContentMapEntity>()
.InnerJoin<TrackContentEntity>((contentMap, sourceContent) =>
contentMap.TrackContentId == sourceContent.Id && !sourceContent.IsDeleted)
.InnerJoin<TrackContentEntity>((contentMap, sourceContent, targetContent) =>
contentMap.MapTrackContentId == targetContent.Id && !targetContent.IsDeleted)
.LeftJoin<VSystemEntity>((contentMap, sourceContent, targetContent, sourceSystem) =>
contentMap.SubSystemId == sourceSystem.Id && !sourceSystem.IsDeleted)
.LeftJoin<VSystemEntity>((contentMap, sourceContent, targetContent, sourceSystem, targetSystem) =>
contentMap.MapSubSystemId == targetSystem.Id && !targetSystem.IsDeleted)
.LeftJoin<TrackStatusEntity>(
(contentMap, sourceContent, targetContent, sourceSystem, targetSystem, sourceStatus) =>
sourceContent.TrackStatusId == sourceStatus.Id && !sourceStatus.IsDeleted)
.LeftJoin<TrackStatusEntity>(
(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<GetTrackContentMapDetailOutputVO> GetMapDetailAsync(int id)
{
return await Context.Queryable<TrackContentMapEntity>()
.InnerJoin<TrackContentEntity>((contentMap, sourceContent) =>
contentMap.TrackContentId == sourceContent.Id && !sourceContent.IsDeleted)
.InnerJoin<TrackContentEntity>((contentMap, sourceContent, targetContent) =>
contentMap.MapTrackContentId == targetContent.Id && !targetContent.IsDeleted)
.LeftJoin<VSystemEntity>((contentMap, sourceContent, targetContent, sourceSystem) =>
contentMap.SubSystemId == sourceSystem.Id && !sourceSystem.IsDeleted)
.LeftJoin<VSystemEntity>((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<bool> IsMapAnyAsync(Expression<Func<TrackContentMapEntity, bool>> whereExpression)
{
return await Context.Queryable<TrackContentMapEntity>().Where(whereExpression).AnyAsync();
}
public async Task<bool> InsertMapAsync(TrackContentMapEntity insertObj)
{
return await Context.Insertable(insertObj).ExecuteCommandAsync() > 0;
}
public async Task<TrackContentMapEntity> GetMapByIdAsync(int id)
{
return await Context.Queryable<TrackContentMapEntity>().InSingleAsync(id);
}
public async Task<bool> UpdateMapAsync(TrackContentMapEntity updateObj)
{
return await Context.Updateable(updateObj).ExecuteCommandHasChangeAsync();
}
public async Task<bool> SoftDeleteMapAsync(Expression<Func<TrackContentMapEntity, bool>> whereExpression)
{
return await Context.Updateable<TrackContentMapEntity>()
.Where(whereExpression)
.SetColumns(nameof(ISoftDeleteBasicEntity.IsDeleted), true)
.ExecuteCommandHasChangeAsync();
}
public async Task<bool> UpdateMapAsync(Expression<Func<TrackContentMapEntity, TrackContentMapEntity>> columns,
Expression<Func<TrackContentMapEntity, bool>> whereExpression)
{
return await Context.Updateable<TrackContentMapEntity>().SetColumns(columns).Where(whereExpression)
.ExecuteCommandAsync() > 0;
}
}