<?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.hzs.system.sys.mapper.SysDeptMapper">

    <resultMap type="com.hzs.common.domain.system.sys.SysDept" id="SysDeptResult">
        <id property="deptId" column="dept_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="deptCode" column="dept_code"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="pkCreator" column="PK_CREATOR"/>
        <result property="creationTime" column="CREATION_TIME"/>
        <result property="pkModified" column="PK_MODIFIED"/>
        <result property="modifiedTime" column="MODIFIED_TIME"/>
    </resultMap>

    <!-- 查询部门管理数据 -->
    <select id="selectDeptList" resultType="com.hzs.system.sys.vo.SysDeptQueryVO">
        select sd.*, su.user_count
        from sys_dept sd
        left join (select pk_dept, nvl(count(pk_dept), 0) user_count
        from sys_user su
        where su.del_flag = 0
        group by su.pk_dept) su
        on su.pk_dept = sd.dept_id
        where sd.del_flag = 0
        <if test="param.deptCode != null and param.deptCode != ''">
            AND sd.dept_code #{param.deptCode} || '%'
        </if>
        <if test="param.deptName != null and param.deptName != ''">
            AND sd.dept_name #{param.deptName} || '%'
        </if>
        <if test="param.status != null ">
            AND sd.status = #{param.status}
        </if>
        <if test="param.creationTime != null and param.creationTime.length > 1">
            and sd.creation_time >= #{param.creationTime[0]}
            and #{param.creationTime[1]} >= sd.creation_time
        </if>
        order by sd.creation_time desc
    </select>

    <!-- 根据部门ID查询信息 -->
    <select id="selectDeptById" parameterType="Long" resultMap="SysDeptResult">
        select *
        from sys_dept d
        where dept_id = #{deptId}
          and del_flag = 0
    </select>

    <!-- 查询部门是否存在用户 -->
    <select id="checkDeptExistUser" parameterType="Long" resultType="int">
        select count(1)
        from sys_user
        where pk_dept = #{deptId}
          and del_flag = 0
    </select>

    <!-- 校验部门名称是否唯一 -->
    <select id="checkDeptNameUnique" resultMap="SysDeptResult">
        select *
        from sys_dept d
        where dept_name = #{deptName}
          and del_flag = 0
          and rownum = 1
    </select>

    <!-- 新增部门信息 -->
    <insert id="insertDept" parameterType="SysDept">
        insert into sys_dept(
        <if test="deptName != null and deptName != ''">dept_name,</if>
        <if test="deptCode != null and deptCode != ''">dept_code,</if>
        <if test="status != null">status,</if>
        <if test="pkCreator != null">pk_Creator,</if>
        creation_time
        )values(
        <if test="deptName != null and deptName != ''">#{deptName},</if>
        <if test="deptCode != null and deptCode != ''">#{deptCode},</if>
        <if test="status != null">#{status},</if>
        <if test="pkCreator != null">#{pkCreator},</if>
        sysdate
        )
    </insert>

    <!-- 修改部门信息 -->
    <update id="updateDept" parameterType="SysDept">
        update sys_dept
        <set>
            <if test="deptName != null and deptName != ''">dept_name = #{deptName},</if>
            <if test="deptCode != null and deptCode != ''">dept_code = #{deptCode},</if>
            <if test="status != null ">status = #{status},</if>
            <if test="pkModified != null">PK_MODIFIED = #{pkModified},</if>
            MODIFIED_TIME = sysdate
        </set>
        where dept_id = #{deptId}
    </update>

    <!-- 删除部门管理信息 -->
    <delete id="deleteDeptById" parameterType="Long">
        update sys_dept
        set del_flag      = 1,
            pk_modified   = #{userId},
            modified_time = sysdate
        where dept_id = #{deptId}
    </delete>

    <!-- 获取最大部门编号 -->
    <select id="getMaxDeptCode" resultType="string">
        select max(dept_code)
        from SYS_DEPT
    </select>

    <!-- 查询重复部门 -->
    <select id="queryRepeatDept" resultType="long">
        select sd.dept_id
        from sys_user su
        left join sys_dept sd
        on sd.dept_id = su.pk_dept
        where su.del_flag = 0
        and sd.del_flag = 0
        and su.user_id in
        <foreach collection="userIdList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        group by sd.dept_id
        having count(sd.dept_id) > 1
    </select>

</mapper>
