<?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.zzyl.nursing.mapper.DeviceDataMapper">
    
    <resultMap type="DeviceData" id="DeviceDataResult">
        <result property="id"    column="id"    />
        <result property="deviceName"    column="device_name"    />
        <result property="iotId"    column="iot_id"    />
        <result property="productKey"    column="product_key"    />
        <result property="productName"    column="product_name"    />
        <result property="functionId"    column="function_id"    />
        <result property="accessLocation"    column="access_location"    />
        <result property="locationType"    column="location_type"    />
        <result property="physicalLocationType"    column="physical_location_type"    />
        <result property="deviceDescription"    column="device_description"    />
        <result property="dataValue"    column="data_value"    />
        <result property="alarmTime"    column="alarm_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectDeviceDataVo">
        select id, device_name, iot_id, product_key, product_name, function_id, access_location, location_type, physical_location_type, device_description, data_value, alarm_time, create_time, update_time, create_by, update_by, remark from device_data
    </sql>

    <select id="selectDeviceDataList" parameterType="DeviceData" resultMap="DeviceDataResult">
        <include refid="selectDeviceDataVo"/>
        <where>  
            <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="iotId != null  and iotId != ''"> and iot_id = #{iotId}</if>
            <if test="productKey != null  and productKey != ''"> and product_key = #{productKey}</if>
            <if test="productName != null  and productName != ''"> and product_name like concat('%', #{productName}, '%')</if>
            <if test="functionId != null  and functionId != ''"> and function_id = #{functionId}</if>
            <if test="accessLocation != null  and accessLocation != ''"> and access_location = #{accessLocation}</if>
            <if test="locationType != null "> and location_type = #{locationType}</if>
            <if test="physicalLocationType != null "> and physical_location_type = #{physicalLocationType}</if>
            <if test="deviceDescription != null  and deviceDescription != ''"> and device_description = #{deviceDescription}</if>
            <if test="dataValue != null  and dataValue != ''"> and data_value = #{dataValue}</if>
            <if test="alarmTime != null "> and alarm_time = #{alarmTime}</if>
        </where>
    </select>
    
    <select id="selectDeviceDataById" parameterType="Long" resultMap="DeviceDataResult">
        <include refid="selectDeviceDataVo"/>
        where id = #{id}
    </select>

    <select id="queryDeviceDataListByDay" resultType="java.util.Map">
        select a.time dateTime, round(avg(a.data_value)) dataValue
        from (select data_value, DATE_FORMAT(alarm_time, '%H:00') time
              from zzyl.device_data
              where function_id = #{functionId} and iot_id = #{iotId} and alarm_time between #{startTime} and #{endTime}
              group by function_id, iot_id, data_value, DATE_FORMAT(alarm_time, '%H:00')
              order by DATE_FORMAT(alarm_time, '%H:00'), data_value) a
        group by a.time
    </select>

    <select id="queryDeviceDataListByWeek" resultType="java.util.Map">
        select a.time dateTime, round(avg(a.data_value)) dataValue
        from (select data_value, DATE_FORMAT(alarm_time, '%m.%d') time
              from zzyl.device_data
              where function_id = #{functionId} and iot_id = #{iotId} and alarm_time between #{startTime} and #{endTime}
              group by iot_id, function_id, data_value, DATE_FORMAT(alarm_time, '%m.%d')
              order by DATE_FORMAT(alarm_time, '%m.%d'), data_value) a
        group by a.time;
    </select>

    <insert id="insertDeviceData" parameterType="DeviceData" useGeneratedKeys="true" keyProperty="id">
        insert into device_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceName != null">device_name,</if>
            <if test="iotId != null">iot_id,</if>
            <if test="productKey != null">product_key,</if>
            <if test="productName != null">product_name,</if>
            <if test="functionId != null">function_id,</if>
            <if test="accessLocation != null">access_location,</if>
            <if test="locationType != null">location_type,</if>
            <if test="physicalLocationType != null">physical_location_type,</if>
            <if test="deviceDescription != null">device_description,</if>
            <if test="dataValue != null">data_value,</if>
            <if test="alarmTime != null">alarm_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceName != null">#{deviceName},</if>
            <if test="iotId != null">#{iotId},</if>
            <if test="productKey != null">#{productKey},</if>
            <if test="productName != null">#{productName},</if>
            <if test="functionId != null">#{functionId},</if>
            <if test="accessLocation != null">#{accessLocation},</if>
            <if test="locationType != null">#{locationType},</if>
            <if test="physicalLocationType != null">#{physicalLocationType},</if>
            <if test="deviceDescription != null">#{deviceDescription},</if>
            <if test="dataValue != null">#{dataValue},</if>
            <if test="alarmTime != null">#{alarmTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateDeviceData" parameterType="DeviceData">
        update device_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="iotId != null">iot_id = #{iotId},</if>
            <if test="productKey != null">product_key = #{productKey},</if>
            <if test="productName != null">product_name = #{productName},</if>
            <if test="functionId != null">function_id = #{functionId},</if>
            <if test="accessLocation != null">access_location = #{accessLocation},</if>
            <if test="locationType != null">location_type = #{locationType},</if>
            <if test="physicalLocationType != null">physical_location_type = #{physicalLocationType},</if>
            <if test="deviceDescription != null">device_description = #{deviceDescription},</if>
            <if test="dataValue != null">data_value = #{dataValue},</if>
            <if test="alarmTime != null">alarm_time = #{alarmTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDeviceDataById" parameterType="Long">
        delete from device_data where id = #{id}
    </delete>

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