<?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.DataOutputEntityMapper">

    <sql id="Base_Column_List">
		t.id,
		t.site_id AS siteId,
		s.site_name AS siteName,
		t.main_tree_id AS mainTreeId,
		tree.name AS mainTreeName,
		t.sub_tree_id AS subTreeId,
		CONCAT_WS('', t_tree.name, g_tree.name, a_tree.name, o_tree.name,c_tree.name,ge_tree.name) AS subTreeName,
		t.day,
		t.file_path AS filePath, 
		t.created_time AS createdTime,
		t.download_times AS downloadTimes,
		t.file_name AS fileName,
		t.data_desc as dataDesc,
		CONCAT_WS('', tb.manufacturer, gb.manufactor,os.manufactor,gen.manufacturer,cb.manufacturer) AS manufacturer,
		CONCAT_WS('', tb.equipment_delivery_time, gb.transport_time,os.use_date,ar.transport_time,gen.equipment_delivery_time) AS equipmentDeliveryTime
    </sql>
    <sql id="table_ref">
		data_output_entity t LEFT JOIN busi_site s ON t.site_id = s.id
		LEFT JOIN busi_site_tree tree ON t.main_tree_id = tree.id
		LEFT JOIN busi_transformer_sub_tree t_tree ON t.sub_tree_id = t_tree.id
		LEFT JOIN busi_gis_sub_tree g_tree ON t.sub_tree_id = g_tree.id
		LEFT JOIN busi_generator_sub_tree ge_tree ON t.sub_tree_id = ge_tree.id
		LEFT JOIN busi_arrester_sub_tree a_tree ON t.sub_tree_id = a_tree.id
		LEFT JOIN busi_cable_sub_tree c_tree ON t.sub_tree_id = c_tree.id
		LEFT JOIN busi_other_sub_tree o_tree ON t.sub_tree_id = o_tree.id
		LEFT JOIN busi_offline_transformer_brand tb ON t.main_tree_id = tb.main_equipment_id
		LEFT JOIN busi_offline_gis_brand gb ON t.main_tree_id = gb.main_equipment_id
		LEFT JOIN busi_other_sensor os ON t.sub_tree_id = os.equipment_id
		LEFT JOIN busi_offline_arrester_brand ar ON t.main_tree_id = ar.main_equipment_id
		LEFT JOIN busi_offline_cable_brand cb ON t.main_tree_id = cb.main_equipment_id
		LEFT JOIN busi_offline_generator_brand gen ON t.main_tree_id = gen.main_equipment_id
    </sql>
	<select id="dlList" resultType="com.huatek.busi.vo.dataoutput.OutputEquipmentVo">
		SELECT
		s.id AS siteId,
		s.site_name AS siteName,
		tree.id AS mainTreeId,
		tree.name AS mainTreeName,
		tree.equipment_type AS mainEquipmentType,
		t.id AS subTreeId,
		t.name AS subTreeName,
		t.equipment_type AS equipmentType,
		t.system_code AS systemCode
		FROM busi_cable_sub_tree t
		LEFT JOIN busi_site_tree tree ON t.equipment_id = tree.id
		LEFT JOIN busi_site s ON t.site_id = s.id
		WHERE t.status = '1' AND s.status = '1' AND tree.status = '1'
		AND t.equipment_type IN ('011', '021', '031', '041', '051', '061')

	</select>

	<select id="selectDataOutputEntityPage" resultType="com.huatek.busi.vo.dataoutput.DataOutputEntityVo">
        SELECT
        	<include refid="Base_Column_List"/>
        FROM
        	<include refid="table_ref"/>
        <where>
        	<if test="day != null">
        		and t.day &gt;= #{day}
        	</if>
        	<if test="dayEnd != null">
        		and t.day &lt;= #{dayEnd}
        	</if>
        	<if test="siteName != null and siteName != ''">
        		and s.site_name like CONCAT(CONCAT('%', #{siteName}), '%')
        	</if>
        	<if test="mainTreeName != null and mainTreeName != ''">
        		and tree.name like CONCAT(CONCAT('%', #{mainTreeName}), '%')
        	</if>
        	<if test="dataDesc != null and dataDesc != ''">
        		and t.data_desc like CONCAT(CONCAT('%', #{dataDesc}), '%')
        	</if>
        	<if test="subTreeName != null and subTreeName != ''">
        		and (
        			t_tree.name like CONCAT(CONCAT('%', #{subTreeName}), '%')
        			or 
        			g_tree.name like CONCAT(CONCAT('%', #{subTreeName}), '%')
        			or 
        			a_tree.name like CONCAT(CONCAT('%', #{subTreeName}), '%')
        			or 
        			o_tree.name like CONCAT(CONCAT('%', #{subTreeName}), '%')
					or
					c_tree.name like CONCAT(CONCAT('%', #{subTreeName}), '%')
					or
					ge_tree.name like CONCAT(CONCAT('%', #{subTreeName}), '%')
					)
        	</if>
        </where>
        order by t.created_time desc,tree.equipment_type asc, tree.name ASC, t.sub_tree_id
    </select>

	<select id="findOutputMainEquips" resultType="com.huatek.busi.vo.dataoutput.OutputEquipmentVo">
		SELECT
		  s.id AS siteId,
		  s.site_name AS siteName,
		  tree.id AS mainTreeId,
		  tree.name AS mainTreeName,
		  tree.equipment_type AS mainEquipmentType,
		  'run_status' AS equipmentType,
		  tree.system_code AS systemCode
		FROM busi_site_tree tree
		  LEFT JOIN busi_site s
		    ON tree.site_id = s.id
		WHERE tree.status = '1'
		AND s.status = '1'
		AND tree.equipment_type = #{mainEquipmentType}
	</select>

	<delete id="deleteByDay">
		delete from data_output_entity where day = #{day}
	</delete>

	<select id="getDataOutputEntityVo" resultType="com.huatek.busi.vo.dataoutput.DataOutputEntityVo">
		SELECT
        	<include refid="Base_Column_List"/>
        FROM
        	<include refid="table_ref"/>
        where t.id = #{id}
	</select>


	<select id="getSimulateResultDto" resultType="com.huatek.busi.dto.SimulateResultDto">
		SELECT
			A.main_id AS mainId,
			A.p_id AS pId,
			A.day AS day,
			A.file_path AS filePath,
			A.file_name AS fileName,
			A.created_time AS createTime
		FROM
			data_output_entity_simulation A
		WHERE
			 A.main_id =#{mainId}
			 ORDER BY day desc
		LIMIT 1
	</select>

	<select id="transformerList" resultType="com.huatek.busi.vo.dataoutput.OutputEquipmentVo">
		SELECT
		  s.id AS siteId,
		  s.site_name AS siteName,
		  tree.id AS mainTreeId,
		  tree.name AS mainTreeName,
		  tree.equipment_type AS mainEquipmentType,
		  t.id AS subTreeId,
		  t.name AS subTreeName,
		  t.equipment_type AS equipmentType,
		  t.system_code AS systemCode
		FROM busi_transformer_sub_tree t 
		    LEFT JOIN busi_site_tree tree ON t.equipment_id = tree.id
		    LEFT JOIN busi_site s ON t.site_id = s.id
		WHERE t.status = '1' AND s.status = '1' AND tree.status = '1'
		  AND t.equipment_type IN ('011', '021', '031', '041', '051', '061', '071', '081', '091', '101')
	</select>
	<select id="gisList" resultType="com.huatek.busi.vo.dataoutput.OutputEquipmentVo">
		SELECT
		  s.id AS siteId,
		  s.site_name AS siteName,
		  tree.id AS mainTreeId,
		  tree.name AS mainTreeName,
		  tree.equipment_type AS mainEquipmentType,
		  t.id AS subTreeId,
		  t.name AS subTreeName,
		  t.equipment_type AS equipmentType,
		  t.system_code AS systemCode
		FROM busi_gis_sub_tree t
		    LEFT JOIN busi_site_tree tree ON t.equipment_id = tree.id
		    LEFT JOIN busi_site s ON t.site_id = s.id
		WHERE t.status = '1' AND s.status = '1' AND tree.status = '1'
		  AND t.equipment_type IN ('011', '021', '031', '041', '051', '061')
	</select>
	<select id="arresterList" resultType="com.huatek.busi.vo.dataoutput.OutputEquipmentVo">
		SELECT
		  s.id AS siteId,
		  s.site_name AS siteName,
		  tree.id AS mainTreeId,
		  tree.name AS mainTreeName,
		  tree.equipment_type AS mainEquipmentType,
		  t.id AS subTreeId,
		  t.name AS subTreeName,
		  t.equipment_type AS equipmentType,
		  t.system_code AS systemCode
		FROM busi_arrester_sub_tree t 
		    LEFT JOIN busi_site_tree tree ON t.equipment_id = tree.id
		    LEFT JOIN busi_site s ON t.site_id = s.id
		WHERE t.status = '1' AND s.status = '1' AND tree.status = '1'
		  AND t.equipment_type IN ('011', '021', '031', '041', '051')
	</select>
	<select id="otherList" resultType="com.huatek.busi.vo.dataoutput.OutputEquipmentVo">
		SELECT
		  s.id AS siteId,
		  s.site_name AS siteName,
		  tree.id AS mainTreeId,
		  tree.name AS mainTreeName,
		  tree.equipment_type AS mainEquipmentType,
		  t.id AS subTreeId,
		  t.name AS subTreeName,
		  t.equipment_type AS equipmentType,
		  t.system_code AS systemCode
		FROM busi_other_sub_tree t 
		    LEFT JOIN busi_site_tree tree ON t.equipment_id = tree.id
		    LEFT JOIN busi_site s ON t.site_id = s.id
		WHERE t.status = '1' AND s.status = '1' AND tree.status = '1'
		  AND t.equipment_type IN ('011', '021', '031', '041', '051')
	</select>

	<select id="generatorList" resultType="com.huatek.busi.vo.dataoutput.OutputEquipmentVo">
		SELECT
		  s.id AS siteId,
		  s.site_name AS siteName,
		  tree.id AS mainTreeId,
		  tree.name AS mainTreeName,
		  tree.equipment_type AS mainEquipmentType,
		  t.id AS subTreeId,
		  t.name AS subTreeName,
		  t.equipment_type AS equipmentType,
		  t.system_code AS systemCode
		FROM busi_generator_sub_tree t
		    LEFT JOIN busi_site_tree tree ON t.equipment_id = tree.id
		    LEFT JOIN busi_site s ON t.site_id = s.id
		WHERE t.status = '1' AND s.status = '1' AND tree.status = '1'
		  AND t.equipment_type IN ('011', '021', '031', '041', '051', '061')
	</select>

	<select id="cwgList" resultType="com.huatek.busi.vo.dataoutput.OutputEquipmentVo">
		SELECT
		  s.id AS siteId,
		  s.site_name AS siteName,
		  tree.id AS mainTreeId,
		  tree.equipment_type AS mainEquipmentType,
		  tree.name AS mainTreeName,
		  tree.system_code AS systemCode
		FROM busi_site_tree tree LEFT JOIN busi_site s ON tree.site_id = s.id
		WHERE tree.status = '1' AND s.status = '1' AND tree.equipment_type = '311'
	</select>


	<select id="findWarinList" resultType="com.huatek.busi.vo.warning.BusiEqmWarningTaskVo">
		SELECT id FROM `busi_eqm_warning`;
	</select>
</mapper>