<?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.fs.store.mapper.FsPatientMapper">
    
    <resultMap type="FsPatient" id="FsPatientResult">
        <result property="patientId"    column="patient_id"    />
        <result property="patientName"    column="patient_name"    />
        <result property="userId"    column="user_id"    />
        <result property="idCard"    column="id_card"    />
        <result property="birthday"    column="birthday"    />
        <result property="gender"    column="gender"    />
        <result property="status"    column="status"    />
        <result property="isDel"    column="is_del"    />
        <result property="patientRelation"    column="patient_relation"    />
    </resultMap>

    <sql id="selectFsPatientVo">
        select patient_id, patient_name, user_id, id_card, birthday, gender, status, is_del, patient_relation from fs_patient
    </sql>

    <select id="selectFsPatientList" parameterType="FsPatient" resultMap="FsPatientResult">
        <include refid="selectFsPatientVo"/>
        <where>  
            <if test="patientName != null  and patientName != ''"> and patient_name like concat('%', #{patientName}, '%')</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="idCard != null  and idCard != ''"> and id_card = #{idCard}</if>
            <if test="birthday != null "> and birthday = #{birthday}</if>
            <if test="gender != null "> and gender = #{gender}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
            <if test="patientRelation != null "> and patient_relation = #{patientRelation}</if>
        </where>
        order by patient_id desc
    </select>
    
    <select id="selectFsPatientById" parameterType="Long" resultMap="FsPatientResult">
        <include refid="selectFsPatientVo"/>
        where patient_id = #{patientId}
    </select>
        
    <insert id="insertFsPatient" parameterType="FsPatient" useGeneratedKeys="true" keyProperty="patientId">
        insert into fs_patient
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="patientName != null">patient_name,</if>
            <if test="userId != null">user_id,</if>
            <if test="idCard != null">id_card,</if>
            <if test="birthday != null">birthday,</if>
            <if test="gender != null">gender,</if>
            <if test="status != null">status,</if>
            <if test="isDel != null">is_del,</if>
            <if test="patientRelation != null">patient_relation,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="patientName != null">#{patientName},</if>
            <if test="userId != null">#{userId},</if>
            <if test="idCard != null">#{idCard},</if>
            <if test="birthday != null">#{birthday},</if>
            <if test="gender != null">#{gender},</if>
            <if test="status != null">#{status},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="patientRelation != null">#{patientRelation},</if>
         </trim>
    </insert>

    <update id="updateFsPatient" parameterType="FsPatient">
        update fs_patient
        <trim prefix="SET" suffixOverrides=",">
            <if test="patientName != null">patient_name = #{patientName},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="idCard != null">id_card = #{idCard},</if>
            <if test="birthday != null">birthday = #{birthday},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="patientRelation != null">patient_relation = #{patientRelation},</if>
        </trim>
        where patient_id = #{patientId}
    </update>

    <update id="deleteFsPatientById" parameterType="Long">
        update fs_patient set is_del=1 where patient_id = #{patientId}
    </update>

    <update id="deleteFsPatientByIds" parameterType="String">
        update fs_patient set is_del=1 where patient_id in
        <foreach item="patientId" collection="array" open="(" separator="," close=")">
            #{patientId}
        </foreach>
    </update>
    
</mapper>