<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bonus.system.dept.dao.ProDeptRoleDao">

    <select id="getRoleUserList" resultType="com.bonus.system.dept.entity.ProDeptRoleBean">
        select * from(
        SELECT
        so.id AS orgId,
        so.org_name,
        sdr.id as departmentId,
        sdr.reviewer_status,
        sd.id AS deptRoleId,
        sd.`value` as deptRoleName,
        GROUP_CONCAT( DISTINCT su.user_name ) AS userName,
        su2.user_name AS createUser,
        so.update_time
        FROM
        sys_organization so
        LEFT JOIN sys_dict sd ON sd.type = 'deptRole'
        AND sd.is_active = '1'
        LEFT JOIN sys_department_role sdr ON so.id = sdr.org_id
        AND sd.id = sdr.department_role_id
        AND sdr.is_active = '1'
        LEFT JOIN sys_user su2 ON su2.user_id = sdr.update_user_id
        AND su2.is_active = '1'
        LEFT JOIN sys_department_user sdu ON sdr.id = sdu.department_id
        AND sdu.is_active = '1'
        LEFT JOIN sys_user su ON su.user_id = sdu.user_id
        AND su.is_active = '1'
        where
        so.is_department = '1'
        AND so.is_active = '1'
        and so.reviewer_status = '1'
        GROUP BY
        so.id,
        sd.id ) aa
        <where>
            <if test="bean.orgName != null and bean.orgName != '' ">
                and locate(#{bean.orgName},aa.org_name)
            </if>
            <if test="bean.deptRoleName != null and bean.deptRoleName != '' ">
                and locate(#{bean.deptRoleName},aa.deptRoleName)
            </if>
            <if test="bean.userName != null and bean.userName != '' ">
                and locate(#{bean.userName},userName)
            </if>
            <if test="bean.reviewerStatus != null and bean.reviewerStatus != '' ">
                and aa.reviewer_status = #{bean.reviewerStatus}
            </if>
            <if test='bean.departmentId == -1L'>
                and (aa.departmentId is null or userName is null or aa.reviewer_status !='1')
            </if>
            <if test='bean.orgList != null and bean.orgList.size() > 0'>
                and orgId in (
                <foreach collection="bean.orgList" item="item" separator=",">
                    #{item}
                </foreach>
                )
            </if>
            <if test="bean.userId != null and bean.userId != ''">
                AND su.user_id = #{bean.userId}
            </if>
        </where>
        Order BY aa.update_time DESC
    </select>

    <select id="getRoleUserNoManagerList" resultType="com.bonus.system.dept.entity.ProDeptRoleBean">
        select * from(
        SELECT
        so.id AS orgId,
        so.org_name,
        sdr.id as departmentId,
        sdr.reviewer_status,
        sd.id AS deptRoleId,
        sd.`value` as deptRoleName,
        GROUP_CONCAT( DISTINCT if(sdr.reviewer_status = 1, su.user_name,null) ) AS userName,
        su2.user_name AS createUser,
        so.update_time
        FROM
        sys_organization so
        LEFT JOIN sys_dict sd ON sd.type = 'deptRole'
        AND sd.is_active = '1'
        LEFT JOIN sys_department_role sdr ON so.id = sdr.org_id
        AND sd.id = sdr.department_role_id
        AND sdr.is_active = '1'
        LEFT JOIN sys_user su2 ON su2.user_id = sdr.update_user_id
        AND su2.is_active = '1'
        LEFT JOIN sys_department_user sdu ON sdr.id = sdu.department_id
        AND sdu.is_active = '1'
        LEFT JOIN sys_user su ON su.user_id = sdu.user_id
        AND su.is_active = '1'
        where
        so.is_department = '1'
        AND so.is_active = '1'
        and so.reviewer_status = '1'
        GROUP BY
        so.id,
        sd.id ) aa
        <where>
            <if test="bean.orgName != null and bean.orgName != '' ">
                and locate(#{bean.orgName},aa.org_name)
            </if>
            <if test="bean.deptRoleName != null and bean.deptRoleName != '' ">
                and locate(#{bean.deptRoleName},aa.deptRoleName)
            </if>
            <if test="bean.userName != null and bean.userName != '' ">
                and locate(#{bean.userName},userName)
            </if>
            <if test="bean.reviewerStatus != null and bean.reviewerStatus != '' ">
                and aa.reviewer_status = #{bean.reviewerStatus}
            </if>
            <if test='bean.departmentId == -1L'>
                and (aa.departmentId is null or userName is null or aa.reviewer_status !='1')
            </if>
            <if test='bean.orgList != null and bean.orgList.size() > 0'>
                and orgId in (
                <foreach collection="bean.orgList" item="item" separator=",">
                    #{item}
                </foreach>
                )
            </if>
            <if test="bean.userId != null and bean.userId != ''">
                AND su.user_id = #{bean.userId}
            </if>
        </where>
        Order BY aa.update_time DESC
    </select>

    <select id="selectOrgUserList" resultType="com.bonus.system.dept.entity.OrgUserVo">
        SELECT
        CONCAT( 'o', id ) AS id,
        org_name AS `name`,
        CONCAT(
        'o',
        ifnull( parent_id, 0 )) AS parent_id,
        true as disabled,
        0 AS isCheck
        FROM
        sys_organization
        where is_active = '1'
        UNION
        SELECT
        suo.user_id AS id,
        su.user_name AS `name`,
        CONCAT( 'o', suo.org_id ) AS parent_id,
        false as disabled,
        IF
        ( mm.org_id IS NULL, 0, 1 ) AS isCheck
        FROM
        sys_user_org suo
        LEFT JOIN sys_user su ON su.user_id = suo.user_id
        LEFT JOIN (
        SELECT
        org_id,
        department_role_id,
        user_id
        FROM
        sys_department_role sdr
        INNER JOIN sys_department_user sdu ON sdu.department_id = sdr.id
        WHERE
        sdr.is_active = '1'
        AND sdu.is_active = '1'
        AND department_role_id = #{deptRoleId}
        <if test='orgId != -1L'>
            AND org_id = #{orgId}
        </if>
        ) mm ON mm.user_id = su.user_id
    </select>

    <select id="getRoleUserById" resultType="java.lang.String">
        SELECT sdu.user_id
        FROM sys_department_user sdu
        WHERE sdu.department_id = #{departmentId}
          AND sdu.is_active = 1
    </select>

    <select id="getUserByRoleAndOrg" resultType="java.lang.String">
        SELECT
        distinct user_id
        FROM
        sys_department_role sdr
        INNER JOIN sys_department_user sdu ON sdu.department_id = sdr.id
        WHERE
        department_role_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        AND org_id = #{bean.orgId}
        AND sdu.is_active = 1
        AND sdr.is_active = 1
    </select>
    <select id="getUserByOtherRoleAndOrg" resultType="java.lang.String">
        SELECT distinct user_id
        FROM sys_department_role sdr
                 INNER JOIN sys_department_user sdu ON sdu.department_id = sdr.id
        WHERE org_id != #{orgId}
          AND sdu.is_active = 1
          AND sdr.is_active = 1
    </select>
    <select id="getOrgEightRoleList" resultType="com.bonus.system.dept.entity.EightRoleBean">
        SELECT
        parentId,
        parentOrgName,
        GROUP_CONCAT(
        IF
        ( roleNum != 8, id, NULL )) AS lackOrgId,
        GROUP_CONCAT(
        IF
        ( roleNum != 8, org_name, NULL )) AS lackOrgName,
        GROUP_CONCAT(
        IF
        ( userNum = 1, id, NULL )) AS moreOrgId,
        GROUP_CONCAT(
        IF
        ( userNum = 1, org_name, NULL )) AS moreOrgName
        FROM
        (
        SELECT
        so2.id AS parentId,
        so2.org_name AS parentOrgName,
        so.id,
        so.org_name,
        ifNULL( mm.roleNum, 0 ) AS roleNum,
        ifNULL( nn.userNum, 0 ) AS userNum
        FROM
        sys_organization so
        LEFT JOIN sys_organization so2 ON so2.id = so.parent_id
        AND so2.is_active = '1'
        LEFT JOIN ( SELECT org_id, COUNT( department_role_id ) AS roleNum FROM sys_department_role sdr
            INNER JOIN sys_department_user sdu ON sdu.department_id = sdr.id
            WHERE sdr.is_active = 1 AND department_role_id != 9 GROUP BY org_id ) mm ON mm.org_id = so.id
        LEFT JOIN (
        SELECT
        org_id,
        1 AS userNum
        FROM
        (
        SELECT
        org_id,
        sdu.user_id,
        count( user_id ) AS userNum
        FROM
        sys_department_role sdr
        INNER JOIN sys_department_user sdu ON sdu.department_id = sdr.id
        WHERE
        sdu.is_active = 1
        AND sdr.is_active = 1
        AND department_role_id != 9
        GROUP BY
        org_id,
        user_id
        HAVING
        userNum > 1
        ) aa
        GROUP BY
        org_id
        ) nn ON nn.org_id = so.id
        WHERE
        so.is_department = 1
        AND so.is_active = '1'
        AND so.reviewer_status = '1'
        ) xx
        <where>
            <if test="parentOrgName != null and parentOrgName != '' ">
                and locate(#{parentOrgName},parentOrgName)
            </if>
        </where>
        GROUP BY
        parentId
    </select>
    <select id="getOrgEightRoleDetail" resultType="java.lang.String">
        select id from sys_organization where id in
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        <if test="parentOrgName != null and parentOrgName != '' ">
            and locate(#{parentOrgName},org_name)
        </if>
    </select>
    <select id="getDataDetail" resultType="com.bonus.system.dept.entity.ProDeptRoleBean">
        SELECT
        *
        FROM
        (
        SELECT
        so.id AS orgId,
        so.org_name,
        sdr.reviewer_status,
        sd.id AS deptRoleId,
        sd.`value` AS deptRoleName,
        IF(sdr.reviewer_status  = 1,GROUP_CONCAT( DISTINCT su.user_name ),null) AS userName
        FROM
        sys_organization so
        LEFT JOIN sys_dict sd ON sd.type = 'deptRole'
        AND sd.is_active = '1'
        LEFT JOIN sys_department_role sdr ON so.id = sdr.org_id
        AND sd.id = sdr.department_role_id
        AND sdr.is_active = '1'
        LEFT JOIN sys_user su2 ON su2.user_id = sdr.update_user_id
        AND su2.is_active = '1'
        LEFT JOIN sys_department_user sdu ON sdr.id = sdu.department_id
        AND sdu.is_active = '1'
        LEFT JOIN sys_user su ON su.user_id = sdu.user_id
        AND su.is_active = '1'
        WHERE
        so.id in
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
        AND so.is_active = '1'
        GROUP BY
        so.id,
        sd.id
        ) aa
    </select>
    <select id="getMoreOrgData" resultType="com.bonus.system.dept.entity.ProDeptRoleBean">
        SELECT sd.`value` AS deptRoleName,
               su.user_id,
               su.user_name
        FROM sys_department_role sdr
                 LEFT JOIN sys_dict sd ON sd.id = sdr.department_role_id
            AND sd.type = 'deptRole'
            AND sd.is_active = '1'
                 LEFT JOIN sys_department_user sdu ON sdr.id = sdu.department_id
            AND sdu.is_active = '1'
                 LEFT JOIN sys_user su ON su.user_id = sdu.user_id
            AND su.is_active = '1'
        WHERE sdr.department_role_id = #{deptRoleId}
          AND sdr.org_id = #{orgId}
          AND sdr.is_active = '1'
    </select>
    <select id="getMoreOrgDataByUserName" resultType="com.bonus.system.dept.entity.ProDeptRoleBean">
        SELECT
        sd.`value` AS deptRoleName,
        su.user_id,
        su.user_name
        FROM
        sys_department_role sdr
        LEFT JOIN sys_dict sd ON sd.id = sdr.department_role_id
        AND sd.type = 'deptRole'
        AND sd.is_active = '1'
        LEFT JOIN sys_department_user sdu ON sdr.id = sdu.department_id
        AND sdu.is_active = '1'
        LEFT JOIN sys_user su ON su.user_id = sdu.user_id
        AND su.is_active = '1'
        WHERE
        sdr.org_id = #{bean.orgId}
        and su.user_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.userId}
        </foreach>
        AND sdr.is_active = '1'
    </select>
    <select id="getInfoById" resultType="com.bonus.system.dept.entity.ProDeptRoleBean">
        SELECT so.id                                AS orgId,
               so.org_name,
               sdr.id                               AS departmentId,
               sd.id                                AS deptRoleId,
               sd.`value`                           AS deptRoleName,
               GROUP_CONCAT(DISTINCT su.user_name ) AS userName,
               GROUP_CONCAT(DISTINCT su.user_id )   AS userId
        FROM sys_organization so
                 LEFT JOIN sys_dict sd ON sd.type = 'deptRole'
            AND sd.is_active = '1'
                 LEFT JOIN sys_department_role sdr ON so.id = sdr.org_id
            AND sd.id = sdr.department_role_id
            AND sdr.is_active = '1'
                 LEFT JOIN sys_user su2 ON su2.user_id = sdr.update_user_id
            AND su2.is_active = '1'
                 LEFT JOIN sys_department_user sdu ON sdr.id = sdu.department_id
            AND sdu.is_active = '1'
                 LEFT JOIN sys_user su ON su.user_id = sdu.user_id
            AND su.is_active = '1'
        WHERE so.is_department = '1'
          AND so.is_active = '1'
          AND so.id = #{orgId}
          AND sd.id = #{deptRoleId}
    </select>
    <select id="getSubAllOrgList" resultType="java.lang.Long">
        SELECT o.id
        FROM sys_organization o
        JOIN (
        SELECT parent_id
        FROM sys_organization
        WHERE id in (
        <foreach collection="list" item="item" separator=",">
            #{item.id}
        </foreach>
        )
        ) p ON o.parent_id = p.parent_id;
    </select>
    <select id="getParentOrgById" resultType="java.lang.Long">
        SELECT
            @r AS orgId,
            ( SELECT `org_name` FROM sys_organization WHERE id = orgId AND is_active = '1' ) AS orgName,
            ( SELECT @r := parent_id FROM sys_organization WHERE id = orgId AND is_active = '1' ) AS parentId,
            @l := @l + 1 AS `level`
        FROM
            ( SELECT @r := #{id}, @l := 0 ) vars,
            sys_organization h
        WHERE
            @r != 0
          AND parent_id > 0
        ORDER BY
            `level` DESC
            limit 1
    </select>
    <select id="getOrgChildById" resultType="java.lang.String">
        SELECT getChildList(#{orgId})
    </select>

    <delete id="deletePersonAssignment">
        update sys_department_user set is_active = 0
        where department_id = #{departmentId} and user_id in
        <foreach collection="userIds" item="userId" separator="," open="(" close=")">
            #{userId}
        </foreach>
    </delete>

    <insert id="insertDeptRole" useGeneratedKeys="true" keyColumn="id" keyProperty="departmentId">
        insert into sys_department_role(org_id, department_role_id, create_user_id, update_user_id, reviewer_status)
        values (#{orgId}, #{deptRoleId}, #{createUserId}, #{updateUserId}, #{reviewerStatus})
    </insert>
    <insert id="insertPersonAssignment">
        INSERT INTO sys_department_user(department_id, user_id)
        VALUES
        <foreach collection="list" item="item" separator=",">
            (#{item.departmentId}, #{item.userId})
        </foreach>
    </insert>

    <update id="updateDeptRole">
        update sys_department_role
        set org_id             = #{orgId},
            department_role_id = #{deptRoleId},
            update_user_id     = #{updateUserId},
            reviewer_user_id   = null,
            reviewer_status    = 0,
            reviewer_time      = null
        where id = #{departmentId}
    </update>
    <update id="checkPersonAssignment">
        update sys_department_role
        set reviewer_user_id = #{reviewerUserId},
            reviewer_status  = #{reviewerStatus},
            reviewer_time    = #{reviewerTime}
        where id = #{departmentId}
    </update>
    <!--项目部管理->项目部角色管理->项目部角色一键审批-->
    <update id="batchRoleCheck">
        UPDATE sys_department_role
        SET reviewer_user_id = #{reviewerUserId},
            reviewer_status  = #{reviewerStatus},
            reviewer_time    = #{reviewerTime}
        WHERE id IN
        <foreach collection="ids" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </update>
</mapper>