<?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.xinelu.manage.mapper.hospitaldepartmentinfo.HospitalDepartmentInfoMapper">

    <resultMap type="com.xinelu.manage.domain.hospitaldepartmentinfo.HospitalDepartmentInfo" id="HospitalDepartmentInfoResult">
        <result property="id" column="id"/>
        <result property="parentId" column="parent_id"/>
        <result property="hospitalId" column="hospital_id"/>
        <result property="hospitalName" column="hospital_name"/>
        <result property="departmentCode" column="department_code"/>
        <result property="departmentName" column="department_name"/>
        <result property="departmentPhone" column="department_phone"/>
        <result property="departmentPerson" column="department_person"/>
        <result property="departmentAddress" column="department_address"/>
        <result property="departmentLevel" column="department_level"/>
        <result property="departmentSort" column="department_sort"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectHospitalDepartmentInfoVo">
        select d.id,
               d.parent_id,
               d.hospital_id,
               d.hospital_name,
               d.department_code,
               d.department_name,
               d.department_phone,
               d.department_person,
               d.department_address,
               d.department_level,
               d.department_sort,
               d.create_by,
               d.create_time,
               d.update_by,
               d.update_time
        from hospital_department_info d
    </sql>

    <select id="selectHospitalDepartmentInfoList" parameterType="com.xinelu.manage.domain.hospitaldepartmentinfo.HospitalDepartmentInfo"
	    resultType="com.xinelu.manage.domain.hospitaldepartmentinfo.HospitalDepartmentInfo">
        <include refid="selectHospitalDepartmentInfoVo"/>
        <where>
            <if test="parentId != null ">
                and d.parent_id = #{parentId}
            </if>
            <if test="hospitalId != null ">
                and d.hospital_id = #{hospitalId}
            </if>
            <if test="hospitalName != null  and hospitalName != ''">
                and d.hospital_name like concat('%', #{hospitalName}, '%')
            </if>
            <if test="departmentCode != null  and departmentCode != ''">
                and dd.epartment_code = #{departmentCode}
            </if>
            <if test="departmentName != null  and departmentName != ''">
                and d.department_name like concat('%', #{departmentName}, '%')
            </if>
            <if test="departmentPhone != null  and departmentPhone != ''">
                and d.department_phone = #{departmentPhone}
            </if>
            <if test="departmentPerson != null  and departmentPerson != ''">
                and d.department_person = #{departmentPerson}
            </if>
            <if test="departmentAddress != null  and departmentAddress != ''">
                and d.department_address = #{departmentAddress}
            </if>
            <if test="departmentLevel != null ">
                and d.department_level = #{departmentLevel}
            </if>
            <if test="departmentSort != null ">
                and d.department_sort = #{departmentSort}
            </if>
	        <!-- 数据范围过滤 -->
	        ${params.dataScope}
        </where>
        ORDER BY d.id DESC
    </select>

    <select id="selectHospitalDepartmentInfoById" parameterType="Long"
            resultMap="HospitalDepartmentInfoResult">
        <include refid="selectHospitalDepartmentInfoVo"/>
        where d.id = #{id}
    </select>

    <insert id="insertHospitalDepartmentInfo" parameterType="com.xinelu.manage.domain.hospitaldepartmentinfo.HospitalDepartmentInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into hospital_department_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,
            </if>
            <if test="hospitalId != null">hospital_id,
            </if>
            <if test="hospitalName != null">hospital_name,
            </if>
            <if test="departmentCode != null">department_code,
            </if>
            <if test="departmentName != null">department_name,
            </if>
            <if test="departmentPhone != null">department_phone,
            </if>
            <if test="departmentPerson != null">department_person,
            </if>
            <if test="departmentAddress != null">department_address,
            </if>
            <if test="departmentLevel != null">department_level,
            </if>
            <if test="departmentSort != null">department_sort,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="parentId != null">#{parentId},
            </if>
            <if test="hospitalId != null">#{hospitalId},
            </if>
            <if test="hospitalName != null">#{hospitalName},
            </if>
            <if test="departmentCode != null">#{departmentCode},
            </if>
            <if test="departmentName != null">#{departmentName},
            </if>
            <if test="departmentPhone != null">#{departmentPhone},
            </if>
            <if test="departmentPerson != null">#{departmentPerson},
            </if>
            <if test="departmentAddress != null">#{departmentAddress},
            </if>
            <if test="departmentLevel != null">#{departmentLevel},
            </if>
            <if test="departmentSort != null">#{departmentSort},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateHospitalDepartmentInfo" parameterType="com.xinelu.manage.domain.hospitaldepartmentinfo.HospitalDepartmentInfo">
        update hospital_department_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id =
                #{parentId},
            </if>
            <if test="hospitalId != null">hospital_id =
                #{hospitalId},
            </if>
            <if test="hospitalName != null">hospital_name =
                #{hospitalName},
            </if>
            <if test="departmentCode != null">department_code =
                #{departmentCode},
            </if>
            <if test="departmentName != null">department_name =
                #{departmentName},
            </if>
            <if test="departmentPhone != null">department_phone =
                #{departmentPhone},
            </if>
            <if test="departmentPerson != null">department_person =
                #{departmentPerson},
            </if>
            <if test="departmentAddress != null">department_address =
                #{departmentAddress},
            </if>
            <if test="departmentLevel != null">department_level =
                #{departmentLevel},
            </if>
            <if test="departmentSort != null">department_sort =
                #{departmentSort},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteHospitalDepartmentInfoById" parameterType="Long">
        delete
        from hospital_department_info
        where id = #{id}
    </delete>

    <delete id="deleteHospitalDepartmentInfoByIds" parameterType="String">
        delete from hospital_department_info where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <insert id="insertHospitalDepartmentList">
        insert into hospital_department_info(
        parent_id,
        hospital_id,
        hospital_name,
        department_code,
        department_name,
        department_phone,
        department_person,
        department_address,
        department_level,
        department_sort,
        create_by,
        create_time
        )values
        <foreach item="hospitalDepartmentInfoList" index="index" collection="list" separator=",">
            (
            #{hospitalDepartmentInfoList.parentId},
            #{hospitalDepartmentInfoList.hospitalId},
            #{hospitalDepartmentInfoList.hospitalName},
            #{hospitalDepartmentInfoList.departmentCode},
            #{hospitalDepartmentInfoList.departmentName},
            #{hospitalDepartmentInfoList.departmentPhone},
            #{hospitalDepartmentInfoList.departmentPerson},
            #{hospitalDepartmentInfoList.departmentAddress},
            #{hospitalDepartmentInfoList.departmentLevel},
            #{hospitalDepartmentInfoList.departmentSort},
            #{hospitalDepartmentInfoList.createBy},
            #{hospitalDepartmentInfoList.createTime}
            )
        </foreach>
    </insert>

    <select id="selectHospitalDepartmentCount" resultType="java.lang.Integer">
        select count(1)
        from hospital_department_info
        where hospital_id = #{hospitalId}
          and department_name = #{departmentName}
    </select>

    <select id="getDepartmentInfoList" resultType="com.xinelu.manage.domain.hospitaldepartmentinfo.HospitalDepartmentInfo">
        select id, hospital_id, hospital_name, department_code, department_name from hospital_department_info
        where hospital_id in
        <foreach item="hospitalId" collection="hospitalIdList" open="(" separator="," close=")">
            #{hospitalId}
        </foreach>
    </select>
</mapper>