track/src/SJZY.Track.Application/Services/Warning/CompleteWarningService.cs

261 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.Caching;
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.WarningAggregate;
using SJZY.Track.Domain.Aggregates.WarningAggregate.Complete;
using SJZY.Track.Domain.Dtos.Warning;
using SJZY.Track.Domain.Enums;
using SJZY.Track.Domain.Extensions;
namespace SJZY.Track.Application.Services.Warning;
/// <summary>
/// 完整预警服务
/// </summary>
[ApiExplorerSettings(GroupName = "预警服务")]
[Route("Warning/Complete")]
public class CompleteWarningService : ApplicationService
{
private readonly IOrderRepository _orderRepository;
private readonly IWarningRuleRepository _warningRuleRepository;
private readonly IWarningDetailsEsRepository _warningDetailsEsRepository;
private readonly ITrackDetailsEsRepository _trackDetailsEsRepository;
private readonly ICompleteWarningHandler _completeWarningHandler;
private readonly TrackStatusNameDictCache _trackStatusNameDictCache;
private readonly CustomAbbreviationDictCache _customAbbreviationDictCache;
private readonly SystemCnNameDictCache _systemCnNameDictCache;
public CompleteWarningService(
IOrderRepository orderRepository,
IWarningRuleRepository warningRuleRepository,
IWarningDetailsEsRepository warningDetailsEsRepository,
ITrackDetailsEsRepository trackDetailsEsRepository,
ICompleteWarningHandler completeWarningHandler,
TrackStatusNameDictCache trackStatusNameDictCache,
CustomAbbreviationDictCache customAbbreviationDictCache,
SystemCnNameDictCache systemCnNameDictCache
)
{
_orderRepository = orderRepository;
_warningRuleRepository = warningRuleRepository;
_warningDetailsEsRepository = warningDetailsEsRepository;
_trackDetailsEsRepository = trackDetailsEsRepository;
_completeWarningHandler = completeWarningHandler;
_trackStatusNameDictCache = trackStatusNameDictCache;
_customAbbreviationDictCache = customAbbreviationDictCache;
_systemCnNameDictCache = systemCnNameDictCache;
}
/// <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>());
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 _trackStatusNameDictCache.SafeGetBatchAsync(trackStatusIds);
// 查订单类型字典
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>(),
result.Pagination.PageIndex,
result.Pagination.PageSize,
result.Pagination.Total);
}
// 订单类型名称字典
var orderTypeDict = await _orderRepository.GetTrackOrderTypeDictAsync();
var trackStatusIds = result.List
.SelectMany(x => x.GetMissingTrackStatusIdGroups().SelectMany(y => y))
.ToHashSet();
// 轨迹状态名称字典
var trackStatusDict = await _trackStatusNameDictCache.SafeGetBatchAsync(trackStatusIds);
// 客户id简称字典
var customIds = result.List.Select(x => x.CustomId).ToHashSet();
var customDict = await _customAbbreviationDictCache.SafeGetBatchAsync(customIds);
// 子系统id中文名字典
var subSystemIds = result.List.Select(x => x.SubSystemId).ToHashSet();
var subSystemDict = await _systemCnNameDictCache.SafeGetBatchAsync(subSystemIds);
// 查最新轨迹(创建时间)
var orderIds = result.List.Select(x => x.OrderId).ToHashSet();
var orderIdTrackDict = await _trackDetailsEsRepository.GetLatestDictAsync(orderIds);
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),
LatestTrackStatus = orderIdTrack?.TrackStatus,
LatestTrackContent = orderIdTrack?.TrackContent,
LatestTrackPlace = orderIdTrack?.TrackPlace,
LatestTrackTime = orderIdTrack?.TrackTime,
LatestTrackTimeZone = DateTimeHelper.ConvertFloatToTimeZone(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<RequireTrackStatusItem> BuildMissingTrackStatus(CompleteWarningDetailsIndex detail)
{
return detail.GetMissingTrackStatusIdGroups()
.Select(g => new RequireTrackStatusItem
{
Statuses = g.Select(id => new TrackStatusDto
{
TrackStatusId = id,
TrackStatus = trackStatusDict.GetValueOrDefault(id, GlobalConst.EmptyString)
}).ToList()
}).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);
// 同一个主体下同一个检查点,订单类型不允许出现交集
foreach (var group in bos.GroupBy(x => x.CheckPointTrackStatusId))
{
var allOrderTypes = new HashSet<int>();
var count = 0;
foreach (var orderTypes in group.Select(x => x.OrderTypes))
{
allOrderTypes.UnionWith(orderTypes);
count += orderTypes.Count;
}
if (allOrderTypes.Count < count)
{
throw new BusinessException(message: "同一个检查点下,订单类型不允许出现交集!");
}
}
await _completeWarningHandler.HandleEditAsync(rule, bos);
return new UnifyResultDto(true);
}
}