寻找儿童部门的工作人员 - PHP

时间:2017-04-12 10:08:33

标签: php mysql codeigniter recursion activerecord

我正在使用Codeigniter + MySQL + Active Record构建一个带有组织结构图的项目。

有部门列为组织树,员工信息,员工角色和员工部门,我存储匹配: 部门 - 员工 - 角色

您可以看到以下结构:

部门 (parent_id用于构建树)

enter image description here

员工 (原始员工信息)

enter image description here

员工角色 (权重最低,层次结构最高)

enter image description here

员工部门 (在哪个部门 - 谁 - 什么角色)

enter image description here

在稍后阶段,员工可能属于两个或更多具有不同角色的部门。这就是为什么我使用一个单独的表Staff_departments为多对多。在这种情况下,让我们保持简单,并假设1名员工属于1个部门。

我想做什么:

  1. 部门中的经理(角色权重= 0 || role_id = 1 )可以查看在其部门工作的员工(员工和主管) AND 所有来自部门儿童部的工作人员(员工和监督员)。树的深度未知。
  2. 主管可以查看仅在其部门工作的员工(仅限员工)。
  3. 员工只能自己查看。
  4. 对于主管和员工来说,过程很简单,所以我觉得我很好。对于经理人来说,我可能需要做一些递归的事情,但每次我开始编写一些代码行时,我都会挣扎。

    我的想法是在我的控制器中有一个函数 find_related_staff($ staff_id){} ,我将传递登录的工作人员的ID,它将返回一个包含ID的数组他的相关工作人员。我唯一得到的是登录的工作人员的身份证。

      

    如果经理返回其部门和经理,主管和员工相关的经理,主管和员工的ID   他所在部门的儿童部门。

         

    如果主管仅返回其部门相关的主管和员工的身份证明。

         

    如果员工返回其ID

    关于如何实现这一点的任何想法?

4 个答案:

答案 0 :(得分:3)

是的,要完成它,您必须使用递归程序。 (我使用的是MySQL 5.6.19)

我在存储过程之前创建了一些测试数据:

  1. 根据您的问题要求提供的示例数据:

    create table departments
    (
        id int not null primary key auto_increment,
        parent_id int,
        department_name varchar(100)
    );
    
    insert into departments (id,parent_id,department_name)
    values
    (1,0,'Test A'),
    (2,1,'Test B'),
    (3,2,'Test C');
    
    
    create table staff
    (
        id int not null primary key auto_increment,
        ip_address varchar(100),
        username varchar(100)
    );
    
    insert into staff values
    (1,'127.0.0.1','ats'),
    (2,'127.0.0.1','admin'),
    (3,'127.0.0.1','george'),
    (4,'127.0.0.1','jhon')
    ;
    
    create table staff_roles
    (
        role_id int not null primary key auto_increment,
        role_name varchar(100),
        role_height int
    );
    
    insert into staff_roles values
    (1,'Manager',0),
    (2,'Supervisor',1),
    (3,'Employee',2)
    ;
    
    create table staff_departments
    (
        staff_department_id int not null primary key auto_increment,
        department_id int,
        staff_id int,
        role_id int
    );
    
    insert into staff_departments values
    (1,1,2,1),
    (2,2,1,2),
    (3,3,3,3),
    (4,3,4,3);
    
  2. 是时候创建存储过程了:

    • find_related_staff是接收staff_id参数的过程,根据该值会在role_id表中找到staff_departments

      变量@result将以逗号分隔值累积最终结果。

    • find_recursive是在子部门中搜索并将staff_id变为@result变量的过程;

    程序代码:

    delimiter $$
    drop procedure if exists find_related_staff$$
    create procedure  find_related_staff(p_id int)
    begin
        declare p_role_id int;
        declare p_department_id int;
        declare p_return varchar(255) default '';
        declare p_role varchar(100);
    
        select d.role_id, d.department_id, r.role_name
            into p_role_id,p_department_id, p_role
            from staff_departments d
            inner join staff_roles r on d.role_id = r.role_id
            where d.staff_id = p_id
            limit 1;
    
        case p_role_id
        when 3 then -- employee (return the same id)
                set @result = p_id;
    
            when 2 then -- supervisor 
    
                select group_concat(s.staff_id)
            into @result
            from staff_departments s
            where 
                  s.role_id = 3
                  and s.department_id in 
                     ( select d.id 
                       from departments d
                       where d.parent_id = p_department_id )
                  and s.role_id <> p_id;
    
    
            when 1 then -- manager (complex recursive query)
    
                select coalesce(group_concat(s.staff_id),'')
                  into @result
                from staff_departments s
                where 
                  s.department_id =  p_department_id
                  and s.staff_id <>  p_id;
    
               -- here we go!
               call find_recursive(p_department_id);
        end case;
    
        select @result as result, p_role as role;
    end $$
    delimiter ;
    
    delimiter $$
    drop procedure if exists find_recursive$$
    create procedure  find_recursive(p_dept_id int)
    begin
        declare done int default false;
        declare p_department int default false;
    
        declare tmp_result varchar(255) default '';
        -- cursor for all depend departments
        declare c_departments cursor for
            select s.department_id
            from staff_departments s
            where 
                  s.department_id in 
              ( select d.id 
                    from departments d
                    where d.parent_id = p_dept_id );
    
        declare continue handler for not found set done = true;
    
    
        -- getting current departmens
        set tmp_result = 
            (select coalesce(group_concat(s.staff_id),'')
                from staff_departments s
                where 
                  s.department_id in 
                  ( select d.id 
                    from departments d
                    where d.parent_id = p_dept_id ));
    
    
        if length(tmp_result) > 0 then
    
            if length(@result) > 0 then
                set @result = concat(@result,',',tmp_result);
            else
                set @result = tmp_result;
            end if;
    
            open c_departments;
    
            read_loop: loop
                fetch c_departments into  p_department;
                if done then
                  leave read_loop;
                end if;
    
            call find_recursive(p_department);
    
            end loop;
            close c_departments;            
    
        end if;
    
    end $$
    delimiter ;
    
  3. 测试:

    重要提示:默认情况下,递归的最大值为0,我们必须更改该值:

    SET max_sp_recursion_depth=255; 
    

    现在我们在staff_departments表上有以下配置:

    +---------------------+---------------+----------+---------+
    | staff_department_id | department_id | staff_id | role_id |
    +---------------------+---------------+----------+---------+
    |                   1 |             1 |        2 |       1 |
    |                   2 |             2 |        1 |       2 |
    |                   3 |             3 |        3 |       3 |
    |                   4 |             3 |        4 |       3 |
    +---------------------+---------------+----------+---------+
    

    运行每个案例:

    call find_related_staff(2);
    +--------+---------+
    | result | role    |
    +--------+---------+
    | 1,3,4  | Manager |
    +--------+---------+
    
    
    
    call find_related_staff(1);
    +--------+------------+
    | result | role       |
    +--------+------------+
    | 3,4    | Supervisor |
    +--------+------------+
    
    
    call find_related_staff(3);
    +--------+----------+
    | result | role     |
    +--------+----------+
    |      3 | Employee |
    +--------+----------+
    
    call find_related_staff(4);
    +--------+----------+
    | result | role     |
    +--------+----------+
    |      4 | Employee |
    +--------+----------+
    
  4. 享受!

答案 1 :(得分:3)

好吧,我认为,为了让事情更容易理解,我们需要将你的问题分解成小块(我只关注你说你真的需要帮助的部分:经理的递归)

首先,我们获取与经过身份验证的用户关联的当前部门。正如您所说,您只有当前工作人员的ID,所以我们将从那开始。假设用户ID分配给变量$ user_id。

$user_department = $this->db->get_where('staff_departments', ['staff_id' => $user_id])->row();

现在我们有了部门,我们检查用户在该部门中的角色。我们将该信息添加到$ user_department对象:

$user_department->role = $this->db->get_where('staff_roles', ['role_id' => $user_department->role_id])->row();

让我们检查用户角色的重量,不是吗?如果它是0,我们知道它是该部门的经理,所以我们将递归地找到嵌套的部门及其员工信息。根据您的逻辑规定,我们可以在此处检查用户是否也是主管,并在必要时进行升级。像这样:

if ($user_department->role->role_weight <= 1) {
    // the user is a supervisor OR a manager, but both those can see, at least, the current department's staff information
    $user_department->staff = $this->db->get_where('staff_departments', ['department_id' => $user_department->department_id]);

    // now is the user a manager? If so, let's find nested departments
    if ($user_department->role->role_weight === 0) {
        $user_department->childs = $this->getChildDepartmentsAndStaffOf($user_department->department_id);
    }
}

正如您可能注意到的,有一个函数可以递归调用。它必须是这样的:

public function getChildDepartmentsAndStaffOf($department_id)
{
    $child_departments = $this->db->get_where('departments', ['parent_id' => $department_id]);

    if (! $child_departments) {
        return null;
    }

    foreach ($child_departments as &$department) {
        $department->staff = $this->db->get_where('staff_departments', ['department_id' => $department->department_id]);

        $department->childs = $this->getChildDepartmentsAndStaffOf($department->department_id);
    }

    return $child_departments;
}

现在,您拥有所需的结构。我知道这可能会被重构,但我认为这足以得到答案并指出正确的道路。

希望我能帮助一点。

答案 2 :(得分:1)

我认为关系数据库中分层数据最强大的架构是transitive-closure-table

给出departments表的样本数据:

department_id | parent_id | department_name
--------------|-----------|----------------
            1 |         0 | TEST A
            2 |         1 | TEST B
            3 |         2 | TEST C

你的闭包表(让我们称之为departments_tree)就像:

super_id | sub_id
---------|-------
       1 |      1
       1 |      2
       1 |      3
       2 |      2
       2 |      3
       3 |      3

将其读作:super_id =上级department_id; sub_id =从属department_id。

假设登录用户是department_id = 2部门的经理,则获取所有“受监督”员工的查询是:

SELECT DISTINCT s.*
FROM departments_tree t
JOIN stuff_departments sd ON sd.department_id = t.sub_id
JOIN staff s ON s.id = sd.staff_id
WHERE t.super_id = 2

您可以使用触发器来填充和更新闭包表。

插入触发器:

DELIMITER //
CREATE TRIGGER `departments_after_insert` AFTER INSERT ON `departments` FOR EACH ROW BEGIN
    INSERT INTO departments_tree (super_id, sub_id)
        SELECT new.department_id, new.department_id
        UNION ALL
        SELECT super_id, new.department_id
        FROM departments_tree
        WHERE sub_id = new.parent_id;
END//
DELIMITER ;

删除触发器:

DELIMITER //
CREATE TRIGGER `departments_before_delete` BEFORE DELETE ON `departments` FOR EACH ROW BEGIN
    DELETE FROM departments_tree
    WHERE sub_id = old.department_id;
END//
DELIMITER ;

更新触发器:

DELIMITER //
CREATE TRIGGER `departments_before_update` BEFORE UPDATE ON `departments` FOR EACH ROW BEGIN
    DELETE t
    FROM       departments_tree p 
    CROSS JOIN departments_tree c
    INNER JOIN departments_tree t
      ON  t.super_id = p.super_id
      AND t.sub_id = c.sub_id
    WHERE p.sub_id   = old.parent_id
      AND c.super_id = new.department_id;

    INSERT INTO departments_tree (super_id, sub_id)
        SELECT p.super_id, c.sub_id
        FROM       departments_tree p
        CROSS JOIN departments_tree c
        WHERE p.sub_id   = new.parent_id
          AND c.super_id = new.department_id;
END//

注意

如果您使用带ON DELETE CASCADE的前缀键:

,则不需要删除触发器
CREATE TABLE `departments_tree` (
    `super_id` INT(10) UNSIGNED NOT NULL,
    `sub_id` INT(10) UNSIGNED NOT NULL,
    PRIMARY KEY (`super_id`, `sub_id`),
    INDEX `sub_id_super_id` (`sub_id`, `super_id`),
    FOREIGN KEY (`super_id`) REFERENCES `departments` (`department_id`) ON DELETE CASCADE,
    FOREIGN KEY (`sub_id`)   REFERENCES `departments` (`department_id`) ON DELETE CASCADE
);

注2

在传递闭包表的许多实现中,您会找到depthlevel列。 但是你不需要它来满足给定的要求。我相信你永远不会真的需要它, 只要您不尝试在SQL中格式化树输出。

答案 3 :(得分:0)

我认为你的主要问题是如何遍历以便getRecursiveDepts()解决。我还没有完成代码,但你可以试试这样的东西

档案db.php

class DB {
    private $servername = "127.0.0.1";
    private $username = "root";
    private $password = "root";
    private $dbname = "test";
    private $port = '3306';

    public function getRecursiveDepts($deptIds) {
        if (!is_array($deptIds)) {
            $deptIds = array($deptIds);
        }
        $sql = "SELECT id FROM Departments WHERE parentId IN (";
        $sql .= implode(', ', $deptIds);
        $sql .= ")";

        $conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname, $this->port);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            $newDept = array();
            while($row = $result->fetch_assoc()) {
                array_push($newDept, $row['id']);
            }
            $conn->close();
            $moreDepts = $this->getRecursiveDepts($newDept);
            if (is_null($moreDepts)) {
                $finalIds = array_unique(array_merge($deptIds, $newDept));
            } else {
                $finalIds = array_unique(array_merge($deptIds, $newDept, $moreDepts));
            }
            return $finalIds;
        } else {
            $conn->close();
            return null;
        }
    }

    public function getRoles($empId) {
        $sql = "SELECT role_id, department_id FROM staff_departmen_role WHERE staff_id = '$empId' GROUP BY role_id, department_id";
        $conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname, $this->port);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            $emp = array();
            while($row = $result->fetch_assoc()) {
                if (!array_key_exists($row['role_id'], $emp)) {
                    $emp[$row['role_id']] = array();
                }
                array_push($emp[$row['role_id']], $row['department_id']);
            }
        }
        $conn->close();
        return $emp;
    }

    public function getEmpDetails($empId) {
        $sql = "SELECT role_id, department_id FROM staff_departmen_role WHERE staff_id = '$empId' GROUP BY role_id, department_id";
        $conn = new mysqli($this->servername, $this->username, $this->password, $this->dbname, $this->port);
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            $emp = array();
            while($row = $result->fetch_assoc()) {
                if (!array_key_exists($row['role_id'], $emp)) {
                    $emp[$row['role_id']] = array();
                }
                array_push($emp[$row['role_id']], $row['department_id']);
            }
        }
        $conn->close();
        return $emp;
    }
}

档案index.php

<?php
include_once 'db.php';

$objDB = new DB();

$empId = 2;

$emps = $objDB->getRoles($empId);
foreach ($emps as $roleId => $deptIds) {
    switch ($roleId) {
        case 1:
            $allDeptIds = $objDB->getRecursiveDepts($deptIds);
            break;

        case 2://Supervisor GetEmpIds of current dept role >= 2

            break;

        case 3://Employee GetEmpIds of current dept role >= 2
            $emp = $objDB->getEmpDetails($empId);
            break;

        default:
            # code...
            break;
    }
}
$data = $objDB->getRecursiveDepts($empId);
print_r($data);
?>