mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2025-12-07 01:23:27 +08:00
177 lines
8.1 KiB
C#
177 lines
8.1 KiB
C#
/***********************************************************************
|
|
* 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.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using CoreCms.Net.Caching.AccressToken;
|
|
using CoreCms.Net.Configuration;
|
|
using CoreCms.Net.IServices;
|
|
using CoreCms.Net.Loging;
|
|
using CoreCms.Net.WeChat.Service.HttpClients;
|
|
using InitQ.Abstractions;
|
|
using InitQ.Attributes;
|
|
using Newtonsoft.Json;
|
|
using SKIT.FlurlHttpClient.Wechat.Api;
|
|
using SKIT.FlurlHttpClient.Wechat.Api.Models;
|
|
|
|
namespace CoreCms.Net.RedisMQ
|
|
{
|
|
/// <summary>
|
|
/// 自定义交易组件-同步发货信息
|
|
/// </summary>
|
|
public class TCOrderShipSyncSubscribe : IRedisSubscribe
|
|
{
|
|
private readonly string _wxOpenAppId = AppSettingsConstVars.WxOpenAppId;
|
|
private readonly string _wxOpenAppSecret = AppSettingsConstVars.WxOpenAppSecret;
|
|
|
|
private readonly ICoreCmsBillDeliveryServices _billDeliveryServices;
|
|
private readonly ICoreCmsBillDeliveryItemServices _billDeliveryItemServices;
|
|
private readonly ICoreCmsOrderServices _orderServices;
|
|
private readonly ICoreCmsOrderItemServices _orderItemServices;
|
|
private readonly IWeChatTransactionComponentOrderServices _tcOrderServices;
|
|
private readonly WeChat.Service.HttpClients.IWeChatApiHttpClientFactory _weChatApiHttpClientFactory;
|
|
|
|
|
|
|
|
public TCOrderShipSyncSubscribe(ICoreCmsBillDeliveryServices billDeliveryServices, ICoreCmsOrderServices orderServices, IWeChatTransactionComponentOrderServices tcOrderServices, ICoreCmsOrderItemServices orderItemServices, IWeChatApiHttpClientFactory weChatApiHttpClientFactory, ICoreCmsBillDeliveryItemServices billDeliveryItemServices)
|
|
{
|
|
_billDeliveryServices = billDeliveryServices;
|
|
_orderServices = orderServices;
|
|
_tcOrderServices = tcOrderServices;
|
|
_orderItemServices = orderItemServices;
|
|
_weChatApiHttpClientFactory = weChatApiHttpClientFactory;
|
|
_billDeliveryItemServices = billDeliveryItemServices;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 自定义交易组件-同步发货信息
|
|
/// </summary>
|
|
/// <param name="msg"></param>
|
|
/// <returns></returns>
|
|
[Subscribe(RedisMessageQueueKey.TransactionComponentOrderShipSync)]
|
|
private async Task TransactionComponentOrderShipSync(string msg)
|
|
{
|
|
try
|
|
{
|
|
var deliveryId = msg;
|
|
if (!string.IsNullOrEmpty(deliveryId))
|
|
{
|
|
var billDelivery = await _billDeliveryServices.QueryByClauseAsync(p => p.deliveryId == deliveryId, true);
|
|
if (billDelivery == null)
|
|
{
|
|
NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.RedisMessageQueue, "自定义交易组件-同步发货信息", "同步发货信息");
|
|
return;
|
|
}
|
|
|
|
var tcOrderModel = await _tcOrderServices.QueryByClauseAsync(p => p.outOrderId == billDelivery.orderId, true);
|
|
if (tcOrderModel == null)
|
|
{
|
|
NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.RedisMessageQueue, "自定义交易组件-同步发货信息", "未查询到远程订单推送记录");
|
|
return;
|
|
}
|
|
|
|
var orderInfo = await _orderServices.QueryByClauseAsync(p => p.orderId == billDelivery.orderId, true);
|
|
if (orderInfo == null)
|
|
{
|
|
NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.RedisMessageQueue, "自定义交易组件-同步发货信息", "订单信息获取失败");
|
|
return;
|
|
}
|
|
|
|
|
|
var items = await _billDeliveryItemServices.QueryListByClauseAsync(p => p.deliveryId == billDelivery.deliveryId);
|
|
|
|
//判断是否发货发完
|
|
var finishAllDelivery = false;
|
|
|
|
//总发货数量
|
|
var allNeedSendNum = await _orderItemServices.GetSumAsync(p => p.orderId == orderInfo.orderId, p => p.nums, true);
|
|
//已经发货数量
|
|
var isSendNum = await _orderItemServices.GetSumAsync(p => p.orderId == orderInfo.orderId, p => p.sendNums, true);
|
|
|
|
finishAllDelivery = allNeedSendNum == isSendNum ? true : false;
|
|
|
|
//获取小程序认证
|
|
var accessToken = WeChatCacheAccessTokenHelper.GetWxOpenAccessToken();
|
|
var client = _weChatApiHttpClientFactory.CreateWxOpenClient();
|
|
var request = new ShopDeliverySendRequest();
|
|
request.AccessToken = accessToken;
|
|
|
|
if (orderInfo.receiptType == (int)GlobalEnumVars.OrderReceiptType.Logistics)
|
|
{
|
|
request.OutOrderId = orderInfo.orderId;
|
|
request.OpenId = tcOrderModel.openid;
|
|
request.IsFinishAll = finishAllDelivery;
|
|
|
|
var delivery = new ShopDeliverySendRequest.Types.Delivery
|
|
{
|
|
DeliveryId = billDelivery.thirdPartylogiCode,
|
|
WaybillId = billDelivery.logiNo,
|
|
ProductList = new List<ShopDeliverySendRequest.Types.Delivery.Types.Product>()
|
|
};
|
|
|
|
items.ForEach(p =>
|
|
{
|
|
delivery.ProductList.Add(new ShopDeliverySendRequest.Types.Delivery.Types.Product()
|
|
{
|
|
OutProductId = p.goodsId.ToString(),
|
|
OutSKUId = p.productId.ToString(),
|
|
Count = p.nums
|
|
});
|
|
});
|
|
|
|
request.DeliveryList = new List<ShopDeliverySendRequest.Types.Delivery>()
|
|
{
|
|
delivery
|
|
};
|
|
|
|
request.ShipDoneTime = new DateTimeOffset(billDelivery.createTime);
|
|
}
|
|
else
|
|
{
|
|
request.OutOrderId = orderInfo.orderId;
|
|
request.OpenId = tcOrderModel.openid;
|
|
request.IsFinishAll = true;
|
|
}
|
|
|
|
var response = await client.ExecuteShopDeliverySendAsync(request);
|
|
if (response.IsSuccessful())
|
|
{
|
|
NLogUtil.WriteAll(NLog.LogLevel.Info, LogType.RedisMessageQueue, "自定义交易组件-同步发货信息", JsonConvert.SerializeObject(response));
|
|
}
|
|
else
|
|
{
|
|
NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.RedisMessageQueue, "自定义交易组件-同步发货信息", JsonConvert.SerializeObject(response));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.RedisMessageQueue, "自定义交易组件-同步发货信息", "发货单编号获取失败");
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
var obj = new
|
|
{
|
|
wxOpenAppId = _wxOpenAppId,
|
|
wxOpenAppSecret = _wxOpenAppSecret,
|
|
msg
|
|
};
|
|
NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.RedisMessageQueue, "自定义交易组件-同步发货信息", JsonConvert.SerializeObject(obj), ex);
|
|
throw;
|
|
}
|
|
await Task.CompletedTask;
|
|
}
|
|
|
|
|
|
}
|
|
}
|