<?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.zehong.system.mapper.TGasuserSafetyDeviceInfoMapper">
    
    <resultMap type="TGasuserSafetyDeviceInfo" id="TGasuserSafetyDeviceInfoResult">
        <result property="gasUserSafetyDeviceId"    column="f_gasUser_safety_device_id"    />
        <result property="relationGasuserId"    column="f_relation_gasUser_id"    />
        <result property="relationDeviceType"    column="f_relation_device_type"    />
        <result property="deviceName"    column="f_device_name"    />
        <result property="deviceModel"    column="f_device_model"    />
        <result property="fIotNo"    column="f_iot_no"    />
        <result property="detectionMedium"    column="f_detection_medium"    />
        <result property="deviceInstallTime"    column="f_device_install_time"    />
        <result property="deviceInstallPosition"    column="f_device_install_position"    />
        <result property="head"    column="f_head"    />
        <result property="phone"    column="f_phone"    />
        <result property="isDel"    column="is_del"    />
    </resultMap>

    <sql id="selectTGasuserSafetyDeviceInfoVo">
        select f_gasUser_safety_device_id, f_relation_gasUser_id, f_relation_device_type, f_device_name, f_device_model, f_iot_no, f_detection_medium, f_device_install_time, f_device_install_position, f_head, f_phone, is_del from t_gasuser_safety_device_info
    </sql>

    <select id="selectTGasuserSafetyDeviceInfoList" parameterType="TGasuserSafetyDeviceInfo" resultMap="TGasuserSafetyDeviceInfoResult">
        <include refid="selectTGasuserSafetyDeviceInfoVo"/>
        <where>  
            <if test="relationGasuserId != null "> and f_relation_gasUser_id = #{relationGasuserId}</if>
            <if test="relationDeviceType != null  and relationDeviceType != ''"> and f_relation_device_type = #{relationDeviceType}</if>
            <if test="deviceName != null  and deviceName != ''"> and f_device_name like concat('%', #{deviceName}, '%')</if>
            <if test="deviceModel != null  and deviceModel != ''"> and f_device_model = #{deviceModel}</if>
            <if test="fIotNo != null  and fIotNo != ''"> and f_iot_no = #{fIotNo}</if>
            <if test="detectionMedium != null  and detectionMedium != ''"> and f_detection_medium = #{detectionMedium}</if>
            <if test="deviceInstallTime != null "> and f_device_install_time = #{deviceInstallTime}</if>
            <if test="deviceInstallPosition != null "> and f_device_install_position = #{deviceInstallPosition}</if>
            <if test="head != null  and head != ''"> and f_head = #{head}</if>
            <if test="phone != null  and phone != ''"> and f_phone = #{phone}</if>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
        </where>
    </select>

    <select id="queryNoSelectSafeDeviceInfoList" parameterType="tGasuserSafetyDeviceInfo" resultMap="TGasuserSafetyDeviceInfoResult">
        <include refid="selectTGasuserSafetyDeviceInfoVo"/>
        <where>
            is_del='0' and f_relation_gasUser_id is  null
            <if test="relationGasuserId != null "> and f_relation_gasUser_id = #{relationGasuserId}</if>
            <if test="relationDeviceType != null  and relationDeviceType != ''"> and f_relation_device_type = #{relationDeviceType}</if>
            <if test="deviceName != null  and deviceName != ''"> and f_device_name like concat('%', #{deviceName}, '%')</if>
            <if test="deviceModel != null  and deviceModel != ''"> and f_device_model = #{deviceModel}</if>
            <if test="fIotNo != null  and fIotNo != ''"> and f_iot_no = #{fIotNo}</if>
            <if test="detectionMedium != null  and detectionMedium != ''"> and f_detection_medium = #{detectionMedium}</if>
            <if test="deviceInstallTime != null "> and f_device_install_time = #{deviceInstallTime}</if>
            <if test="deviceInstallPosition != null "> and f_device_install_position = #{deviceInstallPosition}</if>
            <if test="head != null  and head != ''"> and f_head = #{head}</if>
            <if test="phone != null  and phone != ''"> and f_phone = #{phone}</if>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
        </where>
    </select>
    
    <select id="selectTGasuserSafetyDeviceInfoById" parameterType="Long" resultMap="TGasuserSafetyDeviceInfoResult">
        <include refid="selectTGasuserSafetyDeviceInfoVo"/>
        where f_gasUser_safety_device_id = #{gasUserSafetyDeviceId}
    </select>
        
    <insert id="insertTGasuserSafetyDeviceInfo" parameterType="TGasuserSafetyDeviceInfo" useGeneratedKeys="true" keyProperty="gasUserSafetyDeviceId">
        insert into t_gasuser_safety_device_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="relationGasuserId != null">f_relation_gasUser_id,</if>
            <if test="relationDeviceType != null">f_relation_device_type,</if>
            <if test="deviceName != null">f_device_name,</if>
            <if test="deviceModel != null">f_device_model,</if>
            <if test="fIotNo != null">f_iot_no,</if>
            <if test="detectionMedium != null">f_detection_medium,</if>
            <if test="deviceInstallTime != null">f_device_install_time,</if>
            <if test="deviceInstallPosition != null">f_device_install_position,</if>
            <if test="head != null">f_head,</if>
            <if test="phone != null">f_phone,</if>
            <if test="isDel != null">is_del,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="relationGasuserId != null">#{relationGasuserId},</if>
            <if test="relationDeviceType != null">#{relationDeviceType},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="deviceModel != null">#{deviceModel},</if>
            <if test="fIotNo != null">#{fIotNo},</if>
            <if test="detectionMedium != null">#{detectionMedium},</if>
            <if test="deviceInstallTime != null">#{deviceInstallTime},</if>
            <if test="deviceInstallPosition != null">#{deviceInstallPosition},</if>
            <if test="head != null">#{head},</if>
            <if test="phone != null">#{phone},</if>
            <if test="isDel != null">#{isDel},</if>
         </trim>
    </insert>

    <insert id="insertBatch" parameterType="list">

        insert into t_gasuser_safety_device_info (f_relation_gasUser_id, f_relation_device_type, f_device_name,
                                                  f_device_model, f_iot_no,f_detection_medium, f_device_install_time,
                                                  f_device_install_position,f_head,f_phone)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.relationGasuserId}, #{item.relationDeviceType,jdbcType=VARCHAR},
            #{item.deviceName,jdbcType=VARCHAR}, #{item.deviceModel,jdbcType=VARCHAR}, #{item.fIotNo,jdbcType=VARCHAR},
            #{item.detectionMedium,jdbcType=DECIMAL}, #{item.deviceInstallTime,jdbcType=DECIMAL},
             #{item.deviceInstallPosition,jdbcType=VARCHAR},#{item.head,jdbcType=VARCHAR}, #{item.phone,jdbcType=TIMESTAMP}
            )
        </foreach>
    </insert>

    <update id="updateTGasuserSafetyDeviceInfo" parameterType="TGasuserSafetyDeviceInfo">
        update t_gasuser_safety_device_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="relationGasuserId != null">f_relation_gasUser_id = #{relationGasuserId},</if>
            <if test="relationDeviceType != null">f_relation_device_type = #{relationDeviceType},</if>
            <if test="deviceName != null">f_device_name = #{deviceName},</if>
            <if test="deviceModel != null">f_device_model = #{deviceModel},</if>
            <if test="fIotNo != null">f_iot_no = #{fIotNo},</if>
            <if test="detectionMedium != null">f_detection_medium = #{detectionMedium},</if>
            <if test="deviceInstallTime != null">f_device_install_time = #{deviceInstallTime},</if>
            <if test="deviceInstallPosition != null">f_device_install_position = #{deviceInstallPosition},</if>
            <if test="head != null">f_head = #{head},</if>
            <if test="phone != null">f_phone = #{phone},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
        </trim>
        where f_gasUser_safety_device_id = #{gasUserSafetyDeviceId}
    </update>

    <delete id="deleteTGasuserSafetyDeviceInfoById" parameterType="Long">
        delete from t_gasuser_safety_device_info where f_gasUser_safety_device_id = #{gasUserSafetyDeviceId}
    </delete>
    <select id="deleteTGasuserSafetyDeviceInfoByUserId" parameterType="Long">
        delete from t_gasuser_safety_device_info where f_relation_gasUser_id = #{userId}
    </select>

    <delete id="deleteTGasuserSafetyDeviceInfoByIds" parameterType="String">
        delete from t_gasuser_safety_device_info where f_gasUser_safety_device_id in 
        <foreach item="gasUserSafetyDeviceId" collection="array" open="(" separator="," close=")">
            #{gasUserSafetyDeviceId}
        </foreach>
    </delete>
    <update id="deleteSafetyDeviceListInfo" >
        update  t_gasuser_safety_device_info set is_del='1' where f_gasUser_safety_device_id in
        <foreach item="deviceId" collection="array" open="(" separator="," close=")">
            #{deviceId}
        </foreach>
    </update>
    <update id="deleteBatchSafetyDeviceByUserIds">
        update  t_gasuser_safety_device_info set is_del='1' where f_relation_gasUser_id in
        <foreach item="deviceId" collection="userIds" open="(" separator="," close=")">
            #{deviceId}
        </foreach>
    </update>
    <update id="deleteSafetyDeviceInfo">
        update t_gasuser_safety_device_info set is_del='1' where f_gasUser_safety_device_id = #{deviceId}
    </update>
    <select id="selectSafetyDeviceDetailInfoList" resultMap="TGasuserSafetyDeviceInfoResult" parameterType="Long">
        <include refid="selectTGasuserSafetyDeviceInfoVo"/>
        where f_relation_gasUser_id = #{userId} and is_del='0'
    </select>

    <!--将关联设备信息与设备进行绑定-->
    <update id="updatetRelationSafetyDeviceInfo" parameterType="java.util.List">
        update t_gasuser_safety_device_info set f_relation_gasUser_id = #{id}
        where
        f_gasUser_safety_device_id in
        <foreach collection="gettDeviceInfoS" item="items" index="key" open="(" separator="," close=")">
            #{items.gasUserSafetyDeviceId}
        </foreach>
    </update>
</mapper>