<?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.projectdevice.ProjectDeviceMapper">

    <resultMap type="ProjectDevice" id="ProjectDeviceResult">
            <result property="id" column="id"/>
            <result property="patientId" column="patient_id"/>
            <result property="patientName" column="patient_name"/>
            <result property="cardNo" column="card_no"/>
            <result property="deviceType" column="device_type"/>
            <result property="deviceName" column="device_name"/>
            <result property="deviceCode" column="device_code"/>
            <result property="deviceStatus" column="device_status"/>
            <result property="deviceRemark" column="device_remark"/>
            <result property="deviceBindTime" column="device_bind_time"/>
            <result property="deviceUnbindTime" column="device_unbind_time"/>
            <result property="deviceIp" column="device_ip"/>
            <result property="devicePort" column="device_port"/>
            <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="selectProjectDeviceVo">
        select id, patient_id, patient_name, card_no, device_type, device_name, device_code, device_status, device_remark, device_bind_time, device_unbind_time, device_ip, device_port, create_by, create_time, update_by, update_time from project_device
    </sql>

    <select id="selectProjectDeviceList" parameterType="ProjectDevice" resultMap="ProjectDeviceResult">
        <include refid="selectProjectDeviceVo"/>
        <where>
	        <if test="patientId != null">
		        and patient_id = #{patientId}
	        </if>
            <if test="patientName != null  and patientName != ''">
                and patient_name like concat('%', #{patientName}, '%')
            </if>
            <if test="cardNo != null  and cardNo != ''">
                and card_no = #{cardNo}
            </if>
            <if test="deviceType != null  and deviceType != ''">
                and device_type = #{deviceType}
            </if>
            <if test="deviceName != null  and deviceName != ''">
                and device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="deviceCode != null  and deviceCode != ''">
                and device_code = #{deviceCode}
            </if>
            <if test="deviceStatus != null  and deviceStatus != ''">
                and device_status = #{deviceStatus}
            </if>
            <if test="deviceRemark != null  and deviceRemark != ''">
                and device_remark = #{deviceRemark}
            </if>
            <if test="deviceBindTime != null ">
                and device_bind_time = #{deviceBindTime}
            </if>
            <if test="deviceUnbindTime != null ">
                and device_unbind_time = #{deviceUnbindTime}
            </if>
            <if test="deviceIp != null  and deviceIp != ''">
                and device_ip = #{deviceIp}
            </if>
            <if test="devicePort != null ">
                and device_port = #{devicePort}
            </if>
        </where>
    </select>

    <select id="selectProjectDeviceById" parameterType="Long"
            resultMap="ProjectDeviceResult">
            <include refid="selectProjectDeviceVo"/>
            where id = #{id}
    </select>

    <insert id="insertProjectDevice" parameterType="ProjectDevice" useGeneratedKeys="true"
            keyProperty="id">
        insert into project_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="patientId != null">patient_id,
            </if>
            <if test="patientName != null">patient_name,
            </if>
            <if test="cardNo != null">card_no,
            </if>
            <if test="deviceType != null">device_type,
            </if>
            <if test="deviceName != null">device_name,
            </if>
            <if test="deviceCode != null">device_code,
            </if>
            <if test="deviceStatus != null">device_status,
            </if>
            <if test="deviceRemark != null">device_remark,
            </if>
            <if test="deviceBindTime != null">device_bind_time,
            </if>
            <if test="deviceUnbindTime != null">device_unbind_time,
            </if>
            <if test="deviceIp != null">device_ip,
            </if>
            <if test="devicePort != null">device_port,
            </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="patientId != null">#{patientId},
            </if>
            <if test="patientName != null">#{patientName},
            </if>
            <if test="cardNo != null">#{cardNo},
            </if>
            <if test="deviceType != null">#{deviceType},
            </if>
            <if test="deviceName != null">#{deviceName},
            </if>
            <if test="deviceCode != null">#{deviceCode},
            </if>
            <if test="deviceStatus != null">#{deviceStatus},
            </if>
            <if test="deviceRemark != null">#{deviceRemark},
            </if>
            <if test="deviceBindTime != null">#{deviceBindTime},
            </if>
            <if test="deviceUnbindTime != null">#{deviceUnbindTime},
            </if>
            <if test="deviceIp != null">#{deviceIp},
            </if>
            <if test="devicePort != null">#{devicePort},
            </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="updateProjectDevice" parameterType="ProjectDevice">
        update project_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="patientId != null">patient_id =
                #{patientId},
            </if>
            <if test="patientName != null">patient_name =
                #{patientName},
            </if>
            <if test="cardNo != null">card_no =
                #{cardNo},
            </if>
            <if test="deviceType != null">device_type =
                #{deviceType},
            </if>
            <if test="deviceName != null">device_name =
                #{deviceName},
            </if>
            <if test="deviceCode != null">device_code =
                #{deviceCode},
            </if>
            <if test="deviceStatus != null">device_status =
                #{deviceStatus},
            </if>
            <if test="deviceRemark != null">device_remark =
                #{deviceRemark},
            </if>
            <if test="deviceBindTime != null">device_bind_time =
                #{deviceBindTime},
            </if>
            <if test="deviceUnbindTime != null">device_unbind_time =
                #{deviceUnbindTime},
            </if>
            <if test="deviceIp != null">device_ip =
                #{deviceIp},
            </if>
            <if test="devicePort != null">device_port =
                #{devicePort},
            </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>

    <update id="deleteProjectDeviceById" parameterType="Long">
	    update project_device set device_status = 'UN_BIND',device_unbind_time = sysdate()
        where id = #{id}
    </update>

    <update id="deleteProjectDeviceByIds" parameterType="String">
        update project_device set device_status = 'UN_BIND',device_unbind_time = sysdate()
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
</mapper>