<?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.hospitalpersoncertificate.HospitalPersonCertificateMapper">

    <resultMap type="com.xinelu.manage.domain.hospitalpersoncertificate.HospitalPersonCertificate" id="HospitalPersonCertificateResult">
        <result property="id" column="id"/>
        <result property="hospitalPersonId" column="hospital_person_id"/>
        <result property="certificateName" column="certificate_name"/>
        <result property="certificateCode" column="certificate_code"/>
        <result property="certificateUrl" column="certificate_url"/>
        <result property="certificateSort" column="certificate_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="selectHospitalPersonCertificateVo">
        select id,
               hospital_person_id,
               certificate_name,
               certificate_code,
               certificate_url,
               certificate_sort,
               create_by,
               create_time,
               update_by,
               update_time
        from hospital_person_certificate
    </sql>

    <select id="selectHospitalPersonCertificateList"
            parameterType="com.xinelu.manage.dto.hospitalpersoncertificate.HospitalPersonCertificateDTO"
            resultType="com.xinelu.manage.vo.hospitalpersoncertificate.HospitalPersonCertificateVO">
        SELECT
        hpc.id,
        hpi.person_name,
        hpc.hospital_person_id,
        hpc.certificate_name,
        hpc.certificate_code,
        hpc.certificate_url,
        hpc.certificate_sort,
        hpc.create_by,
        hpc.create_time,
        hpc.update_by,
        hpc.update_time
        FROM
        hospital_person_certificate hpc
        LEFT JOIN hospital_person_info hpi ON hpi.id = hpc.hospital_person_id
        <where>
            <if test="hospitalPersonId != null ">
                and hpc.hospital_person_id = #{hospitalPersonId}
            </if>
            <if test="certificateName != null  and certificateName != ''">
                and hpc.certificate_name like concat('%', #{certificateName}, '%')
            </if>
            <if test="certificateCode != null  and certificateCode != ''">
                and hpc.certificate_code = #{certificateCode}
            </if>
            <if test="certificateUrl != null  and certificateUrl != ''">
                and hpc.certificate_url = #{certificateUrl}
            </if>
            <if test="certificateSort != null ">
                and hpc.certificate_sort = #{certificateSort}
            </if>
        </where>
        GROUP BY
        hpc.hospital_person_id
        ORDER BY
        hpc.id
    </select>

    <select id="selectHospitalPersonCertificateById" parameterType="Long"
            resultType="com.xinelu.manage.vo.hospitalpersoncertificate.HospitalPersonCertificateVO">
        SELECT hpc.id,
               hpi.person_name,
               hpc.hospital_person_id,
               hpc.certificate_name,
               hpc.certificate_code,
               hpc.certificate_url,
               hpc.certificate_sort,
               hpc.create_by,
               hpc.create_time,
               hpc.update_by,
               hpc.update_time
        FROM hospital_person_certificate hpc
                 LEFT JOIN hospital_person_info hpi ON hpi.id = hpc.hospital_person_id
        where hpi.id = #{id}
    </select>

    <insert id="insertHospitalPersonCertificate" parameterType="com.xinelu.manage.domain.hospitalpersoncertificate.HospitalPersonCertificate" useGeneratedKeys="true"
            keyProperty="id">
        insert into hospital_person_certificate
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="hospitalPersonId != null">hospital_person_id,
            </if>
            <if test="certificateName != null">certificate_name,
            </if>
            <if test="certificateCode != null">certificate_code,
            </if>
            <if test="certificateUrl != null">certificate_url,
            </if>
            <if test="certificateSort != null">certificate_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="hospitalPersonId != null">#{hospitalPersonId},
            </if>
            <if test="certificateName != null">#{certificateName},
            </if>
            <if test="certificateCode != null">#{certificateCode},
            </if>
            <if test="certificateUrl != null">#{certificateUrl},
            </if>
            <if test="certificateSort != null">#{certificateSort},
            </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="updateHospitalPersonCertificate" parameterType="com.xinelu.manage.domain.hospitalpersoncertificate.HospitalPersonCertificate">
        update hospital_person_certificate
        <trim prefix="SET" suffixOverrides=",">
            <if test="hospitalPersonId != null">hospital_person_id =
                #{hospitalPersonId},
            </if>
            <if test="certificateName != null">certificate_name =
                #{certificateName},
            </if>
            <if test="certificateCode != null">certificate_code =
                #{certificateCode},
            </if>
            <if test="certificateUrl != null">certificate_url =
                #{certificateUrl},
            </if>
            <if test="certificateSort != null">certificate_sort =
                #{certificateSort},
            </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="deleteHospitalPersonCertificateById" parameterType="Long">
        delete
        from hospital_person_certificate
        where hospital_person_id = #{id}
    </delete>

    <delete id="deleteHospitalPersonCertificateByIds">
        delete from hospital_person_certificate where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <insert id="insertHospitalPersonCertificateList" parameterType="java.util.List">
        insert into hospital_person_certificate(
        hospital_person_id,
        certificate_name,
        certificate_code,
        certificate_url,
        certificate_sort,
        create_by,
        create_time,
        update_by,
        update_time
        ) values
        <foreach item="HospitalPersonCertificate" index="index" collection="list" separator=",">
            (
            #{HospitalPersonCertificate.hospitalPersonId},
            #{HospitalPersonCertificate.certificateName},
            #{HospitalPersonCertificate.certificateCode},
            #{HospitalPersonCertificate.certificateUrl},
            #{HospitalPersonCertificate.certificateSort},
            #{HospitalPersonCertificate.createBy},
            #{HospitalPersonCertificate.createTime},
            #{HospitalPersonCertificate.updateBy},
            #{HospitalPersonCertificate.updateTime}
            )
        </foreach>
    </insert>


    <select id="selectHospitalPersonCertificateByIds" parameterType="Long"
            resultType="com.xinelu.manage.domain.hospitalpersoncertificate.HospitalPersonCertificate">
        select id,
        hospital_person_id,
        certificate_name,
        certificate_code,
        certificate_url,
        certificate_sort,
        create_by,
        create_time,
        update_by,
        update_time
        from hospital_person_certificate
        <where>
            <if test="hospitalPersonIdsList != null  and hospitalPersonIdsList.size()>0 ">
                and hospital_person_id in
                <foreach item="hospitalPersonIds" collection="hospitalPersonIdsList" open="(" separator=","
                         close=")">
                    #{hospitalPersonIds}
                </foreach>
            </if>
        </where>
    </select>

    <delete id="deletePersonCertificateByPersonIds">
        delete from hospital_person_certificate where hospital_person_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
