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
{
///
/// 重要通知任务订阅
///
public class CautionNoticeTaskSubscriber : IEventSubscriber
{
private IServiceProvider _services { get; }
private readonly ILogger _logger;
//private readonly INamedServiceProvider _namedTaskManageServiceProvider;
public CautionNoticeTaskSubscriber(IServiceProvider services, ILogger logger)
{
_services = services;
_logger = logger;
// _namedTaskManageServiceProvider = namedTaskManageServiceProvider;
}
///
/// 新增重要通知任务
///
/// 订阅上下文
///
[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>();
//var bookingSlotRepo = scope.ServiceProvider.GetRequiredService>();
var service = scope.ServiceProvider.GetRequiredService();
//var service = _namedTaskManageServiceProvider.GetService(nameof(TaskManageService));
DateTime nowDate = DateTime.Now;
/*
1、生成重要提醒任务
2、先从订舱订单提取操作OP和销售OP,推送钉钉和邮件提醒。
3、如果没有订舱订单,可以从舱位获取操作OP,推送钉钉和邮件提醒。
*/
CautionNoticeTaskDto dto = GetInfo(paraObj);
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,
}
};
messageInfo.Main.TaskTenatId = dto.tenentId;
messageInfo.Main.TaskTenatName = dto.tenentName;
messageInfo.Main.VesselVoyno = $"{dto.vessel}/{dto.voyno}";
messageInfo.Main.TaskUserId = dto.userId.ToString();
messageInfo.Main.TaskUserName = dto.userName;
messageInfo.Main.CarrierId = dto.carrier;
messageInfo.Main.CustomerId = dto.customerId;
messageInfo.Main.CustomerName = dto.customerName;
messageInfo.Main.TaskTitle = $"重要提醒-{dto.cautionNoticeType.GetDescription()} {dto.vessel}/{dto.voyno} {dto.etd} BLNo:{dto.mblNo}";
messageInfo.Main.TaskDesp = $"重要提醒-{dto.cautionNoticeType.GetDescription()} {dto.vessel}/{dto.voyno} {dto.etd} BLNo:{dto.mblNo}";
messageInfo.Main.CautionNoticeInfo = new TaskManageOrderCautionNoticeInfo
{
BookingId = dto.bookingId,
BookingSlotId = dto.bookingSlotId,
CautionNoticeType = dto.cautionNoticeType,
Carrier = dto.carrier,
MBlNo = dto.mblNo,
OrigVal = dto.origVal,
NewVal = dto.newVal,
NotifyContent = dto.notifyContent,
SourceSystem = dto.sourceSystem,
SourceBusiType = dto.sourceBusiType,
SourceBusiTypeName = dto.sourceBusiTypeName,
CreateTime = DateTime.Now,
NoticeList = new List()
};
if (dto.notifyList != null && dto.notifyList.Count > 0)
{
dto.notifyList.ForEach(p =>
{
foreach(var item in p.notifyMethod)
{
var notifyInfo = new TaskManageOrderCautionNoticeDetailInfo
{
CautionNoticeType = item,
Email = p.notifyEmail,
UserId = p.notifyUserId,
UserName = p.notifyUserName,
Mobile = p.notifyMobile,
};
messageInfo.Main.CautionNoticeInfo.NoticeList.Add(notifyInfo);
}
});
}
var rlt = await service.CreateTaskJob(messageInfo);
}
private CautionNoticeTaskDto GetInfo(object payload)
{
CautionNoticeTaskDto dto = null;
try
{
dto = JSON.Deserialize(JSON.Serialize(payload));
}
catch(Exception ex)
{
_logger.LogInformation($"解析订阅参数失败,原因:{ex.Message}");
throw Oops.Bah($"解析订阅参数失败,原因:{ex.Message}");
}
return dto;
}
}
}