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

338 lines
12 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.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;
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;
}
/// <summary>
/// 海运订舱请求规则引擎校验
/// </summary>
/// <param name="model">海运订舱请求业务</param>
/// <returns>返回用户信息</returns>
[ApiDescriptionSettings(LowercaseRoute = false, SplitCamelCase = false)]
[HttpPost]
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>
[ApiDescriptionSettings(LowercaseRoute = false, SplitCamelCase = false)]
[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 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($"订舱主键{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>
[ApiDescriptionSettings(LowercaseRoute = false, SplitCamelCase = false)]
[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 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 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;
}
}
}