<?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.yeejoin.equipmanage.mapper.EquipmentIndexMapper">

    <resultMap id="Page" type="com.yeejoin.equipmanage.common.entity.vo.EquipmentIndexVO">
        <result property="id" column="id"></result>
        <result property="perfQuotaName" column="prefQuotaNAME"></result>
        <result property="perfQuotaDefinitionId" column="perf_quota_definition_id"></result>
        <result property="unitName" column="unitName"></result>
        <result property="unitId" column="unitId"></result>
        <result property="groupName" column="group_name"></result>
        <result property="isImportentParameter" column="is_importent_parameter"></result>
    </resultMap>

    <select id="queryEquipmentIndexListByEquipId" resultType="Map">
	   SELECT * FROM(
		    SELECT
				wei.id AS id,
				we.id AS equipDefId,
				'' AS perfQuotaStr,
				wei.unit AS measureUnitName,
				wei.name AS performanceIndexName,
				'true' AS isImportentParameter 
			FROM
				wl_equipment_index wei
				LEFT JOIN wl_equipment we ON wl.equipment_id = we.id
			WHERE
				we.id = #{equipmentId} 
			UNION ALL
			SELECT
				equipment.id AS id,
				'' AS perfQuotaDefinitionId,
				equipment.id AS equipDefId,
				equipment.pref_quota AS perfQuotaStr,
				'' AS measureUnitName,
				equipment.pref_quota AS performanceIndexName,
				'false' AS isImportentParameter 
			FROM
				wl_equipment equipment 
			WHERE
				equipment.pref_quota IS NOT NULL 
				AND equipment.id =#{equipmentId}
		)temp
        LIMIT #{start},#{page}
	 </select>

    <select id="queryEquipmentIndexCountByEquipId" resultType="Integer">
	   	  SELECT COUNT(1) FROM(
		    SELECT
				wei.id AS id,
				we.id AS equipDefId,
				'' AS perfQuotaStr,
				wei.unit AS measureUnitName,
				wei.name AS performanceIndexName,
				'true' AS isImportentParameter 
			FROM
				wl_equipment_index wei
				LEFT JOIN wl_equipment we ON wl.equipment_id = we.id
			WHERE
				we.id = #{equipmentId} 
			UNION ALL
			SELECT
				equipment.id AS id,
				'' AS perfQuotaDefinitionId,
				equipment.id AS equipDefId,
				equipment.pref_quota AS perfQuotaStr,
				'' AS measureUnitName,
				equipment.pref_quota AS performanceIndexName,
				'false' AS isImportentParameter 
			FROM
				wl_equipment equipment 
			WHERE
				equipment.pref_quota IS NOT NULL 
				AND equipment.id =#{equipmentId}
		)temp
	 </select>
    <select id="getPage" resultMap="Page">
		select
			* from
			( select
			 null as equipment_id,
			 null as id,
			 pref_quota as prefQuotaNAME,
			 null as perf_quota_definition_id,
			 null AS unitName,
			 0 as is_iot,
			 null as type_name,
			 null as type_code,
			 null as group_name,
			 null as index_id,
			 0 as is_importent_parameter,
			 0 as sort_num
			 from wl_equipment
			 where
			 id = #{id}
			 and pref_quota is not null
			 and pref_quota !=''
			UNION all
			 select * from (select
			 equipment_id,
			 id,
			 name,
			 name_key,
			 unit,
			 is_iot ,
			 type_name ,
			 type_code ,
			 group_name,
			 index_definition_id,
			 1 as is_importent_parameter,
			 sort_num
			 from
			 wl_equipment_index
			 where equipment_id = #{id}
			 order by group_name,sort_num ) impor) data
			 where prefQuotaNAME  like concat('%',#{groupName},'%')
	</select>

	<select id="getPerfQutoaList" resultType="com.yeejoin.equipmanage.common.entity.vo.EquipmentIndexVO">
		select
			* from
			( select
			 pref_quota as perfQuotaName,
			 null as perfQuotaDefinitionId,
			 0 as isIot
			 from wl_equipment
			 where
			 id = #{id}
			 and pref_quota is not null
			 and pref_quota !=''
			UNION all
			 select * from (select
			 name as perfQuotaName,
			 name_key as perfQuotaDefinitionId,
			 is_iot as isIot
			 from
			 wl_equipment_index
			 where equipment_id = #{id}
			 order by group_name,sort_num ) impor) data
	</select>

	<select id="getOnePrefQuota" resultMap="Page">
		SELECT
			 equipment_id ,
			 id,
			 name as prefQuotaNAME,
			 name_key as perf_quota_definition_id,
			 unit as unitName,
			 is_iot  ,
			 type_name  ,
			 type_code  ,
			 group_name ,
			 index_definition_id as index_id,
			 1 as is_importent_parameter,
			 sort_num,
			 type
			 from
			 wl_equipment_index
			 where id = #{id}
	</select>
	<select id="checkName" resultType="int">
		select count(1) from wl_equipment_index where
		index_definition_id=#{id}
		and equipment_id = #{fId}
		<if test="dataId != null and dataId !='' ">
			and id != #{dataId}
		</if>
	</select>
	<update id="updateE">
			  UPDATE
              wl_equipment
              SET
              pref_quota = ''
              where id = #{id}
	</update>
	<insert id="savePerfQuota" >
	 insert into wl_equipment_index
             VALUES(
             #{id},
             #{equipmentId},
             #{perfQuotaName},
             #{perfQuotaDefinitionId},
             #{unitName},
             #{indexId},
		     #{isIot},
		   	 #{typeName},
			 #{typeCode},
			 #{groupName},
			 #{sortNum},
			 #{type},
             now())
	</insert>

	<select id="checkUnit" resultType="int">
		select count(1) from wl_equipment_index where
		unit=#{unitName}
	</select>

	<select id="getEquipmentIndexByIot" resultType="com.yeejoin.equipmanage.common.entity.vo.EquipmentIndexVO">
		SELECT
		wei.id AS id,
		wei.name_key AS nameKey
		FROM
		wl_equipment_index wei
		WHERE
		wei.is_iot = 1
		AND
		wei.name_key != ""
	</select>
	<select id="getGruopName" resultType="java.lang.String">
		SELECT
			group_name
		FROM
			wl_equipment_index
		WHERE
			group_name != ' '
			AND group_name IS NOT NULL
			AND equipment_id = #{equipmentId}
		GROUP BY
			group_name
	</select>
</mapper>
