<?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.TDeviceReportDataMapper">
    
    <resultMap type="TDeviceReportData" id="TDeviceReportDataResult">
        <result property="deviceReportDataId"    column="device_report_data_id"    />
        <result property="deviceNum"    column="device_num"    />
        <result property="standardConditionAccumulation"    column="standard_condition_accumulation"    />
        <result property="workingConditionAccumulation"    column="working_condition_accumulation"    />
        <result property="residualQuantity"    column="residual_quantity"    />
        <result property="standardConditionFlow"    column="standard_condition_flow"    />
        <result property="workingConditionFlow"    column="working_condition_flow"    />
        <result property="temperature"    column="temperature"    />
        <result property="pressure"    column="pressure"    />
        <result property="reportTime"    column="report_time"    />
        <result property="communicationStatus"    column="communication_status"    />
        <result property="deviceStatus"    column="device_status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <resultMap type="DeviceReportDataVo" id="TDeviceReportDataResultVo">
        <result property="deviceReportDataId"    column="device_report_data_id"    />
        <result property="deviceNum"    column="device_num"    />
        <result property="deviceId"    column="device_id"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceType"    column="device_type"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceModel"    column="device_model"    />
        <result property="standardConditionAccumulation"    column="standard_condition_accumulation"    />
        <result property="workingConditionAccumulation"    column="working_condition_accumulation"    />
        <result property="residualQuantity"    column="residual_quantity"    />
        <result property="standardConditionFlow"    column="standard_condition_flow"    />
        <result property="workingConditionFlow"    column="working_condition_flow"    />
        <result property="temperature"    column="temperature"    />
        <result property="pressure"    column="pressure"    />
        <result property="reportTime"    column="report_time"    />
        <result property="communicationStatus"    column="communication_status"    />
        <result property="deviceStatus"    column="device_status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectTDeviceReportDataVo">
        select device_report_data_id, device_num, standard_condition_accumulation, working_condition_accumulation, residual_quantity, standard_condition_flow, working_condition_flow, temperature, pressure, report_time, communication_status, device_status, create_time, update_time
        from t_device_report_data
    </sql>

    <select id="selectTDeviceReportDataList" parameterType="DeviceReportDataForm" resultMap="TDeviceReportDataResultVo">
        select t.report_time, t.device_report_data_id, t.device_num, t.standard_condition_accumulation, t.working_condition_accumulation, t.residual_quantity, t.standard_condition_flow, t.working_condition_flow, t.temperature, t.pressure, t.communication_status, t.device_status, t.create_time, t.update_time,
               d.device_id, d.device_name, d.device_code, d.device_type, d.device_model
        from t_device_report_data t
        left join t_device_info d on t.device_num = d.iot_no
        <where>  
            <if test="deviceCode != null "> and d.device_code like concat('%',#{deviceCode},'%')</if>
            <if test="deviceType != null "> and d.device_type = #{deviceType}</if>
            <if test="startReportTime != null "> and t.report_time &gt;= #{startReportTime}</if>
            <if test="endReportTime != null "> and t.report_time &lt;= #{endReportTime}</if>
            <if test="communicationStatus != null  and communicationStatus != ''"> and t.communication_status = #{communicationStatus}</if>
            <if test="deviceStatus != null  and deviceStatus != ''"> and t.device_status = #{deviceStatus}</if>
        </where>
    </select>

    <select id="selectRealtimeDataList" parameterType="DeviceReportDataForm" resultMap="TDeviceReportDataResultVo">
        select max(t.report_time) report_time, t.device_report_data_id, t.device_num, t.standard_condition_accumulation, t.working_condition_accumulation, t.residual_quantity, t.standard_condition_flow, t.working_condition_flow, t.temperature, t.pressure, t.communication_status, t.device_status, t.create_time, t.update_time,
               d.device_id, d.device_name, d.device_code, d.device_type, d.device_model
        from t_device_report_data t
        right join t_device_info d on t.device_num = d.iot_no
        <where>
            <if test="deviceCode != null "> and d.device_code like concat('%',#{deviceCode},'%')</if>
            <if test="deviceType != null "> and d.device_type = #{deviceType}</if>
            <if test="deviceType == null "> and (d.device_type = '3' or d.device_type = '4')</if>
            <if test="startReportTime != null "> and t.report_time &gt;= #{startReportTime}</if>
            <if test="endReportTime != null "> and t.report_time &lt;= #{endReportTime}</if>
            <if test="communicationStatus != null  and communicationStatus != ''"> and t.communication_status = #{communicationStatus}</if>
            <if test="deviceStatus != null  and deviceStatus != ''"> and t.device_status = #{deviceStatus}</if>
        </where>
        group by d.device_id
    </select>

    <select id="selectTDeviceReportDataByDeviceNum" parameterType="String" resultMap="TDeviceReportDataResult">
        select max(report_time) report_time, device_report_data_id, device_num, standard_condition_accumulation, working_condition_accumulation, residual_quantity, standard_condition_flow, working_condition_flow, temperature, pressure, communication_status, device_status, create_time, update_time
        from t_device_report_data
        where device_num = #{deviceNum}
    </select>

    <select id="getTDeviceReportDataList" parameterType="String" resultMap="TDeviceReportDataResult">
        select report_time, device_report_data_id, device_num, standard_condition_accumulation, working_condition_accumulation, residual_quantity, standard_condition_flow, working_condition_flow, temperature, pressure, communication_status, device_status, create_time, update_time
        from t_device_report_data
        where device_num = #{deviceNum} and report_time &gt;= DATE_SUB(NOW(),INTERVAL 2 HOUR)
    </select>

    <select id="getAlarmDeviceReportDataList" parameterType="TDeviceReportData" resultMap="TDeviceReportDataResult">
        select report_time, device_report_data_id, device_num, standard_condition_accumulation, working_condition_accumulation, residual_quantity, standard_condition_flow, working_condition_flow, temperature, pressure, communication_status, device_status, create_time, update_time
        from t_device_report_data
        where device_num = #{deviceNum} and report_time &gt;= DATE_SUB(#{reportTime},INTERVAL 1 HOUR)
    </select>

    <insert id="insertTDeviceReportData" parameterType="TDeviceReportData" useGeneratedKeys="true" keyProperty="deviceReportDataId">
        insert into t_device_report_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceNum != null">device_num,</if>
            <if test="standardConditionAccumulation != null">standard_condition_accumulation,</if>
            <if test="workingConditionAccumulation != null">working_condition_accumulation,</if>
            <if test="residualQuantity != null">residual_quantity,</if>
            <if test="standardConditionFlow != null">standard_condition_flow,</if>
            <if test="workingConditionFlow != null">working_condition_flow,</if>
            <if test="temperature != null">temperature,</if>
            <if test="pressure != null">pressure,</if>
            <if test="reportTime != null">report_time,</if>
            <if test="communicationStatus != null">communication_status,</if>
            <if test="deviceStatus != null">device_status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceNum != null">#{deviceNum},</if>
            <if test="standardConditionAccumulation != null">#{standardConditionAccumulation},</if>
            <if test="workingConditionAccumulation != null">#{workingConditionAccumulation},</if>
            <if test="residualQuantity != null">#{residualQuantity},</if>
            <if test="standardConditionFlow != null">#{standardConditionFlow},</if>
            <if test="workingConditionFlow != null">#{workingConditionFlow},</if>
            <if test="temperature != null">#{temperature},</if>
            <if test="pressure != null">#{pressure},</if>
            <if test="reportTime != null">#{reportTime},</if>
            <if test="communicationStatus != null">#{communicationStatus},</if>
            <if test="deviceStatus != null">#{deviceStatus},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateTDeviceReportData" parameterType="TDeviceReportData">
        update t_device_report_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceNum != null">device_num = #{deviceNum},</if>
            <if test="standardConditionAccumulation != null">standard_condition_accumulation = #{standardConditionAccumulation},</if>
            <if test="workingConditionAccumulation != null">working_condition_accumulation = #{workingConditionAccumulation},</if>
            <if test="residualQuantity != null">residual_quantity = #{residualQuantity},</if>
            <if test="standardConditionFlow != null">standard_condition_flow = #{standardConditionFlow},</if>
            <if test="workingConditionFlow != null">working_condition_flow = #{workingConditionFlow},</if>
            <if test="temperature != null">temperature = #{temperature},</if>
            <if test="pressure != null">pressure = #{pressure},</if>
            <if test="reportTime != null">report_time = #{reportTime},</if>
            <if test="communicationStatus != null">communication_status = #{communicationStatus},</if>
            <if test="deviceStatus != null">device_status = #{deviceStatus},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where device_report_data_id = #{deviceReportDataId}
    </update>

    <delete id="deleteTDeviceReportDataById" parameterType="Long">
        delete from t_device_report_data where device_report_data_id = #{deviceReportDataId}
    </delete>

    <delete id="deleteTDeviceReportDataByIds" parameterType="String">
        delete from t_device_report_data where device_report_data_id in 
        <foreach item="deviceReportDataId" collection="array" open="(" separator="," close=")">
            #{deviceReportDataId}
        </foreach>
    </delete>
</mapper>