protected virtual void OnGetForSelectItem(TEntity entity, GeneralTreeNodeDto node) { }
public virtual async Task <IList <GeneralTreeNodeDto> > GetTreeForSelectAsync(GeneralTreeGetForSelectInput <long?> input) { //权限判断 await CheckGetPermissionAsync(); //得到实体扁平集合 string parentCode = ""; if (input.Id.HasValue && input.Id.Value > 0) { var top = await ownRepository.GetAsync(input.Id.Value); parentCode = top.Code; } var query = ownRepository.GetAll() .Where(c => c.Code.StartsWith(parentCode)) .OrderBy(c => c.Code); //可以调用虚方法简介给子类一个机会做过滤,暂未实现 var list = await AsyncQueryableExecuter.ToListAsync(query); //先踢出父节点 //TEntity parent = list.SingleOrDefault(c => c.Id == input.Id); //if (parent != null) // list.Remove(parent); //转换为Dtop //上面没有直接用投影是为了给子类一个机会来参与遍历过程 var dtoList = new List <GeneralTreeNodeDto>(); foreach (var c in list) { var temp = new GeneralTreeNodeDto { id = c.Id.ToString(), parentId = c.ParentId.ToString(), text = c.DisplayName }; temp.attributes = new ExpandoObject(); temp.attributes.code = c.Code;//这里可以搞个虚方法,允许子类填充自己的字段 dtoList.Add(temp); } dtoList.ForEach(c => { c.children = dtoList.Where(d => d.parentId == c.id).ToList(); var entity = list.Single(d => d.Id.ToString() == c.id); if (c.children != null && c.children.Count > 0) { c.state = "closed";//默认值为 open } if (!string.IsNullOrWhiteSpace(entity.ExtensionData)) { c.attributes.extData = JsonConvert.DeserializeObject <dynamic>(entity.ExtensionData); } OnGetForSelectItem(entity, c); }); var parentDto = input.Id.HasValue ? dtoList.SingleOrDefault(c => c.id == input.Id.ToString()) : null; if (input.Id.HasValue) { dtoList = dtoList.Where(c => c.parentId == input.Id.ToString()).ToList(); } else { dtoList = dtoList.Where(c => string.IsNullOrWhiteSpace(c.parentId)).ToList(); } if (input.ForType > 0 && input.ForType < 5 && !string.IsNullOrWhiteSpace(input.ParentText)) { return new List <GeneralTreeNodeDto> { new GeneralTreeNodeDto { id = null, text = L(input.ParentText), children = dtoList } } } ; if ((input.ForType == 1 || input.ForType == 3) && input.Id.HasValue) { return new List <GeneralTreeNodeDto> { parentDto } } ; if (input.ForType == 1 || input.ForType == 2) { return new List <GeneralTreeNodeDto> { new GeneralTreeNodeDto { id = null, text = L(this.allTextForSearch), children = dtoList } } } ; if (input.ForType == 3 || input.ForType == 4) { return new List <GeneralTreeNodeDto> { new GeneralTreeNodeDto { id = null, text = L(this.allTextForForm), children = dtoList } } } ; return(dtoList); }