|
|
using Furion;
|
|
|
using Furion.DependencyInjection;
|
|
|
using Furion.DynamicApiController;
|
|
|
using Furion.FriendlyException;
|
|
|
using Furion.JsonSerialization;
|
|
|
using Furion.RemoteRequest.Extensions;
|
|
|
using HtmlAgilityPack;
|
|
|
using Mapster;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
using Myshipping.Application.ConfigOption;
|
|
|
using Myshipping.Application.Entity;
|
|
|
using Myshipping.Application.Enum;
|
|
|
using Myshipping.Application.Helper;
|
|
|
using Myshipping.Application.Service.TaskManagePlat.Interface;
|
|
|
using Myshipping.Core;
|
|
|
using Myshipping.Core.Entity;
|
|
|
using Myshipping.Core.Helper;
|
|
|
using Myshipping.Core.Service;
|
|
|
using Newtonsoft.Json;
|
|
|
using Npoi.Mapper;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Net.Http;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
namespace Myshipping.Application
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 任务目的港未提货未返箱
|
|
|
/// </summary>
|
|
|
[ApiDescriptionSettings("Application", Name = "PODDischargeGateoutFull", Order = 10)]
|
|
|
public class TaskManagePODDischargeGateoutFullService: ITaskManagePODDischargeGateoutFullService, IDynamicApiController, ITransient
|
|
|
{
|
|
|
private readonly SqlSugarRepository<TaskPODDischargeGateoutFullInfo> _taskPODDischargeGateoutFullInfoRep;
|
|
|
private readonly SqlSugarRepository<TaskPODDischargeGateoutFullDetailInfo> _taskPODDischargeGateoutFullDetailInfoRep;
|
|
|
private readonly SqlSugarRepository<TaskBaseInfo> _taskBaseRepository;
|
|
|
private readonly ILogger<TaskManagePODDischargeGateoutFullService> _logger;
|
|
|
private readonly SqlSugarRepository<BookingOrder> _bookingOrderRepository;
|
|
|
private readonly SqlSugarRepository<DjyUserMailAccount> _djyUserMailAccount;
|
|
|
private readonly SqlSugarRepository<BookingOrderContact> _bookingOrderContactRepository;
|
|
|
private readonly SqlSugarRepository<SysUser> _sysUserRepository;
|
|
|
private readonly SqlSugarRepository<BookingPrintTemplate> _repPrintTemplate;
|
|
|
private readonly ISysCacheService _cache;
|
|
|
|
|
|
public TaskManagePODDischargeGateoutFullService(ILogger<TaskManagePODDischargeGateoutFullService> logger,
|
|
|
SqlSugarRepository<TaskPODDischargeGateoutFullInfo> taskPODDischargeGateoutFullInfoRep,
|
|
|
SqlSugarRepository<TaskPODDischargeGateoutFullDetailInfo> taskPODDischargeGateoutFullDetailInfoRep,
|
|
|
SqlSugarRepository<TaskBaseInfo> taskBaseRepository,
|
|
|
SqlSugarRepository<DjyUserMailAccount> djyUserMailAccount,
|
|
|
SqlSugarRepository<BookingOrderContact> bookingOrderContactRepository,
|
|
|
SqlSugarRepository<SysUser> sysUserRepository,
|
|
|
SqlSugarRepository<BookingPrintTemplate> repPrintTemplate,
|
|
|
ISysCacheService cache,
|
|
|
SqlSugarRepository<BookingOrder> bookingOrderRepository)
|
|
|
{
|
|
|
_taskPODDischargeGateoutFullInfoRep = taskPODDischargeGateoutFullInfoRep;
|
|
|
_taskBaseRepository = taskBaseRepository;
|
|
|
_taskPODDischargeGateoutFullDetailInfoRep = taskPODDischargeGateoutFullDetailInfoRep;
|
|
|
_logger = logger;
|
|
|
_djyUserMailAccount = djyUserMailAccount;
|
|
|
_bookingOrderRepository = bookingOrderRepository;
|
|
|
_bookingOrderContactRepository = bookingOrderContactRepository;
|
|
|
_sysUserRepository = sysUserRepository;
|
|
|
_repPrintTemplate = repPrintTemplate;
|
|
|
_cache = cache;
|
|
|
}
|
|
|
|
|
|
#region 获取目的港未提货未返箱详情
|
|
|
/// <summary>
|
|
|
/// 获取目的港未提货未返箱详情
|
|
|
/// </summary>
|
|
|
/// <param name="pkId">目的港未提货未返箱主键</param>
|
|
|
/// <returns>返回回执</returns>
|
|
|
[HttpGet("/PODDischargeGateoutFull/GetInfo")]
|
|
|
public async Task<List<TaskPODDischargeGateoutFullShowDto>> GetInfo(string pkId)
|
|
|
{
|
|
|
List<TaskPODDischargeGateoutFullShowDto> list = new List<TaskPODDischargeGateoutFullShowDto>();
|
|
|
|
|
|
var dischargeFull = _taskPODDischargeGateoutFullInfoRep.AsQueryable()
|
|
|
.First(a => a.PK_ID == pkId);
|
|
|
|
|
|
if (dischargeFull == null)
|
|
|
throw Oops.Oh($"目的港未提货未返箱主键{pkId}无法获取业务信息");
|
|
|
|
|
|
var dischargeFullDetail = _taskPODDischargeGateoutFullDetailInfoRep.AsQueryable()
|
|
|
.Where(a => a.P_ID == dischargeFull.PK_ID).ToList();
|
|
|
|
|
|
var taskBase = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == dischargeFull.TASK_ID);
|
|
|
|
|
|
if (taskBase == null)
|
|
|
throw Oops.Oh($"任务主键无法获取业务信息");
|
|
|
|
|
|
list = dischargeFullDetail.Select(x => {
|
|
|
var dto = x.Adapt<TaskPODDischargeGateoutFullShowDto>();
|
|
|
|
|
|
dto.PKId = dischargeFull.PK_ID;
|
|
|
dto.TaskId = dischargeFull.TASK_ID;
|
|
|
dto.IsComplete = taskBase.IS_COMPLETE == 1 ? true : false;
|
|
|
dto.CompleteTime = taskBase.COMPLETE_DATE;
|
|
|
dto.NoticeType = dischargeFull.NOTICE_TYPE;
|
|
|
dto.NoticeTypeName = dischargeFull.NOTICE_TYPE_NAME;
|
|
|
|
|
|
return dto;
|
|
|
}).ToList();
|
|
|
|
|
|
return list;
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 通过任务主键获取目的港未提货未返箱详情
|
|
|
/// <summary>
|
|
|
/// 通过任务主键获取目的港未提货未返箱详情
|
|
|
/// </summary>
|
|
|
/// <param name="taskPkId">目的港未提货未返箱任务主键</param>
|
|
|
/// <returns>返回回执</returns>
|
|
|
[HttpGet("/PODDischargeGateoutFull/GetInfoByTaskId")]
|
|
|
public async Task<List<TaskPODDischargeGateoutFullShowDto>> GetInfoByTaskId(string taskPkId)
|
|
|
{
|
|
|
List<TaskPODDischargeGateoutFullShowDto> list = new List<TaskPODDischargeGateoutFullShowDto>();
|
|
|
|
|
|
var taskBase = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == taskPkId);
|
|
|
|
|
|
if (taskBase == null)
|
|
|
throw Oops.Oh($"任务主键{taskPkId}无法获取业务信息");
|
|
|
|
|
|
var dischargeFull = _taskPODDischargeGateoutFullInfoRep.AsQueryable().First(a => a.TASK_ID == taskBase.PK_ID);
|
|
|
|
|
|
if (dischargeFull == null)
|
|
|
throw Oops.Oh($"目的港未提货未返箱主键{taskPkId}无法获取业务信息");
|
|
|
|
|
|
var dischargeFullDetail = _taskPODDischargeGateoutFullDetailInfoRep.AsQueryable()
|
|
|
.Where(a => a.P_ID == dischargeFull.PK_ID).ToList();
|
|
|
|
|
|
list = dischargeFullDetail.Select(x => {
|
|
|
var dto = x.Adapt<TaskPODDischargeGateoutFullShowDto>();
|
|
|
|
|
|
dto.PKId = dischargeFull.PK_ID;
|
|
|
dto.TaskId = dischargeFull.TASK_ID;
|
|
|
dto.IsComplete = taskBase.IS_COMPLETE == 1 ? true : false;
|
|
|
dto.CompleteTime = taskBase.COMPLETE_DATE;
|
|
|
dto.NoticeType = dischargeFull.NOTICE_TYPE;
|
|
|
dto.NoticeTypeName = dischargeFull.NOTICE_TYPE_NAME;
|
|
|
dto.Carrier = dischargeFull.CARRIER;
|
|
|
|
|
|
return dto;
|
|
|
}).ToList();
|
|
|
|
|
|
return list;
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 推送及时消息
|
|
|
/// <summary>
|
|
|
/// 推送及时消息
|
|
|
/// </summary>
|
|
|
/// <param name="taskPkId">目的港未提货未返箱任务主键</param>
|
|
|
/// <returns>返回回执</returns>
|
|
|
[HttpGet("/PODDischargeGateoutFull/SendInstantMessage")]
|
|
|
public async Task<TaskManageOrderResultDto> SendInstantMessage(string taskPkId)
|
|
|
{
|
|
|
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
|
|
|
|
|
|
var taskBase = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == taskPkId);
|
|
|
|
|
|
if (taskBase == null)
|
|
|
throw Oops.Oh($"任务主键{taskPkId}无法获取业务信息");
|
|
|
|
|
|
var dischargeFull = _taskPODDischargeGateoutFullInfoRep.AsQueryable().First(a => a.TASK_ID == taskBase.PK_ID);
|
|
|
|
|
|
if (dischargeFull == null)
|
|
|
throw Oops.Oh($"目的港未提货未返箱主键{taskPkId}无法获取业务信息");
|
|
|
|
|
|
var detailList = _taskPODDischargeGateoutFullDetailInfoRep.AsQueryable()
|
|
|
.Where(a => a.P_ID == dischargeFull.PK_ID).ToList();
|
|
|
|
|
|
//如果都有订单订单记录,则提示错误不进行对应
|
|
|
if (!detailList.Any(t => t.BOOKING_ID.HasValue))
|
|
|
{
|
|
|
throw Oops.Oh($"当前目的港未提货未返箱没有匹配的订舱订单,不能执行操作");
|
|
|
}
|
|
|
|
|
|
var ids = detailList.Where(a => a.BOOKING_ID.HasValue).Select(a => a.BOOKING_ID.Value).ToList();
|
|
|
|
|
|
var bookingList = _bookingOrderRepository.AsQueryable().Filter(null, true)
|
|
|
.Where(a => ids.Contains(a.Id) && a.IsDeleted == false && (a.ParentId == null || a.ParentId == 0)).ToList();
|
|
|
|
|
|
if (bookingList.Count == 0)
|
|
|
throw Oops.Oh($"提单号未提取有效的订舱订单");
|
|
|
|
|
|
Dictionary<string, List<string>> msgList = new Dictionary<string, List<string>>();
|
|
|
|
|
|
msgList = bookingList.GroupBy(a => a.OP).Select(a =>
|
|
|
{
|
|
|
var currList = a.ToList();
|
|
|
|
|
|
return new { Key = a.Key, list = currList.Select(a => a.MBLNO).ToList() };
|
|
|
}).ToDictionary(a => a.Key, b => b.list);
|
|
|
|
|
|
if (msgList.Count > 0)
|
|
|
{
|
|
|
msgList.ForEach(t =>
|
|
|
{
|
|
|
string title = "目的港未提货消息";
|
|
|
|
|
|
if (dischargeFull.NOTICE_TYPE == "GATEOUT_FULL")
|
|
|
title = "提箱未返空箱消息";
|
|
|
|
|
|
DingTalkGroupHelper.SendDingTalkGroupMessage("KangqianNotify", title, $"@{t.Key} 收到如下提单号{(string.Join(",", t.Value.ToArray()))}的{title}消息");
|
|
|
});
|
|
|
}
|
|
|
|
|
|
if (detailList.Any(t => !t.BOOKING_ID.HasValue))
|
|
|
throw Oops.Oh($"提单号{(string.Join(",", detailList.Where(t => !t.BOOKING_ID.HasValue).Select(t => t.MBL_NO).ToArray()))}未提取有效的订舱订单");
|
|
|
|
|
|
result.succ = true;
|
|
|
result.msg = "成功";
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 重新处理目的港未提货未返箱任务
|
|
|
/// <summary>
|
|
|
/// 重新处理目的港未提货未返箱任务
|
|
|
/// 对未匹配目的港未提货未返箱的任务记录重新对应订单
|
|
|
/// </summary>
|
|
|
/// <param name="taskPkId">目的港未提货未返箱任务主键</param>
|
|
|
/// <returns>返回回执</returns>
|
|
|
[HttpGet("/PODDischargeGateoutFull/SearchAndConnectBookingInfo")]
|
|
|
public async Task<TaskManageOrderResultDto> SearchAndConnectBookingInfo(string taskPkId)
|
|
|
{
|
|
|
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
|
|
|
|
|
|
var taskBase = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == taskPkId);
|
|
|
|
|
|
if (taskBase == null)
|
|
|
throw Oops.Oh($"任务主键{taskPkId}无法获取业务信息");
|
|
|
|
|
|
var dischargeFull = _taskPODDischargeGateoutFullInfoRep.AsQueryable().First(a => a.TASK_ID == taskBase.PK_ID);
|
|
|
|
|
|
if (dischargeFull == null)
|
|
|
throw Oops.Oh($"目的港未提货未返箱主键{taskPkId}无法获取业务信息");
|
|
|
|
|
|
var detailList = _taskPODDischargeGateoutFullDetailInfoRep.AsQueryable()
|
|
|
.Where(a => a.P_ID == dischargeFull.PK_ID).ToList();
|
|
|
|
|
|
//如果都有订单订单记录,则提示错误不进行对应
|
|
|
if (!detailList.Any(t => !t.BOOKING_ID.HasValue))
|
|
|
{
|
|
|
throw Oops.Oh($"目的港未提货未返箱已有匹配的订舱订单,不能执行操作");
|
|
|
}
|
|
|
|
|
|
var mblList = detailList.Select(a => a.MBL_NO).Distinct().ToList();
|
|
|
|
|
|
var bookingList = _bookingOrderRepository.AsQueryable().Filter(null, true)
|
|
|
.Where(a => mblList.Contains(a.MBLNO) && a.IsDeleted == false && (a.ParentId == null || a.ParentId == 0)).ToList();
|
|
|
|
|
|
if (bookingList.Count == 0)
|
|
|
throw Oops.Oh($"提单号未提取有效的订舱订单");
|
|
|
|
|
|
List<string> msgList = new List<string>();
|
|
|
|
|
|
detailList.ForEach(async t =>
|
|
|
{
|
|
|
//只处理没有订舱ID的记录
|
|
|
if (!t.BOOKING_ID.HasValue)
|
|
|
{
|
|
|
var currBook = bookingList.FirstOrDefault(p => p.MBLNO == t.MBL_NO);
|
|
|
|
|
|
if (currBook != null)
|
|
|
{
|
|
|
t.BOOKING_ID = currBook.Id;
|
|
|
t.UpdatedUserId = UserManager.UserId;
|
|
|
t.UpdatedUserName = UserManager.Name;
|
|
|
|
|
|
//更新任务BC
|
|
|
await _taskPODDischargeGateoutFullDetailInfoRep.AsUpdateable(t).UpdateColumns(it => new
|
|
|
{
|
|
|
it.BOOKING_ID,
|
|
|
it.UpdatedTime,
|
|
|
it.UpdatedUserId,
|
|
|
it.UpdatedUserName
|
|
|
|
|
|
}).ExecuteCommandAsync();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
msgList.Add(t.MBL_NO);
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
|
|
|
if (msgList.Count > 0)
|
|
|
throw Oops.Oh($"提单号{(string.Join(",", msgList.ToArray()))}未提取有效的订舱订单");
|
|
|
|
|
|
result.succ = true;
|
|
|
result.msg = "成功";
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
/// <summary>
|
|
|
/// 检索对应的订舱订单
|
|
|
/// </summary>
|
|
|
/// <param name="taskPKId">目的港未提货未返箱任务主键</param>
|
|
|
/// <returns>返回回执</returns>
|
|
|
public async Task<TaskManageOrderResultDto> QueryBookingOrder(string taskPKId)
|
|
|
{
|
|
|
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
|
|
|
|
|
|
try
|
|
|
{
|
|
|
var taskBase = _taskBaseRepository.AsQueryable().Filter(null, true).First(a => a.PK_ID == taskPKId);
|
|
|
|
|
|
if (taskBase == null)
|
|
|
throw Oops.Oh($"任务主键{taskPKId}无法获取业务信息");
|
|
|
|
|
|
var entityInfo = _taskPODDischargeGateoutFullInfoRep.AsQueryable().Filter(null, true).First(a => a.TASK_ID == taskBase.PK_ID);
|
|
|
|
|
|
string name = entityInfo.NOTICE_TYPE_NAME;
|
|
|
|
|
|
if (entityInfo == null)
|
|
|
throw Oops.Oh($"{name}主键{taskPKId}无法获取业务信息");
|
|
|
|
|
|
//通过船名航次取是主单的订舱记录列表
|
|
|
var bookingInfo = _bookingOrderRepository.AsQueryable().Filter(null, true).First(a => a.MBLNO == entityInfo.MBL_NO
|
|
|
&& a.IsDeleted == false && (a.ParentId == null || a.ParentId == 0) && a.TenantId == UserManager.TENANT_ID);
|
|
|
|
|
|
if (bookingInfo != null)
|
|
|
{
|
|
|
entityInfo.BOOKING_ID = bookingInfo.Id;
|
|
|
|
|
|
entityInfo.UpdatedTime = DateTime.Now;
|
|
|
entityInfo.UpdatedUserId = UserManager.UserId;
|
|
|
entityInfo.UpdatedUserName = UserManager.Name;
|
|
|
|
|
|
await _taskPODDischargeGateoutFullInfoRep.AsUpdateable(entityInfo).UpdateColumns(x => new {
|
|
|
x.BOOKING_ID,
|
|
|
x.UpdatedTime,
|
|
|
x.UpdatedUserId,
|
|
|
x.UpdatedUserName
|
|
|
}).ExecuteCommandAsync();
|
|
|
|
|
|
result.succ = true;
|
|
|
result.msg = "检索对应成功";
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
result.succ = false;
|
|
|
result.msg = $"检索对应失败,提单号:{entityInfo.MBL_NO} 没有对应的订舱记录";
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
result.succ = false;
|
|
|
result.msg = $"检索失败,原因:{ex.Message}";
|
|
|
|
|
|
_logger.LogInformation($"taskPKId={taskPKId} 检索起运港未提箱订舱记录 处理异常,原因:{ex.Message}");
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 发送邮件通知给客户
|
|
|
/// </summary>
|
|
|
/// <param name="taskPKId">目的港未提货未返箱任务主键</param>
|
|
|
/// <returns>返回回执</returns>
|
|
|
public async Task<TaskManageOrderResultDto> SendEmailToCustomer(string taskPKId)
|
|
|
{
|
|
|
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
|
|
|
|
|
|
try
|
|
|
{
|
|
|
var entityInfo = _taskPODDischargeGateoutFullInfoRep.AsQueryable().Filter(null, true).First(a => a.TASK_ID == taskPKId);
|
|
|
|
|
|
string name = entityInfo.NOTICE_TYPE_NAME;
|
|
|
|
|
|
if (!entityInfo.BOOKING_ID.HasValue)
|
|
|
{
|
|
|
new EmailNoticeHelper().SendEmailNotice($"taskid={taskPKId} mblno={entityInfo.MBL_NO} {name} 转发通知邮件失败", $"taskid={taskPKId} mblno={entityInfo.MBL_NO} 当前任务没有对应的订舱订单,不能转发邮件,请先检索对应的订舱订单", App.Configuration["EmailNoticeDefaultUser"].GetUserEmailList());
|
|
|
|
|
|
throw Oops.Oh($"当前任务没有对应的订舱订单,不能转发邮件,请先检索对应的订舱订单");
|
|
|
}
|
|
|
|
|
|
var list = _taskPODDischargeGateoutFullDetailInfoRep.AsQueryable().Filter(null, true).Where(a => a.P_ID == entityInfo.PK_ID).ToList();
|
|
|
|
|
|
var bookingOrderList = _bookingOrderRepository.AsQueryable().Filter(null, true).Where(a => a.Id == entityInfo.BOOKING_ID.Value && a.IsDeleted == false && a.TenantId == UserManager.TENANT_ID).ToList();
|
|
|
|
|
|
if (bookingOrderList.Count == 0)
|
|
|
{
|
|
|
new EmailNoticeHelper().SendEmailNotice($"taskid={taskPKId} mblno={entityInfo.MBL_NO} {name} 转发通知邮件失败", $"taskid={taskPKId} mblno={entityInfo.MBL_NO} 当前任务对应的订舱订单不存在或已作废", App.Configuration["EmailNoticeDefaultUser"].GetUserEmailList());
|
|
|
|
|
|
throw Oops.Oh($"当前任务对应的订舱订单不存在或已作废");
|
|
|
}
|
|
|
|
|
|
var bookingId = entityInfo.BOOKING_ID.Value;
|
|
|
|
|
|
var bookingContactList = _bookingOrderContactRepository.AsQueryable().Filter(null, true)
|
|
|
.Where(a => bookingId == a.BookingId.Value && a.IsDeleted == false).ToList();
|
|
|
|
|
|
result = await GenerateSendEmail(entityInfo, list, bookingOrderList, bookingContactList, name);
|
|
|
|
|
|
var model = _taskPODDischargeGateoutFullInfoRep.AsQueryable().Filter(null, true).First(a => a.TASK_ID == taskPKId);
|
|
|
|
|
|
if (model != null)
|
|
|
{
|
|
|
model.IS_TRANSFER_USER = true;
|
|
|
model.LST_STATUS = result.succ ? "SUCC" : "FAILURE";
|
|
|
model.LST_STATUS_NAME = result.succ ? "成功" : "失败";
|
|
|
model.LST_TRANSFER_USER_DATE = DateTime.Now;
|
|
|
model.LST_TRANSFER_NOTES = result.msg;
|
|
|
|
|
|
await _taskPODDischargeGateoutFullInfoRep.AsUpdateable(model).UpdateColumns(x => new
|
|
|
{
|
|
|
x.LST_TRANSFER_NOTES,
|
|
|
x.LST_TRANSFER_USER_DATE,
|
|
|
x.LST_STATUS,
|
|
|
x.LST_STATUS_NAME,
|
|
|
x.IS_TRANSFER_USER
|
|
|
}).ExecuteCommandAsync();
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
result.succ = false;
|
|
|
result.msg = $"发送邮件失败,原因:{ex.Message}";
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
#region 生成并转发通知邮件
|
|
|
/// <summary>
|
|
|
/// 生成并转发通知邮件
|
|
|
/// </summary>
|
|
|
/// <param name="model"></param>
|
|
|
/// <param name="bookingOrderList"></param>
|
|
|
/// <param name="bookingContactList"></param>
|
|
|
/// <param name="taskBaskInfo"></param>
|
|
|
/// <returns></returns>
|
|
|
[NonAction]
|
|
|
private async Task<TaskManageOrderResultDto> GenerateSendEmail(TaskPODDischargeGateoutFullInfo model,List<TaskPODDischargeGateoutFullDetailInfo> detailList, List<BookingOrder> bookingOrderList,
|
|
|
List<BookingOrderContact> bookingContactList,string name)
|
|
|
{
|
|
|
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
|
|
|
|
|
|
try
|
|
|
{
|
|
|
//TO 邮件接收人
|
|
|
string toEmail = string.Empty;
|
|
|
//订舱OP的邮箱
|
|
|
string opEmail = string.Empty;
|
|
|
|
|
|
//去重客户联系人的邮箱
|
|
|
toEmail = string.Join(";", bookingContactList.Select(x => x.Email.Trim()).Distinct().ToArray());
|
|
|
|
|
|
List<string> opEmailList = new List<string>();
|
|
|
|
|
|
SysUser opUserInfo = null;
|
|
|
|
|
|
bookingOrderList.ForEach(bk =>
|
|
|
{
|
|
|
//获取操作OP的邮箱
|
|
|
if (!string.IsNullOrWhiteSpace(bk.OPID))
|
|
|
{
|
|
|
var opId = long.Parse(bk.OPID);
|
|
|
var opUser = _sysUserRepository.AsQueryable().Filter(null, true).First(a => a.Id == opId);
|
|
|
|
|
|
if (opUser != null)
|
|
|
{
|
|
|
if (opUserInfo == null)
|
|
|
opUserInfo = opUser;
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(opUser.Email))
|
|
|
{
|
|
|
opEmailList.Add(opUser.Email.Trim());
|
|
|
|
|
|
_logger.LogInformation($"id={bk.Id} mblno={bk.MBLNO} 获取操作OP的邮箱,opEmail={opEmail} opid={opId} name={opUser.Name}");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
_logger.LogInformation($"id={bk.Id} mblno={bk.MBLNO} 获取操作OP的邮箱失败,opEmail={opUser.Email} opid={opId} name={opUser.Name}");
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
_logger.LogInformation($"id={bk.Id} mblno={bk.MBLNO} 检索操作OP信息失败,opid={opId} name={opUser.Name}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//获取客服的邮箱
|
|
|
if (!string.IsNullOrWhiteSpace(bk.CUSTSERVICEID))
|
|
|
{
|
|
|
var opId = long.Parse(bk.CUSTSERVICEID);
|
|
|
var opUser = _sysUserRepository.AsQueryable().Filter(null, true).First(a => a.Id == opId);
|
|
|
|
|
|
if (opUser != null)
|
|
|
{
|
|
|
if (!string.IsNullOrWhiteSpace(opUser.Email))
|
|
|
{
|
|
|
opEmailList.Add(opUser.Email.Trim());
|
|
|
|
|
|
_logger.LogInformation($"id={bk.Id} mblno={bk.MBLNO} 获取客服的邮箱,opEmail={opEmail} opid={opId} name={opUser.Name}");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
_logger.LogInformation($"id={bk.Id} mblno={bk.MBLNO} 获取客服的邮箱失败,opEmail={opUser.Email} opid={opId} name={opUser.Name}");
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
_logger.LogInformation($"id={bk.Id} mblno={bk.MBLNO} 检索客服信息失败,opid={opId} name={opUser.Name}");
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
|
|
|
if (opEmailList.Count > 0)
|
|
|
opEmail = string.Join(";", opEmailList.Distinct().ToArray());
|
|
|
|
|
|
string emailTitle = $"{model.MBL_NO} Action Required - Imports Longstandings - Discharge full Reminder";
|
|
|
|
|
|
if (model.NOTICE_TYPE.Equals("GATEOUT_FULL"))
|
|
|
{
|
|
|
emailTitle = $"{model.MBL_NO} Action Required - Imports Longstandings - Gate out full Reminder";
|
|
|
}
|
|
|
|
|
|
//提取当前公共邮箱的配置
|
|
|
DjyUserMailAccount publicMailAccount = _djyUserMailAccount.AsQueryable().Filter(null, true).First(x => x.TenantId == UserManager.TENANT_ID && x.ShowName == "PublicSend"
|
|
|
&& x.SmtpPort > 0 && x.SmtpServer != null && x.SmtpServer != "");
|
|
|
|
|
|
_logger.LogInformation($"提取当前公共邮箱的配置完成,id={publicMailAccount.Id}");
|
|
|
|
|
|
if (publicMailAccount == null)
|
|
|
{
|
|
|
throw Oops.Oh($"提取公共邮箱配置失败,请在用户邮箱账号管理增加配置显示名为PublicSend或者配置个人邮箱");
|
|
|
}
|
|
|
|
|
|
//获取邮件模板
|
|
|
var printTemplate = _repPrintTemplate.AsQueryable().Filter(null, true).First(x => x.CateCode.Contains("import_longstanding_discharge_full") && x.TenantId == UserManager.TENANT_ID);
|
|
|
|
|
|
if (model.NOTICE_TYPE.Equals("GATEOUT_FULL"))
|
|
|
{
|
|
|
printTemplate = _repPrintTemplate.AsQueryable().Filter(null, true).First(x => x.CateCode.Contains("import_longstanding_gateout_full") && x.TenantId == UserManager.TENANT_ID);
|
|
|
}
|
|
|
|
|
|
if (printTemplate == null)
|
|
|
{
|
|
|
throw Oops.Bah(BookingErrorCode.BOOK115);
|
|
|
}
|
|
|
|
|
|
//读取邮件模板并填充数据
|
|
|
string emailHtml = GenerateSendEmailHtml(model, detailList, bookingOrderList, printTemplate.FilePath, opUserInfo, UserManager.TENANT_NAME).GetAwaiter().GetResult();
|
|
|
|
|
|
EmailApiUserDefinedDto emailApiUserDefinedDto = new EmailApiUserDefinedDto
|
|
|
{
|
|
|
SendTo = toEmail,
|
|
|
//CCTo = opEmail,
|
|
|
Title = emailTitle,
|
|
|
Body = emailHtml,
|
|
|
Account = publicMailAccount.MailAccount?.Trim(),
|
|
|
Password = publicMailAccount.Password?.Trim(),
|
|
|
Server = publicMailAccount.SmtpServer?.Trim(),
|
|
|
Port = publicMailAccount.SmtpPort.HasValue ? publicMailAccount.SmtpPort.Value : 465,
|
|
|
UseSSL = publicMailAccount.SmtpSSL.HasValue ? publicMailAccount.SmtpSSL.Value : true,
|
|
|
Attaches = new List<AttachesInfo>()
|
|
|
};
|
|
|
|
|
|
_logger.LogInformation($"生成请求邮件参数,结果:{JSON.Serialize(emailApiUserDefinedDto)}");
|
|
|
|
|
|
//推送邮件
|
|
|
var emailRlt = await PushEmail(emailApiUserDefinedDto);
|
|
|
|
|
|
_logger.LogInformation($"推送邮件完成,结果:{JSON.Serialize(emailRlt)}");
|
|
|
|
|
|
|
|
|
if (emailRlt.succ)
|
|
|
{
|
|
|
result.succ = true;
|
|
|
result.msg = "成功";
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
result.succ = false;
|
|
|
result.msg = emailRlt.msg;
|
|
|
|
|
|
new EmailNoticeHelper().SendEmailNotice($"taskid={model.TASK_ID} {name} 转发通知邮件失败", $"taskid={model.TASK_ID} 转发通知邮件失败,原因:{emailRlt.msg}", App.Configuration["EmailNoticeDefaultUser"].GetUserEmailList());
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
result.succ = false;
|
|
|
result.msg = $"失败,原因:{ex.Message}";
|
|
|
|
|
|
new EmailNoticeHelper().SendEmailNotice($"taskid={model.TASK_ID} {name} 转发通知邮件失败", $"taskid={model.TASK_ID} 转发通知邮件失败,原因:{ex.Message}", App.Configuration["EmailNoticeDefaultUser"].GetUserEmailList());
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 通过邮件模板生成HTML
|
|
|
/// <summary>
|
|
|
/// 通过邮件模板生成HTML
|
|
|
/// </summary>
|
|
|
/// <param name="model"></param>
|
|
|
/// <param name="bookingOrderList"></param>
|
|
|
/// <param name="filePath"></param>
|
|
|
/// <param name="opUserInfo"></param>
|
|
|
/// <param name="tenantName"></param>
|
|
|
/// <returns></returns>
|
|
|
[NonAction]
|
|
|
private async Task<string> GenerateSendEmailHtml(TaskPODDischargeGateoutFullInfo model, List<TaskPODDischargeGateoutFullDetailInfo> detailList,List<BookingOrder> bookingOrderList, string filePath, SysUser opUserInfo, string tenantName)
|
|
|
{
|
|
|
string result = string.Empty;
|
|
|
string baseHtml = string.Empty;
|
|
|
|
|
|
try
|
|
|
{
|
|
|
var opt = App.GetOptions<PrintTemplateOptions>();
|
|
|
var dirAbs = opt.basePath;
|
|
|
if (string.IsNullOrEmpty(dirAbs))
|
|
|
{
|
|
|
dirAbs = App.WebHostEnvironment.WebRootPath;
|
|
|
}
|
|
|
|
|
|
var fileAbsPath = Path.Combine(dirAbs, filePath);
|
|
|
_logger.LogInformation($"查找模板文件:{fileAbsPath}");
|
|
|
|
|
|
if (!File.Exists(fileAbsPath))
|
|
|
{
|
|
|
throw Oops.Bah(BookingErrorCode.BOOK115);
|
|
|
}
|
|
|
|
|
|
baseHtml = File.ReadAllText(fileAbsPath);
|
|
|
|
|
|
if (opUserInfo != null && !string.IsNullOrWhiteSpace(opUserInfo.Name))
|
|
|
{
|
|
|
baseHtml = baseHtml.Replace("#opname#", opUserInfo.Name);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
baseHtml = baseHtml.Replace("#opname#", "操作");
|
|
|
}
|
|
|
|
|
|
if (opUserInfo != null && !string.IsNullOrWhiteSpace(opUserInfo.Email))
|
|
|
{
|
|
|
baseHtml = baseHtml.Replace("#opemail#", opUserInfo.Email);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
baseHtml = baseHtml.Replace("#opemail#", "");
|
|
|
}
|
|
|
|
|
|
if (opUserInfo != null && !string.IsNullOrWhiteSpace(opUserInfo.Phone))
|
|
|
{
|
|
|
baseHtml = baseHtml.Replace("#optel#", opUserInfo.Phone);
|
|
|
}
|
|
|
else if (opUserInfo != null && !string.IsNullOrWhiteSpace(opUserInfo.Tel))
|
|
|
{
|
|
|
baseHtml = baseHtml.Replace("#optel#", opUserInfo.Tel);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
baseHtml = baseHtml.Replace("#optel#", "");
|
|
|
}
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(model.MBL_NO))
|
|
|
{
|
|
|
baseHtml = baseHtml.Replace("#BillNo#", model.MBL_NO);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
baseHtml = baseHtml.Replace("#BillNo#", "");
|
|
|
}
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(tenantName))
|
|
|
{
|
|
|
baseHtml = baseHtml.Replace("#TenantCompanyName#", tenantName);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
baseHtml = baseHtml.Replace("#TenantCompanyName#", "");
|
|
|
}
|
|
|
|
|
|
HtmlDocument html = new HtmlDocument();
|
|
|
html.LoadHtml(baseHtml);
|
|
|
|
|
|
//From Port Start
|
|
|
HtmlNode tdPortNode = html.DocumentNode.SelectSingleNode("//table[@id='tabledetail']");
|
|
|
|
|
|
if (tdPortNode == null)
|
|
|
throw Oops.Oh($"读取邮件模板格式错误,定位table id='tabledetail'失败");
|
|
|
|
|
|
if (model.NOTICE_TYPE.Equals("GATEOUT_FULL"))
|
|
|
{
|
|
|
detailList.ForEach(d =>
|
|
|
{
|
|
|
StringBuilder portBuilder = new StringBuilder();
|
|
|
|
|
|
portBuilder.Append("<tr>");
|
|
|
portBuilder.Append($"<td>{d.CTN_NO}</td>");
|
|
|
portBuilder.Append($"<td>{d.MBL_NO}</td>");
|
|
|
portBuilder.Append($"<td>{d.CNEE}</td>");
|
|
|
portBuilder.Append($"<td>{(d.DISCHARGE_DATE.HasValue ? d.DISCHARGE_DATE.Value.ToString("yyyy-MM-dd HH:mm") : "")}</td>");
|
|
|
portBuilder.Append($"<td>{d.DISCHARGE_PORT}</td>");
|
|
|
portBuilder.Append($"<td>{(d.OVERDUE_DAYS.HasValue ? d.OVERDUE_DAYS.Value.ToString() : "")}</td>");
|
|
|
portBuilder.Append("</tr>");
|
|
|
|
|
|
tdPortNode.ChildNodes.Add(HtmlNode.CreateNode(portBuilder.ToString()));
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
detailList.ForEach(d =>
|
|
|
{
|
|
|
StringBuilder portBuilder = new StringBuilder();
|
|
|
|
|
|
portBuilder.Append("<tr>");
|
|
|
portBuilder.Append($"<td>{d.CTN_NO}</td>");
|
|
|
portBuilder.Append($"<td>{d.MBL_NO}</td>");
|
|
|
portBuilder.Append($"<td>{d.CNEE}</td>");
|
|
|
portBuilder.Append($"<td>{(d.DISCHARGE_DATE.HasValue ? d.DISCHARGE_DATE.Value.ToString("yyyy-MM-dd HH:mm") : "")}</td>");
|
|
|
portBuilder.Append($"<td>{d.CUR_CONTA_LOCATION}</td>");
|
|
|
portBuilder.Append($"<td>{(d.OVERDUE_DAYS.HasValue ? d.OVERDUE_DAYS.Value.ToString() : "")}</td>");
|
|
|
portBuilder.Append($"<td>{(d.DETENTION_FEE_TILL_DATE.HasValue ? d.DETENTION_FEE_TILL_DATE.Value.ToString("0.##") : "")}</td>");
|
|
|
portBuilder.Append($"<td>{d.CHARGE_CURRENCY}</td>");
|
|
|
portBuilder.Append("</tr>");
|
|
|
|
|
|
tdPortNode.ChildNodes.Add(HtmlNode.CreateNode(portBuilder.ToString()));
|
|
|
|
|
|
});
|
|
|
}
|
|
|
|
|
|
result = html.DocumentNode.OuterHtml;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogInformation($"生成起运港未提箱正文失败,原因:{ex.Message}");
|
|
|
|
|
|
throw Oops.Bah($"生成起运港未提箱正文失败,原因:{ex.Message}");
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 推送邮件
|
|
|
/// <summary>
|
|
|
/// 推送邮件
|
|
|
/// </summary>
|
|
|
/// <param name="emailApiUserDefinedDto">自定义邮件详情</param>
|
|
|
/// <param name="filePath">文件路径</param>
|
|
|
/// <returns>返回回执</returns>
|
|
|
[NonAction]
|
|
|
private async Task<CommonWebApiResult> PushEmail(EmailApiUserDefinedDto emailApiUserDefinedDto)
|
|
|
{
|
|
|
CommonWebApiResult result = new CommonWebApiResult { succ = true };
|
|
|
|
|
|
List<EmailApiUserDefinedDto> emailList = new List<EmailApiUserDefinedDto>();
|
|
|
|
|
|
var emailUrl = _cache.GetAllDictData().GetAwaiter().GetResult()
|
|
|
.FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "email_api_url")?.Value;
|
|
|
|
|
|
if (emailUrl == null)
|
|
|
throw Oops.Bah("字典未配置 url_set->email_api_url 请联系管理员");
|
|
|
|
|
|
|
|
|
|
|
|
emailList.Add(emailApiUserDefinedDto);
|
|
|
|
|
|
//string strJoin = System.IO.File.ReadAllText(filePath);
|
|
|
|
|
|
DateTime bDate = DateTime.Now;
|
|
|
|
|
|
HttpResponseMessage res = null;
|
|
|
|
|
|
try
|
|
|
{
|
|
|
res = await emailUrl.SetBody(emailList, "application/json").PostAsync();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
_logger.LogInformation($"发送邮件异常:{ex.Message}");
|
|
|
}
|
|
|
|
|
|
DateTime eDate = DateTime.Now;
|
|
|
TimeSpan ts = eDate.Subtract(bDate);
|
|
|
var timeDiff = ts.TotalMilliseconds;
|
|
|
|
|
|
_logger.LogInformation($"发送邮件返回:{JSON.Serialize(res)}");
|
|
|
|
|
|
if (res != null && res.StatusCode == System.Net.HttpStatusCode.OK)
|
|
|
{
|
|
|
var userResult = await res.Content.ReadAsStringAsync();
|
|
|
|
|
|
var respObj = JsonConvert.DeserializeAnonymousType(userResult, new
|
|
|
{
|
|
|
Success = false,
|
|
|
Message = string.Empty,
|
|
|
Code = -9999,
|
|
|
});
|
|
|
|
|
|
result.succ = respObj.Success;
|
|
|
result.msg = respObj.Message;
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
#region 自动转发目的港未提货未返箱
|
|
|
/// <summary>
|
|
|
/// 自动转发目的港未提货未返箱
|
|
|
/// </summary>
|
|
|
/// <param name="taskPKId">目的港未提货未返箱任务主键</param>
|
|
|
/// <returns>返回回执</returns>
|
|
|
[HttpGet("/PODDischargeGateoutFull/AutoTransferNotice")]
|
|
|
public async Task<TaskManageOrderResultDto> AutoTransferNotice(string taskPKId)
|
|
|
{
|
|
|
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
|
|
|
|
|
|
try
|
|
|
{
|
|
|
var queryRlt = QueryBookingOrder(taskPKId);
|
|
|
|
|
|
_logger.LogInformation($"taskPKId={taskPKId} 检索对应的订舱记录完成,结果:{JSON.Serialize(queryRlt)}");
|
|
|
|
|
|
|
|
|
//_logger.LogInformation($"taskPKId={taskPKId} 当前租户未开启货物运输计划已变更是否同批次同客户合并邮件通知,按照单票推送邮件处理");
|
|
|
|
|
|
//如果没有配置批量,则按单票发送邮件
|
|
|
var rlt = SendEmailToCustomer(taskPKId);
|
|
|
|
|
|
_logger.LogInformation($"taskPKId={taskPKId} 推送邮件完成,结果:{JSON.Serialize(rlt)}");
|
|
|
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
result.succ = false;
|
|
|
result.msg = $"自动转发目的港未提货未返箱失败,原因:{ex.Message}";
|
|
|
|
|
|
_logger.LogInformation($"taskPKId={taskPKId} 自动转发目的港未提货未返箱失败,原因:{ex.Message}");
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
#endregion
|
|
|
}
|
|
|
}
|