コード例 #1
0
        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);
        }