예제 #1
0
        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);
        }