using Furion;
using Furion.EventBus;
using Furion.FriendlyException;
using Furion.RemoteRequest.Extensions;
using Mapster;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Myshipping.Application.ConfigOption;
using Myshipping.Application.Entity;
using Myshipping.Application.Service.BookingOrder.Dto;
using Myshipping.Core;
using Myshipping.Core.Entity;
using Myshipping.Core.Service;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application.Event
{
///
/// 运踪
///
public class BillTraceSubscriber : IEventSubscriber
{
private IServiceProvider _services { get; }
private readonly ILogger _logger;
public BillTraceSubscriber(IServiceProvider services, ILogger logger)
{
_services = services;
_logger = logger;
}
//更新订阅
[EventSubscribe("BillTrace:Update")]
public async Task BillTraceUpdate(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到更新订阅请求:{context.Source.Payload}");
var paraObj = context.Source.Payload as dynamic;
using var scope = _services.CreateScope();
var repoOrder = scope.ServiceProvider.GetRequiredService>();
var _cache = scope.ServiceProvider.GetRequiredService();
var _repBookingStatus = scope.ServiceProvider.GetRequiredService>();
var _repBookinglog = scope.ServiceProvider.GetRequiredService>();
var _repBookinglogdetail = scope.ServiceProvider.GetRequiredService>();
//var _webAccountConfig = scope.ServiceProvider.GetRequiredService();
var _repUser = scope.ServiceProvider.GetRequiredService>();
var _repStatuslog = scope.ServiceProvider.GetRequiredService>();
var _repStatuslogDetail = scope.ServiceProvider.GetRequiredService>();
var _repWebsiteAccountConfig = scope.ServiceProvider.GetRequiredService>();
long bookingId = paraObj.BookingId;
var order = repoOrder.AsQueryable().Filter(null, true).First(x => x.Id == bookingId);
long userId = paraObj.UserId;
long tenentId = paraObj.TenentId;
string userName = paraObj.UserName;
string djyUserId = paraObj.DjyUserId;
var key = _repWebsiteAccountConfig.AsQueryable().Filter(null, true).First(x => x.TypeCode == "seae_billtraceurl" && x.CreatedUserId == userId);
if (key == null)
{
key = _repWebsiteAccountConfig.AsQueryable().Filter(null, true).First(x => x.TypeCode == "seae_billtraceurl" && x.TenantId == tenentId && x.IsTenant);
}
if (key == null)
{
_logger.LogError("订阅运踪失败:调用运踪接口相关账号未维护!");
return;
}
/*
* 2023-9-8,允许分单的主提单号和主单的主提单号不一致之后,操作填写的分单中的提单号可能是错误的,
* 导致订阅运踪且后期回推数据,然后又推送东胜,导致分单会在东胜中生成主单信息,
* 跟和川操作确认,取消分单订阅运踪
*/
if (order.ParentId > 0)
{
_logger.LogError("分单不再订阅运踪");
return;
}
var url = _cache.GetAllDictData().Result;
BillTraceDto billdto = new BillTraceDto();
List billTraceList = new List();
var dicdatalist = _cache.GetAllDictData().Result;
_logger.LogInformation("调用运踪修改接口提单号:" + order.MBLNO + " 调用运踪接口");
var uidList = new List();
if (!string.IsNullOrEmpty(order.OPID))
{
uidList.Add(Convert.ToInt64(order.OPID));
}
if (!string.IsNullOrEmpty(order.CUSTSERVICEID))
{
uidList.Add(Convert.ToInt64(order.CUSTSERVICEID));
}
var emailList = _repUser.AsQueryable().Filter(null, true).Where(x => uidList.Contains(x.Id) && !string.IsNullOrEmpty(x.Email)).Select(x => x.Email).ToList();
billTraceList.Add(new BillTraceList
{
BusinessId = order.Id.ToString(),
MBLNO = order.MBLNO,
YARD = order.YARD == "" ? null : order.YARD,
YardCode = order.YARDID == "" ? null : order.YARDID,
CARRIER = order.CARRIER == "" ? null : order.CARRIER,
CARRIERID = order.CARRIERID == "" ? null : order.CARRIERID,
isBook = false,
AlertEmail = string.Join(";", emailList)
});
var status = "订阅起运港";
//if (!string.IsNullOrWhiteSpace(order.YARDID) && !string.IsNullOrWhiteSpace(order.CARRIERID))
//{
// status = "订阅起运港,目的港";
//}
//else
//if (string.IsNullOrWhiteSpace(order.YARDID) && !string.IsNullOrWhiteSpace(order.CARRIERID))
//{
// status = "订阅目的港";
//}
//else if (!string.IsNullOrWhiteSpace(order.YARDID) && string.IsNullOrWhiteSpace(order.CARRIERID))
//{
// status = "订阅起运港";
//}
var stalogids = _repStatuslog.AsQueryable().Filter(null, true).Where(x => x.BookingId == order.Id).Select(x => x.Id).ToList();
_repBookingStatus.Delete(x => x.BookingId == order.Id);
_repStatuslogDetail.Delete(x => stalogids.Contains(x.PId));
_repStatuslog.Delete(x => x.BookingId == order.Id);
////添加booking日志
var bid = await _repBookinglog.InsertReturnSnowflakeIdAsync(new BookingLog
{
Type = "Trace",
BookingId = order.Id,
TenantId = tenentId,
CreatedTime = DateTime.Now,
CreatedUserId = userId,
CreatedUserName = userName
});
await _repBookinglogdetail.InsertReturnSnowflakeIdAsync(new BookingLogDetail
{
PId = bid,
Field = "",
OldValue = "",
NewValue = status,
});
billdto.Children = billTraceList;
billdto.Key = key.Account;
billdto.PWD = key.Password;
billdto.url = url.FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "response_seae_billtraceurl").Value;
billdto.Gid = djyUserId;
var json = billdto.ToJsonString();
_logger.LogInformation("调用运踪更改接口发送josn:" + json);
var html = await url.FirstOrDefault(x => x.TypeCode == "url_set" && x.Code == "request_seae_updatebilltraceurl").Value.SetHttpMethod(HttpMethod.Post).SetQueries(new { msg = json }).SetRetryPolicy(3, 5000).SendAsAsync();
_logger.LogInformation("调用运踪更改接口返回" + html.ToJsonString());
if (html.Success != true)
{
_logger.LogError("调用运踪更改接口返回:" + html.ToJsonString());
throw Oops.Bah(html.Message);
}
}
}
}