|
|
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;
|
|
|
|
|
|
namespace Myshipping.Application
|
|
|
{
|
|
|
/// <summary>
|
|
|
/// 请求规则平台
|
|
|
/// </summary>
|
|
|
public class RulesEngineClientService: IRulesEngineClientService, IDynamicApiController, ITransient
|
|
|
{
|
|
|
private readonly SqlSugarRepository<BookingOrder> _bookingOrderRepository;
|
|
|
/// <summary>
|
|
|
///
|
|
|
/// </summary>
|
|
|
public RulesEngineClientService(SqlSugarRepository<BookingOrder> bookingOrderRepository)
|
|
|
{
|
|
|
_bookingOrderRepository = bookingOrderRepository;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 海运订舱请求规则引擎校验
|
|
|
/// </summary>
|
|
|
/// <param name="model">海运订舱请求业务</param>
|
|
|
/// <returns>返回用户信息</returns>
|
|
|
public async Task<RulesEngineExcuteResultDto> ExcuteRulesOceanBookingByMsg(RulesEngineOrderBookingMessageInfo model)
|
|
|
{
|
|
|
RulesEngineExcuteResultDto result = new RulesEngineExcuteResultDto();
|
|
|
|
|
|
var ruleResult = await ExcuteRulesEngine(model);
|
|
|
|
|
|
if (ruleResult == null)
|
|
|
throw Oops.Oh($"订舱请求规则失败,返回为空");
|
|
|
|
|
|
var ruleDetailList = ruleResult.rows as List<RulesEngineExcuteResultDetailDto>;
|
|
|
|
|
|
result.succ = ruleResult.succ;
|
|
|
result.msg = ruleResult.msg;
|
|
|
|
|
|
if (ruleDetailList.Count > 0)
|
|
|
{
|
|
|
result.rows = ruleDetailList;
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 海运订舱请求规则引擎校验
|
|
|
/// </summary>
|
|
|
/// <param name="bookingId">海运订舱主键</param>
|
|
|
/// <returns>返回用户信息</returns>
|
|
|
[HttpGet]
|
|
|
public async Task<RulesEngineExcuteResultDto> ExcuteRulesOceanBooking(string bookingId)
|
|
|
{
|
|
|
/*
|
|
|
处理逻辑
|
|
|
1、订单保存后触发调取此方法,传入海运订舱主键。
|
|
|
2、调取订舱的详情。
|
|
|
3、对应请求报文。
|
|
|
4、请求规则接口。
|
|
|
5、返回回执。
|
|
|
*/
|
|
|
|
|
|
RulesEngineExcuteResultDto result = new RulesEngineExcuteResultDto();
|
|
|
|
|
|
try
|
|
|
{
|
|
|
DateTime nowDate = DateTime.Now;
|
|
|
|
|
|
var model = _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 ruleResult = await ExcuteRulesEngine(msgModel);
|
|
|
|
|
|
if(ruleResult == null)
|
|
|
throw Oops.Oh($"订舱主键{bookingId}请求规则失败,返回为空");
|
|
|
|
|
|
var ruleDetailList = ruleResult.rows as List<RulesEngineExcuteResultDetailDto>;
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// 海运订舱请求规则引擎校验
|
|
|
/// </summary>
|
|
|
/// <param name="model">海运订舱报文类</param>
|
|
|
/// <returns>返回回执</returns>
|
|
|
[HttpPost]
|
|
|
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 ruleResult = await ExcuteRulesEngine(msgModel);
|
|
|
|
|
|
if (ruleResult == null)
|
|
|
throw Oops.Oh($"订舱主键{model.BOOKINGNO}请求规则失败,返回为空");
|
|
|
|
|
|
var ruleDetailList = ruleResult.rows as List<RulesEngineExcuteResultDetailDto>;
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 请求规则平台
|
|
|
/// </summary>
|
|
|
/// <param name="BusinessMsg"></param>
|
|
|
/// <returns></returns>
|
|
|
private async Task<RulesEngineWebApiResult> ExcuteRulesEngine(RulesEngineOrderBookingMessageInfo info)
|
|
|
{
|
|
|
RulesEngineWebApiResult model = null;
|
|
|
/*
|
|
|
1、读取配置文件中的规则引擎URL
|
|
|
2、填充请求的类,并生成JSON报文
|
|
|
3、POST请求接口,并记录回执。
|
|
|
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;
|
|
|
}
|
|
|
}
|
|
|
}
|