/***********************************************************************
* Project: CoreCms
* ProjectName: 核心内容管理系统
* Web: https://www.corecms.net
* Author: 大灰灰
* Email: jianweie@163.com
* CreateTime: 2021/7/10 22:41:46
* Description: 暂无
***********************************************************************/
using System;
using System.Threading.Tasks;
using CoreCms.Net.Caching.AccressToken;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Loging;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.WeChat.Service.HttpClients;
using InitQ.Abstractions;
using InitQ.Attributes;
using Newtonsoft.Json;
using NLog;
using SKIT.FlurlHttpClient.Wechat.Api;
using SKIT.FlurlHttpClient.Wechat.Api.Models;
namespace CoreCms.Net.RedisMQ
{
///
/// 自定义交易组件-同步订单确认收货
///
public class TCOrderDeliveryRecieveSyncSubscribe : IRedisSubscribe
{
private readonly ICoreCmsBillDeliveryServices _billDeliveryServices;
private readonly ICoreCmsOrderItemServices _orderItemServices;
private readonly ICoreCmsOrderServices _orderServices;
private readonly IWeChatTransactionComponentOrderServices _tcOrderServices;
private readonly ICoreCmsUserWeChatInfoServices _userWeChatInfoServices;
private readonly IWeChatApiHttpClientFactory _weChatApiHttpClientFactory;
private readonly ICoreCmsUserWeChatInfoServices _weChatInfoServices;
public TCOrderDeliveryRecieveSyncSubscribe(ICoreCmsUserWeChatInfoServices userWeChatInfoServices,
ICoreCmsBillDeliveryServices billDeliveryServices, ICoreCmsOrderServices orderServices,
ICoreCmsUserWeChatInfoServices weChatInfoServices, IWeChatTransactionComponentOrderServices tcOrderServices,
ICoreCmsOrderItemServices orderItemServices, IWeChatApiHttpClientFactory weChatApiHttpClientFactory)
{
_userWeChatInfoServices = userWeChatInfoServices;
_billDeliveryServices = billDeliveryServices;
_orderServices = orderServices;
_weChatInfoServices = weChatInfoServices;
_tcOrderServices = tcOrderServices;
_orderItemServices = orderItemServices;
_weChatApiHttpClientFactory = weChatApiHttpClientFactory;
}
///
/// 自定义交易组件-同步订单确认收货
///
///
///
[Subscribe(RedisMessageQueueKey.TransactionComponentOrderDeliveryRecieveSync)]
private async Task TransactionComponentOrderDeliveryRecieveSync(string msg)
{
try
{
var orderModel = JsonConvert.DeserializeObject(msg);
if (orderModel is {scene: > 0})
{
var tcOrderModel =
await _tcOrderServices.QueryByClauseAsync(p => p.outOrderId == orderModel.orderId, true);
if (tcOrderModel == null)
{
NLogUtil.WriteAll(LogLevel.Error, LogType.RedisMessageQueue, "自定义交易组件-同步订单确认收货",
"未查询到远程订单推送记录");
return;
}
var orderInfo = await _orderServices.QueryByClauseAsync(p => p.orderId == orderModel.orderId, true);
if (orderInfo == null)
{
NLogUtil.WriteAll(LogLevel.Error, LogType.RedisMessageQueue, "自定义交易组件-同步订单确认收货", "订单信息获取失败");
return;
}
//获取小程序认证
var accessToken = WeChatCacheAccessTokenHelper.GetWxOpenAccessToken();
var client = _weChatApiHttpClientFactory.CreateWxOpenClient();
var request = new ShopDeliveryReceiveRequest();
request.AccessToken = accessToken;
request.OpenId = tcOrderModel.openid;
request.OutOrderId = orderModel.orderId;
var response = await client.ExecuteShopDeliveryReceiveAsync(request);
NLogUtil.WriteAll(response.IsSuccessful() ? LogLevel.Info : LogLevel.Error,
LogType.RedisMessageQueue, "自定义交易组件-同步订单确认收货", JsonConvert.SerializeObject(response));
}
else
{
NLogUtil.WriteAll(LogLevel.Error, LogType.RedisMessageQueue, "自定义交易组件-同步订单确认收货", "订单获取失败");
}
}
catch (Exception ex)
{
NLogUtil.WriteAll(LogLevel.Error, LogType.RedisMessageQueue, "自定义交易组件-同步订单确认收货", msg, ex);
throw;
}
await Task.CompletedTask;
}
}
}