<?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.hospitalinfo.HospitalInfoMapper">

    <resultMap type="com.xinelu.manage.domain.hospitalinfo.HospitalInfo" id="HospitalInfoResult">
        <result property="id" column="id"/>
        <result property="hospitalName" column="hospital_name"/>
        <result property="hospitalCode" column="hospital_code"/>
        <result property="hospitalAddress" column="hospital_address"/>
        <result property="phone" column="phone"/>
        <result property="hospitalIntroduce" column="hospital_introduce"/>
        <result property="hospitalSort" column="hospital_sort"/>
        <result property="orgCode" column="org_code"/>
	    <result property="category" column="category"/>
	    <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="selectHospitalInfoVo">
        select id,
               hospital_name,
               hospital_code,
               hospital_address,
               phone,
               hospital_introduce,
               hospital_sort,
               org_code,
               category,
               create_by,
               create_time,
               update_by,
               update_time
        from hospital_info
    </sql>

    <select id="selectHospitalInfoList" parameterType="com.xinelu.manage.domain.hospitalinfo.HospitalInfo" resultMap="HospitalInfoResult">
        <include refid="selectHospitalInfoVo"/>
        <where>
	        <if test="id != null">
		        and id = #{id}
	        </if>
            <if test="hospitalName != null  and hospitalName != ''">
                and hospital_name like concat('%', #{hospitalName}, '%')
            </if>
            <if test="hospitalCode != null  and hospitalCode != ''">
                and hospital_code = #{hospitalCode}
            </if>
            <if test="hospitalAddress != null  and hospitalAddress != ''">
                and hospital_address = #{hospitalAddress}
            </if>
            <if test="phone != null  and phone != ''">
                and phone = #{phone}
            </if>
            <if test="hospitalIntroduce != null  and hospitalIntroduce != ''">
                and hospital_introduce = #{hospitalIntroduce}
            </if>
            <if test="hospitalSort != null">
                and hospital_sort = #{hospitalSort}
            </if>
	        <if test="category != null and category != ''">
		        and category = #{category}
	        </if>
        </where>
        ORDER BY id DESC
    </select>

    <select id="selectHospitalInfoById" parameterType="Long"
            resultMap="HospitalInfoResult">
        <include refid="selectHospitalInfoVo"/>
        where id = #{id}
    </select>

    <insert id="insertHospitalInfo" parameterType="com.xinelu.manage.domain.hospitalinfo.HospitalInfo" useGeneratedKeys="true"
            keyProperty="id">
        insert into hospital_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="hospitalName != null">hospital_name,
            </if>
            <if test="hospitalCode != null">hospital_code,
            </if>
            <if test="hospitalAddress != null">hospital_address,
            </if>
            <if test="phone != null">phone,
            </if>
            <if test="hospitalIntroduce != null">hospital_introduce,
            </if>
            <if test="hospitalSort != null">hospital_sort,
            </if>
	        <if test="orgCode != null">
		        org_code,
	        </if>
	        <if test="category != null">
		        category,
	        </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="hospitalName != null">#{hospitalName},
            </if>
            <if test="hospitalCode != null">#{hospitalCode},
            </if>
            <if test="hospitalAddress != null">#{hospitalAddress},
            </if>
            <if test="phone != null">#{phone},
            </if>
            <if test="hospitalIntroduce != null">#{hospitalIntroduce},
            </if>
            <if test="hospitalSort != null">#{hospitalSort},
            </if>
	        <if test="orgCode != null">
		        #{orgCode},
	        </if>
	        <if test="category != null">
		        #{category},
	        </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="updateHospitalInfo" parameterType="com.xinelu.manage.domain.hospitalinfo.HospitalInfo">
        update hospital_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="hospitalName != null">hospital_name =
                #{hospitalName},
            </if>
            <if test="hospitalCode != null">hospital_code =
                #{hospitalCode},
            </if>
            <if test="hospitalAddress != null">hospital_address =
                #{hospitalAddress},
            </if>
            <if test="phone != null">phone =
                #{phone},
            </if>
            <if test="hospitalIntroduce != null">hospital_introduce =
                #{hospitalIntroduce},
            </if>
            hospital_sort = #{hospitalSort},
	        <if test="orgCode != null">
		        org_code = #{orgCode},
	        </if>
	        <if test="category != null">
		        category = #{category},
	        </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="deleteHospitalInfoById" parameterType="Long">
        delete
        from hospital_info
        where id = #{id}
    </delete>

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

    <select id="selectHospitalNameCount" resultType="java.lang.Integer">
        select count(1)
        from hospital_info
        where hospital_name = #{hospitalName}
    </select>

    <select id="selectHospitalList" resultType="com.xinelu.manage.domain.hospitalinfo.HospitalInfo">
        <include refid="selectHospitalInfoVo"/>
        where id in
        <foreach item="ids" collection="array" open="(" separator="," close=")">
            #{ids}
        </foreach>
    </select>

    <!-- 根据家医机构编码查询医院 -->
    <select id="getHosptalByOrgCode" resultType="com.xinelu.manage.domain.hospitalinfo.HospitalInfo">
        <include refid="selectHospitalInfoVo"/>
        where org_code = #{orgCode} limit 1
    </select>
</mapper>
