You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Application/Event/BillTraceSubscriber.cs

178 lines
7.8 KiB
C#

1 year ago
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
{
/// <summary>
/// 运踪
/// </summary>
public class BillTraceSubscriber : IEventSubscriber
{
private IServiceProvider _services { get; }
private readonly ILogger<BillTraceSubscriber> _logger;
public BillTraceSubscriber(IServiceProvider services, ILogger<BillTraceSubscriber> 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<SqlSugarRepository<BookingOrder>>();
var _cache = scope.ServiceProvider.GetRequiredService<ISysCacheService>();
var _repBookingStatus = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingStatus>>();
var _repBookinglog = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingLog>>();
var _repBookinglogdetail = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingLogDetail>>();
//var _webAccountConfig = scope.ServiceProvider.GetRequiredService<IDjyWebsiteAccountConfigService>();
var _repUser = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<SysUser>>();
var _repStatuslog = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingStatusLog>>();
var _repStatuslogDetail = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingStatusLogDetail>>();
var _repWebsiteAccountConfig = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<DjyWebsiteAccountConfig>>();
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> billTraceList = new List<BillTraceList>();
var dicdatalist = _cache.GetAllDictData().Result;
_logger.LogInformation("调用运踪修改接口提单号:" + order.MBLNO + " 调用运踪接口");
var uidList = new List<long>();
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 = true,
AlertEmail = string.Join(";", emailList)
});
var status = string.Empty;
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<RespCommon>();
_logger.LogInformation("调用运踪更改接口返回" + html.ToJsonString());
if (html.Success != true)
{
_logger.LogError("调用运踪更改接口返回:" + html.ToJsonString());
throw Oops.Bah(html.Message);
}
}
}
}