using CoreCms.Net.Configuration;
using CoreCms.Net.DTO;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.Loging;
using CoreCms.Net.Model.Entities.baifenbingfa.DistributionSendOder;
using CoreCms.Net.Utility.Extensions;
using CoreCms.Net.Utility.Helper;
using InitQ.Abstractions;
using InitQ.Attributes;
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CoreCms.Net.RedisMQ
{
///
/// 供应商配送订单分配记录
///
public class DistributionSendOderMQ: IRedisSubscribe
{
private readonly IUnitOfWork _unitOfWork;
private readonly ICoreCmsUserBalanceServices _userBalanceServices;
private readonly ICoreCmsSettingServices _coreCmsSettingServices; //注入服务
public DistributionSendOderMQ(IUnitOfWork unitOfWork, ICoreCmsUserBalanceServices userBalanceServices, ICoreCmsSettingServices coreCmsSettingServices)
{
_unitOfWork = unitOfWork;
_userBalanceServices = userBalanceServices;
_coreCmsSettingServices = coreCmsSettingServices;
}
///
/// 增加一个记录
///
///
[Subscribe(RedisMessageQueueKey.AddDistributionSendOderMQ)]
public async void AddDistributionSendOder(string message)
{
var Param = JsonConvert.DeserializeObject(message);
if(Param == null) {
NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.RedisMessageQueue, "AddDistributionSendOder 增加一个记录 处理失败", $"转换模型失败===》 {message}");
return;
}
Param.createTime = DateTime.Now;
Param.createBy = "系统队列";
var allConfigs = await _coreCmsSettingServices.GetConfigDictionaries();//获取所有配置
//筛选配置
var DictionaryDeliverFees = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.DictionaryDeliverFees).ObjectToDecimal(0);
Param.amount = DictionaryDeliverFees;
await _unitOfWork.GetDbClient().Insertable(Param).ExecuteCommandAsync();
}
///
/// 修改状态
///
///
[Subscribe(RedisMessageQueueKey.ActiveDistributionSendOderMQ)]
public async void ActiveDistributionSendOder(string message)
{
var Param = JsonConvert.DeserializeObject(message);
if (Param == null)
{
NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.RedisMessageQueue, "ActiveDistributionSendOder 修改状态 处理失败", $"转换模型失败===》 {message}");
return;
}
DistributionSendOder oder = null;
if (Param.SendOderActive == Model.Entities.sendDistributionAcceptType.OderFreeComplete)
oder=await _unitOfWork.GetDbClient().Queryable().Includes(x=>x.delivery).Where(x => x.deliveryID == Param.deliveryID ).FirstAsync();
else
oder = await _unitOfWork.GetDbClient().Queryable().Includes(x => x.delivery).Where(x => x.deliveryID == Param.deliveryID && x.userID == Param.userID).FirstAsync();
oder.upDataTime = DateTime.Now;
oder.upDataBy = "ActiveDistributionSendOder 队列";
oder.description = Param.Dec;
oder.sendDistributionAccept= Param.SendOderActive;
_unitOfWork.BeginTran();
try
{
if (Param.SendOderActive == Model.Entities.sendDistributionAcceptType.OderFreeComplete) {
if (oder.userID != null && oder.delivery?.orderId != null)
{
//钱挪到会员余额里面
var result = await _userBalanceServices.Change(oder.userID ?? 0, (int)GlobalEnumVars.UserBalanceSourceTypes.DistributionDelivery,
oder.amount, oder.delivery.orderId);
}
else
NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.Order, "经销商发货结算失败", $"DistributionSendOder ID={oder.id} 该订单的用户ID或者是deliveryID或者是CoreCmsBillDelivery 中的 oderId");
}
await _unitOfWork.GetDbClient().Updateable(oder).ExecuteCommandAsync();
_unitOfWork.CommitTran();
}
catch (Exception )
{
_unitOfWork.RollbackTran();
throw;
}
}
}
}