using Furion;
using Furion.DependencyInjection;
using Furion.DistributedIDGenerator;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Furion.JsonSerialization;
using Furion.RemoteRequest.Extensions;
using HtmlAgilityPack;
using Mapster;
using Microsoft.AspNetCore.Http;
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.BookingOrder;
using Myshipping.Application.Service.BookingSlot.Dto;
using Myshipping.Core;
using Myshipping.Core.Entity;
using Myshipping.Core.Service;
using Newtonsoft.Json;
using Npoi.Mapper;
using NPOI.HPSF;
using NPOI.SS.Formula.Functions;
using NPOI.Util;
using NPOI.XWPF.UserModel;
using Org.BouncyCastle.Asn1.Tsp;
using RabbitMQ.Client;
using SqlSugar;
using StackExchange.Profiling.Internal;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Principal;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
using Yitter.IdGenerator;
namespace Myshipping.Application
{
///
/// BC任务
///
[ApiDescriptionSettings("Application", Name = "TaskManageBC", Order = 10)]
public class TaskManageBCService: ITaskManageBCService, IDynamicApiController, ITransient
{
private readonly ISysCacheService _cache;
private readonly ILogger _logger;
private readonly SqlSugarRepository _taskBCInfoRepository;
private readonly SqlSugarRepository _taskBCCTNInfoRepository;
private readonly SqlSugarRepository _taskBaseRepository;
private readonly SqlSugarRepository _taskFileRepository;
private readonly SqlSugarRepository _bookingOrderRepository;
private readonly SqlSugarRepository _bookingCtnRepository;
private readonly SqlSugarRepository _sysUserRepository;
private readonly SqlSugarRepository _bookingFileRepository;
private readonly SqlSugarRepository _djyUserMailAccount;
private readonly SqlSugarRepository _bookingOrderContactRepository;
private readonly SqlSugarRepository _bookingSlotCompareRepository;
private readonly SqlSugarRepository _bookingSlotBaseRepository;
private readonly SqlSugarRepository _bookingSlotBaseCtnRepository;
private readonly SqlSugarRepository _bookingSlotAllocationRepository;
private readonly SqlSugarRepository _bookingSlotAllocationCtnRepository;
private readonly SqlSugarRepository _bookingDeliveryRecordRep;
private readonly SqlSugarRepository _bookingDeliveryRecordCtnRep;
private readonly IServiceWorkFlowBaseService _serviceWorkFlowBaseService;
private readonly IBookingOrderService _bookingOrderService;
private readonly IBookingSlotService _bookingSlotService;
private readonly IBookingValueAddedService _bookingValueAddedService;
private readonly IDjyCustomerService _djyCustomerService;
private readonly INamedServiceProvider _namedBookingMSKAPIServiceProvider;
private readonly IDjyTenantParamService _djyTenantParamService;
private readonly INamedServiceProvider _namedBookingSlotServiceProvider;
const string CONST_BC_FILE_CODE = "bc";
const string CONST_BC_FILE_NAME = "Booking Confirmation";
const string CONST_BC_NOTICE_FILE_CODE = "bc_notice";
const string CONST_BC_NOTICE_FILE_NAME = "Booking Confirmation Notice";
//租户BC默认转发KEY
const string CONST_BC_DEFAULT_PARAM = "BC_TASK_AUTO_TRANSMIT";
//租户AMENDMENT默认转发KEY
const string CONST_AMENDMENT_DEFAULT_PARAM = "AMENDMENT_TASK_AUTO_TRANSMIT";
//租户CANCELLATION默认转发KEY
const string CONST_CANCELLATION_DEFAULT_PARAM = "CANCELLATION_TASK_AUTO_TRANSMIT";
//租户AMENDMENT默认钉钉消息KEY
const string CONST_AMENDMENT_DING_DEFAULT_PARAM = "AMENDMENT_TASK_DING_NOTICE";
//租户CANCELLATION默认钉钉消息KEY
const string CONST_CANCELLATION_DING_DEFAULT_PARAM = "CANCELLATION_TASK_DING_NOTICE";
//租户默认参数管理(BC任务或舱位生成订舱客户联系人必填)
const string CONST_CREATE_BOOKING_NEED_CONTACT = "BC_TASK_OR_SLOT_BOOKING_NEED_CONTACT";
public TaskManageBCService(SqlSugarRepository taskBCInfoRepository,
SqlSugarRepository taskBaseRepository,
SqlSugarRepository taskBCCTNInfoRepository,
SqlSugarRepository taskFileRepository,
SqlSugarRepository bookingOrderRepository,
SqlSugarRepository bookingCtnRepository,
SqlSugarRepository sysUserRepository,
SqlSugarRepository bookingFileRepository,
SqlSugarRepository djyUserMailAccount,
IServiceWorkFlowBaseService serviceWorkFlowBaseService,
IBookingOrderService bookingOrderService, ILogger logger,
IDjyCustomerService djyCustomerService,
IBookingSlotService bookingSlotService, ISysCacheService cache, IBookingValueAddedService bookingValueAddedService,
INamedServiceProvider namedBookingSlotServiceProvider,
INamedServiceProvider namedBookingMSKAPIServiceProvider,
IDjyTenantParamService djyTenantParamService,
SqlSugarRepository bookingSlotBaseRepository,
SqlSugarRepository bookingSlotBaseCtnRepository,
SqlSugarRepository bookingSlotAllocationRepository,
SqlSugarRepository bookingSlotAllocationCtnRepository,
SqlSugarRepository bookingOrderContactRepository,
SqlSugarRepository bookingSlotCompareRepository)
{
_taskBaseRepository = taskBaseRepository;
_taskBCInfoRepository = taskBCInfoRepository;
_taskBCCTNInfoRepository = taskBCCTNInfoRepository;
_taskFileRepository = taskFileRepository;
_bookingOrderRepository = bookingOrderRepository;
_bookingCtnRepository = bookingCtnRepository;
_sysUserRepository = sysUserRepository;
_serviceWorkFlowBaseService = serviceWorkFlowBaseService;
_bookingOrderService = bookingOrderService;
_bookingSlotService = bookingSlotService;
_cache = cache;
_bookingValueAddedService = bookingValueAddedService;
_bookingFileRepository = bookingFileRepository;
_djyCustomerService = djyCustomerService;
_djyUserMailAccount = djyUserMailAccount;
_bookingOrderContactRepository = bookingOrderContactRepository;
_bookingSlotCompareRepository = bookingSlotCompareRepository;
_bookingSlotBaseRepository = bookingSlotBaseRepository;
_bookingSlotBaseCtnRepository = bookingSlotBaseCtnRepository;
_bookingSlotAllocationRepository = bookingSlotAllocationRepository;
_bookingSlotAllocationCtnRepository = bookingSlotAllocationCtnRepository;
_namedBookingMSKAPIServiceProvider = namedBookingMSKAPIServiceProvider;
_namedBookingSlotServiceProvider = namedBookingSlotServiceProvider;
_djyTenantParamService = djyTenantParamService;
_logger = logger;
}
#region 获取BC详情
///
/// 获取BC详情
///
/// BC主键
/// 返回回执
[HttpGet("/TaskManageBC/GetInfo")]
public async Task GetInfo(string pkId)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
var bcOrder = _taskBCInfoRepository.AsQueryable().First(a => a.PK_ID == pkId);
if (bcOrder == null)
throw Oops.Oh($"BC主键{pkId}无法获取业务信息");
var BCCtnList = _taskBCCTNInfoRepository.AsQueryable().Where(a => a.P_ID == pkId).ToList();
TaskBCShowBaseDto model = bcOrder.Adapt();
if (BCCtnList.Count > 0)
model.CtnList = BCCtnList.Adapt>();
var fileList = _taskFileRepository.AsQueryable().Where(a => a.TASK_PKID == bcOrder.TASK_ID).ToList();
if (fileList.Count > 0)
model.FileList = fileList.Adapt>();
result.succ = true;
result.ext = model;
//如果当前BC有对应记录,则读取订舱详情
if (bcOrder.BOOKING_ORDER_ID.HasValue)
{
var bkOrder = await _bookingOrderRepository.AsQueryable().
FirstAsync(a => a.Id == bcOrder.BOOKING_ORDER_ID.Value);
if (bkOrder != null)
{
var showBKOrder = bkOrder.Adapt();
var ctnList = await _bookingCtnRepository.AsQueryable().
Where(a => a.BILLID == bkOrder.Id).ToListAsync();
if (ctnList.Count > 0)
showBKOrder.ctnInputs = ctnList.Adapt>();
result.ext2 = showBKOrder;
}
}
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"获取BC详情异常,原因:{ex.Message}";
}
return result;
}
#endregion
#region 通过任务主键获取BC详情
///
/// 通过任务主键获取BC详情
///
/// BC任务主键
/// 返回回执
[HttpGet("/TaskManageBC/GetInfoByTaskId")]
public async Task GetInfoByTaskId(string taskPkId)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
var taskBase = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == taskPkId);
if (taskBase == null)
throw Oops.Oh($"任务主键{taskPkId}无法获取业务信息");
var bcOrder = _taskBCInfoRepository.AsQueryable().First(a => a.TASK_ID == taskBase.PK_ID);
if (bcOrder == null)
throw Oops.Oh($"任务主键{taskPkId}无法获取BC业务信息");
var bcCtnList = _taskBCCTNInfoRepository.AsQueryable().Where(a => a.P_ID == bcOrder.PK_ID).ToList();
TaskBCShowBaseDto model = bcOrder.Adapt();
if (bcCtnList.Count > 0)
model.CtnList = bcCtnList.Adapt>();
var fileList = _taskFileRepository.AsQueryable().Where(a => a.TASK_PKID == bcOrder.TASK_ID).ToList();
if (fileList.Count > 0)
model.FileList = fileList.Adapt>();
model.taskStatus = taskBase.STATUS;
//生成关键信息
#region 生成关键信息
model.Keywords = new List();
if(bcOrder.CARRIAGE_TYPE == "DIRECT_SHIP")
{
model.Keywords.Add(new TaskBCShowBaseKeywordDto() { Name = $"承运方式:{bcOrder.CARRIAGE_TYPE_NAME}", Background = "#FFFF80",Icon= "icon-yunshu1" });
}
else if (bcOrder.CARRIAGE_TYPE == "TRANSFER_SHIP")
{
model.Keywords.Add(new TaskBCShowBaseKeywordDto() { Name = $"承运方式:{bcOrder.CARRIAGE_TYPE_NAME}", Background = "#CAF982", Icon = "icon-shuaxin" });
}
if (bcOrder.BOOKING_SLOT_TYPE == "CONTRACT_ORDER")
{
model.Keywords.Add(new TaskBCShowBaseKeywordDto() { Name = $"订舱方式:{bcOrder.BOOKING_SLOT_TYPE_NAME}", Background = "#81D3F8", Icon = "icon-touzijilu" });
}
else if (bcOrder.BOOKING_SLOT_TYPE == "SPOT_ORDER")
{
model.Keywords.Add(new TaskBCShowBaseKeywordDto() { Name = $"订舱方式:{bcOrder.BOOKING_SLOT_TYPE_NAME}", Background = "#FACD91", Icon = "icon-beizhu1" });
}
#endregion
result.succ = true;
result.ext = model;
//如果当前BC有对应记录,则读取订舱详情
if (bcOrder.BOOKING_ORDER_ID.HasValue)
{
var bkOrder = await _bookingOrderRepository.AsQueryable().
FirstAsync(a => a.Id == bcOrder.BOOKING_ORDER_ID.Value);
if(bkOrder != null)
{
var showBKOrder = bkOrder.Adapt();
var ctnList = await _bookingCtnRepository.AsQueryable().
Where(a => a.BILLID == bkOrder.Id).ToListAsync();
if (ctnList.Count > 0)
showBKOrder.ctnInputs = ctnList.Adapt>();
result.ext2 = showBKOrder;
}
}
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"获取BC详情异常,原因:{ex.Message}";
}
return result;
}
#endregion
#region 获取待处理的BC任务
///
/// 获取待处理的BC任务(来自邮件解析需要对应订舱,系统会根据用户的订舱台账预配)
///
/// 返回回执
[HttpGet("/TaskManageBC/GetToDoBCList")]
public async Task GetToDoBCList()
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
/*
1、优先匹配提单号一致的
2、判断船名航次一致的
*/
try
{
Dictionary> toDoListDict = new Dictionary>();
//获取所有待处理的BC任务
var taskList = await _taskBCInfoRepository.AsQueryable().InnerJoin((a,b)=>a.TASK_ID == b.PK_ID)
.Where((a, b)=> !a.BOOKING_ORDER_ID.HasValue && b.STATUS == TaskStatusEnum.Create.ToString() && b.TASK_BASE_TYPE == TaskBaseTypeEnum.BC.ToString())
.Select((a,b)=>new { BC = a,TSK = b }).ToListAsync();
if (taskList.Count > 0)
{
taskList.ForEach(async tsk =>
{
var curList = await _bookingOrderRepository.AsQueryable()
.Where(a => a.VESSEL.Contains(tsk.BC.VESSEL) && a.VOYNO.Contains(tsk.BC.VOYNO) || a.MBLNO.Contains(tsk.BC.MBL_NO)
).ToListAsync();
if (curList.Count > 0)
{
toDoListDict.Add(tsk.BC.PK_ID, curList);
}
else
{
toDoListDict.Add(tsk.BC.PK_ID, new List());
}
});
}
//这里最后清洗一下对应的订舱数据,只保留一条符合的数据
if (toDoListDict.Count > 0)
{
List> tupList = new List>();
int num = 1;
int odNum = 1;
foreach (var kvp in toDoListDict)
{
var bcInfo = taskList.FirstOrDefault(a => a.BC.PK_ID == kvp.Key).BC.Adapt();
bcInfo.Indx = num;
if (kvp.Value.Count > 0)
{
var bookingOrder = kvp.Value.Select(a =>
{
if (a.MBLNO.Equals(bcInfo.MBLNo, StringComparison.OrdinalIgnoreCase))
{
return new { Sort = 90, OBJ = a };
}
else if (a.VESSEL.Equals(bcInfo.Vessel, StringComparison.OrdinalIgnoreCase)
&& a.VOYNO.Equals(bcInfo.VoyNo, StringComparison.OrdinalIgnoreCase))
{
return new { Sort = 80, OBJ = a };
}
return new { Sort = 1, OBJ = a };
}).OrderByDescending(a => a.Sort).FirstOrDefault().OBJ.Adapt();
bookingOrder.Indx = odNum;
bookingOrder.BCIndx = num;
bcInfo.BKOrderIndx = odNum;
odNum++;
tupList.Add(new Tuple(
bcInfo,
bookingOrder
));
}
else
{
tupList.Add(new Tuple(
bcInfo,
null
));
}
num++;
}
result.ext = tupList.Select(a=>a.Item1).ToList();
result.ext2 = tupList.Select(a => a.Item2).ToList();
}
result.succ = true;
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"获取派车详情异常,原因:{ex.Message}";
}
return result;
}
#endregion
#region 任务ID下载附件
///
/// 任务ID下载附件
///
/// BC任务主键
/// 附件分类代码
/// 返回数据流
[HttpGet("/TaskManageBC/DownloadFile")]
public async Task DownloadFile([FromQuery] string taskPKId, [FromQuery] string fileCategory = "BC")
{
var bcTaskInfo = await _taskBaseRepository.AsQueryable().FirstAsync(u => u.PK_ID == taskPKId);
if (bcTaskInfo == null)
{
throw Oops.Oh($"任务主键{taskPKId}无法获取业务信息");
}
TaskFileCategoryEnum fileCategoryEnum = TaskFileCategoryEnum.NONE;
System.Enum.TryParse(fileCategory, out fileCategoryEnum);
if (fileCategoryEnum == TaskFileCategoryEnum.NONE)
{
throw Oops.Oh($"附件分类代码错误,请提供正确的分类代码");
}
string name = fileCategoryEnum.ToString();
var fileInfo = await _taskFileRepository.AsQueryable().FirstAsync(u => u.TASK_PKID == taskPKId && u.FILE_CATEGORY == name);
if (fileInfo == null)
{
throw Oops.Oh($"任务主键{taskPKId}没有可下载的附件");
}
var opt = App.GetOptions();
var dirAbs = opt.basePath;
if (string.IsNullOrEmpty(dirAbs))
{
dirAbs = App.WebHostEnvironment.WebRootPath;
}
var fileFullPath = Path.Combine(dirAbs, fileInfo.FILE_PATH);
if (!File.Exists(fileFullPath))
{
throw Oops.Oh($"任务主键{taskPKId} 附件下载请求失败,请确认文件是否存在");
}
var fileName = HttpUtility.UrlEncode(fileInfo.FILE_NAME, Encoding.GetEncoding("UTF-8"));
var result = new FileStreamResult(new FileStream(fileFullPath, FileMode.Open), "application/octet-stream") { FileDownloadName = fileName };
return result;
}
#endregion
#region 检索订舱信息
///
/// 检索订舱信息
///
/// 检索条件
/// 返回回执
[HttpPost("/TaskManageBC/QueryBookingOrderList")]
public async Task QueryBookingOrderList([FromBody] BookingOrderBCQuery query)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
bool isAvailable = false;
var queryWhere = _bookingOrderRepository.AsQueryable();
#region 查询条件
if (query.beginETD.HasValue || query.endETD.HasValue)
{
/*
起始结束时间间隔不能超过7天
*/
DateTime beginDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
DateTime endDate = beginDate;
if (query.beginETD.HasValue)
beginDate = query.beginETD.Value;
if (query.endETD.HasValue)
endDate = query.endETD.Value;
if (endDate > beginDate.AddDays(7))
throw Oops.Oh($"船期的日期范围不能超过7天");
endDate = endDate.AddDays(1);
queryWhere = queryWhere.Where(a => a.ETD >= beginDate && a.ETD < endDate);
isAvailable = true;
}
if (query.beginCreated.HasValue || query.endCreated.HasValue)
{
/*
起始结束时间间隔不能超过7天
*/
DateTime beginDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd"));
DateTime endDate = beginDate;
if (query.beginCreated.HasValue)
beginDate = query.beginCreated.Value;
if (query.endCreated.HasValue)
endDate = query.endCreated.Value;
if (endDate > beginDate.AddDays(7))
throw Oops.Oh($"制单的日期范围不能超过7天");
endDate = endDate.AddDays(1);
queryWhere = queryWhere.Where(a => a.CreatedTime >= beginDate && a.CreatedTime < endDate);
isAvailable = true;
}
if (!string.IsNullOrWhiteSpace(query.mblNo))
{
queryWhere = queryWhere.Where(a => a.MBLNO.Contains(query.mblNo));
isAvailable = true;
}
if (!string.IsNullOrWhiteSpace(query.custNo))
{
queryWhere = queryWhere.Where(a => a.CUSTNO.Contains(query.custNo));
isAvailable = true;
}
if (!string.IsNullOrWhiteSpace(query.vessel))
{
queryWhere = queryWhere.Where(a => a.VESSEL.Contains(query.vessel));
isAvailable = true;
}
if (!string.IsNullOrWhiteSpace(query.voyno))
{
queryWhere = queryWhere.Where(a => a.VOYNO.Contains(query.voyno));
isAvailable = true;
}
#endregion
if (!isAvailable)
throw Oops.Oh($"查询条件不能为空");
var list = await queryWhere.OrderBy(a => a.CreatedTime)
.Take(query.topNum).ToListAsync();
var bkList = list.Adapt>();
result.succ = true;
result.ext = bkList;
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"查询失败,原因:{ex.Message}";
}
return result;
}
#endregion
#region 转移任务(将任务指定给其他人)
///
/// 转移任务(将任务指定给其他人)
///
/// BC任务主键,若多条以逗号分隔
/// 用户ID
/// 返回回执
[HttpGet("/TaskManageBC/TransferTask")]
public async Task TransferTask([FromQuery] string taskPKId, [FromQuery] long userId)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
//判断用户信息有效性
if (userId < 0)
throw Oops.Oh($"指定用户ID不能为空");
var targetUserId = _sysUserRepository.AsQueryable().First(u => u.Id == userId);
if (targetUserId == null)
throw Oops.Oh($"指定用户不存在");
string[] strings = taskPKId.Split(',');
foreach (var item in strings)
{
var bcTaskInfo = await _taskBaseRepository.AsQueryable().FirstAsync(u => u.PK_ID == item);
if (bcTaskInfo == null)
{
throw Oops.Oh($"任务主键{item}无法获取业务信息");
}
if (bcTaskInfo.IS_PUBLIC == 1)
{
bcTaskInfo.IS_PUBLIC = 0;
bcTaskInfo.RealUserId = targetUserId.Id;
bcTaskInfo.RealUserName = targetUserId.Name;
bcTaskInfo.UpdatedTime = DateTime.Now;
bcTaskInfo.UpdatedUserId = targetUserId.Id;
bcTaskInfo.UpdatedUserName = targetUserId.Name;
await _taskBaseRepository.AsUpdateable(bcTaskInfo).IgnoreColumns(it => new
{
it.TenantId,
it.CreatedTime,
it.IsDeleted,
it.TASK_NO,
it.TASK_TYPE,
it.TASK_SOURCE
}).ExecuteCommandAsync();
}
else
{
bcTaskInfo.RealUserId = targetUserId.Id;
bcTaskInfo.RealUserName = targetUserId.Name;
bcTaskInfo.UpdatedTime = DateTime.Now;
bcTaskInfo.UpdatedUserId = targetUserId.Id;
bcTaskInfo.UpdatedUserName = targetUserId.Name;
await _taskBaseRepository.AsUpdateable(bcTaskInfo).IgnoreColumns(it => new
{
it.TenantId,
it.CreatedTime,
it.IsDeleted,
it.TASK_NO,
it.TASK_TYPE,
it.TASK_SOURCE
}).ExecuteCommandAsync();
}
}
result.succ = true;
result.msg = "成功";
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"转移任务失败,原因:{ex.Message}";
}
return result;
}
#endregion
#region 生成订舱或舱位
///
/// 生成订舱或舱位
///
/// 生成订舱或者舱位请求
/// 返回回执
[HttpPost("/TaskManageBC/CreateBookingAndSlot")]
public async Task CreateBookingAndSlot([FromBody] BookingOrSlotGenerateDto model)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
_logger.LogInformation($"接收生成订舱或舱位请求,参数{JSON.Serialize(model)}");
try
{
/*
1、GEN_BOOKING_SLOT-生成舱位和订舱
(1)生成舱位
(2)生成海运订舱
(3)更新舱位库存
(4)更新BC任务
2、GEN_BOOKING-只生成订舱
(1)生成海运订舱
(4)更新BC任务
3、GEN_SLOT-只生成舱位
(1)生成舱位
(4)更新BC任务
3、GEN_SLOT-只生成舱位
(1)查找订舱记录
(3)更新舱位库存
(4)更新BC任务
*/
if (string.IsNullOrWhiteSpace(model.BCTaskId))
throw Oops.Oh($"BC任务主键不能为空");
//生成方式(GEN_BOOKING_SLOT-生成舱位和订舱;GEN_BOOKING-只生成订舱;GEN_SLOT-只生成舱位;GEN_EXIST_BOOKING-匹配指定的订舱)
if (string.IsNullOrWhiteSpace(model.GenerateMethod))
throw Oops.Oh($"生成方式不能为空,需要指定一种生成方式");
var bcTaskInfo = await _taskBaseRepository.AsQueryable().FirstAsync(u => u.PK_ID == model.BCTaskId);
if (bcTaskInfo == null)
{
throw Oops.Oh($"任务主键{model.BCTaskId}无法获取业务信息");
}
var bcOrder = _taskBCInfoRepository.AsQueryable().First(a => a.TASK_ID == bcTaskInfo.PK_ID);
if (bcOrder == null)
throw Oops.Oh($"任务主键{model.BCTaskId}无法获取BC业务信息");
var bcCtnList = _taskBCCTNInfoRepository.AsQueryable().Where(a => a.P_ID == bcOrder.PK_ID).ToList();
var fileList = _taskFileRepository.AsQueryable().Where(a => a.TASK_PKID == bcTaskInfo.PK_ID).ToList();
if (model.GenerateMethod != "UPD_BOOKING")
{
if (bcOrder.BOOKING_ORDER_ID.HasValue && bcOrder.BOOKING_ORDER_ID.Value > 0)
{
throw Oops.Oh($"当前BC任务已生成订舱订单,不能重复生成");
}
}
if (model.GenerateMethod == "GEN_BOOKING_SLOT")
{
#region 推送舱位、推送订舱
//推送舱位
long bookingSlotId = 0;
if (bcOrder.BOOKING_SLOT_ID.HasValue && bcOrder.BOOKING_SLOT_ID.Value > 0)
{
//bookingSlotId = bcOrder.BOOKING_SLOT_ID.Value;
throw Oops.Oh($"生成舱位失败,舱位已存在");
}
//这里增加委托单位联系人的校验,重新从后台拉取了委托单位相关联系人,如果比对不一致跳出异常终止执行
ValidateContact(model);
//触发生成舱位
bookingSlotId = await GenerateBookingSlot(bcOrder, bcCtnList, fileList);
_logger.LogInformation($"生成舱位完成,bookingSlotId={bookingSlotId} taskid={bcOrder.TASK_ID}");
if(bookingSlotId < 1)
throw Oops.Oh($"生成舱位失败");
//推送订舱订单
var bookingOrderId = await GenerateBookingOrder(bcOrder, bcCtnList, fileList, model);
_logger.LogInformation($"生成订舱订单完成,bookingOrderId={bookingOrderId} taskid={bcOrder.TASK_ID}");
if (bookingOrderId < 1)
throw Oops.Oh($"生成订舱订单失败");
List slots = new List();
//检索舱位信息
var slotInfo = _bookingSlotService.Detail(bookingSlotId).GetAwaiter().GetResult();
BookingSlotBaseWithCtnDto baseInfo = slotInfo.Adapt();
baseInfo.Id = bookingSlotId;
baseInfo.CtnList = slotInfo.CtnList.Adapt>();
slots.Add(baseInfo);
//对应订舱和舱位关系
var allocRlt = await _bookingSlotService.ImportSlots(slots, bookingOrderId, false);
_logger.LogInformation($"生成订舱和舱位关系完成,allocRlt={JSON.Serialize(allocRlt)} taskid={bcOrder.TASK_ID}");
if (!allocRlt.isSuccess)
{
throw Oops.Oh($"生成订舱和舱位关系失败");
}
var bcEntity = _taskBCInfoRepository.AsQueryable().First(a => a.PK_ID == bcOrder.PK_ID);
if (bcEntity == null)
{
throw Oops.Oh($"未获取有效任务BC失败,更新失败");
}
bcEntity.BOOKING_ORDER_ID = bookingOrderId;
bcEntity.BOOKING_SLOT_ID = bookingSlotId;
bcEntity.UpdatedTime = DateTime.Now;
bcEntity.UpdatedUserId = UserManager.UserId;
bcEntity.UpdatedUserName = UserManager.Name;
//更新任务BC
await _taskBCInfoRepository.AsUpdateable(bcEntity).UpdateColumns(it => new
{
it.BOOKING_ORDER_ID,
it.BOOKING_SLOT_ID,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
var taskEntity = _taskBaseRepository.AsQueryable().First(u => u.PK_ID == bcEntity.TASK_ID);
if (taskEntity == null)
{
throw Oops.Oh($"未获取有效任务记录,更新失败");
}
#region 更新任务
//如果是公共任务,需要变成个人任务 RealUserId = 当前操作人
if (taskEntity.IS_PUBLIC == 1)
{
taskEntity.IS_PUBLIC = 0;
taskEntity.RealUserId = UserManager.UserId;
taskEntity.RealUserName = UserManager.Name;
taskEntity.UpdatedTime = DateTime.Now;
taskEntity.UpdatedUserId = UserManager.UserId;
taskEntity.UpdatedUserName = UserManager.Name;
taskEntity.IS_COMPLETE = 1;
taskEntity.COMPLETE_DATE = DateTime.Now;
taskEntity.COMPLETE_DEAL = "MANUAL";
taskEntity.COMPLETE_DEAL = "手工";
await _taskBaseRepository.AsUpdateable(taskEntity).UpdateColumns(it => new
{
it.IS_PUBLIC,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName,
it.RealUserId,
it.RealUserName,
it.IS_COMPLETE,
it.COMPLETE_DATE,
it.COMPLETE_DEAL,
it.COMPLETE_DEAL_NAME
}).ExecuteCommandAsync();
}
else
{
taskEntity.UpdatedTime = DateTime.Now;
taskEntity.UpdatedUserId = UserManager.UserId;
taskEntity.UpdatedUserName = UserManager.Name;
taskEntity.IS_COMPLETE = 1;
taskEntity.COMPLETE_DATE = DateTime.Now;
taskEntity.COMPLETE_DEAL = "MANUAL";
taskEntity.COMPLETE_DEAL = "手工";
await _taskBaseRepository.AsUpdateable(taskEntity).UpdateColumns(it => new
{
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName,
it.IS_COMPLETE,
it.COMPLETE_DATE,
it.COMPLETE_DEAL,
it.COMPLETE_DEAL_NAME
}).ExecuteCommandAsync();
}
#endregion
var currBCOrder = _taskBCInfoRepository.AsQueryable().First(a => a.PK_ID == bcEntity.PK_ID);
if (currBCOrder != null && model.IsDirectSend)
{
//异步推送邮件
var mailRlt = await GenerateSendEmail(currBCOrder, bcTaskInfo, model.usePersonalEmailSend);
if (!mailRlt.succ)
{
throw Oops.Oh($"邮件推送失败,原因:{mailRlt.msg},可以任务编辑页面重新发送邮件");
}
}
#endregion
}
else if (model.GenerateMethod == "GEN_BOOKING")
{
#region 推送订舱
long bookingId = 0;
long bookingSlotId = 0;
if (bcOrder.BOOKING_ORDER_ID.HasValue && bcOrder.BOOKING_ORDER_ID.Value > 0)
{
//bookingSlotId = bcOrder.BOOKING_SLOT_ID.Value;
throw Oops.Oh($"生成订舱订单失败,订舱订单已存在");
}
if (!bcOrder.BOOKING_SLOT_ID.HasValue || bcOrder.BOOKING_SLOT_ID.Value < 1)
{
//bookingSlotId = bcOrder.BOOKING_SLOT_ID.Value;
throw Oops.Oh($"生成订舱订单失败,舱位未生成不能直接生成订舱订单");
}
bookingSlotId = bcOrder.BOOKING_SLOT_ID.Value;
//这里增加委托单位联系人的校验,重新从后台拉取了委托单位相关联系人,如果比对不一致跳出异常终止执行
ValidateContact(model);
//推送订舱订单
var bookingOrderId = await GenerateBookingOrder(bcOrder, bcCtnList, fileList, model);
List slots = new List();
//检索舱位信息
var slotInfo = _bookingSlotService.Detail(bookingSlotId).GetAwaiter().GetResult();
BookingSlotBaseWithCtnDto baseInfo = slotInfo.Adapt();
baseInfo.Id = bookingSlotId;
baseInfo.CtnList = slotInfo.CtnList.Adapt>();
slots.Add(baseInfo);
//对应订舱和舱位关系
var allocRlt = await _bookingSlotService.ImportSlots(slots, bookingOrderId, false);
_logger.LogInformation($"生成订舱和舱位关系完成,allocRlt={JSON.Serialize(allocRlt)} taskid={bcOrder.TASK_ID}");
if (!allocRlt.isSuccess)
{
throw Oops.Oh($"生成订舱和舱位关系失败");
}
var bcEntity = _taskBCInfoRepository.AsQueryable().First(a => a.PK_ID == bcOrder.PK_ID);
if (bcEntity == null)
{
throw Oops.Oh($"未获取有效任务BC失败,更新失败");
}
bcEntity.BOOKING_ORDER_ID = bookingOrderId;
bcEntity.BOOKING_SLOT_ID = bookingSlotId;
bcEntity.UpdatedTime = DateTime.Now;
bcEntity.UpdatedUserId = UserManager.UserId;
bcEntity.UpdatedUserName = UserManager.Name;
//更新任务BC
await _taskBCInfoRepository.AsUpdateable(bcEntity).UpdateColumns(it => new
{
it.BOOKING_ORDER_ID,
it.BOOKING_SLOT_ID,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
var taskEntity = _taskBaseRepository.AsQueryable().First(u => u.PK_ID == bcEntity.TASK_ID);
if (taskEntity == null)
{
throw Oops.Oh($"未获取有效任务记录,更新失败");
}
#region 更新任务
//如果是公共任务,需要变成个人任务 RealUserId = 当前操作人
if (taskEntity.IS_PUBLIC == 1)
{
taskEntity.IS_PUBLIC = 0;
taskEntity.RealUserId = UserManager.UserId;
taskEntity.RealUserName = UserManager.Name;
taskEntity.UpdatedTime = DateTime.Now;
taskEntity.UpdatedUserId = UserManager.UserId;
taskEntity.UpdatedUserName = UserManager.Name;
taskEntity.IS_COMPLETE = 1;
taskEntity.COMPLETE_DATE = DateTime.Now;
taskEntity.COMPLETE_DEAL = "MANUAL";
taskEntity.COMPLETE_DEAL = "手工";
await _taskBaseRepository.AsUpdateable(taskEntity).UpdateColumns(it => new
{
it.IS_PUBLIC,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName,
it.RealUserId,
it.RealUserName,
it.IS_COMPLETE,
it.COMPLETE_DATE,
it.COMPLETE_DEAL,
it.COMPLETE_DEAL_NAME
}).ExecuteCommandAsync();
}
else
{
taskEntity.UpdatedTime = DateTime.Now;
taskEntity.UpdatedUserId = UserManager.UserId;
taskEntity.UpdatedUserName = UserManager.Name;
taskEntity.IS_COMPLETE = 1;
taskEntity.COMPLETE_DATE = DateTime.Now;
taskEntity.COMPLETE_DEAL = "MANUAL";
taskEntity.COMPLETE_DEAL = "手工";
await _taskBaseRepository.AsUpdateable(taskEntity).UpdateColumns(it => new
{
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName,
it.IS_COMPLETE,
it.COMPLETE_DATE,
it.COMPLETE_DEAL,
it.COMPLETE_DEAL_NAME
}).ExecuteCommandAsync();
}
#endregion
var currBCOrder = _taskBCInfoRepository.AsQueryable().First(a => a.PK_ID == bcEntity.PK_ID);
if (currBCOrder != null && model.IsDirectSend)
{
//异步推送邮件
var mailRlt = await GenerateSendEmail(currBCOrder, bcTaskInfo, model.usePersonalEmailSend);
if (!mailRlt.succ)
{
throw Oops.Oh($"邮件推送失败,原因:{mailRlt.msg},可以任务编辑页面重新发送邮件");
}
}
#endregion
}
else if (model.GenerateMethod == "GEN_SLOT")
{
long bookingSlotId = 0;
if (bcOrder.BOOKING_SLOT_ID.HasValue && bcOrder.BOOKING_SLOT_ID.Value > 0)
{
//bookingSlotId = bcOrder.BOOKING_SLOT_ID.Value;
throw Oops.Oh($"生成舱位失败,舱位已存在");
}
//触发生成舱位
bookingSlotId = await GenerateBookingSlot(bcOrder, bcCtnList, fileList);
_logger.LogInformation($"生成舱位完成,bookingSlotId={bookingSlotId} taskid={bcOrder.TASK_ID}");
if (bookingSlotId < 1)
throw Oops.Oh($"生成舱位失败");
var bcEntity = _taskBCInfoRepository.AsQueryable().First(a => a.PK_ID == bcOrder.PK_ID);
if (bcEntity == null)
{
throw Oops.Oh($"未获取有效任务BC失败,更新失败");
}
bcEntity.BOOKING_SLOT_ID = bookingSlotId;
bcEntity.UpdatedTime = DateTime.Now;
bcEntity.UpdatedUserId = UserManager.UserId;
bcEntity.UpdatedUserName = UserManager.Name;
//更新任务BC
await _taskBCInfoRepository.AsUpdateable(bcEntity).UpdateColumns(it => new
{
it.BOOKING_SLOT_ID,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
var taskEntity = _taskBaseRepository.AsQueryable().First(u => u.PK_ID == bcEntity.TASK_ID);
if (taskEntity == null)
{
throw Oops.Oh($"未获取有效任务记录,更新失败");
}
#region 更新任务
//如果是公共任务,需要变成个人任务 RealUserId = 当前操作人
if (taskEntity.IS_PUBLIC == 1)
{
taskEntity.IS_PUBLIC = 0;
taskEntity.RealUserId = UserManager.UserId;
taskEntity.RealUserName = UserManager.Name;
taskEntity.UpdatedTime = DateTime.Now;
taskEntity.UpdatedUserId = UserManager.UserId;
taskEntity.UpdatedUserName = UserManager.Name;
await _taskBaseRepository.AsUpdateable(taskEntity).UpdateColumns(it => new
{
it.IS_PUBLIC,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName,
it.RealUserId,
it.RealUserName
}).ExecuteCommandAsync();
}
else
{
taskEntity.UpdatedTime = DateTime.Now;
taskEntity.UpdatedUserId = UserManager.UserId;
taskEntity.UpdatedUserName = UserManager.Name;
await _taskBaseRepository.AsUpdateable(taskEntity).UpdateColumns(it => new
{
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
}
#endregion
}
else if (model.GenerateMethod == "UPD_BOOKING")
{
//这里增加委托单位联系人的校验,重新从后台拉取了委托单位相关联系人,如果比对不一致跳出异常终止执行
ValidateContact(model);
//推送订舱订单
var bookingOrderId = await UpdateBookingOrder(bcOrder, bcCtnList, fileList, model);
var taskEntity = _taskBaseRepository.AsQueryable().First(u => u.PK_ID == bcOrder.TASK_ID);
if (taskEntity == null)
{
throw Oops.Oh($"未获取有效任务记录,更新失败");
}
#region 更新任务
//如果是公共任务,需要变成个人任务 RealUserId = 当前操作人
if (taskEntity.IS_PUBLIC == 1)
{
taskEntity.IS_PUBLIC = 0;
taskEntity.RealUserId = UserManager.UserId;
taskEntity.RealUserName = UserManager.Name;
taskEntity.UpdatedTime = DateTime.Now;
taskEntity.UpdatedUserId = UserManager.UserId;
taskEntity.UpdatedUserName = UserManager.Name;
taskEntity.IS_COMPLETE = 1;
taskEntity.COMPLETE_DATE = DateTime.Now;
taskEntity.COMPLETE_DEAL = "MANUAL";
taskEntity.COMPLETE_DEAL = "手工";
await _taskBaseRepository.AsUpdateable(taskEntity).UpdateColumns(it => new
{
it.IS_PUBLIC,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName,
it.RealUserId,
it.RealUserName,
it.IS_COMPLETE,
it.COMPLETE_DATE,
it.COMPLETE_DEAL,
it.COMPLETE_DEAL_NAME
}).ExecuteCommandAsync();
}
else
{
taskEntity.UpdatedTime = DateTime.Now;
taskEntity.UpdatedUserId = UserManager.UserId;
taskEntity.UpdatedUserName = UserManager.Name;
taskEntity.IS_COMPLETE = 1;
taskEntity.COMPLETE_DATE = DateTime.Now;
taskEntity.COMPLETE_DEAL = "MANUAL";
taskEntity.COMPLETE_DEAL = "手工";
await _taskBaseRepository.AsUpdateable(taskEntity).UpdateColumns(it => new
{
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName,
it.IS_COMPLETE,
it.COMPLETE_DATE,
it.COMPLETE_DEAL,
it.COMPLETE_DEAL_NAME
}).ExecuteCommandAsync();
}
#endregion
var currBCOrder = _taskBCInfoRepository.AsQueryable().First(a => a.PK_ID == bcOrder.PK_ID);
if (currBCOrder != null && model.IsDirectSend)
{
//异步推送邮件
var mailRlt = await GenerateSendEmail(currBCOrder, bcTaskInfo, model.usePersonalEmailSend);
if (!mailRlt.succ)
{
throw Oops.Oh($"邮件推送失败,原因:{mailRlt.msg},可以任务编辑页面重新发送邮件");
}
}
}
result.succ = true;
result.msg = "成功";
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"生成订舱或舱位失败,原因:{ex.Message}";
}
return result;
}
#endregion
#region 生成舱位
///
/// 生成舱位
///
/// BC任务详情
/// BC任务集装箱列表
/// BC任务附件列表
/// 返回舱位ID
private async Task GenerateBookingSlot(TaskBCInfo taskBCInfo, List taskBCCtnList, List taskFileList,string opType = "add")
{
long id = 0;
try
{
var carrierInfo = _cache.GetAllCodeCarrier().GetAwaiter().GetResult()
.Where(t => t.Code.Equals(taskBCInfo.CARRIERID, StringComparison.OrdinalIgnoreCase)
|| t.EnName.Equals(taskBCInfo.CARRIERID, StringComparison.OrdinalIgnoreCase)
|| t.CnName.Equals(taskBCInfo.CARRIERID, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
BookingSlotBaseApiDto slotModel = new BookingSlotBaseApiDto
{
DataObj = new BookingSlotBaseApiSaveDto
{
CARRIERID = taskBCInfo.CARRIERID,
CARRIER = carrierInfo.CnName?.Trim(),
SLOT_BOOKING_NO = taskBCInfo.MBL_NO,
BOOKING_PARTY = taskBCInfo.BOOKING_PARTY,
BOOKING_SLOT_TYPE = taskBCInfo.BOOKING_SLOT_TYPE,
BOOKING_SLOT_TYPE_NAME = taskBCInfo.BOOKING_SLOT_TYPE_NAME,
VESSEL = taskBCInfo.VESSEL,
VOYNO = taskBCInfo.VOYNO,
VGM_SUBMISSION_CUT_DATE = taskBCInfo.VGM_CUTOFF_TIME,
//WEEK_AT = taskBCInfo.WEEK_AT,
CARRIAGE_TYPE = taskBCInfo.CARRIAGE_TYPE,
CARRIAGE_TYPE_NAME = taskBCInfo.CARRIAGE_TYPE_NAME,
CONTRACT_NO = taskBCInfo.CONTRACTNO,
CTN_STAT = taskBCInfo.CTN_STAT,
CY_CUT_DATE = taskBCInfo.CY_CUTOFF_TIME,
DETENSION_FREE_DAYS = taskBCInfo.DETENSION_FREE_DAYS,
ETD = taskBCInfo.ETD,
ETA = taskBCInfo.ETA,
LANECODE = taskBCInfo.LANECODE,
LANENAME = taskBCInfo.LANENAME,
MANIFEST_CUT_DATE = taskBCInfo.MANIFEST_CUT_DATE,
MDGF_CUT_DATE = taskBCInfo.MDGF_CUT_DATE,
PLACEDELIVERY = taskBCInfo.PLACEDELIVERY,
PLACERECEIPT = taskBCInfo.PLACERECEIPT,
PORTDISCHARGE = taskBCInfo.PORTDISCHARGE,
PORTLOAD = taskBCInfo.PORTLOAD,
SI_CUT_DATE = taskBCInfo.SI_CUT_DATE,
TRANSFER_PORT_1 = taskBCInfo.TRANSFER_PORT_1,
TRANSFER_PORT_2 = taskBCInfo.TRANSFER_PORT_2,
CtnList = new List()
},
OpType = opType
};
if (int.TryParse(taskBCInfo.WEEK_AT, out int _weekat))
{
slotModel.DataObj.WEEK_AT = _weekat;
}
var ctnCodeList = _cache.GetAllCodeCtn().GetAwaiter().GetResult().ToList();
if (taskBCCtnList.Count > 0)
{
taskBCCtnList.ForEach(t =>
{
var ctnCode = ctnCodeList.FirstOrDefault(a => !string.IsNullOrWhiteSpace(a.Name) &&
a.Name.Equals(t.CTNALL, StringComparison.OrdinalIgnoreCase));
BookingSlotCtnSaveInput ctn = new BookingSlotCtnSaveInput
{
CTNCODE = ctnCode?.Code,
CTNALL = t.CTNALL,
CTNNUM = t.CTNNUM.HasValue ? t.CTNNUM.Value : 1
};
slotModel.DataObj.CtnList.Add(ctn);
});
}
var opt = App.GetOptions();
var dirAbs = opt.basePath;
if (string.IsNullOrEmpty(dirAbs))
{
dirAbs = App.WebHostEnvironment.WebRootPath;
}
DynameFileInfo dynameFile = null;
DynameFileInfo dynameNoticeFile = null;
if (taskFileList.Any(t => t.FILE_CATEGORY == TaskFileCategoryEnum.BC.ToString()))
{
var fileInfo = taskFileList.FirstOrDefault(t => t.FILE_CATEGORY == TaskFileCategoryEnum.BC.ToString());
var fileFullPath = Path.Combine(dirAbs, fileInfo.FILE_PATH);
dynameFile = new DynameFileInfo
{
FileBytes = File.ReadAllBytes(fileFullPath),
FileName = Path.GetFileName(fileFullPath)
};
}
if (taskFileList.Any(t => t.FILE_CATEGORY == TaskFileCategoryEnum.BC_NOTICE.ToString()))
{
var fileInfo = taskFileList.FirstOrDefault(t => t.FILE_CATEGORY == TaskFileCategoryEnum.BC.ToString());
var fileFullPath = Path.Combine(dirAbs, fileInfo.FILE_PATH);
dynameNoticeFile = new DynameFileInfo
{
FileBytes = File.ReadAllBytes(fileFullPath),
FileName = Path.GetFileName(fileFullPath)
};
}
id = await _bookingSlotService.InnerApiReceive(slotModel, dynameFile, dynameNoticeFile);
}
catch (Exception ex)
{
_logger.LogError($"任务BC MBLNO:{taskBCInfo.MBL_NO} 生成舱位异常,原因:{ex.Message}");
throw Oops.Oh($"MBLNO:{taskBCInfo.MBL_NO} 生成舱位异常,原因:{ex.Message}");
}
return id;
}
#endregion
#region 生成订舱
///
/// 生成订舱
///
/// BC任务详情
/// BC任务集装箱列表
/// BC任务附件列表
/// 订舱请求详情
/// 返回订舱ID
private async Task GenerateBookingOrder(TaskBCInfo taskBCInfo, List taskBCCtnList, List taskFileList,
BookingOrSlotGenerateDto generateModel)
{
long id = 0;
try
{
/*
1、新增订舱
2、推送服务项目
3、推送附件
*/
var carrierInfo = _cache.GetAllCodeCarrier().GetAwaiter().GetResult()
.Where(t => t.Code.Equals(taskBCInfo.CARRIERID, StringComparison.OrdinalIgnoreCase)
|| t.EnName.Equals(taskBCInfo.CARRIERID, StringComparison.OrdinalIgnoreCase)
|| t.CnName.Equals(taskBCInfo.CARRIERID, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
SaveBookingOrderInput bkModel = new SaveBookingOrderInput
{
CUSTOMERID = generateModel.CustomerId,
CUSTOMERNAME = generateModel.CustomerName,
CARRIERID = carrierInfo.Code?.Trim(),
CARRIER = carrierInfo.CnName?.Trim(),
MBLNO = taskBCInfo.MBL_NO.ToUpper().Trim(),
CONTRACTNO = !string.IsNullOrWhiteSpace(taskBCInfo.CONTRACTNO) ? taskBCInfo.CONTRACTNO : "",
VESSEL = taskBCInfo.VESSEL.ToUpper().Trim(),
VOYNO = taskBCInfo.VOYNO.ToUpper().Trim(),
VOYNOINNER = taskBCInfo.VOYNO.ToUpper().Trim(),
ETD = taskBCInfo.ETD,
ETA = taskBCInfo.ETA,
SALEID = generateModel.SaleId.ToString(),
SALE = generateModel.SaleName,
OPID = generateModel.OpId.ToString(),
OP = generateModel.OpName,
DOCID = generateModel.DocId.ToString(),
DOC = generateModel.DocName,
ROUTEID = generateModel.RouteID.ToString(),
ROUTE = generateModel.Route,
CZRemark = generateModel.CZRemark,
ShenQingXiangShi = generateModel.ShenQingXiangShi,
LineManageID = generateModel.LineManageID.ToString(),
LineName = generateModel.LineManage,
CLOSEVGMDATE = taskBCInfo.VGM_CUTOFF_TIME,
CLOSINGDATE = taskBCInfo.CY_CUTOFF_TIME,
CLOSEDOCDATE = taskBCInfo.CUT_SINGLE_TIME,
CUSTSERVICEID = generateModel.CustServiceId.HasValue? generateModel.CustServiceId.Value.ToString(): null,
CUSTSERVICE = generateModel.CustServiceName,
ctnInputs = new List()
};
var ctnCodeList = _cache.GetAllCodeCtn().GetAwaiter().GetResult().ToList();
if (taskBCCtnList.Count > 0)
{
taskBCCtnList.ForEach(t =>
{
var ctnCode = ctnCodeList.FirstOrDefault(a => !string.IsNullOrWhiteSpace(a.Name) &&
a.Name.Equals(t.CTNALL, StringComparison.OrdinalIgnoreCase));
BookingCtnDto ctn = new BookingCtnDto
{
CTNCODE = ctnCode?.Code,
CTNALL = t.CTNALL,
CTNNUM = t.CTNNUM.HasValue ? t.CTNNUM.Value : 1
};
bkModel.ctnInputs.Add(ctn);
});
}
var bkRlt = await _bookingOrderService.Save(bkModel);
id = bkRlt.Id;
string batchNo = IDGen.NextID().ToString();
if (id > 0)
{
//这里如果指定了委托单位的邮件联系人,则推送订舱联系人
if (generateModel.CustomerContactList != null && generateModel.CustomerContactList.Count > 0)
{
var bookingContactList = _bookingOrderContactRepository.AsQueryable()
.Where(a => a.BookingId == id && a.IsDeleted == false).ToList();
var djyCustomerInfo = _djyCustomerService.Detail(new GetDjyCustomerInput { Id = generateModel.CustomerId.Value })
.GetAwaiter().GetResult();
Dictionary> finalContactDict = new Dictionary>();
generateModel.CustomerContactList.ForEach(contact =>
{
DjyCustomerContactOutput djyCustomerContactMan = null;
if (djyCustomerInfo.Contacts != null && djyCustomerInfo.Contacts.Count > 0)
{
djyCustomerContactMan = djyCustomerInfo.Contacts.FirstOrDefault(a =>
a.Id == contact.CustomerContactId);
}
finalContactDict.Add(contact.CustomerContactId, new Tuple(djyCustomerContactMan?.Email, contact.CustomerContactName));
if (djyCustomerContactMan != null)
{
var bookingContact = bookingContactList
.FirstOrDefault(x => x.Email.Equals(djyCustomerContactMan.Email, StringComparison.OrdinalIgnoreCase));
if (bookingContact == null)
{
bookingContact = new BookingOrderContact
{
Name = djyCustomerContactMan.Name,
BookingId = id,
Email = djyCustomerContactMan.Email,
Remark = djyCustomerContactMan.Remark,
CreatedTime = DateTime.Now,
CreatedUserId = UserManager.UserId,
CreatedUserName = UserManager.Name
};
_bookingOrderContactRepository.Insert(bookingContact);
}
else
{
bookingContact.Name = djyCustomerContactMan.Name;
bookingContact.Email = djyCustomerContactMan.Email;
bookingContact.Remark = djyCustomerContactMan.Remark;
bookingContact.UpdatedTime = DateTime.Now;
bookingContact.UpdatedUserId = UserManager.UserId;
bookingContact.UpdatedUserName = UserManager.Name;
_bookingOrderContactRepository.AsUpdateable(bookingContact).UpdateColumns(it => new
{
it.Name,
it.Email,
it.Remark,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommand();
}
}
});
//这里对于入库后的联系人再做一下比对,如果有差异需要邮件推送消息
var checkbookingContactList = _bookingOrderContactRepository.AsQueryable()
.Where(a => a.BookingId == id && a.IsDeleted == false).ToList();
if (finalContactDict.Any(x => string.IsNullOrWhiteSpace(x.Value.Item1)))
{
//推送邮件提醒
new EmailNoticeHelper().SendEmailNotice($"MBLNO={taskBCInfo.MBL_NO} 生成订舱的联系人失败"
, $"MBLNO={taskBCInfo.MBL_NO} 生成订舱的联系人失败,有联系人没有关联到邮箱信息 {string.Join(",",finalContactDict.Where(x => string.IsNullOrWhiteSpace(x.Value.Item1)).ToArray())}", App.Configuration["EmailNoticeDefaultUser"].GetUserEmailList());
}
var checkRlt = generateModel.CustomerContactList.GroupJoin(finalContactDict, l => l.CustomerContactId, r => r.Key,
(l, r) =>
{
var currArg = r.ToList();
if(currArg.Count > 0)
{
var currModel = currArg.FirstOrDefault();
if (string.IsNullOrWhiteSpace(currModel.Value.Item1))
{
return new { UName = currModel.Value.Item2, IsNoEmail = true };
}
return new { UName = currModel.Value.Item2, IsNoEmail = false };
}
return new { UName = l.CustomerContactName, IsNoEmail = true };
}).ToList();
if (checkRlt.Any(t => t.IsNoEmail))
{
//推送邮件提醒
new EmailNoticeHelper().SendEmailNotice($"MBLNO={taskBCInfo.MBL_NO} 生成订舱的联系人失败"
, $"MBLNO={taskBCInfo.MBL_NO} 生成订舱的联系人失败,有联系人没有关联到邮箱信息 {string.Join(",", checkRlt.Where(x => x.IsNoEmail).Select(x => x.UName).ToArray())}", App.Configuration["EmailNoticeDefaultUser"].GetUserEmailList());
}
}
if (generateModel.ProjectList != null && generateModel.ProjectList.Count > 0)
{
ModifyServiceProjectDto projectDto = new ModifyServiceProjectDto
{
BookingId = id,
ProjectCodes = generateModel.ProjectList.Distinct().ToArray(),
};
//写入服务项目
var prjRlt = await _bookingValueAddedService.SaveServiceProject(projectDto);
_logger.LogInformation($"推送订舱的服务项目完成 id={id} rlt={JSON.Serialize(prjRlt)}");
}
var opt = App.GetOptions();
var dirAbs = opt.basePath;
if (string.IsNullOrEmpty(dirAbs))
{
dirAbs = App.WebHostEnvironment.WebRootPath;
}
taskFileList.ForEach(file =>
{
if (file.FILE_CATEGORY == TaskFileCategoryEnum.BC.ToString())
{
var fileFullPath = Path.Combine(dirAbs, file.FILE_PATH);
if (File.Exists(fileFullPath))
{
//如果确认文件读取成功
var bookFilePath = FileAttachHelper.MoveFile(id.ToString(), fileFullPath, batchNo
, false, null, true).GetAwaiter().GetResult();
//将格式单附件写入订舱的附件
SaveEDIFile(id, bookFilePath, new System.IO.FileInfo(bookFilePath).Name, taskBCInfo.TenantId.Value,
CONST_BC_FILE_CODE, CONST_BC_FILE_NAME).GetAwaiter();
}
}
else if (file.FILE_CATEGORY == TaskFileCategoryEnum.BC_NOTICE.ToString())
{
var fileFullPath = Path.Combine(dirAbs, file.FILE_PATH);
if (File.Exists(fileFullPath))
{
//如果确认文件读取成功
var bookFilePath = FileAttachHelper.MoveFile(id.ToString(), fileFullPath, batchNo
, false, "bcnoticefile",true).GetAwaiter().GetResult();
//将格式单附件写入订舱的附件
SaveEDIFile(id, bookFilePath, new System.IO.FileInfo(bookFilePath).Name, taskBCInfo.TenantId.Value,
CONST_BC_NOTICE_FILE_CODE, CONST_BC_NOTICE_FILE_NAME).GetAwaiter();
}
}
});
}
_logger.LogInformation($"任务BC MBLNO:{taskBCInfo.MBL_NO} 生成订舱成功 id={id}");
}
catch(Exception ex)
{
_logger.LogError($"任务BC MBLNO:{taskBCInfo.MBL_NO} 生成订舱异常,原因:{ex.Message}");
}
return id;
}
#endregion
#region 取消任务
///
/// 取消任务
///
/// BC任务主键
/// 返回回执
[HttpGet("/TaskManageBC/CancelTask")]
public async Task CancelTask([FromQuery] string taskPKId)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
var bcTaskInfo = await _taskBaseRepository.AsQueryable().FirstAsync(u => u.PK_ID == taskPKId);
if (bcTaskInfo == null)
{
throw Oops.Oh($"任务主键{taskPKId}无法获取业务信息");
}
bcTaskInfo.IsDeleted = true;
bcTaskInfo.UpdatedTime = DateTime.Now;
bcTaskInfo.UpdatedUserId = UserManager.UserId;
bcTaskInfo.UpdatedUserName = UserManager.Name;
await _taskBaseRepository.AsUpdateable(bcTaskInfo).IgnoreColumns(it => new
{
it.TenantId,
it.CreatedTime,
it.CreatedUserId,
it.CreatedUserName,
it.IsDeleted,
it.TASK_NO,
it.TASK_TYPE,
it.TASK_SOURCE
}).ExecuteCommandAsync();
result.succ = true;
result.msg = "成功";
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"取消任务失败,原因:{ex.Message}";
}
return result;
}
#endregion
#region 获取服务项目列表
///
/// 获取服务项目列表
///
/// 返回回执
[HttpGet("/TaskManageBC/GetProjectList")]
public async Task> GetProjectList()
{
List list = new List();
return await _serviceWorkFlowBaseService.GetEnableProjectList(UserManager.TENANT_ID.ToString());
}
#endregion
#region 异步写入附件表
///
/// 异步写入附件表
///
/// 订舱ID
/// 文件路径
/// 文件名
/// 租户ID
/// 附件类型代码
/// 附件类型名称
/// 附件模块代码
///
[NonAction]
private async Task SaveEDIFile(long boookId, string FilePath, string fileName, long tenantId,
string fileTypeCode = "bc", string fileTypeName = "Booking Confirmation",string moudle = "BookingSlot")
{
/*
直接将附件信息写入附件表
*/
//EDI文件
var bookFile = new BookingFile
{
Id = YitIdHelper.NextId(),
FileName = fileName,
FilePath = FilePath,
TypeCode = fileTypeCode,
TypeName = fileTypeName,
BookingId = boookId,
TenantId = tenantId,
Moudle = moudle
};
await _bookingFileRepository.InsertAsync(bookFile);
}
#endregion
#region 生成并推送邮件
///
/// 生成并推送邮件
///
/// BC任务详情
/// 主任务详情
/// 是否默认使用用户个人邮箱发送
/// 返回回执
private async Task GenerateSendEmail(TaskBCInfo taskBCInfo, TaskBaseInfo bcTaskInfo, bool usePersonalEmailSend = false)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
try
{
/*
1、提取邮件接收人、通过订舱的委托客户获取联系人信息(提取联系人中备注是BCNotice的邮箱)
2、提取当票订舱对应的操作人邮箱、通过订舱的委托客户获取操作OP的邮箱
3、读取用户邮箱配置,主要提取显示名称BCNotice的邮箱,用来作为公共邮箱来发送邮件。
4、读取邮件模板,填充详情。
5、推送邮件给邮件接收人
*/
//读取订舱数据
var bookingOrderEntity = _bookingOrderRepository.AsQueryable().Filter(null, true)
.First(a => a.Id == taskBCInfo.BOOKING_ORDER_ID);
if (bookingOrderEntity == null)
{
var checkInfo =_bookingOrderRepository.AsQueryable().Filter(null, true)
.First(a => a.MBLNO == taskBCInfo.MBL_NO && a.IsDeleted == false && (a.ParentId == null || a.ParentId == 0));
if(checkInfo != null)
{
throw Oops.Oh($"订舱详情获取失败,用提单号能检索到,但是没有ID关系,需要人工看看问题");
}
else
{
throw Oops.Oh($"订舱详情获取失败,请确认订舱是否存在或已删除");
}
}
_logger.LogInformation($"获取订舱详情完成,bookid={bookingOrderEntity.Id}");
if (!bookingOrderEntity.CUSTOMERID.HasValue || (bookingOrderEntity.CUSTOMERID.HasValue && bookingOrderEntity.CUSTOMERID.Value == 0))
{
throw Oops.Oh($"订舱的委托客户不能为空");
}
var djyCustomerInfo = _djyCustomerService.Detail(new GetDjyCustomerInput { Id = bookingOrderEntity.CUSTOMERID.Value })
.GetAwaiter().GetResult();
if (djyCustomerInfo == null)
{
throw Oops.Oh($"委托单位详情获取失败,请确认委托单位是否存在或已删除");
}
_logger.LogInformation($"获取委托单位详情完成,djyCustomerId={djyCustomerInfo.Id}");
//DjyCustomerContactOutput djyCustomerContactMan = null;
//TO 邮件接收人
string toEmail = string.Empty;
//订舱OP的邮箱
string opEmail = string.Empty;
var bookingContactList = _bookingOrderContactRepository.AsQueryable().Filter(null, true)
.Where(a => a.BookingId == taskBCInfo.BOOKING_ORDER_ID).ToList();
if (bookingContactList == null || bookingContactList.Count == 0)
{
_logger.LogInformation($"当前订舱未指定的联系人,toEmail={toEmail}");
}
toEmail = string.Join(";", bookingContactList.Select(x => x.Email.Trim()).Distinct().ToArray());
//获取操作OP的邮箱
if (!string.IsNullOrWhiteSpace(bookingOrderEntity.OPID))
{
var opId = long.Parse(bookingOrderEntity.OPID);
var opUser = _sysUserRepository.AsQueryable().Filter(null, true).First(a => a.Id == opId);
if (opUser != null && !string.IsNullOrWhiteSpace(opUser.Email))
{
opEmail = opUser.Email.Trim();
_logger.LogInformation($"获取操作OP的邮箱,opEmail={opEmail} id={opId} name={opUser.Name}");
}
}
//提取当前公共邮箱的配置
DjyUserMailAccount publicMailAccount = null;
if(usePersonalEmailSend)
{
publicMailAccount = _djyUserMailAccount.AsQueryable().Filter(null, true).First(x => x.CreatedUserId == UserManager.UserId
&& x.SmtpPort > 0 && x.SmtpServer != null && x.SmtpServer != "");
}
else
{
//这个是公共邮箱配置
publicMailAccount = _djyUserMailAccount.AsQueryable().Filter(null, true).First(x => x.TenantId == UserManager.TENANT_ID && x.ShowName == "PublicSend"
&& x.SmtpPort > 0 && x.SmtpServer != null && x.SmtpServer != "");
}
if (publicMailAccount == null)
{
throw Oops.Oh($"提取公共邮箱配置失败,请在用户邮箱账号管理增加配置显示名为PublicSend或者配置个人邮箱");
}
_logger.LogInformation($"提取当前公共邮箱的配置完成,id={publicMailAccount.Id}");
string emailTitle = $"Booking Confirmation : {taskBCInfo.MBL_NO}";
string filePath = string.Empty;
SysUser opUserInfo = null;
if (!string.IsNullOrWhiteSpace(bookingOrderEntity.OPID) && Regex.IsMatch(bookingOrderEntity.OPID, "[0-9]+"))
opUserInfo = _sysUserRepository.AsQueryable().Filter(null, true).First(u => u.Id == long.Parse(bookingOrderEntity.OPID));
if (taskBCInfo.BUSI_TYPE == "BookingAmendment")
{
emailTitle = $"【变更】Booking Amendment : {taskBCInfo.MBL_NO}";
}
//读取邮件模板并填充数据
string emailHtml = string.Empty;
if (taskBCInfo.BUSI_TYPE == "BookingAmendment")
{
emailHtml = GenerateSendEmailHtmlAmendment(taskBCInfo, opUserInfo, UserManager.TENANT_NAME).GetAwaiter().GetResult();
}
else
{
emailHtml = GenerateSendEmailHtml(taskBCInfo, opUserInfo, UserManager.TENANT_NAME).GetAwaiter().GetResult();
}
_logger.LogInformation($"生成邮件BODY,结果:{emailHtml}");
TaskFileInfo fileInfo = null;
if (bcTaskInfo.TASK_BASE_TYPE == TaskBaseTypeEnum.BC.ToString())
{
fileInfo = _taskFileRepository.AsQueryable().Filter(null, true).Where(a => a.TASK_PKID == taskBCInfo.TASK_ID && a.FILE_CATEGORY.Contains("BC_NOTICE"))
.OrderByDescending(a => a.CreatedTime).First();
}
else if (bcTaskInfo.TASK_BASE_TYPE == TaskBaseTypeEnum.BC_MODIFY.ToString())
{
fileInfo = _taskFileRepository.AsQueryable().Filter(null, true).Where(a => a.TASK_PKID == taskBCInfo.TASK_ID && a.FILE_CATEGORY.Contains("BC_MODIFY_NOTICE"))
.OrderByDescending(a => a.CreatedTime).First();
}
if (fileInfo == null)
{
throw Oops.Oh($"提取变更文件失败,不能发送邮件");
}
_logger.LogInformation($"获取订舱附件地址,结果:{fileInfo.FILE_PATH}");
var opt = App.GetOptions();
var dirAbs = opt.basePath;
if (string.IsNullOrEmpty(dirAbs))
{
dirAbs = App.WebHostEnvironment.WebRootPath;
}
filePath = Path.Combine(dirAbs, fileInfo.FILE_PATH);
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()
};
_logger.LogInformation($"生成请求邮件参数,结果:{JSON.Serialize(emailApiUserDefinedDto)}");
//推送邮件
var emailRlt = await PushEmail(emailApiUserDefinedDto, filePath);
_logger.LogInformation($"推送邮件完成,结果:{JSON.Serialize(emailRlt)}");
//var taskBcInfo = _taskBCInfoRepository.AsQueryable().Filter(null, true)
//.First(a => a.PK_ID == taskBCInfo.PK_ID);
if (emailRlt.succ)
{
result.succ = true;
result.msg = "成功";
}
else
{
result.succ = false;
result.msg = emailRlt.msg;
new EmailNoticeHelper().SendEmailNotice($"MBLNO={taskBCInfo.MBL_NO} 转发通知邮件失败", $"MBLNO={taskBCInfo.MBL_NO} 转发通知邮件失败,原因:{emailRlt.msg}", App.Configuration["EmailNoticeDefaultUser"].GetUserEmailList());
}
}
catch (Exception ex)
{
_logger.LogInformation($"推送邮件失败,异常:{ex.Message}");
result.succ = false;
result.msg = $"推送邮件失败,{ex.Message}";
new EmailNoticeHelper().SendEmailNotice($"MBLNO={taskBCInfo.MBL_NO} 转发通知邮件失败", $"MBLNO={taskBCInfo.MBL_NO} 转发通知邮件失败,原因:{ex.Message}", App.Configuration["EmailNoticeDefaultUser"].GetUserEmailList());
}
return result;
}
#endregion
#region 通过邮件模板生成HTML
///
/// 通过邮件模板生成HTML
///
/// BC任务详情
/// 订舱OP详情
/// 当前租户全称
/// 返回生成的HTML
public async Task GenerateSendEmailHtml(TaskBCInfo taskBCInfo, SysUser opUserInfo, string tenantName)
{
string result = string.Empty;
/*
1、加载模板文件,读取HTML
2、读取main、conta的tr行,替换业务数据
3、返回HTML的文本信息。
*/
try
{
string templatePath = App.Configuration["EmailTemplateFilePath"];
var opt = App.GetOptions();
var dirAbs = opt.basePath;
if (string.IsNullOrEmpty(dirAbs))
{
dirAbs = App.WebHostEnvironment.WebRootPath;
}
templatePath = $"{dirAbs}{templatePath}\\BCEmailTemplate.html";
string baseHtml = File.ReadAllText(templatePath);
if (string.IsNullOrWhiteSpace(baseHtml))
throw Oops.Oh($"读取邮件模板失败");
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#", "");
}
HtmlDocument html = new HtmlDocument();
html.LoadHtml(baseHtml);
HtmlNode baseTable = html.DocumentNode.SelectNodes("//table[@class='base-table']").FirstOrDefault();
if (baseTable == null)
throw Oops.Oh($"读取邮件模板格式错误,定位base-table失败");
var baseTrList = baseTable.SelectNodes(".//tr");
foreach (var baseTr in baseTrList)
{
var tdList = baseTr.SelectNodes(".//td");
foreach (var baseTd in tdList)
{
if (baseTd.Attributes["class"].Value == "billno-val")
{
baseTd.InnerHtml = taskBCInfo.MBL_NO;
}
else if (baseTd.Attributes["class"].Value == "takebillno-val")
{
baseTd.InnerHtml = taskBCInfo.MBL_NO;
}
else if (baseTd.Attributes["class"].Value == "pol-val")
{
baseTd.InnerHtml = taskBCInfo.PLACERECEIPT;
}
else if (baseTd.Attributes["class"].Value == "pod-val")
{
baseTd.InnerHtml = taskBCInfo.PLACEDELIVERY;
}
else if (baseTd.Attributes["class"].Value == "ctn-val")
{
baseTd.InnerHtml = taskBCInfo.CTN_STAT;
}
else if (baseTd.Attributes["class"].Value == "etd-val")
{
if (taskBCInfo.ETD.HasValue)
{
baseTd.InnerHtml = taskBCInfo.ETD.Value.ToString("yyyy-MM-dd");
}
}
else if (baseTd.Attributes["class"].Value == "eta-val")
{
if (taskBCInfo.ETA.HasValue)
{
baseTd.InnerHtml = taskBCInfo.ETA.Value.ToString("yyyy-MM-dd");
}
}
}
}
var noreplyTr = html.DocumentNode.SelectNodes("//tr[@class='email-noreply']").FirstOrDefault();
if (noreplyTr != null)
{
var currTd = noreplyTr.SelectNodes(".//td").FirstOrDefault();
if(currTd != null)
{
var currPList = currTd.SelectNodes(".//p");
foreach (var baseP in currPList)
{
if (baseP.Attributes["class"].Value == "notice-comp-val")
{
baseP.InnerHtml = tenantName;
}
}
}
}
result = html.DocumentNode.OuterHtml;
}
catch (Exception ex)
{
_logger.LogInformation($"通过邮件模板生成HTML异常,原因={ex.Message}");
throw ex;
}
return result;
}
#endregion
#region 通过邮件模板生成HTML
///
/// 通过邮件模板生成HTML
///
/// BC任务详情
/// 订舱OP详情
/// 当前租户全称
/// 返回生成的HTML
public async Task GenerateSendEmailHtmlAmendment(TaskBCInfo taskBCInfo, SysUser opUserInfo, string tenantName)
{
string result = string.Empty;
/*
1、加载模板文件,读取HTML
2、读取main、conta的tr行,替换业务数据
3、返回HTML的文本信息。
*/
try
{
string templatePath = App.Configuration["EmailTemplateFilePath"];
var opt = App.GetOptions();
var dirAbs = opt.basePath;
if (string.IsNullOrEmpty(dirAbs))
{
dirAbs = App.WebHostEnvironment.WebRootPath;
}
templatePath = $"{dirAbs}{templatePath}\\BCModifyEmailTemplate.html";
string baseHtml = File.ReadAllText(templatePath);
if (string.IsNullOrWhiteSpace(baseHtml))
throw Oops.Oh($"读取邮件模板失败");
List compareList = GetCompareResult(taskBCInfo.TASK_ID).GetAwaiter().GetResult();
if (compareList == null || compareList.Count == 0)
throw Oops.Oh($"读取变更数据失败,没有差异数据");
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#", "");
}
HtmlDocument html = new HtmlDocument();
html.LoadHtml(baseHtml);
HtmlNode baseTable = html.DocumentNode.SelectNodes("//table[@class='base-table']").FirstOrDefault();
if (baseTable == null)
throw Oops.Oh($"读取邮件模板格式错误,定位base-table失败");
var baseTrList = baseTable.SelectNodes(".//tr");
foreach (var baseTr in baseTrList)
{
var tdList = baseTr.SelectNodes(".//td");
foreach (var baseTd in tdList)
{
if (baseTd.Attributes["class"].Value == "billno-val")
{
baseTd.InnerHtml = taskBCInfo.MBL_NO;
}
else if (baseTd.Attributes["class"].Value == "takebillno-val")
{
baseTd.InnerHtml = taskBCInfo.MBL_NO;
}
else if (baseTd.Attributes["class"].Value == "pol-val")
{
baseTd.InnerHtml = taskBCInfo.PLACERECEIPT;
}
else if (baseTd.Attributes["class"].Value == "pod-val")
{
baseTd.InnerHtml = taskBCInfo.PLACEDELIVERY;
}
else if (baseTd.Attributes["class"].Value == "ctn-val")
{
baseTd.InnerHtml = taskBCInfo.CTN_STAT;
}
/*
else if (baseTd.Attributes["class"].Value == "etd-val")
{
if (taskBCInfo.ETD.HasValue)
{
baseTd.InnerHtml = taskBCInfo.ETD.Value.ToString("yyyy-MM-dd");
}
}
else if (baseTd.Attributes["class"].Value == "eta-val")
{
if (taskBCInfo.ETA.HasValue)
{
baseTd.InnerHtml = taskBCInfo.ETA.Value.ToString("yyyy-MM-dd");
}
}*/
}
}
bool isOnlyChangeOne = false;
List nameList = new List();
//船名
if (compareList.Any(x => x.FieldCode.Equals("vessel", StringComparison.OrdinalIgnoreCase)))
{
var name = compareList.FirstOrDefault(x => x.FieldCode.Equals("vessel", StringComparison.OrdinalIgnoreCase)).TargetVal;
baseTable.AppendChild(HtmlNode.CreateNode("船名 变更为: | " + name + " |
"));
nameList.Add("船名");
}
//航次号
if (compareList.Any(x => x.FieldCode.Equals("voyNo", StringComparison.OrdinalIgnoreCase)))
{
var name = compareList.FirstOrDefault(x => x.FieldCode.Equals("voyNo", StringComparison.OrdinalIgnoreCase)).TargetVal;
baseTable.AppendChild(HtmlNode.CreateNode("航次号 变更为: | " + name + " |
"));
nameList.Add("航次号");
}
//ETD
if (compareList.Any(x => x.FieldCode.Equals("ETD", StringComparison.OrdinalIgnoreCase)))
{
var name = compareList.FirstOrDefault(x => x.FieldCode.Equals("ETD", StringComparison.OrdinalIgnoreCase)).TargetVal;
baseTable.AppendChild(HtmlNode.CreateNode("ETD 变更为: | " + name + " |
"));
nameList.Add("ETD");
}
//ETA
if (compareList.Any(x => x.FieldCode.Equals("ETA", StringComparison.OrdinalIgnoreCase)))
{
var name = compareList.FirstOrDefault(x => x.FieldCode.Equals("ETA", StringComparison.OrdinalIgnoreCase)).TargetVal;
baseTable.AppendChild(HtmlNode.CreateNode("ETA 变更为: | " + name + " |
"));
nameList.Add("ETA");
}
//CustomSICutDate 客户样单截止日期
if (compareList.Any(x => x.FieldCode.Equals("CustomSICutDate", StringComparison.OrdinalIgnoreCase)))
{
var name = compareList.FirstOrDefault(x => x.FieldCode.Equals("CustomSICutDate", StringComparison.OrdinalIgnoreCase)).TargetVal;
baseTable.AppendChild(HtmlNode.CreateNode("样单截止日期 变更为: | " + name + " |
"));
nameList.Add("样单截止日期");
}
//CYCutoffTime 截港时间
if (compareList.Any(x => x.FieldCode.Equals("CYCutoffTime", StringComparison.OrdinalIgnoreCase)))
{
var name = compareList.FirstOrDefault(x => x.FieldCode.Equals("CYCutoffTime", StringComparison.OrdinalIgnoreCase)).TargetVal;
baseTable.AppendChild(HtmlNode.CreateNode("截港时间 变更为: | " + name + " |
"));
nameList.Add("截港时间");
}
//ManifestCutDate 舱单截止时间
if (compareList.Any(x => x.FieldCode.Equals("ManifestCutDate", StringComparison.OrdinalIgnoreCase)))
{
var name = compareList.FirstOrDefault(x => x.FieldCode.Equals("ManifestCutDate", StringComparison.OrdinalIgnoreCase)).TargetVal;
baseTable.AppendChild(HtmlNode.CreateNode("舱单截止时间 变更为: | " + name + " |
"));
nameList.Add("舱单截止时间");
}
//VGMCutoffTime MDGF提交截止时间
if (compareList.Any(x => x.FieldCode.Equals("VGMCutoffTime", StringComparison.OrdinalIgnoreCase)))
{
var name = compareList.FirstOrDefault(x => x.FieldCode.Equals("VGMCutoffTime", StringComparison.OrdinalIgnoreCase)).TargetVal;
baseTable.AppendChild(HtmlNode.CreateNode("截VGM时间 变更为: | " + name + " |
"));
nameList.Add("截VGM时间");
}
//MDGFCutDate MDGF提交截止时间
if (compareList.Any(x => x.FieldCode.Equals("MDGFCutDate", StringComparison.OrdinalIgnoreCase)))
{
var name = compareList.FirstOrDefault(x => x.FieldCode.Equals("MDGFCutDate", StringComparison.OrdinalIgnoreCase)).TargetVal;
baseTable.AppendChild(HtmlNode.CreateNode("MDGF提交截止时间 变更为: | " + name + " |
"));
nameList.Add("MDGF提交截止时间");
}
//PlaceReceipt 收货地
if (compareList.Any(x => x.FieldCode.Equals("PlaceReceipt", StringComparison.OrdinalIgnoreCase)))
{
var name = compareList.FirstOrDefault(x => x.FieldCode.Equals("PlaceReceipt", StringComparison.OrdinalIgnoreCase)).TargetVal;
baseTable.AppendChild(HtmlNode.CreateNode("收货地 变更为: | " + name + " |
"));
nameList.Add("收货地");
}
//PlaceDelivery 交货地
if (compareList.Any(x => x.FieldCode.Equals("PlaceDelivery", StringComparison.OrdinalIgnoreCase)))
{
var name = compareList.FirstOrDefault(x => x.FieldCode.Equals("PlaceDelivery", StringComparison.OrdinalIgnoreCase)).TargetVal;
baseTable.AppendChild(HtmlNode.CreateNode("交货地 变更为: | " + name + " |
"));
nameList.Add("交货地");
}
var noreplyTr = html.DocumentNode.SelectNodes("//tr[@class='email-noreply']").FirstOrDefault();
if (noreplyTr != null)
{
var currTd = noreplyTr.SelectNodes(".//td").FirstOrDefault();
if (currTd != null)
{
var currPList = currTd.SelectNodes(".//p");
foreach (var baseP in currPList)
{
if (baseP.Attributes["class"].Value == "notice-comp-val")
{
baseP.InnerHtml = tenantName;
}
else if(baseP.Attributes["class"].Value == "dynamic-val")
{
if (nameList.Count > 0)
{
baseP.InnerHtml = $"请注意{(string.Join("、", nameList.ToArray()))} 变更,请您按相关的更新安排操作,谢谢!";
}
}
}
}
}
result = html.DocumentNode.OuterHtml;
}
catch (Exception ex)
{
_logger.LogInformation($"通过邮件模板生成HTML异常,原因={ex.Message}");
throw ex;
}
return result;
}
#endregion
#region 推送邮件
///
/// 推送邮件
///
/// 自定义邮件详情
/// 文件路径
/// 返回回执
private async Task PushEmail(EmailApiUserDefinedDto emailApiUserDefinedDto, string filePath)
{
CommonWebApiResult result = new CommonWebApiResult { succ = true };
List emailList = new List();
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 请联系管理员");
System.IO.FileStream file = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.Read);
int SplitSize = 5242880;//5M分片长度
int index = 1; //序号 第几片
long StartPosition = 5242880 * (index - 1);
long lastLens = file.Length - StartPosition;//真不知道怎么起命了,就这样吧
if (lastLens < 5242880)
{
SplitSize = (int)lastLens;
}
byte[] heByte = new byte[SplitSize];
file.Seek(StartPosition, SeekOrigin.Begin);
//第一个参数是 起始位置
file.Read(heByte, 0, SplitSize);
//第三个参数是 读取长度(剩余长度)
file.Close();
string base64Str = Convert.ToBase64String(heByte);
emailApiUserDefinedDto.Attaches.Add(new AttachesInfo
{
AttachName = Path.GetFileName(filePath).Replace("_MODIFY",""),
AttachContent = base64Str
});
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($"邮件上传完成 上传文件大小:{heByte.Length} 用时:{timeDiff}ms.,");
_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 发送邮件
///
/// 发送邮件
///
/// BC任务主键
/// 是否默认使用用户个人邮箱发送
/// 返回回执
[HttpGet("/TaskManageBC/SendEmail")]
public async Task SendEmail(string taskPKId, bool usePersonalEmailSend = false)
{
if (string.IsNullOrWhiteSpace(taskPKId))
throw Oops.Oh($"BC任务主键不能为空");
var bcTaskInfo = await _taskBaseRepository.AsQueryable().Filter(null,true).FirstAsync(u => u.PK_ID == taskPKId);
if (bcTaskInfo == null)
{
throw Oops.Oh($"任务主键{taskPKId}无法获取业务信息");
}
var bcOrder = _taskBCInfoRepository.AsQueryable().Filter(null, true).First(a => a.TASK_ID == bcTaskInfo.PK_ID);
if (bcOrder == null)
throw Oops.Oh($"任务主键{taskPKId}无法获取BC业务信息");
return await GenerateSendEmail(bcOrder, bcTaskInfo);
}
#endregion
#region 获取当前比对结果
///
/// 获取当前比对结果
///
/// BC任务主键
/// 返回回执
[HttpGet("/TaskManageBC/GetCompareResult")]
public async Task> GetCompareResult(string taskPKId)
{
if (string.IsNullOrWhiteSpace(taskPKId))
throw Oops.Oh($"BC任务主键不能为空");
var bcTaskInfo = await _taskBaseRepository.AsQueryable().Filter(null,true).FirstAsync(u => u.PK_ID == taskPKId);
if (bcTaskInfo == null)
{
throw Oops.Oh($"任务主键{taskPKId}无法获取业务信息");
}
var bcOrder = _taskBCInfoRepository.AsQueryable().Filter(null, true).First(a => a.TASK_ID == bcTaskInfo.PK_ID);
if (bcOrder == null)
throw Oops.Oh($"任务主键{taskPKId}无法获取BC业务信息");
return await _bookingSlotService.GetSlotCompareResult(bcOrder.BOOKING_SLOT_ID.Value, bcOrder.BATCH_NO);
}
#endregion
#region 同步舱位变更
///
/// 同步舱位变更
///
/// BC任务主键
/// 租户ID
/// 返回回执
[HttpGet("/TaskManageBC/SyncBookingSlotChange")]
public async Task SyncBookingSlotChange(string taskPKId, long tenantId)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
/*
* Amendment
1、需要先匹配舱位,如果没有舱位不能执行后续的步骤。
2、判断当前租户是否配置了自动转发客户邮件。
3、如果配置了自动转发,需要判断订舱是否已有,没有不能执行自动转发。
4、转发需要提取差异数据,如果没有差异数据也不能转发邮件。
5、默认通过钉钉通知操作收到了变更通知。
* Cancellation
1、需要先匹配舱位,如果没有舱位不能执行后续的步骤。
2、判断当前租户是否配置了自动转发客户邮件。
3、如果配置了自动转发,需要判断订舱是否已有,没有不能执行自动转发。
4、转发需要提取差异数据,如果没有差异数据也不能转发邮件。
*/
try
{
var baseTask = _taskBaseRepository.AsQueryable().Filter(null, true).First(u => u.PK_ID == taskPKId && u.TenantId == tenantId);
if (baseTask == null)
throw Oops.Oh($"主任务获取失败,不存在或已作废");
var bcOrder = _taskBCInfoRepository.AsQueryable().Filter(null, true).First(a => a.TASK_ID == taskPKId && a.TenantId == tenantId);
if (bcOrder == null)
throw Oops.Oh($"BC任务获取失败,不存在或已作废");
DjyTenantParamValueOutput paramConfig = null;
DjyTenantParamValueOutput dingdingConfig = null;
var paramList = _djyTenantParamService.GetParaCodeWithValue(new[] { CONST_AMENDMENT_DEFAULT_PARAM,
CONST_CANCELLATION_DEFAULT_PARAM,
CONST_AMENDMENT_DING_DEFAULT_PARAM,
CONST_CANCELLATION_DING_DEFAULT_PARAM }).GetAwaiter().GetResult();
if (baseTask.TASK_TYPE == TaskBaseTypeEnum.BC_MODIFY.ToString())
{
if (paramList != null && paramList.Count > 0)
{
paramConfig = paramList.FirstOrDefault(a => a.ParaCode.Equals(CONST_AMENDMENT_DEFAULT_PARAM, StringComparison.OrdinalIgnoreCase));
dingdingConfig = paramList.FirstOrDefault(a => a.ParaCode.Equals(CONST_AMENDMENT_DING_DEFAULT_PARAM, StringComparison.OrdinalIgnoreCase));
}
}
else if (baseTask.TASK_TYPE == TaskBaseTypeEnum.CANCELLATION.ToString())
{
if (paramList != null && paramList.Count > 0)
{
paramConfig = paramList.FirstOrDefault(a => a.ParaCode.Equals(CONST_CANCELLATION_DEFAULT_PARAM, StringComparison.OrdinalIgnoreCase));
dingdingConfig = paramList.FirstOrDefault(a => a.ParaCode.Equals(CONST_CANCELLATION_DING_DEFAULT_PARAM, StringComparison.OrdinalIgnoreCase));
}
}
_logger.LogInformation($"taskPKId={taskPKId} 判断是否默认转发邮件 paramConfig={JSON.Serialize(paramConfig)}");
if (!bcOrder.BOOKING_SLOT_ID.HasValue || bcOrder.BOOKING_SLOT_ID.Value == 0)
{
var server = _namedBookingSlotServiceProvider.GetService(nameof(BookingSlotService));
var slotModel = server.SearchBookingSlotWithOrderByNo(bcOrder.MBL_NO, bcOrder.TenantId.Value).GetAwaiter().GetResult();
if (slotModel != null && slotModel.BookingSlotId > 0)
{
if (slotModel.HasBookingOrder)
{
bcOrder.BOOKING_SLOT_ID = slotModel.BookingSlotId;
bcOrder.BOOKING_ORDER_ID = slotModel.BookingOrderList.FirstOrDefault();
bcOrder.UpdatedTime = DateTime.Now;
bcOrder.UpdatedUserId = UserManager.UserId;
bcOrder.UpdatedUserName = UserManager.Name;
await _taskBCInfoRepository.AsUpdateable(bcOrder).UpdateColumns(it => new
{
it.BOOKING_SLOT_ID,
it.BOOKING_ORDER_ID,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
}
else
{
bcOrder.BOOKING_SLOT_ID = slotModel.BookingSlotId;
bcOrder.UpdatedTime = DateTime.Now;
bcOrder.UpdatedUserId = UserManager.UserId;
bcOrder.UpdatedUserName = UserManager.Name;
await _taskBCInfoRepository.AsUpdateable(bcOrder).UpdateColumns(it => new
{
it.BOOKING_SLOT_ID,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
}
if (baseTask != null)
{
if (baseTask.IS_PUBLIC == 1)
{
baseTask.IS_PUBLIC = 0;
baseTask.RealUserId = UserManager.UserId;
baseTask.RealUserName = UserManager.Name;
baseTask.UpdatedTime = DateTime.Now;
baseTask.UpdatedUserId = UserManager.UserId;
baseTask.UpdatedUserName = UserManager.Name;
//更新任务台,将当前任务变更为个人任务
await _taskBaseRepository.AsUpdateable(baseTask).UpdateColumns(it => new
{
it.IS_PUBLIC,
it.RealUserId,
it.RealUserName,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
}
}
}
else
{
_logger.LogInformation($"taskPKId={taskPKId} 无法获取舱位信息");
throw Oops.Oh($"无法获取舱位信息");
}
}
//去关联的订舱ID
if(bcOrder.BOOKING_ORDER_ID == null || bcOrder.BOOKING_ORDER_ID.Value == 0)
{
var server = _namedBookingSlotServiceProvider.GetService(nameof(BookingSlotService));
var slotModel = server.SearchBookingSlotWithOrderByNo(bcOrder.MBL_NO, bcOrder.TenantId.Value).GetAwaiter().GetResult();
if (slotModel.HasBookingOrder)
{
bcOrder.BOOKING_ORDER_ID = slotModel.BookingOrderList.FirstOrDefault();
bcOrder.UpdatedTime = DateTime.Now;
bcOrder.UpdatedUserId = UserManager.UserId;
bcOrder.UpdatedUserName = UserManager.Name;
await _taskBCInfoRepository.AsUpdateable(bcOrder).UpdateColumns(it => new
{
it.BOOKING_ORDER_ID,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
}
}
if (bcOrder.BOOKING_ORDER_ID != null && bcOrder.BOOKING_ORDER_ID.HasValue && bcOrder.BOOKING_ORDER_ID.Value > 0)
{
/*
SaveBookingOrderInput bkModel = new SaveBookingOrderInput
{
MBLNO = bcOrder.MBL_NO.ToUpper().Trim(),
CONTRACTNO = !string.IsNullOrWhiteSpace(bcOrder.CONTRACTNO) ? bcOrder.CONTRACTNO : "",
VESSEL = bcOrder.VESSEL.ToUpper().Trim(),
VOYNO = bcOrder.VOYNO.ToUpper().Trim(),
VOYNOINNER = bcOrder.VOYNO.ToUpper().Trim(),
ETD = bcOrder.ETD,
ETA = bcOrder.ETA,
CLOSEVGMDATE = bcOrder.VGM_CUTOFF_TIME,
CLOSINGDATE = bcOrder.CY_CUTOFF_TIME,
CLOSEDOCDATE = bcOrder.CUT_SINGLE_TIME,
ctnInputs = new List()
};
bkModel.ChangedFields.Add(nameof(SaveBookingOrderInput.CONTRACTNO));
bkModel.ChangedFields.Add(nameof(SaveBookingOrderInput.VESSEL));
bkModel.ChangedFields.Add(nameof(SaveBookingOrderInput.VOYNO));
bkModel.ChangedFields.Add(nameof(SaveBookingOrderInput.VOYNOINNER));
bkModel.ChangedFields.Add(nameof(SaveBookingOrderInput.ETD));
bkModel.ChangedFields.Add(nameof(SaveBookingOrderInput.ETA));
bkModel.ChangedFields.Add(nameof(SaveBookingOrderInput.CLOSEVGMDATE));
bkModel.ChangedFields.Add(nameof(SaveBookingOrderInput.CLOSINGDATE));
bkModel.ChangedFields.Add(nameof(SaveBookingOrderInput.CLOSEDOCDATE));
var taskBCCtnList = _taskBCCTNInfoRepository.AsQueryable().Filter(null, true).Where(a => a.P_ID == bcOrder.PK_ID && a.TenantId == tenantId).ToList();
var ctnCodeList = _cache.GetAllCodeCtn().GetAwaiter().GetResult().ToList();
if (taskBCCtnList.Count > 0)
{
taskBCCtnList.ForEach(t =>
{
var ctnCode = ctnCodeList.FirstOrDefault(a => !string.IsNullOrWhiteSpace(a.Name) &&
a.Name.Equals(t.CTNALL, StringComparison.OrdinalIgnoreCase));
BookingCtnDto ctn = new BookingCtnDto
{
CTNCODE = ctnCode?.Code,
CTNALL = t.CTNALL,
CTNNUM = t.CTNNUM.HasValue ? t.CTNNUM.Value : 1
};
bkModel.ctnInputs.Add(ctn);
});
}
var bkRlt = await _bookingOrderService.Save(bkModel);
if (bkRlt.Id != null && bkRlt.Id > 0)
{
_logger.LogInformation($"taskPKId={taskPKId} 更新订舱详情完成");
}
else
{
_logger.LogInformation($"taskPKId={taskPKId} 更新订舱详情失败没有对应舱位ID");
}*/
}
else
{
_logger.LogInformation($"taskPKId={taskPKId} 更新订舱详情失败没有对应舱位ID");
}
//如果值是ENABLE表示可以自动发送
if (paramConfig != null && paramConfig.ParaValue.Equals("ENABLE", StringComparison.OrdinalIgnoreCase))
{
_logger.LogInformation($"taskPKId={taskPKId} 准备发送邮件");
var emailRlt = await SendEmail(taskPKId);
_logger.LogInformation($"taskPKId={taskPKId} 发送邮件完成 结果={JSON.Serialize(emailRlt)}");
}
else
{
_logger.LogInformation($"taskPKId={taskPKId} 未配置自动发送邮件");
}
if (dingdingConfig != null && dingdingConfig.ParaValue.Equals("ENABLE", StringComparison.OrdinalIgnoreCase))
{
_logger.LogInformation($"taskPKId={taskPKId} 准备发送钉钉消息");
var emailRlt = await SendEmail(taskPKId);
_logger.LogInformation($"taskPKId={taskPKId} 发送钉钉消息完成 结果={JSON.Serialize(emailRlt)}");
}
else
{
_logger.LogInformation($"taskPKId={taskPKId} 未配置自动发送邮件");
}
result.succ = true;
result.msg = "执行成功";
}
catch (Exception ex)
{
result.succ = false;
result.msg = $"处理异常,原因:{ex.Message}";
_logger.LogInformation($"taskPKId={taskPKId} 处理异常,原因:{ex.Message}");
//new EmailNoticeHelper().SendEmailNotice($"taskPKId={taskPKId} 转发通知邮件失败", $"taskPKId={taskPKId} 转发通知邮件失败,原因:{ex.Message}", App.Configuration["EmailNoticeDefaultUser"].GetUserEmailList());
}
return result;
}
#endregion
#region 重试处理BC任务
///
/// 重试处理BC任务
/// 对未匹配舱位、订舱订单的任务记录重新对应舱位、订舱订单
///
/// BC任务主键
/// 返回回执
[HttpGet("/TaskManageBC/SearchAndConnectBookingInfo")]
public async Task SearchAndConnectBookingInfo(string taskPkId)
{
/*
只要解决BC变更、BC取消生成任务时未匹配到舱位、订舱订单,需要后边人工来点击匹配上舱位和订舱订单。
1、BC变更,重试匹配舱位,有舱位触发舱位的变更方法。
2、BC取消,重试匹配舱位,有舱位触发舱位的取消方法。
*/
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
var taskBase = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == taskPkId);
if (taskBase == null)
throw Oops.Oh($"任务主键{taskPkId}无法获取业务信息");
var taskBCInfo = _taskBCInfoRepository.AsQueryable().First(a => a.TASK_ID == taskBase.PK_ID);
if (taskBCInfo == null)
throw Oops.Oh($"BC任务主键{taskPkId}无法获取业务信息");
string mblNo = taskBCInfo.MBL_NO.ToUpper().Trim();
var server = _namedBookingSlotServiceProvider.GetService(nameof(BookingSlotService));
//首先检索关联舱位
if (!taskBCInfo.BOOKING_SLOT_ID.HasValue)
{
//var server = _namedBookingSlotServiceProvider.GetService(nameof(BookingSlotService));
var slotInfo = await server.SearchBookingSlotWithOrderByNo(mblNo, taskBCInfo.TenantId.Value);
if (slotInfo == null)
throw Oops.Oh($"提单号{mblNo}未提取有效的舱位信息");
taskBCInfo.BOOKING_SLOT_ID = slotInfo.BookingSlotId;
taskBCInfo.UpdatedUserId = UserManager.UserId;
taskBCInfo.UpdatedUserName = UserManager.Name;
if (slotInfo.HasBookingOrder && !taskBCInfo.BOOKING_ORDER_ID.HasValue)
{
taskBCInfo.BOOKING_ORDER_ID = slotInfo.BookingOrderList.FirstOrDefault();
//更新任务BC
await _taskBCInfoRepository.AsUpdateable(taskBCInfo).UpdateColumns(it => new
{
it.BOOKING_SLOT_ID,
it.BOOKING_ORDER_ID,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
}
else
{
//更新任务BC
await _taskBCInfoRepository.AsUpdateable(taskBCInfo).UpdateColumns(it => new
{
it.BOOKING_SLOT_ID,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
}
_logger.LogInformation($"提单号{mblNo} 提取到舱位ID,更新完成");
}
//对应订舱订单
if (!taskBCInfo.BOOKING_ORDER_ID.HasValue)
{
var slotInfo = await server.SearchBookingSlotWithOrderByNo(mblNo, taskBCInfo.TenantId.Value);
if (slotInfo == null)
throw Oops.Oh($"提单号{mblNo}未提取有效的舱位信息");
if (slotInfo.HasBookingOrder && !taskBCInfo.BOOKING_ORDER_ID.HasValue)
{
taskBCInfo.BOOKING_ORDER_ID = slotInfo.BookingOrderList.FirstOrDefault();
taskBCInfo.UpdatedUserId = UserManager.UserId;
taskBCInfo.UpdatedUserName = UserManager.Name;
//更新任务BC
await _taskBCInfoRepository.AsUpdateable(taskBCInfo).UpdateColumns(it => new
{
it.BOOKING_ORDER_ID,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
_logger.LogInformation($"提单号{mblNo} 提取到订舱订单ID,更新完成");
}
}
//取消舱位
if (taskBCInfo.BUSI_TYPE == TaskBusiTypeEnum.CANCELLATION.ToString())
{
BookingSlotBaseApiDto slotModel = new BookingSlotBaseApiDto
{
DataObj = new BookingSlotBaseApiSaveDto
{
CARRIERID = taskBCInfo.CARRIERID,
SLOT_BOOKING_NO = taskBCInfo.MBL_NO,
CtnList = new List()
},
OpType = "cancellation"
};
_logger.LogInformation($"提单号{mblNo} 推送舱位取消");
var pushRlt = await server.ApiReceive(JSON.Serialize(slotModel));
_logger.LogInformation($"提单号{mblNo} 推送舱位取消,结果={JSON.Serialize(pushRlt)}");
var service = _namedBookingMSKAPIServiceProvider
.GetService(nameof(BookingMSKAPIService));
var synRlt = await service.SyncBCInfo(taskBCInfo.MBL_NO, taskBCInfo.TenantId.Value, "Cancellation");
_logger.LogInformation($"入库完BC,自动推送状态到API订舱列表 mblno={taskBCInfo.MBL_NO} synRlt={JSON.Serialize(synRlt)}");
}
else if (taskBCInfo.BUSI_TYPE == TaskBusiTypeEnum.BC_MODIFY.ToString())
{
BookingSlotBaseApiDto slotModel = new BookingSlotBaseApiDto
{
DataObj = new BookingSlotBaseApiSaveDto
{
CARRIERID = taskBCInfo.CARRIERID,
SLOT_BOOKING_NO = taskBCInfo.MBL_NO,
BOOKING_PARTY = taskBCInfo.BOOKING_PARTY,
BOOKING_SLOT_TYPE = taskBCInfo.BOOKING_SLOT_TYPE,
BOOKING_SLOT_TYPE_NAME = taskBCInfo.BOOKING_SLOT_TYPE_NAME,
VESSEL = taskBCInfo.VESSEL,
VOYNO = taskBCInfo.VOYNO,
VGM_SUBMISSION_CUT_DATE = taskBCInfo.VGM_CUTOFF_TIME,
//WEEK_AT = taskBCInfo.WEEK_AT,
CARRIAGE_TYPE = taskBCInfo.CARRIAGE_TYPE,
CARRIAGE_TYPE_NAME = taskBCInfo.CARRIAGE_TYPE_NAME,
CONTRACT_NO = taskBCInfo.CONTRACTNO,
CTN_STAT = taskBCInfo.CTN_STAT,
CY_CUT_DATE = taskBCInfo.CY_CUTOFF_TIME,
DETENSION_FREE_DAYS = taskBCInfo.DETENSION_FREE_DAYS,
ETD = taskBCInfo.ETD,
ETA = taskBCInfo.ETA,
LANECODE = taskBCInfo.LANECODE,
LANENAME = taskBCInfo.LANENAME,
MANIFEST_CUT_DATE = taskBCInfo.MANIFEST_CUT_DATE,
MDGF_CUT_DATE = taskBCInfo.MDGF_CUT_DATE,
PLACEDELIVERY = taskBCInfo.PLACEDELIVERY,
PLACERECEIPT = taskBCInfo.PLACERECEIPT,
PORTDISCHARGE = taskBCInfo.PORTDISCHARGE,
PORTLOAD = taskBCInfo.PORTLOAD,
SI_CUT_DATE = taskBCInfo.SI_CUT_DATE,
TRANSFER_PORT_1 = taskBCInfo.TRANSFER_PORT_1,
TRANSFER_PORT_2 = taskBCInfo.TRANSFER_PORT_2,
CtnList = new List()
},
OpType = "update"
};
if (int.TryParse(taskBCInfo.WEEK_AT, out int _weekat))
{
slotModel.DataObj.WEEK_AT = _weekat;
}
var ctnList = _taskBCCTNInfoRepository.AsQueryable().Where(a => a.P_ID == taskBCInfo.PK_ID).ToList();
if (ctnList.Count > 0)
{
ctnList.ForEach(t =>
{
BookingSlotCtnSaveInput ctn = new BookingSlotCtnSaveInput
{
CTNALL = t.CTNALL,
CTNNUM = t.CTNNUM.HasValue ? t.CTNNUM.Value : 1
};
slotModel.DataObj.CtnList.Add(ctn);
});
}
var opt = App.GetOptions();
var dirAbs = opt.basePath;
if (string.IsNullOrEmpty(dirAbs))
{
dirAbs = App.WebHostEnvironment.WebRootPath;
}
var taskFileList = _taskFileRepository.AsQueryable().Where(a => a.TASK_PKID == taskBCInfo.TASK_ID).ToList();
DynameFileInfo dynameFile = null;
DynameFileInfo dynameNoticeFile = null;
if (taskFileList.Any(t => t.FILE_CATEGORY == TaskFileCategoryEnum.BC.ToString()))
{
var fileInfo = taskFileList.FirstOrDefault(t => t.FILE_CATEGORY == TaskFileCategoryEnum.BC.ToString());
var fileFullPath = Path.Combine(dirAbs, fileInfo.FILE_PATH);
dynameFile = new DynameFileInfo
{
FileBytes = File.ReadAllBytes(fileFullPath),
FileName = Path.GetFileName(fileFullPath)
};
}
if (taskFileList.Any(t => t.FILE_CATEGORY == TaskFileCategoryEnum.BC_NOTICE.ToString()))
{
var fileInfo = taskFileList.FirstOrDefault(t => t.FILE_CATEGORY == TaskFileCategoryEnum.BC.ToString());
var fileFullPath = Path.Combine(dirAbs, fileInfo.FILE_PATH);
dynameNoticeFile = new DynameFileInfo
{
FileBytes = File.ReadAllBytes(fileFullPath),
FileName = Path.GetFileName(fileFullPath)
};
}
_logger.LogInformation($"提单号{mblNo} 推送舱位更新");
var pushRlt = await server.InnerApiReceive(slotModel, dynameFile, dynameNoticeFile);
_logger.LogInformation($"提单号{mblNo} 推送舱位更新,结果={JSON.Serialize(pushRlt)}");
}
result.succ = true;
result.msg = "成功";
return result;
}
#endregion
#region 重新比对
///
/// 重新比对
///
/// BC任务主键
///
[HttpGet("/TaskManageBC/ReCompareResult")]
public async Task ReCompareResult(string taskPKId)
{
List list = new List();
/*
1、判断舱位是否存在,如果舱位不存在终止重新比对。
2、获取BookingConfirmation任务,如果不存在终止。
3、调取BookingConfirmation文件识别结果,与当前任务的文件识别重新比对。
4、更新到对应的舱位
*/
var baseTask = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == taskPKId);
if (baseTask == null)
{
throw Oops.Oh($"未提取有效的任务信息");
}
if (baseTask.TASK_TYPE != TaskBaseTypeEnum.BC_MODIFY.ToString())
{
throw Oops.Oh($"任务类型不是Amendment不能重新比对");
}
var bcTaskInfo = _taskBCInfoRepository.AsQueryable().First(a => a.TASK_ID == taskPKId);
if (bcTaskInfo == null)
{
throw Oops.Oh($"未提取有效的BC任务信息");
}
long slotId = 0;
string reqBatchNo = bcTaskInfo.BATCH_NO;
var server = _namedBookingSlotServiceProvider.GetService(nameof(BookingSlotService));
//没有舱位先要匹配舱位,才能比对
if (!bcTaskInfo.BOOKING_SLOT_ID.HasValue)
{
var searchInfo = await server.SearchBookingSlotWithOrderByNo(bcTaskInfo.MBL_NO, bcTaskInfo.TenantId.Value);
if (searchInfo != null)
{
if (searchInfo.HasBookingOrder)
{
bcTaskInfo.BOOKING_SLOT_ID = searchInfo.BookingSlotId;
bcTaskInfo.BOOKING_ORDER_ID = searchInfo.BookingOrderList.FirstOrDefault();
bcTaskInfo.UpdatedTime = DateTime.Now;
bcTaskInfo.UpdatedUserId = UserManager.UserId;
bcTaskInfo.UpdatedUserName = UserManager.Name;
await _taskBCInfoRepository.AsUpdateable(bcTaskInfo).UpdateColumns(it => new
{
it.BOOKING_SLOT_ID,
it.BOOKING_ORDER_ID,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
}
else
{
bcTaskInfo.BOOKING_SLOT_ID = searchInfo.BookingSlotId;
bcTaskInfo.UpdatedTime = DateTime.Now;
bcTaskInfo.UpdatedUserId = UserManager.UserId;
bcTaskInfo.UpdatedUserName = UserManager.Name;
await _taskBCInfoRepository.AsUpdateable(bcTaskInfo).UpdateColumns(it => new
{
it.BOOKING_SLOT_ID,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
}
if (baseTask.IS_PUBLIC == 1)
{
baseTask.IS_PUBLIC = 0;
baseTask.RealUserId = UserManager.UserId;
baseTask.RealUserName = UserManager.Name;
baseTask.UpdatedTime = DateTime.Now;
baseTask.UpdatedUserId = UserManager.UserId;
baseTask.UpdatedUserName = UserManager.Name;
//更新任务台,将当前任务变更为个人任务
await _taskBaseRepository.AsUpdateable(baseTask).UpdateColumns(it => new {
it.IS_PUBLIC,
it.RealUserId,
it.RealUserName,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
}
}
}
slotId = bcTaskInfo.BOOKING_SLOT_ID.Value;
var bcInfo = _taskBaseRepository.AsQueryable().Filter(null, true).Where(a => a.MBL_NO == bcTaskInfo.MBL_NO && a.CARRIER_ID == bcTaskInfo.CARRIERID
&& a.TASK_TYPE == TaskBaseTypeEnum.BC.ToString() && a.IsDeleted == false && a.TenantId == UserManager.TENANT_ID).First();
if (bcInfo == null)
throw Oops.Oh($"未检索到对应的BC任务");
var modifyFile = _taskFileRepository.AsQueryable().First(a => a.TASK_PKID == taskPKId && a.FILE_CATEGORY == "BC_MODIFY");
if(modifyFile == null)
throw Oops.Oh($"未获取到当前任务的文件信息");
var bcFile = _taskFileRepository.AsQueryable().First(a => a.TASK_PKID == bcInfo.PK_ID && a.FILE_CATEGORY == "BC");
if (bcFile == null)
throw Oops.Oh($"未获取到当前任务对应BC的文件信息");
var modifyFileName = Path.GetFileName(modifyFile.FILE_PATH);
var bcFileName = Path.GetFileName(bcFile.FILE_PATH);
var opt = App.GetOptions();
var dirAbs = opt.basePath;
if (string.IsNullOrEmpty(dirAbs))
{
dirAbs = App.WebHostEnvironment.WebRootPath;
}
var modifyFileFullPath = Path.Combine(dirAbs, modifyFile.FILE_PATH);
if (!File.Exists(modifyFileFullPath))
{
throw Oops.Oh("Amendment 文件提取失败");
}
var bcFileFullPath = Path.Combine(dirAbs, bcFile.FILE_PATH);
if (!File.Exists(bcFileFullPath))
{
throw Oops.Oh("BC 文件提取失败");
}
TaskBCInfoReadDto modifyBCReadInfo = await GetBCReaderInfo(modifyFileFullPath, modifyFileName, UserManager.TENANT_ID, taskPKId, App.Configuration["BookingAmendReadWebApiUrl"]);
if (modifyBCReadInfo == null)
throw Oops.Oh($"重新读取BC变更文件详情失败");
TaskBCInfoReadDto BCReadInfo = await GetBCReaderInfo(bcFileFullPath, bcFileName, UserManager.TENANT_ID, taskPKId, App.Configuration["BCReadWebApiUrl"]);
if (BCReadInfo == null)
throw Oops.Oh($"重新读取BC文件详情失败");
TaskBCInfoDto bcSrcDto = BCReadInfo.Adapt();
TaskBCInfoDto bcTargetDto = modifyBCReadInfo.Adapt();
if (BCReadInfo.CtnList != null && BCReadInfo.CtnList.Count > 0)
{
bcSrcDto.CtnList = BCReadInfo.CtnList.GroupBy(x => x.CtnALL)
.Select(x =>
{
return new TaskBCCTNInfoDto
{
CtnALL = x.Key,
CTNNUM = x.ToList()
.Sum(a => a.CTNNUM)
};
}).ToList();
}
if (modifyBCReadInfo.CtnList != null && modifyBCReadInfo.CtnList.Count > 0)
{
bcTargetDto.CtnList = modifyBCReadInfo.CtnList.GroupBy(x => x.CtnALL)
.Select(x =>
{
return new TaskBCCTNInfoDto
{
CtnALL = x.Key,
CTNNUM = x.ToList()
.Sum(a => a.CTNNUM)
};
}).ToList();
}
_logger.LogInformation($"触发比对差异开始,reqBatchNo={reqBatchNo}");
//比对差异
await server.PushCompareBCInfo(bcSrcDto, bcTargetDto, slotId, reqBatchNo);
_logger.LogInformation($"触发比对差异结束,reqBatchNo={reqBatchNo}");
}
#endregion
#region 读BC详情详情
///
/// 读BC详情详情
///
/// 文件完整路径
/// 文件名称
/// 所属租户
/// 任务ID
/// 请求URL
///
private async Task GetBCReaderInfo(string attachFullName, string fileName, long tenantId,string taskPKId,string url)
{
TaskBCInfoReadDto taskBCInfoReadDto = null;
try
{
DateTime nowDate = DateTime.Now;
EmailBCReadMessageInfo messageInfo = new EmailBCReadMessageInfo
{
Head = new TaskMessageHead
{
GID = IDGen.NextID().ToString(),
MessageType = "BOOKING_AMENDMENT",
SenderId = "DJY",
SenderName = "新大简云",
ReceiverId = "RulesEngine",
ReceiverName = "大简云规则引擎",
Version = "1.0",
RequestDate = nowDate.ToString("yyyy-MM-dd HH:mm:ss"),
RequestAction = "ReadFile",
},
Main = new EmailBCReadMessageMainInfo
{
TenantId = tenantId > 0 ? tenantId.ToString() : ""
}
};
NameValueCollection par = new NameValueCollection();
par.Add("jsonData", JSON.Serialize(messageInfo));
//解析BookingAmendment
var compareRlt = await ExcuteReadFile(par, url,new
{
file = "file",
fileName = fileName,
fileBytes = File.ReadAllBytes(attachFullName)
});
_logger.LogInformation($"读取BC附件详情 taskPKId={taskPKId},compareRlt={JSON.Serialize(compareRlt)}");
if (compareRlt.succ)
{
taskBCInfoReadDto = JSON.Deserialize(JSON.Serialize(compareRlt.extra));
}
}
catch (Exception ex)
{
_logger.LogError($"读取BC附件详情异常,原因:{ex.Message}");
}
return taskBCInfoReadDto;
}
#endregion
#region 请求BookingAmendment解析
///
/// 请求BookingAmendment解析
///
/// 请求参数
/// 请求url
/// 文件
/// 请求类型
/// 返回回执
[NonAction]
private async Task ExcuteReadFile(NameValueCollection nameValueCollection, string url,dynamic fileInfo,
string contentType = "application/json")
{
ParserReaderExcuteResultDto model = null;
var result = string.Empty;
using (var httpClient = new HttpClient())
{
try
{
using (var reduceAttach = new MultipartFormDataContent())
{
string[] allKeys = nameValueCollection.AllKeys;
foreach (string key in allKeys)
{
var dataContent = new ByteArrayContent(Encoding.UTF8.GetBytes(nameValueCollection[key]));
dataContent.Headers.ContentDisposition = new ContentDispositionHeaderValue($"form-data")
{
Name = key
};
reduceAttach.Add(dataContent);
}
#region 文件参数
if (fileInfo != null)
{
var Content = new ByteArrayContent(fileInfo.fileBytes);
//Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
//{
// Name = fileInfo.file.ToString(),
// FileName = fileInfo.fileName.ToString(),
//};
Content.Headers.Add("Content-Type", contentType);
reduceAttach.Add(Content, fileInfo.file.ToString(), HttpUtility.UrlEncode(fileInfo.fileName.ToString()));
}
#endregion
//httpClient.DefaultRequestHeaders.Add("USER_KEY", App.Configuration["ApiUserKey"]);
//httpClient.DefaultRequestHeaders.Add("USER_SECRET", App.Configuration["ApiUserSecret"]);
//请求
var response = httpClient.PostAsync(url, reduceAttach).Result;
result = response.Content.ReadAsStringAsync().Result;
model = JSON.Deserialize(result);
}
}
catch (Exception ex)
{
_logger.LogInformation("请求读取BC附件详情读取详情异常,原因:{error}", ex.Message);
throw Oops.Oh($"请求读取BC附件详情读取详情异常,原因:{ex.Message}");
}
}
return model;
}
#endregion
#region 获取BC关联的订舱详情
///
/// 获取BC关联的订舱详情
///
/// BC任务主键
/// 返回回执
[HttpGet("/TaskManageBC/GetBookingOrder")]
public async Task GetBookingOrder(string taskPkId)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
var baseTask = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == taskPkId);
if (baseTask == null)
{
throw Oops.Oh($"未提取有效的任务信息");
}
var bcTaskInfo = _taskBCInfoRepository.AsQueryable().First(a => a.TASK_ID == taskPkId);
if (bcTaskInfo == null)
{
throw Oops.Oh($"未提取有效的BC任务信息");
}
if (!bcTaskInfo.BOOKING_ORDER_ID.HasValue)
{
var server = _namedBookingSlotServiceProvider.GetService(nameof(BookingSlotService));
var slotModel = server.SearchBookingSlotWithOrderByNo(bcTaskInfo.MBL_NO, bcTaskInfo.TenantId.Value).GetAwaiter().GetResult();
if (slotModel != null && slotModel.BookingSlotId > 0)
{
if (slotModel.HasBookingOrder)
{
bcTaskInfo.BOOKING_ORDER_ID = slotModel.BookingOrderList.FirstOrDefault();
bcTaskInfo.UpdatedTime = DateTime.Now;
bcTaskInfo.UpdatedUserId = UserManager.UserId;
bcTaskInfo.UpdatedUserName = UserManager.Name;
await _taskBCInfoRepository.AsUpdateable(bcTaskInfo).UpdateColumns(it => new
{
it.BOOKING_ORDER_ID,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
}
}
}
if (bcTaskInfo.BOOKING_ORDER_ID.HasValue)
{
var bkRlt = await _bookingOrderService.Get(bcTaskInfo.BOOKING_ORDER_ID.Value);
if (bkRlt != null)
{
var BookingSubmitResultModel = new BookingOrSlotGenerateDto
{
CustomerId = bkRlt.CUSTOMERID,
CustomerName = bkRlt.CUSTOMERNAME,
CustServiceId = !string.IsNullOrWhiteSpace(bkRlt.CUSTSERVICEID)? long.Parse(bkRlt.CUSTSERVICEID):null,
CustServiceName = bkRlt.CUSTSERVICE,
SaleId = !string.IsNullOrWhiteSpace(bkRlt.SALEID) ? long.Parse(bkRlt.SALEID) : null,
SaleName = bkRlt.SALE,
OpId = !string.IsNullOrWhiteSpace(bkRlt.OPID) ? long.Parse(bkRlt.OPID) : null,
OpName = bkRlt.OP,
DocId = !string.IsNullOrWhiteSpace(bkRlt.DOCID) ? long.Parse(bkRlt.DOCID) : null,
DocName = bkRlt.DOC,
RouteID = !string.IsNullOrWhiteSpace(bkRlt.ROUTEID) ? long.Parse(bkRlt.ROUTEID) : null,
Route = bkRlt.ROUTE,
LineManageID = !string.IsNullOrWhiteSpace(bkRlt.LineManageID) ? long.Parse(bkRlt.LineManageID) : null,
LineManage = bkRlt.LineManage
};
result.ext = BookingSubmitResultModel;
result.succ = true;
}
}
return result;
}
#endregion
#region 更新订舱
///
/// 更新订舱
///
/// BC任务详情
/// BC任务集装箱列表
/// BC任务附件列表
/// 订舱请求详情
/// 返回订舱ID
private async Task UpdateBookingOrder(TaskBCInfo taskBCInfo, List taskBCCtnList, List taskFileList,
BookingOrSlotGenerateDto generateModel)
{
long id = 0;
try
{
/*
1、新增订舱
2、推送服务项目
3、推送附件
*/
var carrierInfo = _cache.GetAllCodeCarrier().GetAwaiter().GetResult()
.Where(t => t.Code.Equals(taskBCInfo.CARRIERID, StringComparison.OrdinalIgnoreCase)
|| t.EnName.Equals(taskBCInfo.CARRIERID, StringComparison.OrdinalIgnoreCase)
|| t.CnName.Equals(taskBCInfo.CARRIERID, StringComparison.OrdinalIgnoreCase)).FirstOrDefault();
SaveBookingOrderInput bkModel = new SaveBookingOrderInput
{
Id = taskBCInfo.BOOKING_ORDER_ID.Value,
CUSTOMERID = generateModel.CustomerId,
CUSTOMERNAME = generateModel.CustomerName,
CARRIERID = carrierInfo.Code?.Trim(),
CARRIER = carrierInfo.CnName?.Trim(),
MBLNO = taskBCInfo.MBL_NO.ToUpper().Trim(),
CONTRACTNO = !string.IsNullOrWhiteSpace(taskBCInfo.CONTRACTNO) ? taskBCInfo.CONTRACTNO : "",
VESSEL = taskBCInfo.VESSEL.ToUpper().Trim(),
VOYNO = taskBCInfo.VOYNO.ToUpper().Trim(),
VOYNOINNER = taskBCInfo.VOYNO.ToUpper().Trim(),
ETD = taskBCInfo.ETD,
ETA = taskBCInfo.ETA,
SALEID = generateModel.SaleId.ToString(),
SALE = generateModel.SaleName,
OPID = generateModel.OpId.ToString(),
OP = generateModel.OpName,
DOCID = generateModel.DocId.ToString(),
DOC = generateModel.DocName,
ROUTEID = generateModel.RouteID.ToString(),
ROUTE = generateModel.Route,
CZRemark = generateModel.CZRemark,
ShenQingXiangShi = generateModel.ShenQingXiangShi,
LineManageID = generateModel.LineManageID.ToString(),
LineName = generateModel.LineManage,
CLOSEVGMDATE = taskBCInfo.VGM_CUTOFF_TIME,
CLOSINGDATE = taskBCInfo.CY_CUTOFF_TIME,
CLOSEDOCDATE = taskBCInfo.CUT_SINGLE_TIME,
CUSTSERVICEID = generateModel.CustServiceId.HasValue ? generateModel.CustServiceId.Value.ToString() : null,
CUSTSERVICE = generateModel.CustServiceName,
ctnInputs = new List()
};
var ctnCodeList = _cache.GetAllCodeCtn().GetAwaiter().GetResult().ToList();
if (taskBCCtnList.Count > 0)
{
taskBCCtnList.ForEach(t =>
{
var ctnCode = ctnCodeList.FirstOrDefault(a => !string.IsNullOrWhiteSpace(a.Name) &&
a.Name.Equals(t.CTNALL, StringComparison.OrdinalIgnoreCase));
BookingCtnDto ctn = new BookingCtnDto
{
CTNCODE = ctnCode?.Code,
CTNALL = t.CTNALL,
CTNNUM = t.CTNNUM.HasValue ? t.CTNNUM.Value : 1
};
bkModel.ctnInputs.Add(ctn);
});
}
bkModel.ChangedFields = new List
{
nameof(SaveBookingOrderInput.CUSTOMERID),
nameof(SaveBookingOrderInput.CUSTOMERNAME),
nameof(SaveBookingOrderInput.CARRIERID),
nameof(SaveBookingOrderInput.CARRIER),
nameof(SaveBookingOrderInput.CONTRACTNO),
nameof(SaveBookingOrderInput.VESSEL),
nameof(SaveBookingOrderInput.VOYNO),
nameof(SaveBookingOrderInput.SALEID),
nameof(SaveBookingOrderInput.SALE),
nameof(SaveBookingOrderInput.OPID),
nameof(SaveBookingOrderInput.OP),
nameof(SaveBookingOrderInput.DOCID),
nameof(SaveBookingOrderInput.DOC),
nameof(SaveBookingOrderInput.ROUTEID),
nameof(SaveBookingOrderInput.ROUTE),
nameof(SaveBookingOrderInput.LineManageID),
nameof(SaveBookingOrderInput.LineName),
nameof(SaveBookingOrderInput.VOYNOINNER),
nameof(SaveBookingOrderInput.ROUTE),
nameof(SaveBookingOrderInput.ETD),
nameof(SaveBookingOrderInput.ETA),
nameof(SaveBookingOrderInput.CZRemark),
nameof(SaveBookingOrderInput.ShenQingXiangShi),
nameof(SaveBookingOrderInput.CLOSEVGMDATE),
nameof(SaveBookingOrderInput.CLOSINGDATE),
nameof(SaveBookingOrderInput.CUSTSERVICEID),
nameof(SaveBookingOrderInput.CUSTSERVICE),
};
var bkRlt = await _bookingOrderService.Save(bkModel);
id = bkRlt.Id;
string batchNo = IDGen.NextID().ToString();
if (id > 0)
{
var hisList = _bookingOrderContactRepository.AsQueryable().Where(a => a.BookingId == id && a.IsDeleted == false).ToList();
if (hisList.Count > 0)
{
//批量作废
hisList.ForEach(async a =>
{
a.IsDeleted = true;
a.UpdatedTime = DateTime.Now;
a.UpdatedUserId = UserManager.UserId;
a.UpdatedUserName = UserManager.Name;
await _bookingOrderContactRepository.UpdateAsync(a);
});
}
//这里如果指定了委托单位的邮件联系人,则推送订舱联系人
if (generateModel.CustomerContactList != null && generateModel.CustomerContactList.Count > 0)
{
var bookingContactList = _bookingOrderContactRepository.AsQueryable()
.Where(a => a.BookingId == id && !a.IsDeleted).ToList();
var djyCustomerInfo = _djyCustomerService.Detail(new GetDjyCustomerInput { Id = generateModel.CustomerId.Value })
.GetAwaiter().GetResult();
generateModel.CustomerContactList.ForEach(contact =>
{
DjyCustomerContactOutput djyCustomerContactMan = null;
if (djyCustomerInfo.Contacts != null && djyCustomerInfo.Contacts.Count > 0)
{
djyCustomerContactMan = djyCustomerInfo.Contacts.FirstOrDefault(a =>
a.Id == contact.CustomerContactId);
}
if (djyCustomerContactMan != null)
{
var bookingContact = bookingContactList
.FirstOrDefault(x => x.Email.Equals(djyCustomerContactMan.Email, StringComparison.OrdinalIgnoreCase));
if (bookingContact == null)
{
bookingContact = new BookingOrderContact
{
Name = djyCustomerContactMan.Name,
BookingId = id,
Email = djyCustomerContactMan.Email,
Remark = djyCustomerContactMan.Remark,
CreatedTime = DateTime.Now,
CreatedUserId = UserManager.UserId,
CreatedUserName = UserManager.Name
};
_bookingOrderContactRepository.Insert(bookingContact);
}
else
{
bookingContact.Name = djyCustomerContactMan.Name;
bookingContact.Email = djyCustomerContactMan.Email;
bookingContact.Remark = djyCustomerContactMan.Remark;
bookingContact.UpdatedTime = DateTime.Now;
bookingContact.UpdatedUserId = UserManager.UserId;
bookingContact.UpdatedUserName = UserManager.Name;
_bookingOrderContactRepository.AsUpdateable(bookingContact).UpdateColumns(it => new
{
it.Name,
it.Email,
it.Remark,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommand();
}
}
});
}
if (generateModel.ProjectList != null && generateModel.ProjectList.Count > 0)
{
ModifyServiceProjectDto projectDto = new ModifyServiceProjectDto
{
BookingId = id,
ProjectCodes = generateModel.ProjectList.Distinct().ToArray(),
};
//写入服务项目
var prjRlt = await _bookingValueAddedService.SaveServiceProject(projectDto);
_logger.LogInformation($"推送订舱的服务项目完成 id={id} rlt={JSON.Serialize(prjRlt)}");
}
}
_logger.LogInformation($"任务BC MBLNO:{taskBCInfo.MBL_NO} 生成订舱成功 id={id}");
}
catch (Exception ex)
{
_logger.LogError($"任务BC MBLNO:{taskBCInfo.MBL_NO} 生成订舱异常,原因:{ex.Message}");
}
return id;
}
#endregion
#region 校验生成订舱或舱位的联系人信息
///
/// 校验生成订舱或舱位的联系人信息
///
/// 请求参数
private void ValidateContact(BookingOrSlotGenerateDto model)
{
//校验前端提交的联系人是否是委托绑定的联系人
if (model.GenerateMethod == "GEN_BOOKING_SLOT" || model.GenerateMethod == "GEN_BOOKING" || model.GenerateMethod == "UPD_BOOKING")
{
DjyTenantParamValueOutput paramConfig = null;
var paramList = _djyTenantParamService.GetParaCodeWithValue(new[] { CONST_CREATE_BOOKING_NEED_CONTACT }).GetAwaiter().GetResult();
if (paramList != null && paramList.Count > 0)
{
paramConfig = paramList.FirstOrDefault(a => a.ParaCode.Equals(CONST_CREATE_BOOKING_NEED_CONTACT, StringComparison.OrdinalIgnoreCase));
}
if (model.CustomerContactList != null && model.CustomerContactList.Count > 0)
{
//取委托客户下面所有的联系人列表
var djyCustomerInfo = _djyCustomerService.Detail(new GetDjyCustomerInput { Id = model.CustomerId.Value })
.GetAwaiter().GetResult();
if (djyCustomerInfo == null)
{
_logger.LogInformation($"委托单位{model.CustomerName} 获取失败,委托单位不存在或已作废 taskid={model.BCTaskId}");
throw Oops.Oh($"委托单位{model.CustomerName} 获取失败,委托单位不存在或已作废");
}
if (djyCustomerInfo.Contacts == null && djyCustomerInfo.Contacts.Count < 1)
{
_logger.LogInformation($"委托单位{model.CustomerName} 获取相关联系人失败,委托单位相关联系人为空 taskid={model.BCTaskId}");
throw Oops.Oh($"委托单位{model.CustomerName} 获取相关联系人失败,委托单位相关联系人为空");
}
model.CustomerContactList.ForEach(contact =>
{
DjyCustomerContactOutput djyCustomerContactMan = djyCustomerInfo.Contacts
.FirstOrDefault(a => a.Id == contact.CustomerContactId);
if (djyCustomerContactMan == null)
{
_logger.LogInformation($"委托单位{model.CustomerName} 联系人 {contact.CustomerContactName}获取失败,联系人不存在或已作废 taskid={model.BCTaskId}");
throw Oops.Oh($"委托单位{model.CustomerName} 联系人 {contact.CustomerContactName}获取失败,联系人不存在或已作废");
}
});
}
else
{
if (paramConfig != null && paramConfig.ParaValue.Equals("ENABLE", StringComparison.OrdinalIgnoreCase))
{
throw Oops.Oh($"生成订舱时往来单位联系必填,请修改");
}
}
}
}
#endregion
}
///
/// 自定义邮件
///
public class EmailApiUserDefinedDto
{
///
/// 接收邮箱
///
public string SendTo { get; set; }
///
/// 抄送
///
public string CCTo { get; set; }
///
/// 邮件标题
///
public string Title { get; set; }
///
/// 邮件正文
///
public string Body { get; set; }
///
/// 账号
///
public string Account { get; set; }
///
/// 密码(126邮箱需要在网站生成密文)
///
public string Password { get; set; }
///
/// SMTP服务器
///
public string Server { get; set; }
///
/// SMTP端口
///
public int Port { get; set; }
///
/// 发件SSL
///
public bool UseSSL { get; set; }
///
/// 附件
///
public List Attaches { get; set; }
}
}