移动系统liao
2024-11-12 1cb49b04ae6709e6054c328f5ed12bff9ca014c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
{
    /// <summary>
    ///  供应商配送订单分配记录
    /// </summary>
    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;
        }
 
        /// <summary>
        /// 增加一个记录
        /// </summary>
        /// <param name="message"></param>
        [Subscribe(RedisMessageQueueKey.AddDistributionSendOderMQ)]
        public async void AddDistributionSendOder(string message)
        {
            var Param = JsonConvert.DeserializeObject<DistributionSendOder>(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();
 
        }
 
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="message"></param>
        [Subscribe(RedisMessageQueueKey.ActiveDistributionSendOderMQ)]
        public async void ActiveDistributionSendOder(string message)
        {
            var Param = JsonConvert.DeserializeObject<ActiveDistributionSendOderMQ>(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<DistributionSendOder>().Includes(x=>x.delivery).Where(x => x.deliveryID == Param.deliveryID ).FirstAsync();
            else
 
                oder = await _unitOfWork.GetDbClient().Queryable<DistributionSendOder>().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;
            }
           
          
 
        }
 
 
    }
}