父子级树的复制创建

通过创建字典,临时保存旧树和新树的主键ID,两次遍历将父子级ID赋值保存

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
Dictionary<string, string> dic = new Dictionary<string, string>();
List<PostEntity> postList = new List<PostEntity>();
var templatePosts=projectMemberPostIBLL.GetList(new PostEntity()
{
resID = modelId.ToString()
});
foreach (var item in templatePosts)
{
//复制新建团队的模板架构
var projectPost = new PostEntity()
{
ID = Guid.NewGuid().ToString(),
pmpParentId = item.pmpParentId,
};
dic.Add(item.ID, projectPost.ID);
postList.Add(projectPost);
}
//再次遍历新树,将旧树父级ID赋值未对应新树的父级ID
foreach (var post in postList)
{
if (post.pmpParentId != "0")
{
post.pmpParentId = dic.ContainsKey(post.pmpParentId) ? dic[post.pmpParentId] : "0";
}
projectMemberPostIBLL.SaveEntity("", post, repository);
}