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

197 lines
8.7 KiB
C#

9 months ago
using Furion;
using Furion.EventBus;
using Furion.FriendlyException;
using Furion.JsonSerialization;
using Furion.RemoteRequest.Extensions;
using Google.Protobuf.WellKnownTypes;
using Mapster;
using Microsoft.AspNetCore.JsonPatch.Internal;
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.Helper;
using Myshipping.Core.Service;
using Newtonsoft.Json.Linq;
using NPOI.SS.Formula.Functions;
using NPOI.SS.Formula.PTG;
using StackExchange.Profiling.Internal;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;
namespace Myshipping.Application.Event
{
/// <summary>
/// 扣费
/// </summary>
public class BookingFeeSubscriber : IEventSubscriber
{
private IServiceProvider _services { get; }
private readonly ILogger<BookingFeeSubscriber> _logger;
public BookingFeeSubscriber(IServiceProvider services
, ILogger<BookingFeeSubscriber> logger)
{
_services = services;
_logger = logger;
}
//扣费
[EventSubscribe("Booking:DoFeeRecord")]
public async Task DoFeeRecord(EventHandlerExecutingContext context)
{
_logger.LogInformation($"收到订舱扣费请求:{context.Source.Payload}");
var paraObj = context.Source.Payload as dynamic;
int bsType = paraObj.bsType;
int sendType = paraObj.sendtype;
List<long> idList = paraObj.idList;
idList = idList.Distinct().ToList();
_logger.LogInformation($"准备处理扣费,bsType{bsType}sendType{sendType}id列表{string.Join(',', idList)}");
using var scope = _services.CreateScope();
var repoUser = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<SysUser>>();
var repoBooking = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingOrder>>();
var repoFeeRecord = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<BookingFeeRecord>>();
9 months ago
var repoTenantParamValue = scope.ServiceProvider.GetRequiredService<SqlSugarRepository<DjyTenantParamValue>>();
9 months ago
var cache = scope.ServiceProvider.GetRequiredService<ISysCacheService>();
9 months ago
var feeOrderList = await repoBooking
.AsQueryable()
.Filter(null, true)
.Where(x => idList.Contains(x.Id))
9 months ago
.Select(x => new { x.Id, x.MBLNO, x.HBLNO, x.OPID, x.CreatedUserId, x.TenantId, x.VESSEL, x.VOYNO, x.ETD, x.CARRIER, x.ATD })
9 months ago
.ToListAsync();
9 months ago
var typeStr = $"{bsType}_{sendType}";
var sysCfg = await cache.GetAllSysConfig();
var feeUrl = sysCfg.FirstOrDefault(x => x.Code == "djyFeeApiUrl");
9 months ago
if (feeUrl == null || string.IsNullOrEmpty(feeUrl.Value))
9 months ago
{
9 months ago
var errMsg = "大简云扣费URL未配置";
9 months ago
_logger.LogError(errMsg);
DingTalkGroupHelper.SendDingTalkGroupMessage("bookingFeeNotify", "扣费失败提醒", errMsg);
return;
}
9 months ago
foreach (var order in feeOrderList)
9 months ago
{
var iptId = string.IsNullOrEmpty(order.OPID) ? order.CreatedUserId.Value : Convert.ToInt64(order.OPID);
9 months ago
var user = await repoUser.AsQueryable().Filter(null, true).FirstAsync(x => x.Id == iptId);
9 months ago
var keyList = await repoTenantParamValue.AsQueryable()
.Filter(null, true)
.Where(x => x.TenantId == order.TenantId && (x.ParaCode == "BOOKING_FEE_USERID" || x.ParaCode == "BOOKING_FEE_USERKEY"))
.ToListAsync();
if (keyList.Count < 2)
{
9 months ago
var errMsg = $"未找到{order.MBLNO}({order.Id})所在租户的授权userid和key无法调用扣费";
9 months ago
_logger.LogError(errMsg);
DingTalkGroupHelper.SendDingTalkGroupMessage("bookingFeeNotify", "扣费失败提醒", errMsg);
continue;
}
var feeUserId = keyList.First(x => x.ParaCode == "BOOKING_FEE_USERID").ItemCode;
var feeUserKey = keyList.First(x => x.ParaCode == "BOOKING_FEE_USERKEY").ItemCode;
9 months ago
if (user == null || string.IsNullOrEmpty(user.DjyUserId))
{
9 months ago
var errMsg = $"未找到{order.MBLNO}({order.Id})的用户信息,无法调用扣费";
9 months ago
_logger.LogError(errMsg);
DingTalkGroupHelper.SendDingTalkGroupMessage("bookingFeeNotify", "扣费失败提醒", errMsg);
continue;
}
9 months ago
var c = repoFeeRecord.AsQueryable().Filter(null, true).Count(x => x.TenantId == order.TenantId && x.MBLNO == order.MBLNO);
if (c > 0)
{
_logger.LogInformation($"已存在扣费记录id{order.Id},提单号:{order.MBLNO},租户:{order.TenantId}");
continue;
}
9 months ago
var seconds = DateTime.Now.ToTimeStamp();
var runId = Guid.NewGuid().ToString();
9 months ago
var srcBeforMD5 = $"{runId}{feeUserId}expend{bsType}{sendType}{order.Id}{order.MBLNO}{seconds}{feeUserKey}";
9 months ago
var postObj = new
{
runId,
9 months ago
userId = feeUserId,
9 months ago
module = "expend",//固定
bsType = $"{bsType}",
sendType = $"{sendType}",
timestamp = seconds,//秒级时间戳
md5 = srcBeforMD5.ToMd5(),// 加密字符串小写 RunId + UserId + Module + BsType + SendType+Timestamp+ Key
Data = new
{
9 months ago
BSNO = order.Id.ToString(),
9 months ago
MBLNO = order.MBLNO,
9 months ago
HBLNO = order.HBLNO,
9 months ago
CtnrInfo = "",
CtnrCount = 1,
IsCredit = 1,//是否是信用支付 1 信用支付 0不允许信用支付默认值为0,
LURURENID = user.DjyUserId,
9 months ago
SENDUSERID = user.DjyUserId,
VESSEL = order.VESSEL,
VOYNO = order.VOYNO,
ETD = order.ATD,
CARRIER = order.CARRIER
9 months ago
}
};
_logger.LogInformation($"调用扣费:{postObj.ToJsonString()}");
var apiRtn = await feeUrl.Value
.SetHttpMethod(HttpMethod.Post)
.SetBody(postObj)
.SetRetryPolicy(3, 5000)
.OnException((c, m, errMsg) =>
{
_logger.LogError($"扣费失败:{errMsg}");
DingTalkGroupHelper.SendDingTalkGroupMessage("bookingFeeNotify", "扣费失败提醒", errMsg);
})
.SendAsStringAsync();
_logger.LogInformation($"调用扣费返回:{apiRtn}");
var jobjApiRtn = JObject.Parse(apiRtn);
9 months ago
var code = jobjApiRtn.GetIntValue("code");
9 months ago
var jobjApiRtnData = jobjApiRtn.GetValue("data") as JObject;
if (code == 200 || code == 450)
{
var jobjApiRtnDataPayInfo = jobjApiRtnData.GetValue("payInfo") as JObject;
var price = Convert.ToDecimal(jobjApiRtnDataPayInfo.GetValue("price").ToString());
var total = Convert.ToDecimal(jobjApiRtnDataPayInfo.GetValue("total").ToString());
//记录扣费
var fr = new BookingFeeRecord();
fr.Id = YitIdHelper.NextId();
9 months ago
fr.BillId = order.Id;
fr.MBLNO = order.MBLNO;
fr.TenantId = order.TenantId.Value;
9 months ago
fr.Type = typeStr;
fr.Amount = total;
await repoFeeRecord.InsertAsync(fr);
}
else
{
9 months ago
var errMsg = jobjApiRtn.GetValue("message").ToString();
9 months ago
_logger.LogError($"扣费失败:{errMsg}");
DingTalkGroupHelper.SendDingTalkGroupMessage("bookingFeeNotify", "扣费失败提醒", errMsg);
}
}
_logger.LogInformation($"扣费处理完成");
}
}
}