using Furion;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Myshipping.Application.Entity;
using Myshipping.Application.Helper;
using Myshipping.Application.Service.TaskManagePlat.Interface;
using Myshipping.Core;
using Myshipping.Core.Entity;
using Myshipping.Core.Helper;
using MySqlX.XDevAPI.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application
{
///
/// VGM任务
///
[ApiDescriptionSettings("Application", Name = "TaskManageVGM", Order = 10)]
public class TaskManageVGMService: ITaskManageVGMService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _taskVGMInfoRepository;
private readonly SqlSugarRepository _taskVGMCtnInfoRepository;
private readonly SqlSugarRepository _taskBaseRepository;
private readonly ILogger _logger;
private readonly SqlSugarRepository _bookingOrderRepository;
private readonly SqlSugarRepository _bookingOrderContaRepository;
private readonly SqlSugarRepository _sysUserRepository;
public TaskManageVGMService(SqlSugarRepository taskVGMInfoRepository,
SqlSugarRepository taskVGMCtnInfoRepository,
SqlSugarRepository taskBaseRepository,
SqlSugarRepository bookingOrderRepository,
SqlSugarRepository bookingOrderContaRepository,
SqlSugarRepository sysUserRepository,
ILogger logger)
{
_taskVGMInfoRepository = taskVGMInfoRepository;
_taskVGMCtnInfoRepository = taskVGMCtnInfoRepository;
_taskBaseRepository = taskBaseRepository;
_logger = logger;
_bookingOrderRepository = bookingOrderRepository;
_bookingOrderContaRepository = bookingOrderContaRepository;
_sysUserRepository = sysUserRepository;
}
#region 获取VGM成功回执详情
///
/// 获取VGM成功回执详情
///
/// BC主键
/// 返回回执
[HttpGet("/TaskManageVGM/GetVGMSubmissionInfo")]
public async Task> GetVGMSubmissionInfo(string pkId)
{
List list = new List();
var vgmOrder = _taskVGMInfoRepository.AsQueryable().First(a => a.PK_ID == pkId);
if (vgmOrder == null)
throw Oops.Oh($"VGM主键{pkId}无法获取业务信息");
var CtnList = _taskVGMCtnInfoRepository.AsQueryable()
.Where(a => a.P_ID == pkId).ToList();
if (CtnList.Count > 0)
{
list = CtnList.OrderBy(p => p.P_ID).ThenBy(p => p.CNTRNO).Select(p =>
{
TaskVGMShowDto model = vgmOrder.Adapt();
model.CntrNo = p.CNTRNO;
model.VGMWeight = p.VGM_WEIGHT;
model.VGMWeightMethod = p.VGM_METHOD;
model.VGMWeightUnit = p.VGM_WEIGHT_UNIT;
return model;
}).ToList();
}
return list;
}
#endregion
#region 通过任务主键获取VGM成功回执详情
///
/// 通过任务主键获取VGM成功回执详情
///
/// VGM任务主键
/// 返回回执
[HttpGet("/TaskManageVGM/GetVGMSubmissionInfoByTaskId")]
public async Task> GetVGMSubmissionInfoByTaskId(string taskPkId)
{
List list = new List();
var taskBase = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == taskPkId);
if (taskBase == null)
throw Oops.Oh($"任务主键{taskPkId}无法获取业务信息");
var vgmOrder = _taskVGMInfoRepository.AsQueryable().First(a => a.TASK_ID == taskBase.PK_ID);
if (vgmOrder == null)
throw Oops.Oh($"任务主键{taskPkId}无法获取VGM信息");
var CtnList = _taskVGMCtnInfoRepository.AsQueryable()
.Where(a => a.P_ID == vgmOrder.PK_ID).ToList();
if (CtnList.Count > 0)
{
list = CtnList.OrderBy(p => p.P_ID).ThenBy(p => p.CNTRNO).Select(p =>
{
TaskVGMShowDto model = vgmOrder.Adapt();
model.CntrNo = p.CNTRNO;
model.VGMWeight = p.VGM_WEIGHT;
model.VGMWeightMethod = p.VGM_METHOD;
model.VGMWeightUnit = p.VGM_WEIGHT_UNIT;
return model;
}).ToList();
}
return list;
}
#endregion
#region 获取VGM未提交详情
///
/// 获取VGM未提交详情
///
/// BC主键
/// 返回回执
[HttpGet("/TaskManageVGM/GetVGMMissingInfo")]
public async Task> GetVGMMissingInfo(string pkId)
{
List list = new List();
var vgmOrder = _taskVGMInfoRepository.AsQueryable().First(a => a.PK_ID == pkId);
if (vgmOrder == null)
throw Oops.Oh($"VGM主键{pkId}无法获取业务信息");
var CtnList = _taskVGMCtnInfoRepository.AsQueryable()
.Where(a => a.P_ID == pkId).ToList();
if (CtnList.Count > 0)
{
list = CtnList.OrderBy(p => p.P_ID).ThenBy(p => p.CNTRNO).Select(p =>
{
TaskVGMShowDto model = vgmOrder.Adapt();
model.CntrNo = p.CNTRNO;
model.VGMWeight = p.VGM_WEIGHT;
model.VGMWeightMethod = p.VGM_METHOD;
model.VGMWeightUnit = p.VGM_WEIGHT_UNIT;
return model;
}).ToList();
}
return list;
}
#endregion
#region 通过任务主键获取VGM未提交详情
///
/// 通过任务主键获取VGM未提交详情
///
/// VGM任务主键
/// 返回回执
[HttpGet("/TaskManageVGM/GetVGMMissingInfoByTaskId")]
public async Task> GetVGMMissingInfoByTaskId(string taskPkId)
{
List list = new List();
var taskBase = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == taskPkId);
if (taskBase == null)
throw Oops.Oh($"任务主键{taskPkId}无法获取业务信息");
var vgmOrderList = _taskVGMInfoRepository.AsQueryable().Where(a => a.TASK_ID == taskBase.PK_ID).ToList();
if (vgmOrderList.Count == 0)
throw Oops.Oh($"任务主键{taskPkId}无法获取VGM信息");
var vgmPkList = vgmOrderList.Select(a => a.PK_ID).ToList();
var CtnList = _taskVGMCtnInfoRepository.AsQueryable()
.Where(a => vgmPkList.Contains(a.P_ID)).ToList();
if (CtnList.Count > 0)
{
vgmOrderList.ForEach(v => {
list.AddRange(CtnList.Where(p => p.P_ID == v.PK_ID).OrderBy(p => p.CNTRNO).Select(p =>
{
TaskVGMShowDto model = v.Adapt();
model.CntrNo = p.CNTRNO;
model.VGMWeight = p.VGM_WEIGHT;
model.VGMWeightMethod = p.VGM_METHOD;
model.VGMWeightUnit = p.VGM_WEIGHT_UNIT;
return model;
}).ToList());
});
}
return list;
}
#endregion
///
/// 发送VGM未提交通知(钉钉消息、邮件通知)
///
///
///
[HttpGet("/TaskManageVGM/SendVGMMissingNotice")]
public async Task SendVGMMissingNotice(string taskPKId)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
string billnoMsg = string.Empty;
try
{
/*
检索对应的订舱订单,提取相关人推送消息和
*/
var taskBase = _taskBaseRepository.AsQueryable().Filter(null, true).First(a => a.PK_ID == taskPKId && a.IsDeleted == false && a.TenantId == UserManager.TENANT_ID);
if (taskBase == null)
throw Oops.Oh($"任务主键{taskPKId}无法获取业务信息");
var vgmTaskList = _taskVGMInfoRepository.AsQueryable().Filter(null, true).Where(a => a.TASK_ID == taskPKId && a.IsDeleted == false && a.TenantId == UserManager.TENANT_ID).ToList();
if (vgmTaskList.Count == 0)
throw Oops.Oh($"VGM未提交通知任务主键{taskPKId}无法获取业务信息");
var blnoList = vgmTaskList.Select(a=>a.MBL_NO).Distinct().ToList();
if (!vgmTaskList.Any(a => a.TASK_TYPE.Equals("VGM_MISSING", StringComparison.OrdinalIgnoreCase)))
{
_logger.LogInformation($"VGM未提交通知任务主键{taskPKId} 不是TASK_TYPE=VGM_MISSING ");
result.succ = false;
result.msg = "当前只推送VGM未提交的通知";
return result;
}
//关联提单号
var bookList = _bookingOrderRepository.AsQueryable().Filter(null, true)
.Where(a => (blnoList.Contains(a.MBLNO) || a.SPLIT_OR_MERGE_FLAG != null && a.SPLIT_OR_MERGE_FLAG.Value == 1
&& blnoList.Contains(a.CUSTNO)) && a.IsDeleted == false && a.TenantId == UserManager.TENANT_ID).ToList();
if (bookList.Count == 0)
{
billnoMsg = (string.Join(",", blnoList.ToArray()));
throw Oops.Oh($"VGM任务主键{taskPKId} 提单号:{billnoMsg} 没有对应的订舱记录");
}
bookList.Select(a => new { Key = $"{a.VESSEL}/{(!string.IsNullOrWhiteSpace(a.VOYNO) ? a.VOYNO : a.VOYNOINNER)}", obj = a })
.GroupBy(a => a.Key).ToList()
.ForEach(async a =>
{
var bkList = a.Select(a=>a.obj).ToList();
var currBillNoList = bkList.Select(a=>a.MBLNO).Distinct().ToList();
if (bkList.Any(b => !string.IsNullOrWhiteSpace(b.CUSTNO)))
currBillNoList.AddRange(bkList.Select(a => a.CUSTNO).Distinct().ToList());
var taskVgmList = vgmTaskList.Where(b => currBillNoList.Contains(b.MBL_NO)).ToList();
await PushDingdingNotice(bkList, taskVgmList,taskPKId);
});
}
catch (Exception ex)
{
_logger.LogInformation($"VGM未提交通知任务主键{taskPKId} 推送钉钉通知异常,相关提单号-{billnoMsg} 原因:{ex.Message}");
new EmailNoticeHelper().SendEmailNotice($"VGM未提交通知任务主键{taskPKId} 推送钉钉通知异常", $"VGM未提交通知任务主键{taskPKId} 推送钉钉通知异常,相关提单号-{billnoMsg} 原因:{ex.Message}", App.Configuration["EmailNoticeDefaultUser"].GetUserEmailList());
}
return result;
}
private async Task PushDingdingNotice(List bookingOrderList, List taskVgmList, string taskPKId)
{
try
{
var bookingInfo = bookingOrderList.FirstOrDefault();
var tenantId = bookingInfo.TenantId;
StringBuilder contentBuilder = new StringBuilder();
contentBuilder.AppendLine("起运港:" + bookingInfo.PORTLOAD);
contentBuilder.AppendLine("目的港:" + bookingInfo.PORTDISCHARGE);
contentBuilder.AppendLine("船名/航次:" + $"{bookingInfo.VESSEL}/{(!string.IsNullOrWhiteSpace(bookingInfo.VOYNO) ? bookingInfo.VOYNO : bookingInfo.VOYNOINNER)}");
contentBuilder.AppendLine("ETD:" + (bookingInfo.ETD.HasValue ? bookingInfo.ETD.Value.ToString("yyyy-MM-dd") : ""));
contentBuilder.AppendLine("船公司:" + bookingInfo.CARRIER);
contentBuilder.AppendLine("=================");
contentBuilder.AppendLine("提单号 |对应操作 ");
List userIdList = new List();
bookingOrderList.ForEach(t =>
{
if (!string.IsNullOrWhiteSpace(t.OPID))
userIdList.Add(long.Parse(t.OPID));
if (!string.IsNullOrWhiteSpace(t.CUSTSERVICEID))
userIdList.Add(long.Parse(t.CUSTSERVICEID));
List userNameList = new List();
if (!string.IsNullOrWhiteSpace(t.OP))
userNameList.Add(t.OP);
if (!string.IsNullOrWhiteSpace(t.CUSTSERVICE))
userNameList.Add(t.CUSTSERVICE);
string billno = t.MBLNO;
if (t.SPLIT_OR_MERGE_FLAG.HasValue && t.SPLIT_OR_MERGE_FLAG.Value == 2)
billno = t.CUSTNO;
contentBuilder.AppendLine($"{billno.PadRight(15, ' ')}|{(string.Join(",", userNameList.ToArray()).PadRight(40, ' '))}");
});
var userInfoList = _sysUserRepository.AsQueryable().Filter(null, true)
.Where(x => userIdList.Contains(x.Id) && x.IsDeleted == false && x.TenantId == UserManager.TENANT_ID).ToList();
var mobileArg = userInfoList.Select(p => p.Phone).Distinct().ToArray();
_logger.LogInformation($"准备钉钉通知,内容:\r\n{contentBuilder.ToString()}");
DingTalkGroupHelper.SendDingTalkGroupMessage($"{tenantId}_Notify", "VGM未提交通知提醒", $"{contentBuilder.ToString()}", false, mobileArg, null);
_logger.LogInformation($"钉钉通知完成");
taskVgmList.ForEach(async t =>
{
t.IS_DINGDING = true;
t.LST_SEND_DINGDING_DATE = DateTime.Now;
t.LST_SEND_STATUS = "SUCC";
await _taskVGMInfoRepository.AsUpdateable(t).UpdateColumns(p => new
{
p.IS_DINGDING,
p.LST_SEND_DINGDING_DATE,
t.LST_SEND_STATUS
}).ExecuteCommandAsync();
});
}
catch (Exception ex)
{
_logger.LogInformation($"VGM未提交通知任务主键{taskPKId} 推送钉钉通知异常 原因:{ex.Message}");
new EmailNoticeHelper().SendEmailNotice($"VGM未提交通知任务主键{taskPKId} 推送钉钉通知异常", $"VGM未提交通知任务主键{taskPKId} 推送钉钉通知异常 原因:{ex.Message}", App.Configuration["EmailNoticeDefaultUser"].GetUserEmailList());
}
}
public async Task SyncBookingOrder()
{
return null;
}
}
}