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