using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Myshipping.Application.Entity;
using Myshipping.Core;
using Myshipping.Core.Helper;
using Npoi.Mapper;
using NPOI.OpenXmlFormats.Wordprocessing;
using Org.BouncyCastle.Crypto;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application
{
///
/// 截止时间变更
///
[ApiDescriptionSettings("Application", Name = "TaskManageCutDateChange", Order = 10)]
public class TaskManageCutDateChangeService: ITaskManageCutDateChangeService, IDynamicApiController
{
private readonly SqlSugarRepository _taskCutDateChangeInfoRepository;
private readonly SqlSugarRepository _taskCutDateChangeDetailInfoRepository;
private readonly SqlSugarRepository _taskBaseRepository;
private readonly SqlSugarRepository _bookingOrderRepository;
public TaskManageCutDateChangeService(SqlSugarRepository taskCutDateChangeInfoRepository,
SqlSugarRepository taskCutDateChangeDetailInfoRepository,
SqlSugarRepository taskBaseRepository,
SqlSugarRepository bookingOrderRepository)
{
_taskCutDateChangeInfoRepository = taskCutDateChangeInfoRepository;
_taskCutDateChangeDetailInfoRepository = taskCutDateChangeDetailInfoRepository;
_taskBaseRepository = taskBaseRepository;
_bookingOrderRepository = bookingOrderRepository;
}
#region 获取截止时间变更详情
///
/// 获取截止时间变更详情
///
/// 截止时间变更主键
/// 返回回执
[HttpGet("/TaskManageCutDate/GetInfo")]
public async Task> GetInfo(string pkId)
{
List list = new List();
var cutChangeOrder = _taskCutDateChangeInfoRepository.AsQueryable().First(a => a.PK_ID == pkId);
if (cutChangeOrder == null)
throw Oops.Oh($"截止时间变更主键{pkId}无法获取业务信息");
var detailList = _taskCutDateChangeDetailInfoRepository.AsQueryable()
.Where(a => a.P_ID == pkId).ToList();
if (detailList.Count > 0)
{
list = detailList.OrderBy(p => p.MBL_NO).Select(p =>
{
TaskCutDateChangeShowDto model = p.Adapt();
model.NoticeDate = cutChangeOrder.NOTICE_DATE;
model.Carrier = cutChangeOrder.CARRIER;
return model;
}).ToList();
}
return list;
}
#endregion
#region 通过任务主键获取截止时间变更详情
///
/// 通过任务主键获取截止时间变更详情
///
/// 截止时间变更任务主键
/// 返回回执
[HttpGet("/TaskManageCutDate/GetInfoByTaskId")]
public async Task> GetInfoByTaskId(string taskPkId)
{
List list = new List();
var taskBase = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == taskPkId);
if (taskBase == null)
throw Oops.Oh($"任务主键{taskPkId}无法获取业务信息");
var cutChangeOrder = _taskCutDateChangeInfoRepository.AsQueryable().First(a => a.TASK_ID == taskBase.PK_ID);
if (cutChangeOrder == null)
throw Oops.Oh($"截止时间变更主键{taskPkId}无法获取业务信息");
var detailList = _taskCutDateChangeDetailInfoRepository.AsQueryable()
.Where(a => a.P_ID == cutChangeOrder.PK_ID).ToList();
if (detailList.Count > 0)
{
list = detailList.OrderBy(p => p.MBL_NO).Select(p =>
{
TaskCutDateChangeShowDto model = p.Adapt();
model.NoticeDate = cutChangeOrder.NOTICE_DATE;
model.Carrier = cutChangeOrder.CARRIER;
return model;
}).ToList();
}
return list;
}
#endregion
#region 重新处理截止时间变更任务
///
/// 重新处理截止时间变更任务
/// 对未匹配订舱订单的任务记录,重新对应订单订单
///
/// 截止时间变更任务主键
/// 返回回执
public async Task SearchAndConnectBookingInfo(string taskPkId)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
var taskBase = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == taskPkId);
if (taskBase == null)
throw Oops.Oh($"任务主键{taskPkId}无法获取业务信息");
var cutDateChange = _taskCutDateChangeInfoRepository.AsQueryable().First(a => a.TASK_ID == taskBase.PK_ID);
if (cutDateChange == null)
throw Oops.Oh($"截止时间变更主键{taskPkId}无法获取业务信息");
var detailList = _taskCutDateChangeDetailInfoRepository.AsQueryable()
.Where(a => a.P_ID == cutDateChange.PK_ID).ToList();
//如果都有订单订单记录,则提示错误不进行对应
if (!detailList.Any(t => !t.BOOKING_ID.HasValue))
{
throw Oops.Oh($"当前截止时间变更已有匹配的订舱订单,不能执行操作");
}
var mblList = detailList.Select(a => a.MBL_NO).Distinct().ToList();
var bookingList = _bookingOrderRepository.AsQueryable().Filter(null, true)
.Where(a => mblList.Contains(a.MBLNO) && a.IsDeleted == false && (a.ParentId == null || a.ParentId == 0)).ToList();
if (bookingList.Count == 0)
throw Oops.Oh($"提单号未提取有效的订舱订单");
List msgList = new List();
detailList.ForEach(async t =>
{
//只处理没有订舱ID的记录
if (!t.BOOKING_ID.HasValue)
{
var currBook = bookingList.FirstOrDefault(p => p.MBLNO == t.MBL_NO);
if (currBook != null)
{
t.BOOKING_ID = currBook.Id;
t.UpdatedUserId = UserManager.UserId;
t.UpdatedUserName = UserManager.Name;
//更新任务BC
await _taskCutDateChangeDetailInfoRepository.AsUpdateable(t).UpdateColumns(it => new
{
it.BOOKING_ID,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
}
else
{
msgList.Add(t.MBL_NO);
}
}
});
if (msgList.Count > 0)
throw Oops.Oh($"提单号{(string.Join(",", msgList.ToArray()))}未提取有效的订舱订单");
result.succ = true;
result.msg = "成功";
return result;
}
#endregion
#region 推送及时消息
///
/// 推送及时消息
///
/// 截止时间任务主键
/// 返回回执
public async Task SendInstantMessage(string taskPkId)
{
TaskManageOrderResultDto result = new TaskManageOrderResultDto();
var taskBase = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == taskPkId);
if (taskBase == null)
throw Oops.Oh($"任务主键{taskPkId}无法获取业务信息");
var cutDateChange = _taskCutDateChangeInfoRepository.AsQueryable().First(a => a.TASK_ID == taskBase.PK_ID);
if (cutDateChange == null)
throw Oops.Oh($"截止时间变更主键{taskPkId}无法获取业务信息");
var detailList = _taskCutDateChangeDetailInfoRepository.AsQueryable()
.Where(a => a.P_ID == cutDateChange.PK_ID).ToList();
//如果都有订单订单记录,则提示错误不进行对应
if (!detailList.Any(t => t.BOOKING_ID.HasValue))
{
throw Oops.Oh($"当前截止时间变更没有匹配的订舱订单,不能执行操作");
}
var ids = detailList.Where(a => a.BOOKING_ID.HasValue).Select(a => a.BOOKING_ID.Value).ToList();
var bookingList = _bookingOrderRepository.AsQueryable().Filter(null, true)
.Where(a => ids.Contains(a.Id) && a.IsDeleted == false && (a.ParentId == null || a.ParentId == 0)).ToList();
if (bookingList.Count == 0)
throw Oops.Oh($"提单号未提取有效的订舱订单");
Dictionary> msgList = new Dictionary>();
msgList = bookingList.GroupBy(a => a.OP).Select(a =>
{
var currList = a.ToList();
return new { Key = a.Key, list = currList.Select(a => a.MBLNO).ToList() };
}).ToDictionary(a => a.Key, b => b.list);
if (msgList.Count > 0)
{
msgList.ForEach(t =>
{
DingTalkGroupHelper.SendDingTalkGroupMessage("KangqianNotify", "截止时间变更消息", $"@{t.Key} 收到如下提单号{(string.Join(",",t.Value.ToArray()))}的截止时间变更消息");
});
}
if(detailList.Any(t=>!t.BOOKING_ID.HasValue))
throw Oops.Oh($"提单号{(string.Join(",", detailList.Where(t => !t.BOOKING_ID.HasValue).Select(t=>t.MBL_NO).ToArray()))}未提取有效的订舱订单");
result.succ = true;
result.msg = "成功";
return result;
}
#endregion
}
}