SysDeptApp.cs
3.55 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
using System;
using System.Collections.Generic;
using System.Linq;
using WebRepository;
namespace WebApp
{
public class SysDeptApp : WebApp<SysDept>
{
public SysDeptApp(IUnitWork unitWork, IRepository<SysDept> repository) : base(unitWork, repository)
{
}
/// <summary>
/// 添加部门
/// </summary>
/// <param name="org">The org.</param>
/// <returns>System.Int32.</returns>
/// <exception cref="System.Exception">未能找到该组织的父节点信息</exception>
public int Add(SysDept org)
{
ChangeModuleCascade(org);
Repository.Add(org);
return org.Id.Value;
}
public SysDeptApp SetLoginInfo(LoginInfo loginInfo)
{
Repository._loginInfo = loginInfo;
return this;
}
public int Update(SysDept org)
{
ChangeModuleCascade(org);
//获取旧的的CascadeId
var cascadeId = Repository.FindSingle(o => o.Id == org.Id).CascadeId;
//根据CascadeId查询子部门
var orgs = Repository.Find(u => u.CascadeId.Contains(cascadeId) && u.Id != org.Id)
.OrderBy(u => u.CascadeId).ToList();
//更新操作
Repository.Update(org);
//更新子部门的CascadeId
foreach (var a in orgs)
{
ChangeModuleCascade(a);
Repository.Update(a);
}
Repository.Save();
return org.Id.Value;
}
/// <summary>
/// 删除指定ID的部门及其所有子部门
/// </summary>
public void Del(int[] ids)
{
var delOrg = Repository.Find(u => ids.Contains(u.Id.Value)).ToList();
foreach (var org in delOrg)
{
Repository.Delete(u => u.CascadeId.Contains(org.CascadeId));
}
}
/// <summary>
/// 加载特定用户的角色
/// TODO:这里会加载用户及用户角色的所有角色,“为用户分配角色”功能会给人一种混乱的感觉,但可以接受
/// </summary>
/// <param name="userId">The user unique identifier.</param>
public List<SysDept> LoadForUser(int userId)
{
//用户角色
var userRoleIds =
UnitWork.Find<SysRelevance>(u => u.FirstId == userId && u.RelKey == Define.USERROLE).Select(u => u.SecondId).ToList();
//用户角色与自己分配到的角色ID
var moduleIds =
UnitWork.Find<SysRelevance>(
u =>
(u.FirstId == userId && u.RelKey == Define.USERORG) ||
(u.RelKey == Define.ROLEORG && userRoleIds.Contains(u.FirstId))).Select(u => u.SecondId).ToList();
if (!moduleIds.Any()) return new List<SysDept>();
return UnitWork.Find<SysDept>(u => moduleIds.Contains(u.Id)).ToList();
}
/// <summary>
/// 加载特定角色的角色
/// </summary>
/// <param name="roleId">The role unique identifier.</param>
public List<SysDept> LoadForRole(int roleId)
{
var moduleIds =
UnitWork.Find<SysRelevance>(u => u.FirstId == roleId && u.RelKey == Define.ROLEORG)
.Select(u => u.SecondId)
.ToList();
if (!moduleIds.Any()) return new List<SysDept>();
return UnitWork.Find<SysDept>(u => moduleIds.Contains(u.Id)).ToList();
}
}
}