using Furion;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Myshipping.Application.ConfigOption;
using Myshipping.Application.Entity;
using Myshipping.Application.Enum;
using Myshipping.Core;
using Myshipping.Core.Service;
using NPOI.SS.Formula.Functions;
using SqlSugar;
using StackExchange.Profiling.Internal;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace Myshipping.Application
{
///
/// BC任务
///
[ApiDescriptionSettings("Application", Name = "TaskManageBC", Order = 10)]
public class TaskManageBCService: ITaskManageBCService, IDynamicApiController
{
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;
public TaskManageBCService(SqlSugarRepository taskBCInfoRepository,
SqlSugarRepository taskBaseRepository,
SqlSugarRepository taskBCCTNInfoRepository,
SqlSugarRepository taskFileRepository,
SqlSugarRepository bookingOrderRepository,
SqlSugarRepository bookingCtnRepository)
{
_taskBaseRepository = taskBaseRepository;
_taskBCInfoRepository = taskBCInfoRepository;
_taskBCCTNInfoRepository = taskBCCTNInfoRepository;
_taskFileRepository = taskFileRepository;
_bookingOrderRepository = bookingOrderRepository;
_bookingCtnRepository = bookingCtnRepository;
}
#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>();
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
///
/// 获取待处理的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())
.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;
}
///
/// 任务ID下载附件
///
/// BC任务主键
/// 返回数据流
[HttpGet("/TaskManageBC/DownloadFile")]
public async Task DownloadFile(string taskPKId)
{
var bcTaskInfo = await _taskBaseRepository.AsQueryable().FirstAsync(u => u.PK_ID == taskPKId);
if (bcTaskInfo == null)
{
throw Oops.Oh($"任务主键{taskPKId}无法获取业务信息");
}
var fileInfo = await _taskFileRepository.AsQueryable().FirstAsync(u => u.TASK_PKID == taskPKId);
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;
}
}
}