<?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.saye.hospitalgd.mapper.system.DepartMapper">

    <resultMap id="BaseResultMap" type="com.saye.hospitalgd.model.Departs">
        <id column="depart_id" property="departId" jdbcType="VARCHAR"/>
        <result column="depart_name" property="departName" jdbcType="VARCHAR"/>
        <result column="parent_id" property="parentId" jdbcType="VARCHAR"/>
        <result column="leaf" property="leaf" jdbcType="VARCHAR"/>
        <result column="isactive" property="isactive" jdbcType="VARCHAR"/>
        <result column="modify_user_name" property="modifyUserName" jdbcType="VARCHAR"/>
        <result column="modify_true_name" property="modifyTrueName" jdbcType="VARCHAR"/>
        <result column="create_time" property="createTime" jdbcType="VARCHAR"/>
        <result column="create_date" property="createDate" jdbcType="VARCHAR"/>
        <result column="modify_time" property="modifyTime" jdbcType="VARCHAR"/>
    </resultMap>


    <select id="findDepartTreeNodeList" resultMap="BaseResultMap">
        select d.depart_id,
               d.depart_name,
               d.parent_id,
               case d.leaf when '0' then 'true' else 'false' end as leaf,
               d.modify_user_name,
               d.create_time
        from departs d
        where d.isactive = '1'
          and d.parent_id = #{parentId}
    </select>

    <select id="selectDepartTreeList" resultMap="BaseResultMap">
        select d.depart_id,
               d.depart_name,
               d.parent_id,
               case d.leaf when '0' then 'true' else 'false' end as leaf,
               d.modify_user_name,
               d.create_time
        from departs d
        where d.isactive = '1'
    </select>

    <select id="selectAllDeparts" resultType="HashMap">
        select d.depart_id,
               d.depart_name,
               d.parent_id,
               d2.DEPART_NAME                                    as parent_name,
               case d.leaf when '0' then 'true' else 'false' end as leaf,
               d.modify_user_name,
               d.create_time
        from departs d
                 left join departs d2 on d.parent_id = d2.DEPART_ID
        where d.isactive = '1'
    </select>

    <select id="selectDepartTreeListByParentId" resultType="HashMap">
        select d.depart_id,
               d.depart_name,
               d.parent_id,
               d.isactive,
               d.modify_user_name,
               d.modify_true_name,
               d.create_time,
               d.create_date,
               d.modify_time
        from departs d
        where d.isactive = '1'
        order by d.DEPART_ID
    </select>

    <insert id="addDepart">
        insert into departs(DEPART_ID, DEPART_NAME, PARENT_ID, LEAF,
                            ISACTIVE, MODIFY_USER_NAME, MODIFY_TRUE_NAME, CREATE_TIME, CREATE_DATE, modify_time)
        values (#{departId}, #{departName}, #{parentId}, '1', '1', #{modifyUserName}, #{modifyTrueName}, #{createTime},
                #{createDate}, #{modifyTime})
    </insert>

    <update id="modifyDepart">
        UPDATE departs
        <set>
            <if test="departName!=null and departName!=''">
                depart_name = #{departName},
            </if>
            <if test="modifyUserName!=null and modifyUserName!=''">
                MODIFY_USER_NAME = #{modifyUserName},
            </if>
            <if test="modifyTrueName!=null and modifyTrueName!=''">
                MODIFY_TRUE_NAME = #{modifyTrueName},
            </if>
            <if test="createTime!=null and createTime!=''">
                CREATE_TIME =#{createTime},
            </if>
            <if test="createDate!=null and createDate!=''">
                CREATE_DATE =#{createDate},
            </if>
            <if test="modifyTime!=null and modifyTime!=''">
                modify_time =#{modifyTime},
            </if>
        </set>
        where depart_id =#{departId}
    </update>

    <update id="removeDepart">
        UPDATE departs
        <set>
            ISACTIVE = '0'
            <if test="modifyUserName!=null and modifyUserName!=''">
                , MODIFY_USER_NAME = #{modifyUserName}
            </if>
            <if test="modifyTrueName!=null and modifyTrueName!=''">
                , MODIFY_TRUE_NAME = #{modifyTrueName}
            </if>
            <if test="createTime!=null and createTime!=''">
                , CREATE_TIME =#{createTime}
            </if>
            <if test="createDate!=null and createDate!=''">
                , CREATE_DATE =#{createDate}
            </if>
            <if test="modifyTime!=null and modifyTime!=''">
                ,modify_time =#{modifyTime}
            </if>
        </set>
        WHERE DEPART_ID = #{departId}
    </update>

    <!-- 根据部门名称查询部门 -->
    <select id="selectDepartBydepartName" resultMap="BaseResultMap">
        select d.depart_id,
               d.depart_name,
               d.parent_id,
               case d.leaf when '0' then 'true' else 'false' end as leaf,
               d.modify_user_name,
               create_time
        from departs d
        where d.depart_name = #{departName}
    </select>
    <!-- 根据父级id查询子集 -->
    <select id="findDepartByParentId" parameterType="HashMap" resultType="HashMap">
        select d.depart_id,
               d.depart_name,
               d.parent_id,
               case d.leaf when '0' then 'true' else 'false' end as leaf,
               d.modify_user_name,
               create_time
        from departs d
        where d.parent_id = #{parent_id}
    </select>

    <select id="findDepartById" parameterType="java.util.HashSet" resultType="HashMap">
        select d.depart_id, d.depart_name, d.parent_id,modify_user_name, create_time
        from departs d where 1=1
        <choose>
            <when test="set != null and set.size()>0">
                and d.depart_id in
                <foreach collection="set" item="item" separator="," open="(" close=")" index="">
                    #{item}
                </foreach>
            </when>
            <otherwise>
                and d.depart_id in ('')
            </otherwise>
        </choose>
    </select>
</mapper>