using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Myshipping.Application.Entity;
using Myshipping.Application.Service.TaskManagePlat.Interface;
using Myshipping.Core;
using Myshipping.Core.Helper;
using Npoi.Mapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application
{
///
/// 任务目的港未提货未返箱
///
[ApiDescriptionSettings("Application", Name = "PODDischargeGateoutFull", Order = 10)]
public class TaskManagePODDischargeGateoutFullService: ITaskManagePODDischargeGateoutFullService, IDynamicApiController
{
private readonly SqlSugarRepository _taskPODDischargeGateoutFullInfoRep;
private readonly SqlSugarRepository _taskPODDischargeGateoutFullDetailInfoRep;
private readonly SqlSugarRepository _taskBaseRepository;
private readonly ILogger _logger;
private readonly SqlSugarRepository _bookingOrderRepository;
public TaskManagePODDischargeGateoutFullService(ILogger logger,
SqlSugarRepository taskPODDischargeGateoutFullInfoRep,
SqlSugarRepository taskPODDischargeGateoutFullDetailInfoRep,
SqlSugarRepository taskBaseRepository,
SqlSugarRepository bookingOrderRepository)
{
_taskPODDischargeGateoutFullInfoRep = taskPODDischargeGateoutFullInfoRep;
_taskBaseRepository = taskBaseRepository;
_taskPODDischargeGateoutFullDetailInfoRep = taskPODDischargeGateoutFullDetailInfoRep;
_logger = logger;
_bookingOrderRepository = bookingOrderRepository;
}
#region 获取目的港未提货未返箱详情
///
/// 获取目的港未提货未返箱详情
///
/// 目的港未提货未返箱主键
/// 返回回执
[HttpGet("/PODDischargeGateoutFull/GetInfo")]
public async Task> GetInfo(string pkId)
{
List list = new List();
var dischargeFull = _taskPODDischargeGateoutFullInfoRep.AsQueryable()
.First(a => a.PK_ID == pkId);
if (dischargeFull == null)
throw Oops.Oh($"目的港未提货未返箱主键{pkId}无法获取业务信息");
var dischargeFullDetail = _taskPODDischargeGateoutFullDetailInfoRep.AsQueryable()
.Where(a => a.P_ID == dischargeFull.PK_ID).ToList();
var taskBase = _taskBaseRepository.AsQueryable().First(a => a.PK_ID == dischargeFull.TASK_ID);
if (taskBase == null)
throw Oops.Oh($"任务主键无法获取业务信息");
list = dischargeFullDetail.Select(x => {
var dto = x.Adapt();
dto.PKId = dischargeFull.PK_ID;
dto.TaskId = dischargeFull.TASK_ID;
dto.IsComplete = taskBase.IS_COMPLETE == 1 ? true : false;
dto.CompleteTime = taskBase.COMPLETE_DATE;
dto.NoticeType = dischargeFull.NOTICE_TYPE;
dto.NoticeTypeName = dischargeFull.NOTICE_TYPE_NAME;
return dto;
}).ToList();
return list;
}
#endregion
#region 通过任务主键获取目的港未提货未返箱详情
///
/// 通过任务主键获取目的港未提货未返箱详情
///
/// 目的港未提货未返箱任务主键
/// 返回回执
[HttpGet("/PODDischargeGateoutFull/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 dischargeFull = _taskPODDischargeGateoutFullInfoRep.AsQueryable().First(a => a.TASK_ID == taskBase.PK_ID);
if (dischargeFull == null)
throw Oops.Oh($"目的港未提货未返箱主键{taskPkId}无法获取业务信息");
var dischargeFullDetail = _taskPODDischargeGateoutFullDetailInfoRep.AsQueryable()
.Where(a => a.P_ID == dischargeFull.PK_ID).ToList();
list = dischargeFullDetail.Select(x => {
var dto = x.Adapt();
dto.PKId = dischargeFull.PK_ID;
dto.TaskId = dischargeFull.TASK_ID;
dto.IsComplete = taskBase.IS_COMPLETE == 1 ? true : false;
dto.CompleteTime = taskBase.COMPLETE_DATE;
dto.NoticeType = dischargeFull.NOTICE_TYPE;
dto.NoticeTypeName = dischargeFull.NOTICE_TYPE_NAME;
return dto;
}).ToList();
return list;
}
#endregion
#region 推送及时消息
///
/// 推送及时消息
///
/// 目的港未提货未返箱任务主键
/// 返回回执
[HttpGet("/PODDischargeGateoutFull/SendInstantMessage")]
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 dischargeFull = _taskPODDischargeGateoutFullInfoRep.AsQueryable().First(a => a.TASK_ID == taskBase.PK_ID);
if (dischargeFull == null)
throw Oops.Oh($"目的港未提货未返箱主键{taskPkId}无法获取业务信息");
var detailList = _taskPODDischargeGateoutFullDetailInfoRep.AsQueryable()
.Where(a => a.P_ID == dischargeFull.PK_ID).ToList();
//如果都有订单订单记录,则提示错误不进行对应
if (!detailList.Any(t => t.BOOKING_ID.HasValue))
{
throw Oops.Oh($"当前目的港未提货未返箱没有匹配的订舱订单,不能执行操作");
}
var ids = detailList.Where(a => a.BOOKING_ID.HasValue).Select(a => a.BOOKING_ID.Value).ToList();
var bookingList = _bookingOrderRepository.AsQueryable().Filter(null, true)
.Where(a => ids.Contains(a.Id) && a.IsDeleted == false && (a.ParentId == null || a.ParentId == 0)).ToList();
if (bookingList.Count == 0)
throw Oops.Oh($"提单号未提取有效的订舱订单");
Dictionary> 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 =>
{
string title = "目的港未提货消息";
if (dischargeFull.NOTICE_TYPE == "GATEOUT_FULL")
title = "提箱未返空箱消息";
DingTalkGroupHelper.SendDingTalkGroupMessage("KangqianNotify", title, $"@{t.Key} 收到如下提单号{(string.Join(",", t.Value.ToArray()))}的{title}消息");
});
}
if (detailList.Any(t => !t.BOOKING_ID.HasValue))
throw Oops.Oh($"提单号{(string.Join(",", detailList.Where(t => !t.BOOKING_ID.HasValue).Select(t => t.MBL_NO).ToArray()))}未提取有效的订舱订单");
result.succ = true;
result.msg = "成功";
return result;
}
#endregion
#region 重新处理目的港未提货未返箱任务
///
/// 重新处理目的港未提货未返箱任务
/// 对未匹配目的港未提货未返箱的任务记录重新对应订单
///
/// 目的港未提货未返箱任务主键
/// 返回回执
[HttpGet("/PODDischargeGateoutFull/SearchAndConnectBookingInfo")]
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 dischargeFull = _taskPODDischargeGateoutFullInfoRep.AsQueryable().First(a => a.TASK_ID == taskBase.PK_ID);
if (dischargeFull == null)
throw Oops.Oh($"目的港未提货未返箱主键{taskPkId}无法获取业务信息");
var detailList = _taskPODDischargeGateoutFullDetailInfoRep.AsQueryable()
.Where(a => a.P_ID == dischargeFull.PK_ID).ToList();
//如果都有订单订单记录,则提示错误不进行对应
if (!detailList.Any(t => !t.BOOKING_ID.HasValue))
{
throw Oops.Oh($"目的港未提货未返箱已有匹配的订舱订单,不能执行操作");
}
var mblList = detailList.Select(a => a.MBL_NO).Distinct().ToList();
var bookingList = _bookingOrderRepository.AsQueryable().Filter(null, true)
.Where(a => mblList.Contains(a.MBLNO) && a.IsDeleted == false && (a.ParentId == null || a.ParentId == 0)).ToList();
if (bookingList.Count == 0)
throw Oops.Oh($"提单号未提取有效的订舱订单");
List 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 _taskPODDischargeGateoutFullDetailInfoRep.AsUpdateable(t).UpdateColumns(it => new
{
it.BOOKING_ID,
it.UpdatedTime,
it.UpdatedUserId,
it.UpdatedUserName
}).ExecuteCommandAsync();
}
else
{
msgList.Add(t.MBL_NO);
}
}
});
if (msgList.Count > 0)
throw Oops.Oh($"提单号{(string.Join(",", msgList.ToArray()))}未提取有效的订舱订单");
result.succ = true;
result.msg = "成功";
return result;
}
#endregion
}
}