using Furion;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Furion.RemoteRequest.Extensions;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Myshipping.Application.Entity;
using Myshipping.Application.Event;
using Myshipping.Application.Job;
using Myshipping.Core;
using Myshipping.Core.Entity;
using Myshipping.Core.Service;
using Newtonsoft.Json.Linq;
using NPOI.XSSF.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application
{
///
/// 订舱自动化相关
///
[ApiDescriptionSettings("Application", Name = "BookingOrderAuto", Order = 1)]
public class BookingOrderAutoService : IDynamicApiController, ITransient
{
private readonly ILogger _logger;
private readonly SqlSugarRepository _repoTenant;
private readonly SqlSugarRepository _repoUser;
private readonly SqlSugarRepository _repoBookingOrder;
private readonly BookingOrderService _servBookingOrder;
private IServiceProvider _services { get; }
public BookingOrderAutoService(ILogger logger,
SqlSugarRepository repoTenant,
SqlSugarRepository repoUser,
SqlSugarRepository repoBookingOrder,
BookingOrderService servBookingOrder, IServiceProvider services)
{
_logger = logger;
_repoTenant = repoTenant;
_repoUser = repoUser;
_repoBookingOrder = repoBookingOrder;
_servBookingOrder = servBookingOrder;
_services = services;
}
///
/// 根据查询编号和公司ID获取操作信息
///
///
[HttpGet("/BookingOrderAuto/GetOpInfoForBooking")]
[AllowAnonymous, ApiUser(ApiCode = "BookingAuto")]
public async Task GetOpInfoForBooking([FromQuery] BookingAutoQueryInput dto)
{
var tenant = await _repoTenant.AsQueryable().Filter(null, true)
.WhereIF(dto.TenantId > 0, x => x.Id == dto.TenantId)
.WhereIF(!string.IsNullOrEmpty(dto.CompId), x => x.CompId == dto.CompId)
.FirstAsync();
if (tenant == null)
{
throw Oops.Bah("未找到租户公司信息");
}
var strOpId = await _repoBookingOrder.AsQueryable().Filter(null, true)
.Where(x => x.IsDeleted == false && x.TenantId == tenant.Id)
.WhereIF(dto.QueryType == BookingAutoQueryType.BookingNO, x => x.CUSTNO == dto.QueryCode)
.WhereIF(dto.QueryType == BookingAutoQueryType.MBLNO, x => x.MBLNO == dto.QueryCode)
.Select(x => x.OPID)
.FirstAsync();
if (string.IsNullOrEmpty(strOpId))
{
throw Oops.Bah("未找到业务或操作数据");
}
var opid = Convert.ToInt64(strOpId);
var opInfo = await _repoUser.AsQueryable().Filter(null, true)
.FirstAsync(x => x.Id == opid);
return opInfo.Adapt();
}
///
/// 发送下货纸
///
///
[HttpPost("/BookingOrderAuto/LetterYard")]
[AllowAnonymous, ApiUser(ApiCode = "BookingAuto")]
public async Task LetterYard(long bookingId)
{
await _servBookingOrder.SendLetterYard(bookingId);
}
///
/// 放舱
///
///
[HttpPost("/BookingOrderAuto/FangCang")]
[AllowAnonymous, ApiUser(ApiCode = "BookingAuto")]
public async Task FangCang()
{
//自动放舱涉及选择模板的问题,得确认方案后实现
throw Oops.Bah("暂未实现");
}
///
/// 更新订舱数据
/// 当前支持如下:
/// 1.提单号-MBLNO 2.开船日期-ETD
///
///
[HttpPost("/BookingOrderAuto/UpdateBookingData")]
[AllowAnonymous, ApiUser(ApiCode = "BookingAuto")]
public async Task UpdateBookingData(BookingAutoUpdateDataInput dto)
{
var tenant = await _repoTenant.AsQueryable().Filter(null, true)
.WhereIF(dto.TenantId > 0, x => x.Id == dto.TenantId)
.WhereIF(!string.IsNullOrEmpty(dto.CompId), x => x.CompId == dto.CompId)
.FirstAsync();
if (tenant == null)
{
throw Oops.Bah("未找到租户公司信息");
}
var order = await _repoBookingOrder.AsQueryable().Filter(null, true)
.Where(x => x.IsDeleted == false && x.TenantId == tenant.Id)
.WhereIF(dto.QueryType == BookingAutoQueryType.BookingNO, x => x.CUSTNO == dto.QueryCode)
.WhereIF(dto.QueryType == BookingAutoQueryType.MBLNO, x => x.MBLNO == dto.QueryCode)
.FirstAsync();
if (order == null)
{
throw Oops.Bah("未找到业务数据");
}
var jobjData = dto.DataJson as JObject;
var updColList = new List();
foreach (var item in jobjData)
{
if (item.Key.ToUpper() == "MBLNO")
{
order.MBLNO = item.Value.ToString();
updColList.Add("MBLNO");
}
if (item.Key.ToUpper() == "ETD")
{
order.ETD = Convert.ToDateTime(item.Value.ToString());
updColList.Add("ETD");
}
}
await _repoBookingOrder.AsUpdateable(order).UpdateColumns(updColList.ToArray()).ExecuteCommandAsync();
_logger.LogInformation($"更新订舱数据 {order.Id},更新字段:{string.Join(",", updColList)},原始数据:{dto.ToJsonString()}");
}
///
/// 获取船司账号信息
///
///
[HttpPost("/BookingOrderAuto/GetCarrierAccount")]
[AllowAnonymous, ApiUser(ApiCode = "BookingAuto")]
public async Task GetCarrierAccount()
{
}
///
/// 获取lara中的提单号
///
///
[HttpPost("/BookingOrderAuto/ScheduleLaraBlno")]
[AllowAnonymous, ApiUser(ApiCode = "BookingAuto")]
public async Task ScheduleLaraBlno(GetLaraBlnoModel model)
{
var listKey = "LaraBlnoTaskList";
var cache = App.GetService();
var list = cache.Get>(listKey);
if (list == null)
{
list = new List();
}
if (list.Count(x => x.CsrCode == model.CsrCode) == 0)
{
model.Start = DateTime.Now;
model.Last = DateTime.Now.AddMinutes(30); //半小时后开始查
list.Add(model);
await cache.SetAsync(listKey, list);
}
}
}
}