<?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.SubOrgDao">

    <select id="selectOrgList" resultType="com.bonus.system.dept.entity.SubOrgBean">
        select * from (
        SELECT
        so2.org_name as parentOrgName,
        so.id,
        so.org_name,
        GROUP_CONCAT(u.user_id) as orgHeadUserId, GROUP_CONCAT(u.user_name) as orgHeadUserName,
        a.user_id as attendanceUserId, a.user_name as attendanceUserName,
        su.user_name as applyName,
        so.create_time,
        so.reviewer_status
        FROM
        sys_organization so
        LEFT JOIN sys_organization so2 ON so2.id = so.parent_id and so2.is_active = '1'
        LEFT JOIN sys_organization_head h on h.org_id = so.id and h.is_active = 1
        left join sys_user u on u.user_id = h.user_id
        left join sys_user a on a.user_id = so.attendance_user_id
        LEFT JOIN sys_user su on su.user_id = so.create_user_id
        WHERE
        so.is_department = 1
        AND so.is_active = '1'
        GROUP BY so.id
        ) a
        <where>
            <if test="orgName != null and orgName != ''">
                AND a.org_name like concat('%', #{orgName}, '%')
            </if>
            <if test="orgHeadUserName != null and orgHeadUserName != ''">
                AND a.orgHeadUserName like concat('%', #{orgHeadUserName}, '%')
            </if>
            <if test="reviewerStatus != null and reviewerStatus != ''">
                AND a.reviewer_status = #{reviewerStatus}
            </if>
            <if test="applyName != null and applyName != ''">
                AND locate(#{applyName},a.applyName)
            </if>
            <if test='orgList != null and orgList.size() > 0'>
                and a.id in (
                <foreach collection="orgList" item="item" separator=",">
                    #{item}
                </foreach>
                )
            </if>
        </where>
        ORDER BY a.create_time DESC
    </select>

    <select id="selectOrgById" resultType="com.bonus.system.dept.entity.SubOrgBean">
        select * from (
                          select o.*, GROUP_CONCAT(u.user_id) as orgHeadUserId, GROUP_CONCAT(u.user_name) as orgHeadUserName,
                                 a.user_id as attendanceUserId, a.user_name as attendanceUserName
                          from sys_organization o
                                   left join sys_organization_head h on h.org_id = o.id and h.is_active = 1
                                   left join sys_user u on u.user_id = h.user_id
                                   left join sys_user a on a.user_id = o.attendance_user_id
                          where o.is_active = 1
                          GROUP BY o.id ) a
        where a.id = #{orgId}
    </select>

    <select id="checkOrgNameUnique" resultType="com.bonus.system.dept.entity.SubOrgBean">
        select * from sys_organization
        where org_name = #{orgName} and is_active = 1 and parent_id = #{parentId} limit 1
    </select>

    <select id="hasChildByOrgId" resultType="java.lang.Integer">
        select count(1) from sys_organization where parent_id = #{orgId} and is_active = 1
    </select>

    <select id="checkOrgExistUser" resultType="java.lang.Integer">
        select count(1) from sys_user_org where org_id = #{orgId} and is_active = 1
    </select>

    <select id="orgPersonSelect" resultType="com.bonus.system.basic.domain.SysTree">
        select id, org_name as name, parent_id, true as disabled from sys_organization where is_active = 1
        union
        select CONCAT(su.user_id,'|', suo.org_id) as id, user_name as name,
               suo.org_id as parnet_id, false as disabled from sys_user su
        left join sys_user_org suo on suo.user_id = su.user_id and suo.is_active = 1
        where su.is_active = 1
    </select>
    <select id="getDeptRoleList" resultType="com.bonus.system.dept.entity.ProDeptRoleBean">
        SELECT
            sd.id AS deptRoleId,
            sdr.id as departmentId,
            GROUP_CONCAT( DISTINCT su.user_name ) AS `userName`,
            GROUP_CONCAT( DISTINCT su.user_id ) AS userId
        FROM
            sys_dict sd
                LEFT JOIN sys_department_role sdr ON sd.id = sdr.department_role_id
                AND sdr.org_id = #{orgId}
                AND sdr.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
            sd.type = 'deptRole'
          AND sd.is_active = '1'
        GROUP BY
            sd.id
    </select>
    <select id="getDepartmentIdByOrgId" resultType="java.lang.Integer">
        SELECT
            id
        FROM
            sys_department_role
        WHERE
            org_id = #{orgId}
    </select>
    <select id="getDeptRoleByOrgId" resultType="com.bonus.system.dept.entity.ProDeptRoleBean">
        SELECT
            id as departmentId,
            department_role_id as deptRoleId
        FROM
            sys_department_role
        WHERE
            org_id = #{orgId}
    </select>
    <select id="getRoleListByOrgId" resultType="com.bonus.system.dept.entity.ProDeptRoleDo">
        SELECT
            id as departmentId
        FROM
            sys_department_role
        WHERE
            org_id = #{orgId} And is_active= '1'
    </select>

    <insert id="insertOrg" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into sys_organization(org_name, attendance_user_id, parent_id, is_province, is_department,
                                     province, address, lon, lat, reviewer_status, create_user_id)
        values (#{orgName}, #{attendanceUserId}, #{parentId}, #{isProvince}, #{isDepartment}, #{province},
                #{address}, #{lon}, #{lat}, #{reviewerStatus}, #{createUserId})
    </insert>

    <update id="updateOrg">
        update sys_organization set
        <if test="orgName != null and orgName != ''">
            org_name = #{orgName},
        </if>
         attendance_user_id = #{attendanceUserId},
        parent_id = #{parentId}, is_province = #{isProvince},
        is_department = #{isDepartment}, province = #{province}, address = #{address}, lon = #{lon},
        lat = #{lat}, reviewer_status = #{reviewerStatus},update_user_id = #{updateUserId}
        where id = #{id}
    </update>
    <update id="updateOrgStatus">
        update sys_organization set reviewer_status = #{reviewerStatus}, update_user_id = #{updateUserId}
        where id = #{id}
    </update>

    <delete id="deleteOrgById">
        update sys_organization set is_active = 0, update_user_id = #{updateUserId} where id = #{orgId}
    </delete>
    <delete id="deleteDeptRoleUser">
        update sys_department_user set is_active = 0 where department_id in
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="deleteDeptRole">
        update sys_department_role set is_active = 0 where id in
        <foreach collection="list" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <delete id="delRoleUserByUserId">
        update sys_department_user set is_active = 0 where user_id = #{userId}
    </delete>
</mapper>