<?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.yeejoin.equipmanage.mapper.CarPropertyMapper">
    <select id="getCarPropListByIotCode" resultType="com.yeejoin.equipmanage.common.entity.CarProperty">
        SELECT
        wcp.id AS id,
        wcp.car_id AS carId,
        wcp.value AS value,
        wcp.remark AS remark,
        wcp.equipment_index_id AS equipmentIndexId,
        wei.name_key AS nameKey,
        wei.name_key AS equipmentIndexKey,
        wei.name as equipmentIndexName,
        wcp.is_alarm as isAlarm,
		wcp.emergency_level_color as emergencyLevelColor,
		wcp.emergency_level as emergencyLevel,
		wcp.emergency_level_describe as emergencyLevelDescribe,
		wc.iot_code as iotCode,
		wc.`name` as carName,
		wc.car_num as carNum,
		wei.unit unitName,
		wei.group_name as groupName
        FROM
        wl_car_property wcp
        LEFT JOIN wl_car AS wc ON wc.id = wcp.car_id
        LEFT JOIN wl_equipment_index AS wei ON wei.id = wcp.equipment_index_id
        WHERE
        wc.iot_code = #{iotCode} and  wei.is_iot = true
    </select>
    <select id="getCarPropertyList" resultType="com.yeejoin.equipmanage.common.entity.vo.CarPropertyVo">
        SELECT
        cp.id,
        cp.car_id,
        IFNULL(ei.`name`,'') AS `name`,
        ei.name_key AS nameKey,
        cp.`value`,
        ei.unit,
        ei.is_iot AS isIot,
        ei.sort_num AS sort,
        cp.create_date ,
        IFNULL(c.qr_code,'') AS mRid
        FROM
        wl_car_property cp
        LEFT JOIN wl_equipment_index ei ON ei.id = cp.equipment_index_id
        LEFT JOIN wl_car c ON c.id = cp.car_id
        <where>
            <if test="id != null">
                cp.car_id = #{id}
            </if>
        </where>
    </select>
    <select id="getCarPropertyListByCarIds" resultType="com.yeejoin.equipmanage.common.entity.vo.CarPropertyVo">
        SELECT
        cp.id,
        cp.car_id,
        ei.`name`,
        ei.name_key AS nameKey,
        cp.`value`,
        ei.unit,
        ei.is_iot AS isIot,
        ei.sort_num AS sort,
        cp.create_date
        FROM
        wl_car_property cp
        LEFT JOIN wl_equipment_index ei ON ei.id = cp.equipment_index_id
        <where>
            <if test="list != null and list.size > 0">
                cp.car_id IN
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>


    <select id="getCarPropertyByCarIds" resultType="map">
        SELECT
            sum( VALUE ) as foamNum,
            b.waterNum as waterNum
        FROM
            `wl_car_property`,
            (SELECT
                 sum( VALUE ) as waterNum
             FROM
                 `wl_car_property`
             WHERE
                 car_id IN
                 <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
        #{item}
    </foreach>
               AND equipment_index_key = '119car_full_water') b
        WHERE
            car_id IN
            <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
        #{item}
    </foreach>
          AND equipment_index_key = '119car_Fire_foam'
</select>

    <select id="selectIndexByTime" resultType="map">
        SELECT
        car_id sourceId,
        update_date updateDate
        FROM
        wl_car_property
        WHERE
        equipment_index_key = #{carStartIndexKey} AND `value` IS NOT NULL
--         AND DATE_SUB(CURDATE( ), INTERVAL 7 DAY ) > update_date
    </select>

</mapper>
