<?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.example.his.api.db.dao.DeptDao">

    <resultMap id="BaseResultMap" type="com.example.his.api.db.pojo.DeptEntity">
            <id property="id" column="id" jdbcType="INTEGER"/>
            <result property="deptName" column="dept_name" jdbcType="VARCHAR"/>
            <result property="tel" column="tel" jdbcType="VARCHAR"/>
            <result property="email" column="email" jdbcType="VARCHAR"/>
            <result property="desc" column="desc" jdbcType="VARCHAR"/>
    </resultMap>



    <!--  部门列表查询全部  -->
    <select id="searchAllDept" resultMap="BaseResultMap">
        select * from tb_dept
    </select>

    <!--  部门列表分页查询  -->
    <select id="searchDept"  resultType="HashMap">
        select
        d.id,
        d.dept_name as deptName,
        d.tel,
        d.email,
        d.desc,
        COUNT(u.id) AS emps from tb_dept d left join tb_user u on u.dept_id = d.id
        <where>
            <if test="searchKeyWord != null and searchKeyWord != ''">
                1 = 1
                AND d.dept_name like "%${searchKeyWord}%"
                OR d.tel = #{searchKeyWord}
                OR d.email = #{searchKeyWord}
                OR d.desc like "%${searchKeyWord}%"
            </if>
        </where>
        GROUP BY d.id
    </select>

    <!--  部门列表查询id  -->
    <select id="searchDeptById"  resultType="HashMap">
        select * from tb_dept where id = #{id}
    </select>

    <!--  部门列表新增  -->
    <insert id="insertDept">
        insert into tb_dept values(#{id},#{deptName},#{tel},#{email},#{desc})
    </insert>

    <!--  部门列表更新  -->
    <update id="updateDept">
        update tb_dept set
        `dept_name` = #{deptName},
        `tel` = #{tel},
        `email` = #{email},
        `desc` = #{desc}
        where `id` = #{id}
    </update>

    <!--  部门列表删除  -->
    <delete id="deleteDept">
        delete from tb_dept where id = #{id}
    </delete>

    <!--  部门列表批量删除  -->
    <delete id="deleteBatchDept">
        delete from tb_dept where id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
