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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using DocumentServiceAPI.Model.cyDocumentModel;
using DocumentServiceAPI.Model.Oder;
using DocumentServiceAPI.Model.Permissions;
using DocumentServiceAPI.Model.UserInfoModel;
using Furion;
using Furion.JsonSerialization;
using Furion.Logging.Extensions;
using Furion.Schedule;
using InitQ.Abstractions;
using InitQ.Attributes;
using Microsoft.Extensions.Options;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace DocumentServiceApi.InitQMessage
{
    /// <summary>
    /// 消息接收处理函数
    /// </summary>
    public class MessageReceive: IRedisSubscribe
    {
 
         
        /// <summary>
        /// 初始化订单处理
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>       
        [Subscribe(INITQMessageDefine.InitTenantOder)]
        private async Task InitTenantOder(string msg)
        {
            TenantOderInfo? obj = JSON.Deserialize<TenantOderInfo>(msg);
            if(obj==null)
            {
                $"InitTenantOder 初始化 msg 对象错误 ,msg为{msg}".LogInformation<MessageReceive>();
                return;
            }
            var    _db=  App.GetService<ISqlSugarClient>();
 
 
            Oder oder = new Oder()
            {
                Amount = obj.Amount,
                CreatBy = "消息队列 InitTenantOder",
                CreatTime = DateTime.Now,
                Description = obj.Description,
                EmployeeCount = obj.EmployeeCount,
                MonthCount = obj.MonthCount,
                UnitCount = obj.UnitCount,
                PayType = obj.PayType,
                PayTime = obj.PayTime,
                ZcUserID = obj.ZcUserID,
                ITCode = obj.ITCode,
            };
            TenantInfo? Tinfo = await _db.Queryable<TenantInfo>().Where(x=>x.ZcUserID==obj.ZcUserID).SingleAsync();
            if(Tinfo==null)
            {
                Tinfo = new TenantInfo
                {
                    ZcUserID = obj.ZcUserID,
                    ItCode = obj.ITCode,
                    CreatBy = "消息队列 InitTenantOder",
                    CreatTime = DateTime.Now,
                    Description = obj.Description,
                    Name = obj.Name??obj.ITCode+"集团账号",
                    PsW = obj.ITCode.Substring(obj.ITCode.Length - 4),
 
 
                };
                TenantPermissions permissions = new TenantPermissions()
                {
                    CreatBy = "消息队列 InitTenantOder",
                    CreatTime = DateTime.Now,
                    Description = obj.Description,
                    EmployeeMaxCount = obj.EmployeeCount ?? 0,
                    UnitMaxCount = obj.UnitCount ?? 0,
                    OverTime = DateTime.Now.AddMonths(obj.MonthCount ?? 0),
 
 
 
                };
 
                //新建租户员工
                Document_EmployeeInfo employeeInfo = new Document_EmployeeInfo()
                {
                    EmployeeName = "集团账号",
                    UserName = Tinfo.ItCode,
                    AdviseFlag = 2,
                    UserId = 0,
                    IsWork = 1,
                    UserPassWord = Tinfo.ItCode.Substring(Tinfo.ItCode.Length - 6),
                    Job = "集团用户",
                    IsLogin = true,
                    LastUpdateName = "消息队列 InitTenantOder",
                    LastUpdateTime = DateTime.Now,
                };
                EmployeeAtTenant employeeAtTenant = new EmployeeAtTenant()
                {
                    CreatBy = "消息队列 InitTenantOder",
                    CreatTime = DateTime.Now,
                     
                };
 
                Document_TenderUnit tenderUnit = new Document_TenderUnit()
                {
                    UnitName = obj.Name ?? "集团公司",
                    AdviseFlag = 2,
                    UserId = 0,
                    CreatTime = DateTime.Now.ToString("yyyy MM dd"),
                    LastUpdateName = "消息队列 InitTenantOder",
                    LastUpdateTime = DateTime.Now,
                    IsDeled = false,
                    IsEn = true,
 
 
                };
 
                try
                {
                    await _db.AsTenant().BeginTranAsync();
 
                  var id=  await _db.Insertable(Tinfo).ExecuteReturnIdentityAsync();
                    var eid = await _db.Insertable(employeeInfo).ExecuteReturnIdentityAsync();
                    permissions.TenantId = id;
                    employeeAtTenant.TenantID = id;
                    employeeAtTenant.EmployeeID = eid;
                    tenderUnit.TenantId = id;
                    await _db.Insertable(tenderUnit).ExecuteCommandAsync();
                    await _db.Insertable(employeeAtTenant).ExecuteCommandAsync();
                    await _db.Insertable(permissions).ExecuteCommandAsync ();
                    await _db.Insertable(oder).ExecuteCommandAsync();
                    await _db.AsTenant().CommitTranAsync();
                }
                catch (Exception e)
                {
                    await _db.AsTenant().RollbackTranAsync();
                    $"新建租户处理失败 msg:{msg},e:{e.ToString()} BY:消息队列 InitTenantOder".LogInformation<MessageReceive>();
                    return;
                }
 
 
            }
            else
            {
                var Employee = await _db.Queryable<EmployeeAtTenant,Document_EmployeeInfo>((EAT, E) => new JoinQueryInfos(
 
    JoinType.Left, EAT.EmployeeID == E.EmployeeId
)).Where((EAT, E) =>  EAT.TenantID==Tinfo.Id).Select((EAT,E)=>E).FirstAsync();
               
                Tinfo.ItCode = obj.ITCode;
                Tinfo.UpdataBy = "消息队列 InitTenantOder";
                Tinfo.UpdataTime = DateTime.Now;
                Tinfo.IsDel = false;
                Tinfo.IsEn = true;
          
              var tp= await _db.Queryable<TenantPermissions>().Where(x=>x.TenantId==Tinfo.Id).FirstAsync();
 
                try
                {
                    await _db.AsTenant().BeginTranAsync();
                    if (Tinfo.ItCode != obj.ITCode)
                    {
                     
                       await _db.Updateable(Tinfo).ExecuteCommandAsync();
                    };
                    if (tp == null)
                    {
                        tp = new TenantPermissions()
                        {
                            CreatBy = "消息队列 InitTenantOder",
                             CreatTime = DateTime.Now,
                              IsDel = false,
                               IsEn = true,
                                EmployeeMaxCount=obj.EmployeeCount??0,
                                 UnitMaxCount=obj.UnitCount??0,
                                  OverTime=DateTime.Now.AddMinutes(obj.MonthCount??0),
                                   TenantId= Tinfo.Id,
                                   
                        };
                        await _db.Insertable(tp).ExecuteCommandAsync();
 
                        
                    }
                    else
                    {
                        tp.IsDel = false;
                        tp.IsEn = true;
                        tp.UnitMaxCount = obj.UnitCount ?? tp.UnitMaxCount;
                        tp.EmployeeMaxCount = obj.EmployeeCount ?? tp.EmployeeMaxCount;
                        tp.OverTime = tp.OverTime.AddMonths(obj.MonthCount??0);
                        tp.UpdataBy = "消息队列 InitTenantOder";
                        tp.UpdataTime = DateTime.Now;
 
                        await _db.Updateable(tp).ExecuteCommandAsync();
 
                    }
                    if (obj.ITCode != Employee.UserName)
                    {
                        //如果发现用户名已经更改,则该集团员工用户名跟随更改
                        Employee.UserName = obj.ITCode;
                        Employee.LastUpdateName= "消息队列 InitTenantOder";
                        Employee.LastUpdateTime = DateTime.Now;
                        await _db.Updateable(Employee).ExecuteCommandAsync();
                    }
                    await _db.Insertable(oder).ExecuteCommandAsync();
                    await _db.AsTenant().CommitTranAsync();
                
 
 
 
 
                }
                catch (Exception e)
                {
 
                    await _db.AsTenant().RollbackTranAsync();
                    $"修改租户权限失败 msg:{msg},e:{e.ToString()}  By:消息队列 InitTenantOder".LogInformation<MessageReceive>();
                    return;
                }
            
            }
        
 
 
 
           
 
         
 
 
 
        }
 
    }
}