<?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.ruoyi.system.mapper.TPatientMapper">

    <resultMap type="TPatient" id="TPatientResult">
        <result property="id"    column="id"    />
        <result property="openId"    column="open_id"    />
        <result property="identifier"    column="identifier"    />
        <result property="name"    column="name"    />
        <result property="phone"    column="phone"    />
        <result property="sex"    column="sex"    />
        <result property="age"    column="age"    />
        <result property="height"    column="height"    />
        <result property="weight"    column="weight"    />
        <result property="marriage"    column="marriage"    />
        <result property="disease"    column="disease"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectTPatientVo">
        select id, open_id, identifier, name, phone, sex, age, height, weight, marriage, disease, del_flag, create_time, update_time from t_patient
    </sql>

    <select id="selectTPatientList" parameterType="TPatient" resultMap="TPatientResult">
        <include refid="selectTPatientVo"/>
        <where>
            <if test="openId != null  and openId != ''"> and open_id = #{openId}</if>
            <if test="identifier != null  and identifier != ''"> and identifier = #{identifier}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="sex != null  and sex != ''"> and sex = #{sex}</if>
            <if test="age != null "> and age = #{age}</if>
            <if test="height != null "> and height = #{height}</if>
            <if test="weight != null "> and weight = #{weight}</if>
            <if test="marriage != null  and marriage != ''"> and marriage = #{marriage}</if>
            <if test="disease != null  and disease != ''"> and disease = #{disease}</if>
        </where>
    </select>

    <select id="selectTPatientById" parameterType="Long" resultMap="TPatientResult">
        <include refid="selectTPatientVo"/>
        where id = #{id}
    </select>

    <insert id="insertTPatient" parameterType="TPatient" useGeneratedKeys="true" keyProperty="id">
        insert into t_patient
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="openId != null">open_id,</if>
            <if test="identifier != null">identifier,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="phone != null">phone,</if>
            <if test="sex != null">sex,</if>
            <if test="age != null">age,</if>
            <if test="height != null">height,</if>
            <if test="weight != null">weight,</if>
            <if test="marriage != null">marriage,</if>
            <if test="disease != null">disease,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="openId != null">#{openId},</if>
            <if test="identifier != null">#{identifier},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="phone != null">#{phone},</if>
            <if test="sex != null">#{sex},</if>
            <if test="age != null">#{age},</if>
            <if test="height != null">#{height},</if>
            <if test="weight != null">#{weight},</if>
            <if test="marriage != null">#{marriage},</if>
            <if test="disease != null">#{disease},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateTPatient" parameterType="TPatient">
        update t_patient
        <trim prefix="SET" suffixOverrides=",">
            <if test="openId != null">open_id = #{openId},</if>
            <if test="identifier != null">identifier = #{identifier},</if>
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="age != null">age = #{age},</if>
            <if test="height != null">height = #{height},</if>
            <if test="weight != null">weight = #{weight},</if>
            <if test="marriage != null">marriage = #{marriage},</if>
            <if test="disease != null">disease = #{disease},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTPatientById" parameterType="Long">
        update t_patient set del_flag = '2' where id = #{id}
    </delete>

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

    <select id="queryTPatient" parameterType="com.ruoyi.system.domain.TPatient" resultMap="TPatientResult">
        <include refid="selectTPatientVo"/>
        <where>
            <if test="phone != null and phone != ''">
                phone = #{phone}
            </if>
        </where>
    </select>

    <select id="queryByPage" resultType="com.ruoyi.system.domain.resp.PcTPatientQueryByPageResp">
        select
        a.id,
        a.identifier,
        a.name,
        a.phone,
        a.sex,
        a.age,
        a.height,
        a.weight,
        a.marriage,
        a.disease,
        a.create_time as createTime,
        a.update_time as updateTime
        from t_patient a left join t_patient_hospital_doctor b on a.id = b.patient_id
        <where>
           a.del_flag = '0'
            <if test="startTime != null and endTime != null">
                AND a.create_time between DATE_FORMAT(#{startTime},'%Y-%m-%d %H:%i:%s') and DATE_FORMAT(#{endTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="identifier != null  and identifier != ''"> and a.identifier like concat('%', #{identifier}, '%')</if>
            <if test="name != null  and name != ''"> and a.name like concat('%', #{name}, '%')</if>

            <if test="hospitalId != null">
                and b.hospital_id = #{hospitalId}
            </if>
            <if test="doctorId != null">
                and b.doctor_d = #{doctorId}
            </if>
        </where>
        order by a.create_time desc
    </select>

    <select id="checkPhoneUnique" parameterType="String" resultType="int">
        select count(1) from t_patient where phone = #{phone} limit 1
    </select>
</mapper>