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/Service/RulesEngine/RulesEngineClientService.cs

351 lines
13 KiB
C#

using Furion.FriendlyException;
using Furion.JsonSerialization;
using Furion;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Furion.RemoteRequest.Extensions;
using Furion.DependencyInjection;
using Microsoft.AspNetCore.Mvc;
using System.Xml.Linq;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Authorization;
using Myshipping.Application.Entity;
using Myshipping.Core;
using Furion.DistributedIDGenerator;
using Mapster;
using Furion.DataValidation;
using Newtonsoft.Json.Linq;
namespace Myshipping.Application
{
/// <summary>
/// 请求规则平台
/// </summary>
[ApiDescriptionSettings("Application", Name = "RulesEngineClient", Order = 9)]
public class RulesEngineClientService: IRulesEngineClientService, IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<BookingOrder> _bookingOrderRepository;
private readonly SqlSugarRepository<BookingCtn> _bookingOrderContaRepository;
private readonly SqlSugarRepository<BookingCtnDetail> _bookingOrderContaCargoRepository;
/// <summary>
///
/// </summary>
public RulesEngineClientService(SqlSugarRepository<BookingOrder> bookingOrderRepository,
SqlSugarRepository<BookingCtn> bookingOrderContaRepository,
SqlSugarRepository<BookingCtnDetail> bookingOrderContaCargoRepository)
{
_bookingOrderRepository = bookingOrderRepository;
_bookingOrderContaRepository = bookingOrderContaRepository;
_bookingOrderContaCargoRepository = bookingOrderContaCargoRepository;
}
#region 海运订舱请求规则引擎校验
/// <summary>
/// 海运订舱请求规则引擎校验
/// </summary>
/// <param name="model">海运订舱请求业务</param>
/// <returns>返回用户信息</returns>
[HttpPost("/RulesEngineClient/ExcuteRulesOceanBookingByMsg")]
public async Task<RulesEngineExcuteResultDto> ExcuteRulesOceanBookingByMsg(RulesEngineOrderBookingMessageInfo model)
{
RulesEngineExcuteResultDto result = new RulesEngineExcuteResultDto();
var ruleResult = await ExcuteRulesEngine(model);
if (ruleResult == null)
throw Oops.Oh($"订舱请求规则失败,返回为空");
var innerRlt = JSON.Deserialize<RemoteRulesEngineExcuteResultDto>(ruleResult.extra.ToString());
var ruleDetailList = innerRlt.DetailList;
result.succ = ruleResult.succ;
result.msg = ruleResult.msg;
if (ruleDetailList.Count > 0)
{
result.rows = ruleDetailList;
}
return result;
}
#endregion
#region 海运订舱请求规则引擎校验
/// <summary>
/// 海运订舱请求规则引擎校验
/// </summary>
/// <param name="bookingId">海运订舱主键</param>
/// <returns>返回用户信息</returns>
[HttpGet("/RulesEngineClient/ExcuteRulesOceanBooking")]
public async Task<RulesEngineExcuteResultDto> ExcuteRulesOceanBooking(string bookingId)
{
/*
1
2
3
4
5
*/
RulesEngineExcuteResultDto result = new RulesEngineExcuteResultDto();
try
{
DateTime nowDate = DateTime.Now;
2 years ago
var model = _bookingOrderRepository.AsQueryable().InSingle(long.Parse(bookingId));
if (model == null)
throw Oops.Oh($"订舱主键{bookingId}无法获取业务信息");
RulesEngineOrderBookingMessageInfo msgModel = new RulesEngineOrderBookingMessageInfo();
msgModel.Head = new RulesEngineWebAPIHeadBase
{
GID = IDGen.NextID().ToString(),
MessageType = "BUSI_RULE",
SenderId = App.Configuration["RulesEngineSender"],
SenderName = App.Configuration["RulesEngineSenderName"],
SenderKey = App.Configuration["RulesEngineAuthKey"],
ReceiverId = "RulesEngine",
ReceiverName = "大简云规则引擎",
Version = "1.0",
RequestDate = nowDate.ToString("yyyy-MM-dd HH:mm:ss"),
RequestAction = "CheckRule",
};
msgModel.Main = new RulesEngineOrderBookingMainInfo {
ProjectCode = App.Configuration["RulesEngineProjects"].Split(new char[] { ','}).ToArray(),
};
msgModel.Main.BusinessInfo = model.Adapt<RulesEngineOrderBookingMainBusinessInfo>();
var contaList = await _bookingOrderContaRepository.AsQueryable().Where(x => x.BILLID == model.Id).ToListAsync();
if (contaList.Count > 0)
{
msgModel.Main.BusinessInfo.ContaList = contaList.Adapt<List<RulesEngineOrderBookingContaInfo>>();
var ctnArg = contaList.Select(t => t.Id).ToArray();
var cargoList = await _bookingOrderContaCargoRepository.AsQueryable()
.Where(x=> ctnArg.Contains(x.CTNID.Value)).ToListAsync();
if(cargoList.Count > 0)
{
msgModel.Main.BusinessInfo.ContaList = contaList.GroupJoin(cargoList, l => l.Id, r => r.CTNID, (l, r) => {
var currList = r.ToList();
if (currList.Count > 0)
{
var info = l.Adapt<RulesEngineOrderBookingContaInfo>();
info.CargoList = currList.Adapt<List<RulesEngineOrderBookingContaCargoInfo>>();
return info;
}
return l.Adapt<RulesEngineOrderBookingContaInfo>();
}).ToList();
}
else
{
msgModel.Main.BusinessInfo.ContaList = contaList.Adapt<List<RulesEngineOrderBookingContaInfo>>();
}
}
var ruleResult = await ExcuteRulesEngine(msgModel);
if(ruleResult == null)
throw Oops.Oh($"订舱主键{bookingId}请求规则失败,返回为空");
var innerRlt = JSON.Deserialize<RemoteRulesEngineExcuteResultDto>(ruleResult.extra.ToString());
var ruleDetailList = innerRlt.DetailList;
result.succ = ruleResult.succ;
result.msg = ruleResult.msg;
if (ruleDetailList != null && ruleDetailList.Count > 0)
{
result.rows = ruleDetailList;
}
}
catch(Exception ex)
{
result.succ = false;
result.msg = "请求规则异常";
}
return result;
}
#endregion
#region 海运订舱请求规则引擎校验
/// <summary>
/// 海运订舱请求规则引擎校验
/// </summary>
/// <param name="model">海运订舱报文类</param>
/// <returns>返回回执</returns>
[HttpPost("/RulesEngineClient/ExcuteRulesOceanBookingByModel")]
public async Task<RulesEngineExcuteResultDto> ExcuteRulesOceanBookingByModel(BookingOrderDto model)
{
/*
1
2
3
4
5
*/
RulesEngineExcuteResultDto result = new RulesEngineExcuteResultDto();
try
{
DateTime nowDate = DateTime.Now;
//var model =
//if()
// _bookingOrderRepository.AsQueryable().InSingle(bookingId);
//if (model == null)
// throw Oops.Oh($"订舱主键{bookingId}无法获取业务信息");
RulesEngineOrderBookingMessageInfo msgModel = new RulesEngineOrderBookingMessageInfo();
msgModel.Head = new RulesEngineWebAPIHeadBase
{
GID = IDGen.NextID().ToString(),
MessageType = "BUSI_RULE",
SenderId = App.Configuration["RulesEngineSender"],
SenderName = App.Configuration["RulesEngineSenderName"],
SenderKey = App.Configuration["RulesEngineAuthKey"],
ReceiverId = "RulesEngine",
ReceiverName = "大简云规则引擎",
Version = "1.0",
RequestDate = nowDate.ToString("yyyy-MM-dd HH:mm:ss"),
RequestAction = "CheckRule",
};
msgModel.Main = new RulesEngineOrderBookingMainInfo
{
ProjectCode = App.Configuration["RulesEngineProjects"].Split(new char[] { ',' }).ToArray(),
};
msgModel.Main.BusinessInfo = model.Adapt<RulesEngineOrderBookingMainBusinessInfo>();
var contaList = await _bookingOrderContaRepository.AsQueryable().Where(x => x.BILLID == model.Id).ToListAsync();
if (contaList.Count > 0)
{
msgModel.Main.BusinessInfo.ContaList = contaList.Adapt<List<RulesEngineOrderBookingContaInfo>>();
var cargoList = await _bookingOrderContaCargoRepository.AsQueryable()
.Where(x => contaList.Any(a => a.Id == x.CTNID)).ToListAsync();
if (cargoList.Count > 0)
{
msgModel.Main.BusinessInfo.ContaList = contaList.GroupJoin(cargoList, l => l.Id, r => r.CTNID, (l, r) =>
{
var currList = r.ToList();
if (currList.Count > 0)
{
var info = l.Adapt<RulesEngineOrderBookingContaInfo>();
info.CargoList = currList.Adapt<List<RulesEngineOrderBookingContaCargoInfo>>();
return info;
}
return l.Adapt<RulesEngineOrderBookingContaInfo>();
}).ToList();
}
else
{
msgModel.Main.BusinessInfo.ContaList = contaList.Adapt<List<RulesEngineOrderBookingContaInfo>>();
}
}
var ruleResult = await ExcuteRulesEngine(msgModel);
if (ruleResult == null)
throw Oops.Oh($"订舱主键{model.BOOKINGNO}请求规则失败,返回为空");
var innerRlt = JSON.Deserialize<RemoteRulesEngineExcuteResultDto>(ruleResult.extra.ToString());
var ruleDetailList = innerRlt.DetailList;
result.succ = ruleResult.succ;
result.msg = ruleResult.msg;
if (ruleDetailList.Count > 0)
{
result.rows = ruleDetailList;
}
}
catch (Exception ex)
{
result.succ = false;
result.msg = "请求规则异常";
}
return result;
}
#endregion
#region 请求规则平台
/// <summary>
/// 请求规则平台
/// </summary>
/// <param name="BusinessMsg"></param>
/// <returns></returns>
private async Task<RulesEngineWebApiResult> ExcuteRulesEngine(RulesEngineOrderBookingMessageInfo info)
{
RulesEngineWebApiResult model = null;
/*
1URL
2JSON
3POST
4
*/
var url = App.Configuration["RulesEngineUrl"];
try
{
var res = await url.SetHttpMethod(HttpMethod.Post)
.SetBody(JSON.Serialize(info), "application/json")
.SetContentEncoding(Encoding.UTF8)
.PostAsync();
if (res.StatusCode == System.Net.HttpStatusCode.OK)
{
var userResult = await res.Content.ReadAsStringAsync();
model = JSON.Deserialize<RulesEngineWebApiResult>(userResult);
}
}
catch (Exception ex)
{
//写日志
if (ex is HttpRequestException)
throw Oops.Oh(10000002);
}
return model;
}
#endregion
}
}