track/src/SJZY.Track.Application/Services/Warning/CompleteWarningService.cs
hardstylewyl a4c5ad619c fix:修复预警明细的排序逻辑
fix:修复预警主体重复性判断增加预警类型
2025-03-24 16:23:25 +08:00

253 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using SJZY.Expand.ABP.Core.PublicMethords;
using SJZY.Track.Application.Contract.Dtos.Warning;
using SJZY.Track.Application.Contract.Dtos.Warning.Complete;
using SJZY.Track.Application.Contract.Dtos.Warning.Return;
using SJZY.Track.Application.Helpers;
using SJZY.Track.Application.Services.Warning.Handlers;
using SJZY.Track.Application.Services.Warning.Model;
using SJZY.Track.Domain.Aggregates.OrderAggregate;
using SJZY.Track.Domain.Aggregates.TrackDetailsAggregate;
using SJZY.Track.Domain.Aggregates.TrackStatusAggregate;
using SJZY.Track.Domain.Aggregates.WarningAggregate;
using SJZY.Track.Domain.Aggregates.WarningAggregate.Complete;
using SJZY.Track.Domain.Dtos.Warning;
using SJZY.Track.Domain.Enums;
using SJZY.Track.Domain.Extensions;
using SJZY.Track.Domain.Queries;
namespace SJZY.Track.Application.Services.Warning;
/// <summary>
/// 完整预警服务
/// </summary>
[ApiExplorerSettings(GroupName = "预警服务")]
[Route("Warning/Complete")]
public class CompleteWarningService : ApplicationService
{
private readonly IOrderRepository _orderRepository;
private readonly IVExternalSubjectQueries _vExternalSubjectQueries;
private readonly IVSystemQueries _vSystemQueries;
private readonly IWarningRuleRepository _warningRuleRepository;
private readonly IWarningDetailsEsRepository _warningDetailsEsRepository;
private readonly ITrackDetailsEsRepository _trackDetailsEsRepository;
private readonly ICompleteWarningHandler _completeWarningHandler;
private readonly ITrackStatusRepository _trackStatusRepository;
public CompleteWarningService(IWarningRuleRepository warningRuleRepository, IWarningDetailsEsRepository warningDetailsEsRepository, ITrackDetailsEsRepository trackDetailsEsRepository, ICompleteWarningHandler completeWarningHandler, ITrackStatusRepository trackStatusRepository, IOrderRepository orderRepository, IVSystemQueries vSystemQueries, IVExternalSubjectQueries vExternalSubjectQueries)
{
_warningRuleRepository = warningRuleRepository;
_warningDetailsEsRepository = warningDetailsEsRepository;
_trackDetailsEsRepository = trackDetailsEsRepository;
_completeWarningHandler = completeWarningHandler;
_trackStatusRepository = trackStatusRepository;
_orderRepository = orderRepository;
_vSystemQueries = vSystemQueries;
_vExternalSubjectQueries = vExternalSubjectQueries;
}
/// <summary>
/// 获取完整预警规则列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("GetCompleteWarningRuleItem")]
public async Task<UnifyResultDto<List<CompleteWarningRuleItemDto>>> GetCompleteWarningRuleItem(GetCompleteWarningRuleItemInput input)
{
var result = await _warningRuleRepository.GetCompleteRuleItemsAsync(input.WarningId);
if (result.Count == 0)
return new UnifyResultDto<List<CompleteWarningRuleItemDto>>(true, new List<CompleteWarningRuleItemDto>());
// todo:缓存优化
var trackStatusIds = result
.SelectMany(x =>
{
// todo:逻辑优化
var checkStatusId = x.CheckPointTrackStatusId;
var requireStatusIds = x.GetRequireTrackStatusIdGroups();
requireStatusIds.Add(new HashSet<int>(1)
{
checkStatusId
});
return requireStatusIds.SelectMany(y => y);
}).ToHashSet();
var trackStatusDict = (await _trackStatusRepository.GetListAsync(x => trackStatusIds.Contains(x.Id), x => new
{
x.Id,
x.Status
})).ToDictionary(x => x.Id, y => y.Status);
// 查订单类型映射
var orderTypeDict = await _orderRepository.GetTrackOrderTypeDictAsync();
var map = result.Select(x => new CompleteWarningRuleItemDto
{
Id = x.Id,
WarningId = x.WarningId,
OrderTypes = BuildOrderTypes(x),
CheckPointTrackStatusId = x.CheckPointTrackStatusId,
CheckPointTrackStatus = trackStatusDict.GetValueOrDefault(x.CheckPointTrackStatusId, GlobalConst.EmptyString),
RequireTrackStatus = BuildRequireTrackStatus(x),
NotifyUsers = TextJsonHelper.Deserialize<List<NotifyUserDto>>(x.NotifyUsers)!
});
return new UnifyResultDto<List<CompleteWarningRuleItemDto>>(true, map.ToList());
List<RequireTrackStatusItem> BuildRequireTrackStatus(CompleteWarningRuleItemEntity entity)
{
return entity.GetRequireTrackStatusIdGroups()
.Select(g => new RequireTrackStatusItem
{
Statuses = g.Select(id => new TrackStatusDto
{
TrackStatusId = id,
TrackStatus = trackStatusDict.GetValueOrDefault(id, GlobalConst.EmptyString)
}).ToList()
}).ToList();
}
List<OrderTypeDto> BuildOrderTypes(CompleteWarningRuleItemEntity entity)
{
return entity.GetRuleOrderTypes()
.Select(x => new OrderTypeDto
{
OrderType = x,
OrderTypeName = orderTypeDict.GetValueOrDefault(x.ToString(), GlobalConst.EmptyString)
}).ToList();
}
}
/// <summary>
/// 分页获取完整预警详情
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("GetPageCompleteWarningDetails")]
public async Task<UnifyPageResultDto<CompleteWarningDetailsDto>> GetPageCompleteWarningDetails(GetPageCompleteWarningDetailsInput input)
{
var vo = ObjectMapper.Map<GetPageCompleteWarningDetailsInput, GetPageCompleteWarningDetailsInputVO>(input);
var result = await _warningDetailsEsRepository.GetPageCompleteWarningDetailsAsync(vo);
if (result.List.Count == 0)
return new UnifyPageResultDto<CompleteWarningDetailsDto>(new List<CompleteWarningDetailsDto>(), input.PageIndex, input.PageSize, 0);
// todo:缓存优化
var trackStatusIds = result.List
.SelectMany(x => x.MissingTrackStatusIds.Split(',').Select(int.Parse))
.ToHashSet();
var trackStatusDict = (await _trackStatusRepository.GetListAsync(x => trackStatusIds.Contains(x.Id), x => new
{
x.Id,
x.Status
})).ToDictionary(x => x.Id, y => y.Status);
// 查最新轨迹
var orderIds = result.List.Select(x => x.OrderId).ToHashSet();
var orderIdTrackDict = await _trackDetailsEsRepository.GetLatestDictAsync(orderIds);
// todo:缓存优化
// 订单类型
var orderTypeDict = await _orderRepository.GetTrackOrderTypeDictAsync();
// 客户简称
var customIds = result.List.Select(x => x.CustomId).ToHashSet();
var customDict = (await _vExternalSubjectQueries
.GetListAsync(x => customIds.Contains(x.Id), x => new
{
x.Id,
x.Abbreviation
})).ToDictionary(x => x.Id, x => x.Abbreviation);
// 子系统
var subSystemIds = result.List.Select(x => x.SubSystemId).ToHashSet();
var subSystemDict = (await _vSystemQueries
.GetListAsync(x => subSystemIds.Contains(x.Id), x => new
{
x.Id,
x.CnName
})).ToDictionary(x => x.Id, x => x.CnName);
var map = result.List.Select(x =>
{
var orderIdTrack = orderIdTrackDict.GetValueOrDefault(x.OrderId);
return new CompleteWarningDetailsDto
{
EsId = x.Id,
SubSystemId = x.SubSystemId,
SubSystemName = subSystemDict.GetValueOrDefault(x.SubSystemId, GlobalConst.EmptyString),
OrderType = x.OrderType,
OrderTypeName = orderTypeDict.GetValueOrDefault(x.OrderType.ToString(), GlobalConst.EmptyString),
OrderId = x.OrderId,
OrderNo = x.OrderNo,
Status = x.Status,
CustomId = x.CustomId,
CustomName = customDict.GetValueOrDefault(x.CustomId, GlobalConst.EmptyString),
MissingTrackStatus = BuildMissingTrackStatus(x.MissingTrackStatusIds),
LatestTrackStatus = orderIdTrack?.TrackStatus,
LatestTrackContent = orderIdTrack?.TrackContent,
LatestTrackPlace = orderIdTrack?.TrackPlace,
LatestTrackTime = orderIdTrack?.TrackTime,
LatestTrackTimeZone = orderIdTrack?.TrackTimeZone,
NotifyUserNames = ToUserNames(x.NotifyUsers),
CreateTime = x.CreateTime,
ProcessedTime = x.ProcessedTime
};
});
// 按状态组+组内时间降序,分割状态组 g1:(待处理) g2:(已处理/已关闭) 确保g1总是在g2之前
var sorted = map
.OrderBy(x => x.Status == (int) CompleteWarningStatus.待处理 ? 1 : 2)
.ThenByDescending(x => x.CreateTime); //同状态组按时间降序;
return new UnifyPageResultDto<CompleteWarningDetailsDto>(
sorted.ToList(),
result.Pagination.PageIndex,
result.Pagination.PageSize,
result.Pagination.Total);
// 通知用户提取用户名称
string ToUserNames(string notifyUsers)
{
if (string.IsNullOrWhiteSpace(notifyUsers))
return string.Empty;
return string.Join(',', notifyUsers.ToObject<List<NotifyUserDto>>().Select(x => x.UserName));
}
List<TrackStatusDto> BuildMissingTrackStatus(string statusIds)
{
var trackStatus = statusIds.Split(',').Select(x =>
{
if (!int.TryParse(x, out var id)) return null;
var trackStatusName = trackStatusDict.GetValueOrDefault(id);
if (string.IsNullOrWhiteSpace(trackStatusName)) return null;
return new TrackStatusDto
{
TrackStatusId = id,
TrackStatus = trackStatusName
};
}).Where(x => x != null);
return trackStatus.ToList()!;
}
}
/// <summary>
/// 编辑完整预警规则列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost("EditCompleteWarningRuleItem")]
public async Task<UnifyResultDto> EditCompleteWarningRuleItem(EditCompleteWarningRuleItemInput input)
{
var rule = await _warningRuleRepository.GetWarningRuleByIdAsync(input.WarningId, WarningRuleType.完整预警);
if (rule == null)
{
throw new BusinessException(message: $"预警规则主体不存在,WarningId=[{input.WarningId}]");
}
var bos = CompleteWarningRuleItemBO.From(input);
await _completeWarningHandler.HandleEditAsync(rule, bos);
return new UnifyResultDto(true);
}
}