<?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.CableCirculationMapper">
    <resultMap id="busiCableCircuMap" type="com.huatek.busi.vo.cable.BusiCableCirculationVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="site_id" property="cacId"/>
        <result column="data_node_num" property="dataNodeNum"/>
        <result column="sensor_id" property="sensorId"/>
        <result column="cac_monitor_type_code" property="cacMonitorTypeCode"/>
        <result column="equipment_id" property="equipmentId"/>
        <result column="time_stamp" property="timeStamp"/>
        <result column="site_id" property="siteId"/>
        <result column="phase" property="phase"/>
        <result column="total_core_current" property="totalCoreCurrent"/>
        <result column="main_equipment_id" property="mainEquipmentId"/>
        <result column="sub_equipment_id" property="subEquipmentId"/>
        <result column="equipment_code" property="equipmentCode"/>
        <result column="ext_equipment_code" property="extEquipmentCode"/>
        <result column="main_tree_id" property="mainTreeId"/>
        <result column="sub_tree_id" property="subTreeId"/>
        <result column="run_state" property="runState"/>
        <result column="serial_number" property="serialNumber"/>
        <result column="status" property="status"/>
        <result column="hbase_rowkey" property="hbaseRowkey"/>
    </resultMap>

    <!-- 通用查询映射 -->
    <sql id="busiCableCirculation">
		SELECT
			id,
			cac_id,
			data_node_num,
			sensor_id,
			cac_monitor_type_code,
			equipment_id,
			time_stamp,
			site_id,
			phase,
			total_core_current,
			main_equipment_id,
			sub_equipment_id,
			equipment_code,
			ext_equipment_code,
			main_tree_id,
			sub_tree_id,
			run_state,
			serial_number,
			status,
			hbase_rowkey
		FROM
		busi_cable_circulation
	</sql>

    <sql id="Base_Column_List">
            bcc.id AS id,
            bcc.cac_id AS cacId,
            bcc.data_node_num AS dataNodeNum,
            bcc.sensor_id AS sensorId,
            bcc.cac_monitor_type_code AS cacMonitorTypeCode,
            bcc.equipment_id AS equipmentId,
            bcc.time_stamp AS timeStamp,
            bcc.equipment_code AS equipmentCode,
            bcc.site_id AS siteId,
            bcc.phase AS phase,
            bcc.total_core_current AS totalCoreCurrent,
            bcc.main_equipment_id AS mainEquipmentId,
            bcc.sub_equipment_id AS subEquipmentId,
            bcc.main_tree_id AS mainTreeId,
            bcc.sub_tree_id AS subTreeId,
            bcc.run_state AS runState
    </sql>

    <!-- 通用字段映射 -->
    <sql id="insertField">
			id,
			cac_id,
			data_node_num,
			sensor_id,
			cac_monitor_type_code,
			equipment_id,
			time_stamp,
			site_id,
			phase,
			total_core_current,
			main_equipment_id,
			sub_equipment_id,
			equipment_code,
			ext_equipment_code,
			main_tree_id,
			sub_tree_id,
			run_state,
			serial_number,
			status,
			hbase_rowkey
	</sql>

    <!-- 通用字段映射 -->
    <sql id="valueField">
		#{id},
		#{cacId},
		#{dataNodeNum},
		#{sensorId}sensor_id,
		#{cacMonitorTypeCode},
		#{equipmentId},
		#{timeStamp},
		#{siteId},
		#{phase},
		#{totalCoreCurrent},
		#{mainEquipmentId},
		#{subEquipmentId},
		#{equipmentCode},
		#{extEquipmentCode},
		#{mainTreeId},
		#{subTreeId},
		#{runState},
		#{serialNumber},
		#{status},
		#{hbaseRowkey}
	</sql>

    <!-- 查询电缆环流信息 -->
    <select id="findCACPageData" resultType="java.util.Map">
        SELECT
        <include refid="Base_Column_List"/>,
        (SELECT tree.system_code FROM busi_site_tree tree WHERE tree.id = bcc.main_tree_id) AS systemCode,
        (SELECT tree.name FROM busi_site_tree tree WHERE tree.id = bcc.main_tree_id) AS equipmentName,
        bccs.sensor_name as sensorName
        FROM
        busi_cable_circulation bcc,
        busi_cable_sensor bccs
        <where>
            bcc.sensor_id = bccs.sensor_code AND bccs.status = '1'
            <if test="cacMonitorTypeCode != null and cacMonitorTypeCode != ''">
                AND bcc.cac_monitor_type_code = #{cacMonitorTypeCode}
            </if>
            <if test="mainTreeId != null and mainTreeId != ''">
                AND bcc.main_tree_id = #{mainTreeId}
            </if>
            <if test="subTreeId != null and subTreeId != ''">
                AND bcc.sub_tree_id = #{subTreeId}
            </if>
            <if test="timeStamp != null and timeStamp != ''">
                AND DATE_FORMAT(bcc.time_stamp,'%Y-%m-%d') <![CDATA[ >= ]]> #{timeStamp}
            </if>
            <if test="timeStampEnd != null and timeStampEnd != ''">
                AND DATE_FORMAT(bcc.time_stamp,'%Y-%m-%d') <![CDATA[ <= ]]> #{timeStampEnd}
            </if>
            <if test="sensorCode != null and sensorCode != ''">
                AND bcc.sensor_id = #{sensorCode}
            </if>
        </where>
        ORDER BY bcc.time_stamp DESC
    </select>

    <!-- 导出电缆环流 -->
    <select id="exportCacMonitorData" resultType="com.huatek.busi.vo.cable.BusiCableCirculationReportVo">
        SELECT
        <include refid="Base_Column_List"/>,
        (SELECT tree.system_code FROM busi_site_tree tree WHERE tree.id = bcc.main_tree_id) AS systemCode,
        (SELECT tree.name FROM busi_site_tree tree WHERE tree.id = bcc.main_tree_id) AS equipmentName,
        bccs.sensor_name AS sensorName
        FROM busi_cable_circulation bcc
        LEFT JOIN busi_cable_sensor bccs ON bcc.sensor_id = bccs.sensor_code
        <where>
            <if test="cacMonitorTypeCode != null and cacMonitorTypeCode != ''">
                AND bcc.cac_monitor_type_code = #{cacMonitorTypeCode}
            </if>
            <if test="mainTreeId != null and mainTreeId != ''">
                AND bcc.main_tree_id = #{mainTreeId}
            </if>
            <if test="timeStamp != null and timeStamp != ''">
                AND DATE_FORMAT(bcc.time_stamp,'%Y-%m-%d') <![CDATA[ >= ]]> #{timeStamp}
            </if>
            <if test="timeStampEnd != null and timeStampEnd != ''">
                AND DATE_FORMAT(bcc.time_stamp,'%Y-%m-%d') <![CDATA[ <= ]]> #{timeStampEnd}
            </if>
            <if test="sensorId !=null and sensorId !=''">
                AND bcc.sensor_id = #{sensorId}
            </if>
        </where>
        ORDER BY bcc.time_stamp DESC
    </select>

    <!-- 根据时间查询数据 -->
    <select id="queryMonitorDataList" resultType="java.util.Map">
        SELECT
        b.sensor_id AS sensorCode,
        b.main_tree_id AS mainTreeId,
        b.main_equipment_id AS mainEquipmentId,
        b.sub_tree_id AS subTreeId,
        b.sub_equipment_id AS subEquipmentId,
        b.run_state AS runState,
        b.site_id AS siteId,
        CAST( AVG( b.total_core_current ) AS DECIMAL ( 20, 4 ) ) AS totalCoreCurrent
        FROM busi_cable_circulation b
        <where>
            <if test="startTime !=null">
                AND b.time_stamp<![CDATA[ >= ]]>concat(#{startTime},' 00:00:00')
            </if>
            <if test="endTime !=null">
                AND b.time_stamp<![CDATA[ < ]]>concat(#{endTime},' 23:59:59')
            </if>
        </where>
        GROUP BY
        b.sensor_id,
        b.main_tree_id,
        b.main_equipment_id,
        b.sub_tree_id,
        b.sub_equipment_id,
        b.run_state,
        b.site_id
    </select>

    <!-- 查询分页 -->
    <select id="selectBusiCableCircuPage" parameterType="com.huatek.busi.dto.cable.BusiCableCirculationDto"
            resultMap="busiCableCircuMap">
        <include refid="busiCableCirculation"/>
    </select>
    <!-- 删除 -->
    <update id="deleteBusiCable">
        UPDATE busi_cable_circulation SET status = '2'
        WHERE
        id IN
        <foreach collection="ids" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>
    <select id="getRealData" parameterType="java.lang.String" resultType="com.huatek.busi.vo.cable.BusiCableCirculationVo">
        SELECT
        bcc.id AS id,
        bcc.cac_id AS cacId,
        bcc.data_node_num AS dataNodeNum,
        bos.equipment_id as equipmentId,
        bos.sensor_code as sensorId,
        bos.position as phase,
        bcc.cac_monitor_type_code AS cacMonitorTypeCode,
        bcc.equipment_id AS equipmentId,
        bcc.time_stamp AS TIMESTAMP,
        bcc.equipment_code AS equipmentCode,
        bcc.site_id AS siteId,
        --         bcc.phase AS phase,
        bcc.total_core_current AS totalCoreCurrent,
        bcc.main_equipment_id AS mainEquipmentId,
        bcc.sub_equipment_id AS subEquipmentId,
        bcc.main_tree_id AS mainTreeId,
        bcc.sub_tree_id AS subTreeId,
        bcc.run_state AS runState
        FROM
        (select * from busi_other_sensor where 1=1
        <if test="equipmentId !=null and equipmentId !=''">
            AND equipment_id = #{equipmentId}
        </if>

        ) bos left JOIN
        (
        SELECT
        b.id,
        b.cac_id,
        b.data_node_num,
        b.sensor_id,
        b.cac_monitor_type_code,
        b.equipment_id,
        b.time_stamp,
        b.site_id,
        b.total_core_current,
        b.main_equipment_id,
        b.sub_equipment_id,
        b.equipment_code,
        b.main_tree_id,
        b.sub_tree_id,
        b.run_state,
        (
        CASE
        b.phase
        WHEN 'A1相' THEN
        '1'
        WHEN 'A2相' THEN
        '2'
        WHEN 'A3相' THEN
        '3'
        WHEN 'B1相' THEN
        '4'
        WHEN 'B2相' THEN
        '5'
        WHEN 'B3相' THEN
        '6'
        WHEN 'C1相' THEN
        '7'
        WHEN 'C2相' THEN
        '8'
        WHEN 'C3相' THEN
        '9' ELSE '0'
        END
        ) AS phase
        FROM
        busi_cable_circulation b,
        ( SELECT sensor_id, MAX( time_stamp ) AS time_stamp FROM busi_cable_circulation GROUP BY sensor_id ) a
        WHERE
        b.sensor_id = a.sensor_id
        AND b.time_stamp = a.time_stamp
        ) bcc
        on
        bcc.sensor_id = bos.sensor_code
        ORDER BY
        bos.position ASC
    </select>
    <select id="getListEquInfo" resultType="com.huatek.busi.vo.cable.BusiCableCirculationStatisticsVo">
        SELECT
        b.site_id AS siteId,
        b.main_tree_id AS mainTreeId,
        b.sub_tree_id AS subTreeId,
        b.main_equipment_id AS mainEquipmentId,
        b.sub_equipment_id AS subEquipmentId,
        b.sensor_id AS sensorCode,
        b.cac_monitor_type_code AS cacMonitorTypeCode,
        COUNT(*) AS dataCount
        FROM
        busi_cable_circulation b
        WHERE
        b.cac_monitor_type_code = #{cacMonitorTypeCode}
        AND b.time_stamp &gt;= #{startTime}
        AND b.time_stamp &lt; #{endTime}
        <if test="sensorCode != null and sensorCode != ''">
            AND b.sensor_id = #{sensorCode}
        </if>
        GROUP BY b.site_id,
        b.main_tree_id,
        b.sub_tree_id,
        b.sensor_id,
        b.cac_monitor_type_code
    </select>

    <select id="getDaliyReportData" resultType="com.huatek.busi.vo.monitor.BusiMonitorCableDataVo">
        SELECT
        t.total_core_current as current,
        t.phase as phase,
        t.run_state as runState,
            date_format(
                    t.time_stamp,
                    '%Y-%m-%d %H:%i:%s'
                ) AS time
        FROM
            busi_cable_circulation t
        WHERE
            t.sensor_id = #{sensorId}
          AND
            t.time_stamp <![CDATA[ >= ]]> #{startDate}
          AND t.time_stamp <![CDATA[ <= ]]> #{endDate}
        order by t.time_stamp asc ;
    </select>

    <select id="getHistoryData" parameterType="com.huatek.busi.dto.cable.BusiCableCirculationDto"
            resultType="com.huatek.busi.vo.cable.BusiCableCirculationVo">
        SELECT
        <include refid="Base_Column_List"/>
        FROM
        (SELECT id,cac_id,data_node_num,sensor_id,cac_monitor_type_code,equipment_id,
        time_stamp,site_id,total_core_current,main_equipment_id,
        sub_equipment_id,equipment_code,main_tree_id,sub_tree_id,run_state,
        (CASE phase WHEN 'A1相' THEN '1'
        WHEN 'A2相' THEN '2'
        WHEN 'A3相' THEN '3'
        WHEN 'B1相' THEN '4'
        WHEN 'B2相' THEN '5'
        WHEN 'B3相' THEN '6'
        WHEN 'C1相' THEN '7'
        WHEN 'C2相' THEN '8'
        WHEN 'C3相' THEN '9'
        ELSE '0' END) AS phase
        FROM busi_cable_circulation) bcc
        LEFT JOIN busi_other_sensor bos ON bcc.sensor_id = bos.sensor_code
        <where>
            bos.position = bcc.phase
            <if test="sensorId !=null and sensorId !=''">
                AND bcc.sensor_id = #{sensorId}
            </if>
            <if test="equipmentId !=null and equipmentId !=''">
                AND bos.equipment_id = #{equipmentId}
            </if>
            <if test="startTime !=null">
                AND bcc.time_stamp<![CDATA[ >= ]]>concat(#{startTime},' 00:00:00')
            </if>
            <if test="endTime !=null">
                AND bcc.time_stamp<![CDATA[ < ]]>concat(#{endTime},' 23:59:59')
            </if>
        </where>
    </select>

    <select id="findDaylyPointList" resultType="com.huatek.busi.vo.curve.PointItemVo">
        SELECT
        t.sensor_id as sensorCode,
        t.time_stamp as timeStamp,
        CAST(t.${subType} AS decimal(20, 4)) as val
        FROM busi_cable_circulation t
        WHERE t.sub_equipment_id = #{dto.mainTreeId}
        <if test="dto.sensorCode != null and dto.sensorCode != ''">
            AND t.sensor_id = #{dto.sensorCode}
        </if>
        AND t.time_stamp &gt;= #{startTime}
        AND t.time_stamp &lt; #{endTime}
        ORDER BY t.time_stamp ASC
    </select>

    <select id="findSensorCodeList" resultType="com.huatek.busi.dto.curve.BaseCurveQueryDto">
        SELECT DISTINCT
        t.sensor_id as sensorCode,
        t.main_tree_id as mainTreeId,
        count(*) as count
        FROM busi_cable_circulation t
        WHERE t.time_stamp &gt;= #{startTime} and t.time_stamp &lt; #{endTime}
        GROUP BY t.sensor_id, t.sub_equipment_id
    </select>

    <select id="getDaylyPointStatistics" resultType="com.huatek.busi.dto.curve.PointItemDto">
        SELECT
        t.sub_equipment_id AS mainTreeId,
        t.sensor_id AS sensorCode,
        COUNT(*) AS count,
        CAST(SUM(t.${subType}) AS decimal(20, 4)) AS total,
        MAX(CAST(t.${subType} AS decimal(20, 4))) AS max,
        MIN(CAST(t.${subType} AS decimal(20, 4))) AS min
        FROM busi_cable_circulation t
        WHERE t.sub_equipment_id = #{mainTreeId}
        <if test="sensorCode != null and sensorCode != ''">
            AND t.sensor_id = #{sensorCode}
        </if>
        AND t.time_stamp &gt;= #{startTime}
        AND t.time_stamp &lt;= #{endTime}
    </select>
</mapper>
