SysUserApp.cs
5.91 KB
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
using Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using WebRepository;
namespace WebApp
{
public class SysUserApp : WebApp<SysUser>
{
private SysRevelanceApp _revelanceApp;
private IAuth _auth;
public SysUserApp(IUnitWork unitWork, IRepository<SysUser> repository,
SysRevelanceApp app, IAuth auth) : base(unitWork, repository)
{
_revelanceApp = app;
_auth = auth;
}
public SysUserApp SetLoginInfo(LoginInfo loginInfo)
{
Repository._loginInfo = loginInfo;
_revelanceApp = _revelanceApp.SetLoginInfo(Repository._loginInfo);
return this;
}
/// <summary>
/// 加载当前登录用户可访问的一个部门及子部门全部用户
/// </summary>
public TableData Load(PageReq request, int? orgId)
{
var loginUser = _auth.GetCurrentUser();
string cascadeId = ".0.";
if (orgId != null)
{
var org = loginUser.Orgs.SingleOrDefault(u => u.Id == orgId.Value);
cascadeId = org.CascadeId;
}
var ids = loginUser.Orgs.Where(u => u.CascadeId.Contains(cascadeId)).Select(u => u.Id.Value).ToArray();
var userIds = _revelanceApp.Get(Define.USERORG, false, ids);
//SQL2008不支持分页的语法
//var data = UnitWork.Find<User>(u => userIds.Contains(u.Id)).ToList();
//var users = data
// .OrderBy(u => u.Name)
// .Skip((request.page - 1) * request.limit)
// .Take(request.limit);
var users = Repository.Find(u => userIds.Contains(u.Id.Value) && u.Account != "System")
.OrderBy(u => u.Name)
.Skip((request.page - 1) * request.limit)
.Take(request.limit);
var records = Repository.GetCount(u => userIds.Contains(u.Id.Value) && u.Account != "System");
var userviews = new List<SysUserView>();
foreach (var user in users.ToList())
{
SysUserView uv = user;
var orgs = LoadByUser(user.Id.Value);
uv.Organizations = string.Join(",", orgs.Select(u => u.Name).ToList());
uv.OrganizationIds = string.Join(",", orgs.Select(u => u.Id).ToList());
userviews.Add(uv);
}
return new TableData
{
count = records,
data = userviews,
};
}
public void Add(SysUserView view)
{
if (string.IsNullOrEmpty(view.OrganizationIds))
throw new Exception("请为用户分配机构");
SysUser user = view;
if (Repository.IsExist(u => u.Account == view.Account))
{
throw new Exception("账号已存在");
}
if (Repository.IsExist(u => u.Name == view.Name))
{
throw new Exception("用户名已存在");
}
user.Password = Encryption.Encrypt(user.Password); //密码加密
Repository.Add(user);
view.Id = user.Id; //要把保存后的ID存入view
UnitWork.Save();
int[] orgIds = Array.ConvertAll(view.OrganizationIds.Split(','), int.Parse);
_revelanceApp.DeleteBy(Define.USERORG, user.Id.Value);
_revelanceApp.AddRelevance(Define.USERORG, orgIds.ToLookup(u => user.Id.Value));
}
public void Update(SysUserView view)
{
if (string.IsNullOrEmpty(view.OrganizationIds))
throw new Exception("请为用户分配机构");
SysUser user = view;
Repository.Update(u => u.Id == view.Id, u => new SysUser
{
Account = user.Account,
Name = user.Name,
Sex = user.Sex,
Status = user.Status,
UpdateBy = Repository._loginInfo.Account,
UpdateTime = DateTime.Now
});
int[] orgIds = Array.ConvertAll(view.OrganizationIds.Split(','), int.Parse);
_revelanceApp.DeleteBy(Define.USERORG, user.Id.Value);
_revelanceApp.AddRelevance(Define.USERORG, orgIds.ToLookup(u => user.Id.Value));
}
/// <summary>
/// 加载用户的所有机构
/// </summary>
public IEnumerable<SysDept> LoadByUser(int userId)
{
var result = from userorg in UnitWork.Find<SysRelevance>(null)
join org in UnitWork.Find<SysDept>(null) on userorg.SecondId equals org.Id
where userorg.FirstId == userId && userorg.RelKey == Define.USERORG
select org;
return result;
}
//修改个人密码
public void ChangeUserPassword(string OldPassword, string Password, SysUser CurrentUser)
{
SysUser user = CurrentUser;
if (user.Password.Equals(Encryption.Encrypt(OldPassword)))
{
if (OldPassword == Password)
{
throw new Exception("新密码不应与旧密码相同");
}
else
{
user.Password = Encryption.Encrypt(Password);
Repository.Update(user);
}
}
else
{
throw new Exception("旧密码不正确");
}
}
//重设用户密码
public void ResetPassword(SysUser CurrentUser)
{
SysUser user = CurrentUser;
user.Password = Encryption.Encrypt("123456");
Repository.Update(user);
}
public SysUser GetByAccount(string account)
{
return Repository.FindSingle(u => u.Account == account);
}
}
}