WebApp.cs
2.78 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
using System;
using System.Linq;
using WebRepository;
namespace WebApp
{
/// <summary>
/// 业务层基类,UnitWork用于事务操作,Repository用于普通的数据库操作
/// <para>如用户管理:Class UserManagerApp:WebApp<User></para>
/// </summary>
/// <typeparam name="T"></typeparam>
public class WebApp<T> where T : SysEntity
{
/// <summary>
/// 用于事务操作
/// </summary>
/// <value>The unit work.</value>
protected IUnitWork UnitWork;
/// <summary>
/// 用于普通的数据库操作
/// </summary>
/// <value>The repository.</value>
protected IRepository<T> Repository;
public WebApp(IUnitWork unitWork, IRepository<T> repository)
{
UnitWork = unitWork;
Repository = repository;
}
/// <summary>
/// 按id批量删除
/// </summary>
/// <param name="ids"></param>
public void Delete(int[] ids)
{
Repository.Delete(u => ids.Contains(u.Id.Value));
}
public T Get(int id)
{
return Repository.FindSingle(u => u.Id == id);
}
/// <summary>
/// 如果一个类有层级结构(树状),则修改该节点时,要修改该节点的所有子节点
/// //修改对象的级联ID,生成类似XXX.XXX.X.XX
/// </summary>
/// <typeparam name="U">U必须是一个继承TreeEntity的结构</typeparam>
/// <param name="entity"></param>
public void ChangeModuleCascade<U>(U entity) where U : TreeEntity
{
string cascadeId;
int currentCascadeId = 1; //当前结点的级联节点最后一位
var sameLevels = UnitWork.Find<U>(o => o.ParentId == entity.ParentId && o.Id != entity.Id);
foreach (var obj in sameLevels)
{
int objCascadeId = int.Parse(obj.CascadeId.TrimEnd('.').Split('.').Last());
if (currentCascadeId <= objCascadeId) currentCascadeId = objCascadeId + 1;
}
if (entity.ParentId != null)
{
var parentOrg = UnitWork.FindSingle<U>(o => o.Id == entity.ParentId);
if (parentOrg != null)
{
cascadeId = parentOrg.CascadeId + currentCascadeId + ".";
entity.ParentName = parentOrg.Name;
}
else
{
throw new Exception("未能找到该组织的父节点信息");
}
}
else
{
cascadeId = ".0." + currentCascadeId + ".";
entity.ParentName = "根节点";
}
entity.CascadeId = cascadeId;
}
}
}