<?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.BusPatientMapper">

    <resultMap type="BusPatient" id="BusPatientResult">
        <result property="id"    column="id"    />
        <result property="code"    column="code"    />
        <result property="name"    column="name"    />
        <result property="gender"    column="gender"    />
        <result property="height"    column="height"    />
        <result property="age"    column="age"    />
        <result property="weight"    column="weight"    />
        <result property="source"    column="source"    />
        <result property="address"    column="address"    />
        <result property="createTime"    column="create_time"    />
        <result property="phone"    column="phone"    />
        <result property="relationName"    column="relation_name"    />
        <result property="relationPhone"    column="relation_phone"    />
        <result property="dieTime"    column="die_time"    />
        <result property="isDelete"    column="is_delete"    />
        <result property="status"    column="status"    />
    </resultMap>

    <sql id="selectBusPatientVo">
        select id, code, name, gender, height, age, weight, source, address, create_time, phone, relation_name, relation_phone, die_time, is_delete, status from bus_patient
    </sql>

    <select id="selectBusPatientList" parameterType="BusPatient" resultMap="BusPatientResult">
        <include refid="selectBusPatientVo"/>
        <where>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="gender != null "> and gender = #{gender}</if>
            <if test="height != null "> and height = #{height}</if>
            <if test="age != null "> and age = #{age}</if>
            <if test="weight != null "> and weight = #{weight}</if>
            <if test="source != null "> and source = #{source}</if>
            <if test="address != null  and address != ''"> and address = #{address}</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="relationName != null  and relationName != ''"> and relation_name like concat('%', #{relationName}, '%')</if>
            <if test="relationPhone != null  and relationPhone != ''"> and relation_phone = #{relationPhone}</if>
            <if test="dieTime != null "> and die_time = #{dieTime}</if>
            <if test="isDelete != null "> and is_delete = #{isDelete}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </select>

    <select id="selectBusPatientById" parameterType="Long" resultMap="BusPatientResult">
        <include refid="selectBusPatientVo"/>
        where id = #{id}
    </select>

    <insert id="insertBusPatient" parameterType="BusPatient" useGeneratedKeys="true" keyProperty="id">
        insert into bus_patient
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="name != null">name,</if>
            <if test="gender != null">gender,</if>
            <if test="height != null">height,</if>
            <if test="age != null">age,</if>
            <if test="weight != null">weight,</if>
            <if test="source != null">source,</if>
            <if test="address != null">address,</if>
            <if test="createTime != null">create_time,</if>
            <if test="phone != null">phone,</if>
            <if test="relationName != null">relation_name,</if>
            <if test="relationPhone != null">relation_phone,</if>
            <if test="dieTime != null">die_time,</if>
            <if test="isDelete != null">is_delete,</if>
            <if test="status != null">status,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="name != null">#{name},</if>
            <if test="gender != null">#{gender},</if>
            <if test="height != null">#{height},</if>
            <if test="age != null">#{age},</if>
            <if test="weight != null">#{weight},</if>
            <if test="source != null">#{source},</if>
            <if test="address != null">#{address},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="phone != null">#{phone},</if>
            <if test="relationName != null">#{relationName},</if>
            <if test="relationPhone != null">#{relationPhone},</if>
            <if test="dieTime != null">#{dieTime},</if>
            <if test="isDelete != null">#{isDelete},</if>
            <if test="status != null">#{status},</if>
        </trim>
    </insert>

    <update id="updateBusPatient" parameterType="BusPatient">
        update bus_patient
        <trim prefix="SET" suffixOverrides=",">
            <if test="code != null">code = #{code},</if>
            <if test="name != null">name = #{name},</if>
            <if test="gender != null">gender = #{gender},</if>
            <if test="height != null">height = #{height},</if>
            <if test="age != null">age = #{age},</if>
            <if test="weight != null">weight = #{weight},</if>
            <if test="source != null">source = #{source},</if>
            <if test="address != null">address = #{address},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="relationName != null">relation_name = #{relationName},</if>
            <if test="relationPhone != null">relation_phone = #{relationPhone},</if>
            <if test="dieTime != null">die_time = #{dieTime},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBusPatientById" parameterType="Long">
        delete from bus_patient where id = #{id}
    </delete>

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

    <update id="updateStatusByIds">
        update bus_device_history
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null">status = #{status},</if>
        </trim>
        where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>