<?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.usky.iot.mapper.DmpDeviceInfoMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.usky.iot.domain.DmpDeviceInfo">
        <id column="id" property="id" />
        <result column="device_id" property="deviceId" />
        <result column="device_name" property="deviceName" />
        <result column="device_type" property="deviceType" />
        <result column="product_id" property="productId" />
        <result column="sim_code" property="simCode" />
        <result column="imsi_code" property="imsiCode" />
        <result column="node_type" property="nodeType" />
        <result column="group_id" property="groupId" />
        <result column="delete_flag" property="deleteFlag" />
        <result column="created_by" property="createdBy" />
        <result column="created_time" property="createdTime" />
        <result column="updated_by" property="updatedBy" />
        <result column="updated_time" property="updatedTime" />
        <result column="tenant_id" property="tenantId" />
    </resultMap>
<!--    <select id="page" resultType="com.usky.iot.domain.DmpDeviceInfo">-->
<!--        select ddi.*,dds.device_status as deviceStatus-->
<!--        from dmp_device ddi-->
<!--        left join dmp_device_status dds-->
<!--        on ddi.device_id  = dds.device_id and ddi.product_id = dds.product_id-->
<!--        <where>-->
<!--            <if test="ar.deviceName != null">-->
<!--                and ddi.device_name like CONCAT('%',#{ar.deviceName},'%')-->
<!--            </if>-->
<!--            <if test="ar.productId != null">-->
<!--                and ddi.product_id = #{ar.productId}-->
<!--            </if>-->
<!--            <if test="ar.serviceStatus != null">-->
<!--                and ddi.service_status = #{ar.serviceStatus}-->
<!--            </if>-->
<!--            <if test="ar.deviceStatus != null">-->
<!--                and dds.device_status = #{ar.deviceStatus}-->
<!--            </if>-->
<!--            <if test="1 == 1">-->
<!--                and ddi.delete_flag = 0 and ddi.tenant_id = #{tenantId}-->
<!--            </if>-->
<!--        </where>-->
<!--        order by ddi.id desc-->
<!--    </select>-->
    <select id="getProductInfo" resultType="com.usky.iot.domain.DmpProductInfo">
        select dpi.*
        from dmp_product dpi
        <where>
            <if test="productCode != null">
                and dpi.product_code = #{productCode}
            </if>
            <if test="1 == 1">
                and dpi.delete_flag = 0
            </if>
        </where>
    </select>

    <select id="attributeList" resultType="com.usky.backend.domain.ProductTVAttributeVO">
        select dpa.product_id as productId,dpa.attribute_code as attributeCode,dpa.attribute_name as attributeName,dpa.attribute_unit as attributeUnit,dpa.maximum as maximum,dpa.minimum as minimum,dpt.product_code as productCode
        from dmp_product_attribute dpa
        left join dmp_product dpt
        on dpa.product_id = dpt.id
        <where>
            <if test="procutIdList != null and procutIdList.size() > 0">
                AND dpa.product_id in
                <foreach item="item" collection="procutIdList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="1 == 1">
                and dpa.delete_flag = 0
            </if>
        </where>
    </select>
    <select id="baseAlarmCount" resultType="integer">
        select count(*)
        from base_alarm
        <where>
            <if test="deviceIdList != null and deviceIdList.size() > 0">
                and device_id in
                <foreach item="item" collection="deviceIdList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="1 == 1">
                and handle_status = 0 and alarm_type != '815'
            </if>
        </where>
    </select>
    <select id="baseFaultCount" resultType="integer">
        select count(*)
        from base_alarm
        <where>
            <if test="deviceIdList != null and deviceIdList.size() > 0">
                and device_id in
                <foreach item="item" collection="deviceIdList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="1 == 1">
                and handle_status = 0 and alarm_type = '815'
            </if>
        </where>
    </select>
    <select id="deviceOfflineCount" resultType="integer">
        select count(*)
        from dmp_device_status
        <where>
            <if test="deviceIdList != null and deviceIdList.size() > 0">
            and device_id in
                <foreach item="item" collection="deviceIdList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="1 == 1">
                and device_status = 2
            </if>
        </where>
    </select>
    <select id="baseAlarmList" resultType="com.usky.iot.domain.BaseAlarm">
        select *
        from base_alarm
        <where>
            and device_id|alarm_time in (select device_id|max(alarm_time) from base_alarm where device_id in
            <foreach item="item" collection="deviceIdList" open="(" separator="," close=")">
                #{item}
            </foreach>
            and handle_status = 0 group by device_id)
        </where>
    </select>

</mapper>
