<?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.zcxk.charge.core.dao.ReportWaterPumpStateMapper">


    <resultMap id="DeviceMonitorInfo" type="com.zcxk.charge.api.vo.ReprotWaterPumpVo">
        <result property="year" column="year" jdbcType="INTEGER"/>
        <result property="month" column="month" jdbcType="INTEGER"/>
        <result property="day" column="day" jdbcType="INTEGER"/>
        <collection property="deviceList"  resultMap="deviceListMap"></collection>
    </resultMap>

    <resultMap id="deviceListMap" type="com.zcxk.charge.api.vo.WaterPumpDataVo">
        <result property="tenantId" column="tenantId" jdbcType="INTEGER"/>
        <result property="deviceId" column="deviceId" jdbcType="INTEGER"/>
        <result property="deviceCode" column="deviceCode" jdbcType="INTEGER"/>
        <result property="attributeId" column="attributeId" jdbcType="INTEGER"/>
        <result property="attributeName" column="attributeName" jdbcType="INTEGER"/>
        <collection property="stateList" resultMap="stateListMap"></collection>
    </resultMap>

    <resultMap id="stateListMap" type="com.zcxk.charge.api.vo.WaterPumpStateVo">
        <result property="state" column="state" jdbcType="INTEGER"/>
        <result property="beginTime" column="beginTime" jdbcType="INTEGER"/>
        <result property="endTime" column="endTime" jdbcType="INTEGER"/>
    </resultMap>

    <select id="findReportWaterPumpStateList" resultMap="DeviceMonitorInfo">
        SELECT
            a.year  as "year",
            a.month as "month",
            a.day   as "day",
            a.state as "state",
            a.tenant_id    as  "tenantId",
            a.DEVICE_CODE  as  "deviceCode",
            a.DEVICE_NAME  as  "deviceName",
            a.DEVICE_ID as  "deviceId",
            a.attribute_id as  "attributeId",
            IFNULL(b.REMARK,a.`attribute_name`) AS "attributeName",
            a.state_begin_time as "beginTime",
            a.state_end_time   as "endTime"
        FROM sms_report_water_pump_state a
        inner join sms_device d on d.id = a.DEVICE_ID
        inner join sms_device_parm b
                   on a.DEVICE_ID = b.DEVICE_ID and a.ATTRIBUTE_ID=b.ATTRIBUTE_ID and b.`STATUS`=1 and b.PARM_TYPE = 2
        <where>
            <if test="waterPump.tenantId != null  and waterPump.tenantId != ''">
               and a.tenant_id = #{waterPump.tenantId}
            </if>
            and b.PARENT_SCENE_ID  = #{waterPump.sceneId}
            <if test="waterPump.deviceIds !=null and waterPump.deviceIds.size > 0">
                and a.DEVICE_ID in
                <foreach collection="waterPump.deviceIds" item="dramaId" open="(" close=")" separator=",">
                    #{dramaId.id}
                </foreach>
            </if>
            and a.state_begin_time >= #{waterPump.beginTime}
        </where>
        <!--order by a.DEVICE_ID,a.state_begin_time -->
    </select>

    <insert id="insertReportWaterPumpState">
        INSERT INTO sms_report_water_pump_state
        (`state`,state_begin_time,state_end_time,tenant_id,device_id,device_name,
        device_code,attribute_id,attribute_name,`year`,`month`,
        `day`,date_create,date_update,md5_query)
        VALUES
            (#{waterPump.state},#{waterPump.stateBeginTime},#{waterPump.stateEndTime},#{waterPump.tenantId},#{waterPump.deviceId},#{waterPump.deviceName},
            #{waterPump.deviceCode},#{waterPump.attributeId},#{waterPump.attributeName},#{waterPump.year},#{waterPump.month},
            #{waterPump.day},#{waterPump.dateCreate},#{waterPump.dateUpdate},#{waterPump.md5Query})
    </insert>

    <insert id="batchInsertReportWaterPumpState">
        INSERT INTO sms_report_water_pump_state
            (`state`,state_begin_time,state_end_time,tenant_id,device_id,device_name,
             device_code,attribute_id,attribute_name,`year`,`month`,
            `day`,date_create,date_update,md5_query)
        VALUES
        <foreach collection="waterPumpList" item="waterPump" index="index" separator=",">
        (#{waterPump.state},#{waterPump.stateBeginTime},#{waterPump.stateEndTime},#{waterPump.tenantId},#{waterPump.deviceId},#{waterPump.deviceName},
         #{waterPump.deviceCode},#{waterPump.attributeId},#{waterPump.attributeName},#{waterPump.year},#{waterPump.month},
         #{waterPump.day},#{waterPump.dateCreate},#{waterPump.dateUpdate},#{waterPump.md5Query})
        </foreach>
    </insert>

    <select id="findReportWaterPumpState" resultType="com.zcxk.charge.core.entity.ReportWaterPumpStateEntity">
        select id,`state`,state_begin_time as "stateBeginTime" from sms_report_water_pump_state s
        where md5_query = #{md5Query}
        order by s.state_begin_time desc limit 1
    </select>

    <update id="updateReportWaterPumpState">
        update sms_report_water_pump_state
        set    state_end_time = #{endTime},
               date_update = #{updateTime}
        where id = #{id}
    </update>
</mapper>