WYB
2021-03-22 91b8cdad021ab052e4991f3d41834a6f0ddc36b8
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using JiepeiWMS.Common.Config;
using JiepeiWMS.Common.Helper;
using JiepeiWMS.IRepository.Base;
using JiepeiWMS.IRepository.UnitOfWork;
using JiepeiWMS.IServices;
using JiepeiWMS.Model.Models;
using JiepeiWMS.Services.BASE;
using System;
using System.Linq;
using System.Threading.Tasks;
 
namespace JiepeiWMS.FrameWork.Services
{
    /// <summary>
    /// sysUserInfoServices
    /// </summary>    
    public class SysUserInfoServices : BaseServices<sysUserInfo>, ISysUserInfoServices
    {
 
        IBaseRepository<sysUserInfo> _dal;
        IUserRoleServices _userRoleServices;
        IBaseRepository<Role> _roleRepository;
        IDepartmentServices _departmentServices;
        IRoleModulePermissionServices _roleModulePermissionServices;
        IUnitOfWork _DB;
        public SysUserInfoServices(IBaseRepository<sysUserInfo> dal, IUserRoleServices userRoleServices, IBaseRepository<Role> roleRepository, IDepartmentServices DepartmentServices, IRoleModulePermissionServices RoleModulePermissionServices, IUnitOfWork DB)
        {
            this._dal = dal;
            this._userRoleServices = userRoleServices;
            this._roleRepository = roleRepository;
            this._departmentServices = DepartmentServices;
            this._roleModulePermissionServices = RoleModulePermissionServices;
            this._DB = DB;
            base.BaseDal = dal;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="loginPwd"></param>
        /// <returns></returns>
        public async Task<sysUserInfo> SaveUserInfo(string loginName, string loginPwd)
        {
            sysUserInfo sysUserInfo = new sysUserInfo(loginName, loginPwd);
            sysUserInfo model = new sysUserInfo();
            var userList = await base.Query(a => a.uLoginName == sysUserInfo.uLoginName && a.uLoginPWD == sysUserInfo.uLoginPWD);
            if (userList.Count > 0)
            {
                model = userList.FirstOrDefault();
            }
            else
            {
                var id = await base.Add(sysUserInfo);
                model = await base.QueryById(id);
            }
 
            return model;
 
        }
 
        /// <summary>
        /// 
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="loginPwd"></param>
        /// <returns></returns>
        public async Task<string> GetUserRoleNameStr(string loginName, string loginPwd)
        {
            string roleName = "";
            var user = (await base.Query(a => a.uLoginName == loginName && a.uLoginPWD == loginPwd)).FirstOrDefault();
            //判断如果查询的用户名为空就用工号登录
            if (user == null)
            {
                user = (await base.Query(a => a.JobNo == loginName && a.uLoginPWD == loginPwd)).FirstOrDefault();
            }
            var roleList = await _roleRepository.Query(a => a.IsDeleted == false);
            if (user != null)
            {
                var userRoles = await _userRoleServices.Query(ur => ur.UserId == user.uID);
                if (userRoles.Count > 0)
                {
                    var arr = userRoles.Select(ur => ur.RoleId.ObjToString()).ToList();
                    var roles = roleList.Where(d => arr.Contains(d.Id.ObjToString()));
 
                    roleName = string.Join(',', roles.Select(r => r.Name).ToArray());
                }
            }
            return roleName;
        }
 
        /// <summary>
        /// 用户是否在指定角色中 add by wyb 2020/9/3
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="RoleName"></param>
        /// <returns></returns>
        public async Task<bool> IsUserIdIntoRoleName(int UserId, params string[] RoleNames)
        {
            var sql = @"
  SELECT * FROM sysUserInfo where uID in (
    SELECT top 1 [UserId] Id FROM UserRole where RoleId in (
        SELECT [RoleId] FROM Role where Name in('" + string.Join("','", RoleNames.Select(t => t.Replace("'", "''"))) + @"')
    ) and UserId=" + UserId + @"
)";
            var userlist = await this.BaseDal.QuerySql(sql);
            var user = userlist.FirstOrDefault();
            if (user == null) { return false; }
            return true;
        }
 
        /// <summary>
        /// OA用户回调
        /// </summary>
        /// <returns></returns>
        public async Task<Tuple<string, int>> BindingUser(string realName, string jobNo, int sex, int orgId)
        {
            var id = 0;
            var error = "";
            var now = DateTime.Now;
 
            try
            {
                _DB.BeginTran();
                var model = await base.GetModel(x => x.uRealName == realName && x.JobNo == jobNo);
                if (model == null)
                {
                    var departmentModel = await _departmentServices.GetModel(x => x.Name.Contains("生产"));
                    if (departmentModel != null)
                    {
                        //添加用户
                        var UserInfo = new sysUserInfo
                        {
                            uLoginName = PinYinHelper.ConvertToAllSpell(realName),
                            uLoginPWD = MD5Helper.MD5Encrypt32("123456"),
                            uRealName = realName,
                            uStatus = 0,
                            uCreateTime = now,
                            uErrorCount = 0,
                            sex = sex,
                            age = 0,
                            birth = now,
                            tdIsDelete = false,
                            JobNo = jobNo,
                            DepartmentId = departmentModel.Id,//默认生产部门
                            SysOrgId = orgId
                            //Mobile = mobile
                        };
                        var userResult = await base.Add(UserInfo);
                        if (userResult > 0)
                        {
                            var roleModel = await _roleRepository.GetModel(x => x.Name == RoleConfig.ProductionAgentStaff);
                            if (roleModel != null)
                            {
                                //添加用户角色关系
                                var UserRole = new UserRole
                                {
                                    IsDeleted = false,
                                    UserId = userResult,
                                    RoleId = roleModel.Id,
                                    CreateId = userResult,
                                    CreateTime = now
                                };
                                var userRoleResult = await _userRoleServices.Add(UserRole);
                                if (userRoleResult > 0)
                                {
                                    //角色模块权限关系表里无数据才添加有主体的权限
                                    var roleModulePermissionModel = await _roleModulePermissionServices.GetModel(x => x.RoleId == roleModel.Id && x.SysOrgId == orgId);
                                    if (roleModulePermissionModel == null)
                                    {
                                        //添加角色模块权限关系
                                        var roleModulePermissionList = await _roleModulePermissionServices.Query(x => x.RoleId == roleModel.Id);
                                        var roleModulePermissionList2 = roleModulePermissionList.Where(x => x.SysOrgId == roleModulePermissionList.Min(x => x.SysOrgId)).ToList();
                                        if (roleModulePermissionList2 != null && roleModulePermissionList2.Any())
                                        {
                                            foreach (var item in roleModulePermissionList2)
                                            {
                                                item.CreateTime = now;
                                                item.ModifyTime = now;
                                                item.SysOrgId = orgId;
                                            }
                                            var roleModulePermissionResult = await _roleModulePermissionServices.Add(roleModulePermissionList2);
                                            if (roleModulePermissionResult <= 0)
                                            {
                                                _DB.RollbackTran();
                                                error = "添加角色模块权限关系失败";
                                            }
                                        }
                                    }
                                    _DB.CommitTran();
                                    id = userResult;
                                    return new Tuple<string, int>(error, id);
                                }
                                else
                                {
                                    _DB.RollbackTran();
                                    error = "添加用户角色关系失败";
                                }
                            }
                            else
                            {
                                _DB.RollbackTran();
                                error = "无相关角色";
                            }
                        }
                        else
                        {
                            _DB.RollbackTran();
                            error = "添加用户失败";
                        }
                    }
                }
                id = model.uID;
                return new Tuple<string, int>(error, id);
            }
            catch (Exception ex)
            {
                _DB.RollbackTran();
                return new Tuple<string, int>(ex.ObjToString(), id);
            }
        }
 
        /// <summary>
        /// 初始化菜单
        /// </summary>
        /// <returns></returns>
        public async Task<Tuple<string, int>> InitializePermission(string realName, int orgId, string roleName)
        {
            var id = 0;
            var error = "";
            var now = DateTime.Now;
 
            try
            {
                _DB.BeginTran();
                var model = await base.GetModel(x => x.uLoginName == realName);
                if (model != null)
                {
                    var roleModel = await _roleRepository.GetModel(x => x.Name.Contains(roleName));
                    if (roleModel != null)
                    {
                        var userRoleModel = await _userRoleServices.GetModel(x => x.UserId == model.uID);
                        if (userRoleModel == null)
                        {
                            //添加用户角色关系
                            var UserRole = new UserRole
                            {
                                IsDeleted = false,
                                UserId = model.uID,
                                RoleId = roleModel.Id,
                                CreateId = model.uID,
                                CreateTime = now
                            };
                            var userRoleResult = await _userRoleServices.Add(UserRole);
                            if (userRoleResult > 0)
                            {
                                //角色模块权限关系表里无数据才添加有主体的权限
                                var roleModulePermissionModel = await _roleModulePermissionServices.GetModel(x => x.RoleId == roleModel.Id && x.SysOrgId == orgId);
                                if (roleModulePermissionModel == null)
                                {
                                    //添加角色模块权限关系
                                    var roleModulePermissionList = await _roleModulePermissionServices.Query(x => x.RoleId == roleModel.Id);
                                    var roleModulePermissionList2 = roleModulePermissionList.Where(x => x.SysOrgId == roleModulePermissionList.Min(x => x.SysOrgId)).ToList();
                                    if (roleModulePermissionList2 != null && roleModulePermissionList2.Any())
                                    {
                                        foreach (var item in roleModulePermissionList2)
                                        {
                                            item.CreateTime = now;
                                            item.ModifyTime = now;
                                            item.SysOrgId = orgId;
                                        }
                                        var roleModulePermissionResult = await _roleModulePermissionServices.Add(roleModulePermissionList2);
                                        if (roleModulePermissionResult <= 0)
                                        {
                                            _DB.RollbackTran();
                                            error = "添加角色模块权限关系失败";
                                        }
                                        else
                                        {
                                            _DB.CommitTran();
                                        }
                                    }
                                }
                            }
                            else
                            {
                                _DB.RollbackTran();
                                error = "添加用户角色关系失败";
                            }
                        }
                    }
                    else
                    {
                        _DB.RollbackTran();
                        error = "无相关角色";
                    }
                    id = model.uID;
                }
                else
                {
                    _DB.RollbackTran();
                    error = "无相关用户";
                }
 
                return new Tuple<string, int>(error, id);
            }
            catch (Exception ex)
            {
                _DB.RollbackTran();
                return new Tuple<string, int>(ex.ObjToString(), id);
            }
        }
    }
}
 
//----------sysUserInfo结束----------