SysRoleApp.cs
4.43 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
using System;
using System.Collections.Generic;
using System.Linq;
using WebRepository;
namespace WebApp
{
public class SysRoleApp : WebApp<SysRole>
{
private SysRevelanceApp _revelanceApp;
private IAuth _auth;
public SysRoleApp(IUnitWork unitWork, IRepository<SysRole> repository,
SysRevelanceApp app, IAuth auth) : base(unitWork, repository)
{
_revelanceApp = app;
_auth = auth;
}
public SysRoleApp SetLoginInfo(LoginInfo loginInfo)
{
Repository._loginInfo = loginInfo;
return this;
}
/// <summary>
/// 加载当前登录用户可访问的一个部门及子部门全部角色
/// </summary>
public TableData Load(PageReq pageRequest, int? orgId)
{
var loginUser = _auth.GetCurrentUser();
string cascadeId = ".0.";
if (orgId != null)
{
var org = loginUser.Orgs.SingleOrDefault(u => u.Id == orgId);
cascadeId = org.CascadeId;
}
var ids = loginUser.Orgs.Where(u => u.CascadeId.Contains(cascadeId)).Select(u => u.Id.Value).ToArray();
var roleIds = _revelanceApp.Get(Define.ROLEORG, false, ids);
//SQL2008不支持分页的语法
//var data = UnitWork.Find<Role>(u => roleIds.Contains(u.Id)).ToList();
//var roles = data
// .OrderBy(u => u.Name)
// .Skip((pageRequest.page - 1) * pageRequest.limit)
// .Take(pageRequest.limit);
var roles = UnitWork.Find<SysRole>(u => roleIds.Contains(u.Id.Value))
.OrderBy(u => u.Name)
.Skip((pageRequest.page - 1) * pageRequest.limit)
.Take(pageRequest.limit);
var records = Repository.GetCount(u => roleIds.Contains(u.Id.Value));
var roleViews = new List<SysRoleView>();
foreach (var role in roles.ToList())
{
SysRoleView uv = role;
var orgs = LoadByRole(role.Id.Value);
uv.Organizations = string.Join(",", orgs.Select(u => u.Name).ToList());
uv.OrganizationIds = string.Join(",", orgs.Select(u => u.Id).ToList());
roleViews.Add(uv);
}
return new TableData
{
count = records,
data = roleViews,
};
}
/// <summary>
/// 加载角色的所有机构
/// </summary>
public IEnumerable<SysDept> LoadByRole(int roleId)
{
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 == roleId && userorg.RelKey == Define.ROLEORG
select org;
return result;
}
public void Add(SysRoleView obj)
{
if (string.IsNullOrEmpty(obj.OrganizationIds))
{
throw new Exception("请为角色分配机构");
}
if (UnitWork.IsExist<SysRole>(u => u.Name == obj.Name))
{
throw new Exception("角色已存在");
}
SysRole role = obj;
role.CreateTime = DateTime.Now;
Repository.Add(role);
obj.Id = role.Id; //要把保存后的ID存入view
UpdateRole(obj);
}
public void Update(SysRoleView obj)
{
if (string.IsNullOrEmpty(obj.OrganizationIds))
throw new Exception("请为角色分配机构");
SysRole role = obj;
Repository.Update(u => u.Id == obj.Id, u => new SysRole
{
Name = role.Name,
Status = role.Status,
UpdateBy = Repository._loginInfo.Account,
UpdateTime = DateTime.Now
});
UpdateRole(obj);
}
/// <summary>
/// 更新相应的多对多关系
/// </summary>
/// <param name="obj"></param>
private void UpdateRole(SysRoleView obj)
{
int[] orgIds = Array.ConvertAll(obj.OrganizationIds.Split(','), int.Parse);
_revelanceApp.DeleteBy(Define.ROLEORG, obj.Id.Value);
_revelanceApp.AddRelevance(Define.ROLEORG, orgIds.ToLookup(u => obj.Id.Value));
}
}
}