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/BookingSyncSubscriber.cs

664 lines
33 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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.Enum;
using Myshipping.Application.Service.BookingOrder.Dto;
using Myshipping.Core;
using Myshipping.Core.Entity;
using Myshipping.Core.Service;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace Myshipping.Application.Event
{
/// <summary>
/// 订舱同步给客户订舱系统
/// </summary>
public class BookingSyncSubscriber : IEventSubscriber
{
private IServiceProvider _services { get; }
private readonly ILogger<BookingSyncSubscriber> _logger;
public BookingSyncSubscriber(IServiceProvider services, ILogger<BookingSyncSubscriber> logger)
{
_services = services;
_logger = logger;
}
#region 运营端事件
//发送订舱同步数据给客户订舱系统
[EventSubscribe("SendToCustomer:Book")]
public async Task SendToCustomer(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到订舱同步客户订舱系统请求:{context.Source.Payload}");
var paraObj = context.Source.Payload as dynamic;
var bookId = (long)paraObj.Id;
var syncType = (string)paraObj.SyncType;
using var scope = _services.CreateScope();
var repoCutomerOrder = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingCustomerOrder>>();
var repoOrder = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingOrder>>();
var repoCtn = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingCtn>>();
var repoFile = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingFile>>();
var repoStaLog = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingStatusLog>>();
var repoStaLogDetail = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingStatusLogDetail>>();
var repoGoodsSta = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingGoodsStatus>>();
var repoGoodsStaCfg = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingGoodsStatusConfig>>();
var repoLog = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingLog>>();
var repoLogDetail = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingLogDetail>>();
var cacheService = scope.ServiceProvider.GetRequiredService<ISysCacheService>();
var custOrder = await repoCutomerOrder.AsQueryable().Filter(null, true).FirstAsync(x => x.BookingId == bookId);
if (custOrder == null) //非客户订舱系统过来的数据
{
_logger.LogInformation($"ID为 {bookId} 的数据并非来自客户订舱系统,不继续处理数据回推");
return;
}
if (string.IsNullOrEmpty(custOrder.FeedbackUrl))
{
_logger.LogInformation($"ID为 {bookId} 的数据未提供回推接口URL不继续处理数据回推");
return;
}
var order = await repoOrder.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == bookId);
var ctns = await repoCtn.AsQueryable().Filter(null, true).Where(x => x.BILLID == bookId).ToListAsync();
var files = await repoFile.AsQueryable().Filter(null, true).Where(x => x.BookingId == bookId).ToListAsync();
var staLogs = await repoStaLog.AsQueryable().Filter(null, true).Where(x => x.BookingId == bookId).ToListAsync();
var staLogDetails = await repoStaLogDetail.AsQueryable().Filter(null, true).Where(x => staLogs.Select(y => y.Id).Contains(x.PId)).ToListAsync();
var goodsLogs = await repoGoodsSta.AsQueryable().Filter(null, true).Where(x => x.bookingId == bookId).ToListAsync();
var goodsLogConfigs = await repoGoodsStaCfg.AsQueryable().Filter(null, true).Where(x => goodsLogs.Select(y => y.ConfigId).Contains(x.Id)).ToListAsync();
var bookLogs = await repoLog.AsQueryable().Filter(null, true).Where(x => x.BookingId == bookId).ToListAsync();
var bookLogDetails = await repoLogDetail.AsQueryable().Filter(null, true).Where(x => bookLogs.Select(y => y.Id).Contains(x.PId)).ToListAsync();
//把ID还原为客户订舱系统中的ID
order.Id = Convert.ToInt64(order.BSNO);
order.BSNO = null;
ctns.ForEach(x => x.BILLID = order.Id);
files.ForEach(x => x.BookingId = order.Id);
staLogs.ForEach(x => x.BookingId = order.Id);
goodsLogs.ForEach(x => x.bookingId = order.Id);
//货运动态
var staLogSendList = staLogs.Adapt<List<BookingStatusLogSyncCustomerDto>>();
staLogSendList.ForEach(x =>
{
x.Details = staLogDetails.Where(y => y.PId == x.Id).Adapt<List<BookingStatusLogDetailSyncCustomerDto>>();
});
//修改日志
var bookLogSendList = bookLogs.Adapt<List<BookingLogSyncCustomerDto>>();
bookLogSendList.ForEach(x =>
{
x.Details = bookLogDetails.Where(y => y.PId == x.Id).Adapt<List<BookingLogDetailSyncCustomerDto>>();
});
//发送对象
var sendObj = new BookingCustomerRecDataFeedbackDto()
{
Order = order.Adapt<BookingOrderSyncCustomerDto>(),
Ctns = ctns.Adapt<List<BookingCtnSyncCustomerDto>>(),
Files = files.Adapt<List<BookingFileSyncCustomerDto>>(),
StatusLogs = staLogSendList,
BookingLogs = bookLogSendList
};
////文件内容
//var opt = App.GetOptions<BookingAttachOptions>();
//var dirAbs = opt.basePath;
//if (string.IsNullOrEmpty(dirAbs))
//{
// dirAbs = App.WebHostEnvironment.WebRootPath;
//}
//foreach (var file in files)
//{
// var fileFullPath = Path.Combine(dirAbs, file.FilePath);
// if (File.Exists(fileFullPath))
// {
// sendObj.Files.First(x => x.Id == file.Id).FileContent = File.ReadAllBytes(fileFullPath);
// }
//}
var feedbackObj = new BookingFeedbackDto(BookingFeedbackType.BookingAll.ToString());
feedbackObj.JsonContent = sendObj.ToJsonString();
feedbackObj.SyncType = syncType;
//回推回执
_logger.LogInformation($"准备发送客户订舱数据同步:{feedbackObj.ToJsonString()}URL{custOrder.FeedbackUrl}");
var rtn = await custOrder.FeedbackUrl
.SetHeaders(new Dictionary<string, object> {
{ CommonConst.API_USER_HEADER_KEY, custOrder.FeedbackKey},
{ CommonConst.API_USER_HEADER_SECRET, custOrder.FeedbackSecret}
})
.SetBody(feedbackObj, "application/x-www-form-urlencoded")
.PostAsStringAsync();
_logger.LogInformation($"回推数据同步返回:{rtn}");
var jobjRtn = JObject.Parse(rtn);
if (jobjRtn.GetIntValue("code") != 200)
{
throw Oops.Bah(jobjRtn.GetStringValue("message"));
}
}
//推送货物状态到客户订舱系统
[EventSubscribe("SyncGoodStatus:Book")]
public async Task SyncGoodStatus(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到推送货物状态到客户订舱系统:{context.Source.Payload}");
GoodsStatusSyncDto payload = context.Source.Payload as GoodsStatusSyncDto;
using var scope = _services.CreateScope();
var repoCutomerOrder = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingCustomerOrder>>();
var repoOrder = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingOrder>>();
var cacheService = scope.ServiceProvider.GetRequiredService<ISysCacheService>();
var custOrder = await repoCutomerOrder.AsQueryable().Filter(null, true).FirstAsync(x => x.BookingId == payload.Id);
if (custOrder == null) //非客户订舱系统过来的数据
{
_logger.LogInformation($"ID为 {payload.Id} 的数据并非来自客户订舱系统,不继续处理数据回推");
return;
}
if (string.IsNullOrEmpty(custOrder.FeedbackUrl))
{
_logger.LogInformation($"ID为 {custOrder.Id} 的数据未提供回推接口URL不继续处理数据回推");
return;
}
var order = await repoOrder.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == payload.Id);
//把ID还原为客户订舱系统中的ID
payload.Id = Convert.ToInt64(order.BSNO);
var feedbackObj = new BookingFeedbackDto(BookingFeedbackType.GoodsStatus.ToString());
feedbackObj.JsonContent = payload.ToJsonString();
//回推回执
_logger.LogInformation($"准备发送货物状态数据同步:{feedbackObj.ToJsonString()}URL{custOrder.FeedbackUrl}");
var rtn = await custOrder.FeedbackUrl
.SetHeaders(new Dictionary<string, object> {
{ CommonConst.API_USER_HEADER_KEY, custOrder.FeedbackKey},
{ CommonConst.API_USER_HEADER_SECRET, custOrder.FeedbackSecret}
})
.SetBody(feedbackObj, "application/x-www-form-urlencoded")
.PostAsStringAsync();
_logger.LogInformation($"回推数据同步返回:{rtn}");
var jobjRtn = JObject.Parse(rtn);
if (jobjRtn.GetIntValue("code") != 200)
{
throw Oops.Bah(jobjRtn.GetStringValue("message"));
}
}
//运营端处理了修改服务项目的消息的回推消息
[EventSubscribe("Message:Process:Booking:ChangeServiceItem")]
public async Task MessageProcessBookingChangeServiceItem(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到回推服务项目修改审核到客户订舱系统:{context.Source.Payload}");
var msgId = (long)context.Source.Payload;
using var scope = _services.CreateScope();
var repMessage = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<DjyMessage>>();
var repoCutomerOrder = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingCustomerOrder>>();
var repoOrder = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingOrder>>();
var repoServiceItem = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingServiceItem>>();
var cacheService = scope.ServiceProvider.GetRequiredService<ISysCacheService>();
var msg = await repMessage.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == msgId);
var extObj = JsonConvert.DeserializeObject<ChangeServiceItemExtModel>(msg.ExtData);
var custOrdId = extObj.CustomerOrderId;
var custOrder = await repoCutomerOrder.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == custOrdId);
//修改服务项目
if (msg.ProcStatus == MessageProcessStatus.Accept.ToString())
{
var serItm = cacheService.GetAllDictData().Result.FirstOrDefault(x => x.TypeCode == "booking_service_item" && x.Code == extObj.Dto.ServiceCode);
var serv = await repoServiceItem.AsQueryable().Filter(null, true).FirstAsync(x => x.BookingId == extObj.OrderId && x.Code == extObj.Dto.ServiceCode);
if (extObj.Dto.IsCancel) //取消服务项目
{
await repoServiceItem.DeleteAsync(serv);
}
else //增加服务项目
{
if (serv == null)
{
serv = new BookingServiceItem();
serv.Id = YitIdHelper.NextId();
serv.BookingId = extObj.OrderId;
serv.Code = extObj.Dto.ServiceCode;
serv.Value = serItm.Value;
await repoServiceItem.InsertAsync(serv);
}
}
}
ChangeServiceItemResponseDto respDto = new ChangeServiceItemResponseDto();
respDto.Dto = extObj.Dto;
respDto.AuditName = msg.ProcUser;
respDto.IsReject = msg.ProcStatus == MessageProcessStatus.Reject.ToString();
respDto.ProcResult = msg.ProcResult;
//回推回执
var feedbackObj = new BookingFeedbackDto(BookingFeedbackType.ServiceItemAudit.ToString());
feedbackObj.JsonContent = respDto.ToJsonString();
_logger.LogInformation($"准备回推服务项目修改审核到客户订舱系统:{feedbackObj.JsonContent}URL{custOrder.FeedbackUrl}");
var rtn = await custOrder.FeedbackUrl
.SetHeaders(new Dictionary<string, object> {
{ CommonConst.API_USER_HEADER_KEY, custOrder.FeedbackKey},
{ CommonConst.API_USER_HEADER_SECRET, custOrder.FeedbackSecret}
})
.SetBody(feedbackObj, "application/x-www-form-urlencoded")
.PostAsStringAsync();
_logger.LogInformation($"回推服务项目修改审核:{rtn}");
var jobjRtn = JObject.Parse(rtn);
if (jobjRtn.GetIntValue("code") != 200)
{
throw Oops.Bah(jobjRtn.GetStringValue("message"));
}
}
//运营端处理了单证补料的消息的回推消息
[EventSubscribe("Message:Process:Booking:DocSupplement")]
public async Task MessageProcessBookingDocSupplement(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到回推单证补料审核到客户订舱系统:{context.Source.Payload}");
var msgId = (long)context.Source.Payload;
using var scope = _services.CreateScope();
var repMessage = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<DjyMessage>>();
var repoCutomerOrder = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingCustomerOrder>>();
var repoOrder = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingOrder>>();
var repoServiceItem = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingServiceItem>>();
var cacheService = scope.ServiceProvider.GetRequiredService<ISysCacheService>();
var _repBookingfile = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingFile>>();
var _repStatuslog = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingStatusLog>>();
var msg = await repMessage.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == msgId);
var extObj = JsonConvert.DeserializeObject<DocSupplementExtModel>(msg.ExtData);
var custOrder = await repoCutomerOrder.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == extObj.CustomerOrderId);
var order = await repoOrder.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == extObj.OrderId);
if (msg.ProcStatus == MessageProcessStatus.Accept.ToString())
{
//进入订舱附件
foreach (var ff in extObj.Dto.FileList)
{
var fileSuffix = Path.GetFileName(ff.FileName);
var id = YitIdHelper.NextId();
var newFile = new BookingFile
{
Id = id,
FileName = ff.FileName,
FilePath = ff.FilePath,
TypeCode = ff.TypeCode,
TypeName = ff.TypeName,
BookingId = extObj.OrderId,
TenantId = order.TenantId,
TenantName = order.TenantName,
};
await _repBookingfile.InsertAsync(newFile);
ff.Id = id; //赋值id回传给客户端
}
}
//记录日志
var staLog = new BookingStatusLog();
staLog.Status = $"审核{(msg.ProcStatus == MessageProcessStatus.Accept.ToString() ? "" : "")}单证补料";
staLog.CreatedUserId = order.CreatedUserId;
staLog.CreatedUserName = msg.ProcUser;
staLog.CreatedTime = DateTime.Now;
staLog.OpTime = DateTime.Now;
staLog.BookingId = order.Id;
staLog.Category = "doc_supplement";
staLog.TenantId = order.TenantId;
_repStatuslog.Insert(staLog);
DocSupplementResponseDto respDto = new DocSupplementResponseDto();
respDto.Dto = extObj.Dto;
respDto.AuditName = msg.ProcUser;
respDto.IsReject = msg.ProcStatus == MessageProcessStatus.Reject.ToString();
respDto.ProcResult = msg.ProcResult;
//回推回执
var feedbackObj = new BookingFeedbackDto(BookingFeedbackType.DocSupplementAudit.ToString());
feedbackObj.JsonContent = respDto.ToJsonString();
_logger.LogInformation($"准备回推单证补料审核到客户订舱系统:{feedbackObj.JsonContent}URL{custOrder.FeedbackUrl}");
var rtn = await custOrder.FeedbackUrl
.SetHeaders(new Dictionary<string, object> {
{ CommonConst.API_USER_HEADER_KEY, custOrder.FeedbackKey},
{ CommonConst.API_USER_HEADER_SECRET, custOrder.FeedbackSecret}
})
.SetBody(feedbackObj, "application/x-www-form-urlencoded")
.PostAsStringAsync();
_logger.LogInformation($"回推单证补料审核:{rtn}");
var jobjRtn = JObject.Parse(rtn);
if (jobjRtn.GetIntValue("code") != 200)
{
throw Oops.Bah(jobjRtn.GetStringValue("message"));
}
}
//运营端手工选中或取消服务项目的回推消息
[EventSubscribe("ServiceItem:ChangeNotify")]
public async Task BookingServiceItemChangeNotify(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到回推服务项目手工修改的通知到客户订舱系统:{context.Source.Payload}");
var dto = context.Source.Payload as DjyChangeServiceItemDto;
using var scope = _services.CreateScope();
var repoCutomerOrder = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingCustomerOrder>>();
var repoOrder = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingOrder>>();
var repoServiceItem = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingServiceItem>>();
var cacheService = scope.ServiceProvider.GetRequiredService<ISysCacheService>();
var bookId = Convert.ToInt64(dto.Id);
var custOrder = await repoCutomerOrder.AsQueryable().Filter(null, true).FirstAsync(x => x.BookingId == bookId);
var order = await repoOrder.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == bookId);
dto.Id = order.BSNO; //将运营端系统的id转为客户系统的id
//回推回执
var feedbackObj = new BookingFeedbackDto(BookingFeedbackType.ServiceItemChange.ToString());
feedbackObj.JsonContent = dto.ToJsonString();
_logger.LogInformation($"准备回推服务项目手工修改的通知到客户订舱系统:{feedbackObj.JsonContent}URL{custOrder.FeedbackUrl}");
var rtn = await custOrder.FeedbackUrl
.SetHeaders(new Dictionary<string, object> {
{ CommonConst.API_USER_HEADER_KEY, custOrder.FeedbackKey},
{ CommonConst.API_USER_HEADER_SECRET, custOrder.FeedbackSecret}
})
.SetBody(feedbackObj, "application/x-www-form-urlencoded")
.PostAsStringAsync();
_logger.LogInformation($"回推服务项目项目手工修改的通知:{rtn}");
var jobjRtn = JObject.Parse(rtn);
if (jobjRtn.GetIntValue("code") != 200)
{
throw Oops.Bah(jobjRtn.GetStringValue("message"));
}
}
//推送订舱批量订舱编号到客户订舱系统
[EventSubscribe("SendToCustomer:MultiCustNO")]
public async Task SendCustomerMultiCustNOSync(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到推送订舱批量订舱编号到客户订舱系统请求:{context.Source.Payload.ToString()}");
var dto = context.Source.Payload as BookingCustomerMultiCustNODto;
using var scope = _services.CreateScope();
var repoCutomerOrder = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingCustomerOrder>>();
var repoOrder = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingOrder>>();
var repoCtn = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingCtn>>();
//var repoFile = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingFile>>();
var servCustOrder = scope.ServiceProvider.GetRequiredService<BookingCustomerOrderService>();
//var repoStaLog = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingStatusLog>>();
//var repoStaLogDetail = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingStatusLogDetail>>();
//var repoGoodsSta = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingGoodsStatus>>();
//var repoGoodsStaCfg = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingGoodsStatusConfig>>();
//var repoLog = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingLog>>();
//var repoLogDetail = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingLogDetail>>();
//var cacheService = scope.ServiceProvider.GetRequiredService<ISysCacheService>();
var custOrder = await repoCutomerOrder.AsQueryable().Filter(null, true).FirstAsync(x => x.BSNO == dto.Id);
if (custOrder == null) //非客户订舱系统过来的数据
{
_logger.LogInformation($"ID为 {dto.Id} 的数据并非来自客户订舱系统,不继续处理数据回推");
return;
}
var ctns = await repoCtn.AsQueryable().Filter(null, true).Where(x => x.IsDeleted == false && x.BILLID == custOrder.Id).ToListAsync();
if (string.IsNullOrEmpty(custOrder.FeedbackUrl))
{
_logger.LogInformation($"ID为 {dto.Id} 的数据未提供回推接口URL不继续处理数据回推");
return;
}
var feedbackObj = new BookingFeedbackDto(BookingFeedbackType.OneToMulti.ToString());
feedbackObj.JsonContent = dto.ToJsonString();
//回推回执
_logger.LogInformation($"准备发送多订舱编号数据同步:{feedbackObj.ToJsonString()}URL{custOrder.FeedbackUrl}");
var rtn = await custOrder.FeedbackUrl
.SetHeaders(new Dictionary<string, object> {
{ CommonConst.API_USER_HEADER_KEY, custOrder.FeedbackKey},
{ CommonConst.API_USER_HEADER_SECRET, custOrder.FeedbackSecret}
})
.SetBody(feedbackObj, "application/x-www-form-urlencoded")
.PostAsStringAsync();
_logger.LogInformation($"回推多订舱编号返回:{rtn}");
var jobjRtn = JObject.Parse(rtn);
if (jobjRtn.GetIntValue("code") != 200)
{
throw Oops.Bah(jobjRtn.GetStringValue("message"));
}
else
{
var dataResp = jobjRtn.GetJArrayValue("data");
var parentBookingNO = custOrder.BOOKINGNO;
foreach (JObject item in dataResp)
{
//生成id并插入客户订舱数据
custOrder.Id = YitIdHelper.NextId();
custOrder.BOOKINGNO = item.GetStringValue("BookingNO");
custOrder.BSNO = item.GetStringValue("CustOrderId");
custOrder.ParentBookingNO = parentBookingNO;
JObject jobjExt = null;
if (!string.IsNullOrEmpty(custOrder.ExtendData))
{
jobjExt = JObject.Parse(custOrder.ExtendData);
}
else
{
jobjExt = new JObject();
}
jobjExt["CustNO"] = item.GetStringValue("CustNO");
await repoCutomerOrder.InsertAsync(custOrder);
foreach (var ctn in ctns)
{
ctn.Id = YitIdHelper.NextId();
ctn.BILLID = custOrder.Id;
await repoCtn.InsertAsync(ctn);
}
_logger.LogInformation($"根据客户订舱反馈数据 {custOrder.BSNO} 生成了相同的数据 {custOrder.Id}");
//插入订舱台账
var ordId = await servCustOrder.CustomerToOrder(custOrder);
custOrder.BookingId = ordId;
await repoCutomerOrder.AsUpdateable(custOrder).UpdateColumns(x => new { x.BookingId }).ExecuteCommandAsync();
_logger.LogInformation($"生成了订舱台账数据 {ordId}");
}
}
}
#endregion
#region 客户端事件
//推送服务项目变动到运营端
[EventSubscribe("ServiceItem:ChangeSubmit")]
public async Task SendServiceItemChangeSubmit(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到推送服务项目变动到运营端订舱系统:{context.Source.Payload}");
var dto = context.Source.Payload as ChangeServiceItemDto;
if (dto != null)
{
using var scope = _services.CreateScope();
var _cache = scope.ServiceProvider.GetRequiredService<ISysCacheService>();
//当前系统的url
var sysUrlCfg = _cache.GetAllSysConfig().Result.FirstOrDefault(x => x.Code == "SystemUrl");
var sysUrl = sysUrlCfg.Value;
if (!sysUrl.EndsWith("/"))
{
sysUrl += "/";
}
var recUrl = _cache.GetAllSysConfig().Result.FirstOrDefault(x => x.Code == "DjyBookingRequestReceiveUrl");
if (recUrl == null || string.IsNullOrEmpty(recUrl.Value))
{
throw Oops.Bah("大简云接收订舱URL地址未配置请联系管理员");
}
var userId = _cache.GetAllSysConfig().Result.FirstOrDefault(x => x.Code == "DjyBookingReceiveUserId");
var userSecret = _cache.GetAllSysConfig().Result.FirstOrDefault(x => x.Code == "DjyBookingReceiveUserSecret");
if (userId == null || string.IsNullOrEmpty(userId.Value) || userSecret == null || string.IsNullOrEmpty(userSecret.Value))
{
throw Oops.Bah("大简云接收订舱用户key和秘钥未配置请联系管理员");
}
//构建完整url
var submitUrl = recUrl.Value;
if (!submitUrl.EndsWith("/"))
{
submitUrl += "/";
}
submitUrl += "BookingCustomerOrder/ChangeServiceItem";
_logger.LogInformation($"提交修改服务项目数据({submitUrl}{userId.Value}{userSecret.Value}{JsonConvert.SerializeObject(dto)}");
var rtn = await submitUrl
.SetHeaders(new Dictionary<string, object> {
{ CommonConst.API_USER_HEADER_KEY, userId.Value},
{ CommonConst.API_USER_HEADER_SECRET, userSecret.Value}
})
.SetBody(dto)
.PostAsStringAsync();
_logger.LogInformation($"返回数据:{rtn}");
var resultText = new StringBuilder();
var jobjRtn = JObject.Parse(rtn);
if (jobjRtn.GetIntValue("code") != 200)
{
throw Oops.Bah(jobjRtn.GetStringValue("message"));
}
}
}
//推送货物状态通知给订阅人
[EventSubscribe("GoodsStatusSubscribeNotify:Book")]
public async Task GoodsStatusSubscribeNotify(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到推送货物状态通知请求:{context.Source.Payload}");
dynamic payload = context.Source.Payload;
long bookId = payload.BookingId;
string statusCode = payload.StatusCode;
string statusName = payload.StatusName;
using var scope = _services.CreateScope();
var cache = scope.ServiceProvider.GetRequiredService<ISysCacheService>();
var repoOrder = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingOrder>>();
var repoBookingGoodsStatusSubscribe = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingGoodsStatusSubscribe>>();
var order = await repoOrder.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == bookId);
var subList = await repoBookingGoodsStatusSubscribe.AsQueryable().Filter(null, true).Where(x => x.IsDeleted == false && x.BookingId == bookId).ToListAsync();
foreach (var sub in subList)
{
if (sub.SendMail && !string.IsNullOrEmpty(sub.Email) && sub.StatusCode.Split(',').Contains(statusCode))
{
var subject = $"大简云-货物通知:{order.MBLNO}---{order.VESSEL}/{order.VOYNO}{statusName}";
var body = $"尊敬的客户,您好:<br/><br/>您所订阅的提单号{order.MBLNO} ,船名航次{order.VESSEL}/{order.VOYNO}的业务{statusName}。<br/><br/>—— 此邮件为大简云平台自动发送,请勿回复。<br/><br/><img src='https://wechat.myshipping.net:8860/wechat_image/banner.png' style='width:300px;'/>";
var sendUrlCfg = cache.GetAllDictData().Result.FirstOrDefault(x => x.Code == "email_api_url");
if (sendUrlCfg == null)
{
_logger.LogError($"未配置邮件发送URL推送货物状态通知邮件未能发送。订舱ID{bookId},状态:{statusName}({statusCode}),邮箱:{sub.Email}");
}
var mailJson = new dynamic[]{
new
{
SendTo = sub.Email,
Title = subject,
Body = body,
SmtpConfig = "SERVICE"
}
};
var mailStr = mailJson.ToJsonString();
_logger.LogInformation($"准备推送货物状态邮件通知JSON{mailStr}订舱ID{bookId},状态:{statusName}({statusCode}),邮箱:{sub.Email}");
var rtn = await sendUrlCfg.Value
.SetBody(mailStr)
.PostAsStringAsync();
_logger.LogError($"推送货物状态通知邮件发送返回:{rtn}。订舱ID{bookId},状态:{statusName}({statusCode}),邮箱:{sub.Email}");
var jsonRtn = JObject.Parse(rtn);
if (jsonRtn.GetBooleanValue("Success"))
{
_logger.LogInformation($"推送货物状态通知邮件发送成功。订舱ID{bookId},状态:{statusName}({statusCode}),邮箱:{sub.Email}");
}
else
{
_logger.LogError($"推送货物状态通知邮件发送失败:{jsonRtn.GetStringValue("Message")}。订舱ID{bookId},状态:{statusName}({statusCode}),邮箱:{sub.Email}。");
}
}
}
}
#endregion
}
}