using Furion;
using Furion.DataEncryption;
using Furion.LinqBuilder;
using Furion.Logging;
using Furion.RemoteRequest.Extensions;
using Furion.TaskScheduler;
using Microsoft.Extensions.Logging;
using Myshipping.Application.Entity;
using Myshipping.Application.Enum;
using Myshipping.Application.MQ;
using Myshipping.Core;
using Myshipping.Core.Entity;
using Myshipping.Core.Helper;
using Myshipping.Core.Service;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NPOI.HSSF.Record.Cont;
using RabbitMQ.Client;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Application.Job
{
///
/// 获取LARA提单号
///
public class GetLaraBlnoWorker : ISpareTimeWorker
{
private const string ListKey = "LaraBlnoTaskList";
[SpareTime(60000, "GetLaraBlnoWorker", Description = "获取LARA提单号", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
public async void GetLaraBlno(SpareTimer timer, long count)
{
Log.Information($"GetLaraBlnoWorker {DateTime.Now}");
var cache = App.GetService();
var sysConfig = await cache.GetAllSysConfig();
var cfgCsrToBlnoSpiderUrl = sysConfig.FirstOrDefault(x => x.Code == "LaraCsrToBlnoSpiderUrl" && x.GroupCode == "DJY_CONST");
if (cfgCsrToBlnoSpiderUrl == null || string.IsNullOrEmpty(cfgCsrToBlnoSpiderUrl.Value))
{
var msg = $"根据CS号从lara获取提单号,未配置爬虫URL地址";
Log.Error(msg);
return;
}
var list = await cache.GetAsync>(ListKey);
if (list != null)
{
list = list.Where(x => x.Start > DateTime.Now.AddHours(-8)).ToList(); //超过8小时不再处理
var toDoList = list.Where(x => x.Last < DateTime.Now.AddMinutes(-10)).ToList(); //10分钟处理一次
toDoList.ForEach(x => x.Last = DateTime.Now);
cache.Set(ListKey, list);
foreach (var item in toDoList)
{
Log.Information($"请求爬虫根据CS号从lara获取提单号,CSR:{item.CsrCode}");
string response = null;
try
{
var apiUrl = $"{cfgCsrToBlnoSpiderUrl.Value}?mblno=&refno={item.CsrCode}";
response = await apiUrl
.SetClientTimeout(30)
.GetAsStringAsync();
}
catch (Exception ex)
{
Log.Error($"请求爬虫根据CS号从lara获取提单号执行出错:{ex.Message},CSR:{item.CsrCode}");
Log.Error(ex.StackTrace);
return;
}
Log.Information($"请求爬虫根据CS号从lara获取提单号({item.CsrCode}),返回:{response}");
var jobjResp = JObject.Parse(response);
if (jobjResp.GetIntValue("status") == 1)
{
var jobjData = jobjResp.GetJObjectValue("data");
var bno = jobjData.GetStringValue("bno");
if (bno != item.CsrCode) //不是CS号
{
//推送订舱自动化消息
var dto = new
{
Carrier = item.Carrier,
CompanyId = item.CompanyId,
TenantId = item.TenantId,
MBLNO = bno,
CsrCode = item.CsrCode
};
Log.Information($"准备推送订舱自动化消息LARA提单号:{JsonConvert.SerializeObject(dto)}");
ConnectionFactory factory = new ConnectionFactory();
factory.Uri = new Uri(App.Configuration["MQBookingAuto"]);
var mqConn = factory.CreateConnection("GetLaraBlnoWorker");
var exchangeName = "djy.booking.auto";
IModel model = mqConn.CreateModel();
model.ExchangeDeclare(exchange: exchangeName, type: ExchangeType.Topic);
model.BasicPublish(exchange: exchangeName,
routingKey: "LaraBlno",
basicProperties: null,
body: Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(dto)));
mqConn.Close();
Log.Information($"推送订舱自动化消息LARA提单号完成");
//移除已完成
list = await cache.GetAsync>(ListKey);
list = list.Where(x => x.CsrCode != item.CsrCode).ToList();
cache.Set(ListKey, list);
}
}
}
}
}
}
public class GetLaraBlnoModel
{
public string CsrCode { get; set; }
public string Carrier { get; set; }
public string CompanyId { get; set; }
public long TenantId { get; set; }
public DateTime Start { get; set; } = DateTime.Now;
public DateTime Last { get; set; } = DateTime.Now;
}
}