You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Application/Event/CautionNoticeTaskSubscriber.cs

163 lines
6.1 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using Furion;
using Furion.DependencyInjection;
using Furion.DistributedIDGenerator;
using Furion.EventBus;
using Furion.FriendlyException;
using Furion.JsonSerialization;
using MathNet.Numerics.LinearAlgebra.Factorization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Myshipping.Application.Entity;
using Myshipping.Core;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application.Event
{
/// <summary>
/// 重要通知任务订阅
/// </summary>
public class CautionNoticeTaskSubscriber : IEventSubscriber
{
private IServiceProvider _services { get; }
private readonly ILogger<CautionNoticeTaskSubscriber> _logger;
private readonly INamedServiceProvider<ITaskManageService> _namedTaskManageServiceProvider;
public CautionNoticeTaskSubscriber(IServiceProvider services, ILogger<CautionNoticeTaskSubscriber> logger,
INamedServiceProvider<ITaskManageService> namedTaskManageServiceProvider)
{
_services = services;
_logger = logger;
_namedTaskManageServiceProvider = namedTaskManageServiceProvider;
}
/// <summary>
/// 新增重要通知任务
/// </summary>
/// <param name="context">订阅上下文</param>
/// <returns></returns>
[EventSubscribe("CautionNoticeTask:Add")]
public async Task CautionNoticeTaskAdd(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到更新订阅请求:{context.Source.Payload}");
var paraObj = context.Source.Payload;
using var scope = _services.CreateScope();
var bookingRepo = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingOrder>>();
var bookingSlotRepo = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingSlotBase>>();
var service = _namedTaskManageServiceProvider.GetService(nameof(TaskManageService));
DateTime nowDate = DateTime.Now;
/*
1、生成重要提醒任务
2、先从订舱订单提取操作OP和销售OP推送钉钉和邮件提醒。
3、如果没有订舱订单可以从舱位获取操作OP推送钉钉和邮件提醒。
*/
CautionNoticeTaskDto dto = GetInfo(paraObj);
//生成任务
if (dto.cautionNoticeType == CautionNoticeTaskEnum.WeekAt)
{
TaskManageOrderMessageInfo messageInfo = new TaskManageOrderMessageInfo
{
Head = new TaskManageOrderMessageHeadInfo
{
GID = IDGen.NextID().ToString(),
MessageType = "CAUTION_TASK",
SenderId = "CautionNoticeTask",
SenderName = "重要提醒任务生成",
ReceiverId = "TaskManage",
ReceiverName = "任务台",
RequestDate = nowDate.ToString("yyyy-MM-dd HH:mm:ss.fff"),
Version = "1.0",
RequestAction = "Add"
},
Main = new TaskManageOrderMessageMainInfo
{
TaskType = TaskBaseTypeEnum.CAUTION_NOTICE,
}
};
BookingSlotBase slotInfo = null;
BookingOrder bookingOrder = null;
string mblNo = string.Empty;
string vesselVoyno = string.Empty;
string etd = string.Empty;
if (dto.bookingSlotId > 0)
{
slotInfo = bookingSlotRepo.AsQueryable().Filter(null, true).First(x => x.Id == dto.bookingSlotId);
}
if (dto.bookingId > 0)
{
bookingOrder = bookingRepo.AsQueryable().Filter(null, true).First(x => x.Id == dto.bookingId);
}
messageInfo.Main.TaskTenatId = slotInfo.TenantId;
messageInfo.Main.TaskTenatName = slotInfo.TenantName;
if (slotInfo != null)
{
if (slotInfo.CARRIERID.Equals("MSK", StringComparison.OrdinalIgnoreCase))
{
mblNo = slotInfo.SLOT_BOOKING_NO;
messageInfo.Main.MBlNo = slotInfo.SLOT_BOOKING_NO;
messageInfo.Main.ETD = slotInfo.ETD;
vesselVoyno = $"{(slotInfo.VESSEL ?? "")}/{slotInfo.VOYNO ?? ""}";
messageInfo.Main.VesselVoyno = vesselVoyno;
messageInfo.Main.TaskUserId = dto.userId.ToString();
messageInfo.Main.TaskUserName = dto.userName;
messageInfo.Main.CarrierId = slotInfo.CARRIERID;
}
}
if (bookingOrder != null)
{
messageInfo.Main.CustomerId = bookingOrder.CUSTOMERID;
messageInfo.Main.CustomerName = bookingOrder.CUSTOMERNAME;
}
messageInfo.Main.TaskTitle = $"重要提醒-计费周发生变更 {vesselVoyno} {etd} BLNo:{mblNo}";
messageInfo.Main.TaskDesp = $"重要提醒-计费周发生变更 {vesselVoyno} {etd} BLNo:{mblNo}";
var rlt = await service.CreateTaskJob(messageInfo);
}
}
private CautionNoticeTaskDto GetInfo(object payload)
{
CautionNoticeTaskDto dto = null;
try
{
dto = JSON.Deserialize<CautionNoticeTaskDto>(JSON.Serialize(payload));
}
catch(Exception ex)
{
_logger.LogInformation($"解析订阅参数失败,原因:{ex.Message}");
throw Oops.Bah($"解析订阅参数失败,原因:{ex.Message}");
}
return dto;
}
}
}