<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.3//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zw.platform.repository.vas.PeripheralDao">

    <!-- 查询  -->
    <select id="findByPage" parameterType="com.zw.platform.domain.basicinfo.query.AssignmentQuery" resultType="com.zw.platform.domain.vas.f3.Peripheral">
        SELECT * FROM zw_m_peripherals
        WHERE flag=1
        <if test="simpleQueryParam != null and simpleQueryParam!=''">
                AND
              (
                name LIKE BINARY
                CONCAT('%',#{simpleQueryParam},'%')
                OR ident_id LIKE
                CONCAT('%',#{simpleQueryParam},'%')
              )
        </if>
        ORDER BY create_data_time desc,id DESC
    </select>

    <!-- 查询  -->
    <select id="findAllow" resultType="com.zw.platform.domain.vas.f3.Peripheral">
        SELECT * FROM zw_m_peripherals
        WHERE flag=1
        ORDER BY `name` asc
    </select>


    <!-- 新增 -->
    <insert id="add" parameterType="com.zw.platform.domain.vas.f3.Peripheral">
        INSERT INTO zw_m_peripherals (
        id, name, ident_id, msg_length,flag,create_data_time,create_data_username
        )
        VALUES(
         #{id}, #{name}, #{identId}, #{msgLength},#{flag},#{createDataTime},#{createDataUsername}
        )
    </insert>
    <!-- 批量新增 -->
    <insert id="addBatch" parameterType="java.util.List">
        INSERT INTO zw_m_peripherals (
        id, name, ident_id, msg_length,flag,create_data_time,create_data_username
        )
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.id}, #{item.name}, #{item.identId}, #{item.msgLength}, #{item.flag},#{item.createDataTime}, #{item.createDataUsername})
        </foreach>
    </insert>
    <!-- 根据id删除一个 Peopel -->
    <update id="delete" parameterType="string">
        UPDATE zw_m_peripherals set flag=0 WHERE id = #{id}
    </update>

    <!-- 批量解除外设信息 -->
    <update id="unbindFenceByBatch" parameterType="string">
        update zw_m_peripherals
        set flag = 0
        where
        flag = 1
        and id in
        <foreach item="item" collection="ids" separator=","
                 open="(" close=")">
            #{item}
        </foreach>
    </update>

    <!-- 修改 PER -->
    <update id="update" parameterType="com.zw.platform.domain.vas.f3.Peripheral">
        UPDATE zw_m_peripherals SET
        name = #{name}, ident_id = #{identId}, msg_length = #{msgLength},
        update_data_time = #{updateDataTime},
        update_data_username = #{updateDataUsername}
        WHERE id = #{id}
    </update>

    <!-- 通过id得到一个 User-->
    <select id="get" parameterType="string" resultType="com.zw.platform.domain.vas.f3.Peripheral">
        SELECT * FROM zw_m_peripherals WHERE flag=1 and id = #{id}
    </select>

    <!-- 通过id得到一个 User-->
    <select id="getByIdentId" parameterType="string" resultType="com.zw.platform.domain.vas.f3.Peripheral">
        SELECT * FROM zw_m_peripherals WHERE flag=1 and ident_id = #{identId}
    </select>

    <!-- 通过identName得到一个 User-->
    <select id="getByIdentName" parameterType="string" resultType="com.zw.platform.domain.vas.f3.Peripheral">
        SELECT * FROM zw_m_peripherals WHERE flag=1 and name= BINARY #{identName}
    </select>

    <!-- 通过ID获取绑定的车辆数 User-->
    <select id="getConfigCountById" parameterType="string" resultType="java.lang.Integer">
        SELECT count(DISTINCT sp.id) FROM `zw_m_sensor_polling` sp
        LEFT JOIN zw_m_peripherals c on c.flag=1 and sp.sensor_type=c.id
        LEFT JOIN zw_m_sensor_config sc on sc.flag=1 and sc.id=sp.config_id
        where sp.flag=1 and c.id=#{id}

    </select>

</mapper>
