public async Task <OrgNodeType> _getOrgNodeTypeByNodeID(int id) { List <OrgNodeType> nodeTypes = await _orgRepo.ListNodeType(); OrgTree node = await _orgRepo.GetNode(id); OrgNodeType changeToNodeType = null; if (node != null) { changeToNodeType = nodeTypes.Where(c => c.Id == node.NodeType) .FirstOrDefault(); } return(changeToNodeType); }
private object _parseOrgTree(OrgTree parentNode, List <OrgTree> nodes , List <OrgNodeType> nodeTypes) { OrgNodeType nodeType = nodeTypes.Where(c => c.Id == parentNode.NodeType) .FirstOrDefault(); var node_p = new { id = parentNode.Id, label = parentNode.Name, node_type = parentNode.NodeType, type = nodeType, children = new List <object>() }; List <OrgTree> nodes_children = nodes.Where(c => c.ParentID == parentNode.Id).ToList(); if (nodes_children.Count > 0) { foreach (OrgTree child in nodes_children) { if (!child.IsDel) { var node_c = _parseOrgTree(child, nodes, nodeTypes); node_p.children.Add(node_c); } } } if (node_p.children.Count == 0) { return(new { id = parentNode.Id, label = parentNode.Name, type = nodeType, node_type = parentNode.NodeType, }); } return(node_p); }
public async Task <ApiResult> BindOrgNodeUsers(OrgUserView nodeView) { ApiResult ret = new ApiResult(); try { using (TransactionScope scope = new TransactionScope()) { // //取消bind当前的用户 // OrgTree node = new OrgTree(); // node.ID = nodeView.ID; // node.UpdatedBy = nodeView.CreatedBy; // node.UpdatedTime = nodeView.CreatedTime; // await _orgRepo.UnbindOrgNodeUsers(node); // 判断此节点是否可绑定人员 bool canBind = true; OrgNodeType nodeType = await _getOrgNodeTypeByNodeID(nodeView.Id); if (nodeType != null && !nodeType.HasUsers) { canBind = false; } if (nodeType != null && nodeType.HasUsers && nodeType.HasUsersLeafOnly) { // 假如该节点目前为叶子节点后期有可能添加子节点,要提示不能添加 bool hasChildren = await _orgRepo.hasChildren(nodeView.Id); if (hasChildren) { canBind = false; } } if (canBind) { // 绑定前检查用户是否已被绑定 List <OrgUser> selectedUsers = await _orgRepo.ListAllOrgUser(); List <OrgUser> conflictUsers = selectedUsers .Where(c => nodeView.UserIDs.Contains(c.UserID)).ToList(); if (conflictUsers.Count == 0) { //bind新用户 List <OrgUser> users = new List <OrgUser>(); foreach (int id in nodeView.UserIDs) { OrgUser user = new OrgUser(); user.UserID = id; user.NodeID = nodeView.Id; user.CreatedBy = nodeView.CreatedBy; user.CreatedTime = nodeView.CreatedTime; users.Add(user); } await _orgRepo.BindOrgNodeUsers(users); ret.code = Code.Success; } else { ret.code = Code.BindUserConflict; } } else { ret.code = Code.CheckDataRulesFail; } scope.Complete(); } } catch (Exception ex) { ret.code = Code.Failure; ret.msg = ex.Message; } return(ret); }
public async Task <ApiResult> UpdateOrgNode(OrgTree node) { ApiResult ret = new ApiResult(); try { using (TransactionScope scope = new TransactionScope()) { List <OrgNodeType> nodeTypes = await _orgRepo.ListNodeType(); bool canUpdate = true; // 如果此节点类型有子节点,则不可变为has_children为false的节点 OrgNodeType changeToNodeType = nodeTypes.Where(c => c.Id == node.NodeType) .FirstOrDefault(); bool hasChildren = await _orgRepo.hasChildren(node.Id); if (hasChildren && changeToNodeType != null && !changeToNodeType.HasChildren) { canUpdate = false; } // 如果此节点有用户,不可变为has_users 为false的节点 List <OrgUser> users = await _orgRepo.ListOrgNodeUsers(node.Id); if (users.Count > 0 && changeToNodeType != null && !changeToNodeType.HasUsers) { canUpdate = false; } if (canUpdate) { // 检查是否存在同名节点 bool isExist = await _orgRepo.CheckNodeExist(node); if (!isExist) { var data = await _orgRepo.UpdateOrgNode(node); //由于节点类型有可能更新,如果更新则节点对应的扩展属性会有不同, //为了逻辑同一,对属性的更新都先删除再添加 //删除已有属性 await _orgRepo.DeleteOrgNodeProperty(node); //保存扩展属性 if (node.PropEx.Count > 0) { bool propSavedOk = await _saveNodeProperty(data); if (!propSavedOk) { throw new Exception("存储节点属性失败"); } } ret.code = Code.Success; ret.data = data; } else { ret.code = Code.DataIsExist; } } else { ret.code = Code.CheckDataRulesFail; } scope.Complete(); } } catch (Exception ex) { ret.code = Code.Failure; ret.msg = ex.Message; } return(ret); }
public async Task <ApiResult> AddOrgNode(OrgTree node) { ApiResult ret = new ApiResult(); try { using (TransactionScope scope = new TransactionScope()) { List <OrgNodeType> nodeTypes = await _orgRepo.ListNodeType(); bool canAdd = true; // 找到父节点,根据父节点类型判断是否可添加此节点 // 如果父节点可以有子节点,但属性为has_users_leafonly为true,且已关联人员则不能添加 if (node.ParentID != null) { OrgTree parent = await _orgRepo.GetNode((int)node.ParentID); if (parent != null) { OrgNodeType nodeType = nodeTypes.Where(c => c.Id == parent.NodeType) .FirstOrDefault(); if (!nodeType.HasChildren) { canAdd = false; } if (nodeType.HasUsersLeafOnly) { List <OrgUser> users = await _orgRepo.ListOrgNodeUsers(parent.Id); if (users.Count > 0) { canAdd = false; } } } } if (canAdd) { bool isExist = await _orgRepo.CheckNodeExist(node); if (!isExist) { var data = await _orgRepo.SaveOrgNode(node); //保存扩展属性 if (node.PropEx != null && node.PropEx.Count > 0) { bool propSavedOk = await _saveNodeProperty(data); if (!propSavedOk) { throw new Exception("存储节点属性失败"); } } ret.code = Code.Success; ret.data = data; } else { ret.code = Code.DataIsExist; } } else { ret.code = Code.CheckDataRulesFail; } scope.Complete(); } } catch (Exception ex) { ret.code = Code.Failure; ret.msg = ex.Message; } return(ret); }