<?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.huatek.busi.mapper.cable.CableCirculationStatisticsMapper">
    <sql id="Base_Column_List">
            id AS id,
            site_id AS siteId,
            sub_equipment_id as subEquipmentId,
            main_equipment_id as mainEquipmentId,
            d_time as dTime,
            total_core_current as totalCoreCurrent,
            sensor_code as sensorCode,
            create_time as createTime
    </sql>

    <insert id="aveCableCirculationStatistics" >
        INSERT INTO busi_cable_circulation_statistics SELECT
        guuid ( ) AS id,
        site_id AS siteid,
        sub_tree_id AS sub_tree_id,
        main_equipment_id AS meqid,
        date(DATE_FORMAT(time_stamp, '%Y-%m-%d' ) ) AS d_time,
        CONVERT ( avg( total_core_current ), DECIMAL ( 18, 2 ) ) AS avgval,
        now( ) AS createtime,
        sensor_id AS sensor_id
        FROM
        busi_cable_circulation
        WHERE
        time_stamp <![CDATA[>= ]]>#{startTime}
        AND time_stamp <![CDATA[< ]]> #{endTime}
        GROUP BY
        sensor_id;
    </insert>




    <select id="getHistoryData" parameterType="com.huatek.busi.dto.cable.BusiCableCirculationStatisticsDto"
            resultType="com.huatek.busi.vo.cable.BusiCableCirculationIncreaseRateCacheVo">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        busi_cable_circulation_statistics
        <where>
            <if test="sensorCode !=null and sensorCode !=''">
                AND sensor_code = #{sensorCode}
            </if>
            <if test="startTime !=null">
                AND d_time<![CDATA[ >= ]]>#{startTime}
            </if>
            <if test="endTime !=null">
                AND d_time<![CDATA[ <= ]]>#{endTime}
            </if>
        </where>
    </select>

    <select id="getRecordBySensorCode" resultType="java.util.Map">
		SELECT
        <include refid="Base_Column_List"/>
		FROM
		busi_cable_circulation_statistics
        <where>
            <if test="sensorCode !=null and sensorCode !=''">
                AND sensor_code = #{sensorCode}
            </if>
            <if test="startTime !=null">
                AND d_time<![CDATA[ >= ]]>#{startTime}
            </if>
            <if test="endTime !=null">
                AND d_time<![CDATA[ <= ]]>#{endTime}
            </if>
        </where>
	</select>


    <select id="getItem"  resultType="java.util.Map">
        select
        <include refid="Base_Column_List"/>
        from
        busi_cable_circulation_statistics
        where
        sensor_code = #{sensorCode} and d_time = #{day}
        Group By
        sensor_code
    </select>

</mapper>