<?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.pqs9300.mapper.strategy.WarningStrategyMapper'>
	<insert id="addEarlyWarn">
		insert into ths_warnform
		(
		WF_INDEX,
		WF_NAME,
		GRADE,
		OPERATION,
		USER_INDEX,
		UPDATETIME
		)values
		(
		#{wfIndex},
		#{wfName},
		#{grade},
		#{operation},
		#{userIndex},
		#{updateTime}
		)
	</insert>

	<update id="updateEarlyWarn">
		update ths_warnform set
		WF_NAME=#{wfName},
		GRADE=#{grade},
		USER_INDEX=#{userIndex},
		UPDATETIME=#{updateTime}
		where
		WF_INDEX=#{wfIndex}
	</update>

	<!--指标类型与或修改-->
	<update id="updateEarlyWarnAndOr">
		update ths_warnform set
		OPERATION=#{operation}
		where
		WF_INDEX=#{wfIndex}
	</update>

	<!--是否启用-->
	<update id="statusController">
	  update ths_warnform set
	  STATE = #{state}
	  where
	  WF_INDEX=#{wfIndex}
	</update>

	<delete id="delEarlyWarn">
		delete from ths_warnform
		where
		WF_INDEX = #{wfIndex}
	</delete>

	<select id="getEarlyWarnList" resultType="com.pqs9300.pojo.strategy.ThsAlarmFormDTO">
		select
		a.WF_INDEX as wfIndex,
		a.WF_NAME as wfName,
		a.GRADE as grade,
		a.OPERATION as operation,
		a.STATE as state,
		a.USER_INDEX as userIndex,
		a.UPDATETIME as updateTime,
		b.NAME as userName
		from ths_warnform a
		left join pqs_user b on a.USER_INDEX = b.USER_INDEX and b.state = 1
		left join pqs_userset c on b.USER_INDEX =c.user_index
		where 1=1
		<if test="grade!=null">
			and a.grade =#{grade}
		</if>
		<if test="wfIndex!=null and wfIndex!=''">
			and a.WF_INDEX !=#{wfIndex}
		</if>
		<if test="list!=null">
			and c.depts_index in
			<foreach collection="list" index="index" item="item" open="(" close=")" separator=",">
				#{item}
			</foreach>
		</if>
		order by a.UPDATETIME desc
	</select>

	<select id="getEarlyWarnById" resultType="com.pqs9300.pojo.strategy.ThsAlarmFormDTO">
		select * from ths_warnform
		where WF_INDEX=#{wfIndex}
	</select>

	<select id="isSameName" resultType="int">
		select count(*) from ths_warnform
		where WF_NAME=#{wfName}
		<if test="wfIndex!=null and wfIndex!=''">
		and WF_INDEX !=#{wfIndex}
		</if>
	</select>

	<select id="getTartgetTypeForDic" resultType="com.pqs9300.pojo.strategy.DicStrategyRVO">
		select a.DIC_INDEX as dicIndex,
		 a.DIC_NAME as dicName,
		 a.DIC_TYPE as dicType
		 from PQS_DICDATA a
		 left join PQS_DICTYPE b on a.DIC_TYPE = b.DICTYPE_INDEX
		 where b.DICTYPE_NAME = #{type}
		 and a.STATE = 1
	</select>

	<insert id="addEarlyWarnAss">
		insert into ths_warnformass (WF_INDEX,WFASS_INDEX,TYPE)
		<foreach collection="list" item="item"  separator="UNION ALL">
			select
			#{item.wfIndex},
			#{item.wfassIndex},
			#{item.type}
			from DUAL
		</foreach>
	</insert>

	<delete id="delEarlyWarnAss">
		delete from ths_warnformass
		where
		WF_INDEX=#{wfIndex}
	</delete>


	<select id="getEarlyWarnAss" resultType="com.pqs9300.pojo.strategy.ThsAlarmFormAss">
		select WF_INDEX as wfIndex,
		WFASS_INDEX as wfassIndex,
		TYPE as type
		from ths_warnformass
		where WF_INDEX =#{wfIndex}
		<if test="type!=null">
		and TYPE = #{type}
		</if>
	</select>

	<select id="isExitBindEvent" resultType="int">
		select count(*) from ths_warnformass
		where WF_INDEX = #{wfIndex}
	</select>

	<select id="isExitLine" resultType="int">
		select count(*) from ths_warnformass a
		left join ths_warnform b on a.WF_INDEX = b.WF_INDEX
		where a.WFASS_INDEX in
		<foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
			#{item.wfassIndex,jdbcType=VARCHAR}
		</foreach>
		and b.GRADE=#{grade}
		and a.TYPE in
		<foreach collection="type" item="item" index="index" open="(" close=")" separator=",">
			#{item}
		</foreach>
		and a.WF_INDEX !=#{wfIndex}
	</select>




	<select id="getEarlyWarnListbyGrade" resultType="com.pqs9300.pojo.strategy.ThsAlarmFormDTO">
		select
		a.WF_INDEX as wfIndex,
		a.WF_NAME as wfName,
		a.GRADE as grade,
		a.OPERATION as operation,
		a.USER_INDEX as userIndex,
		a.UPDATETIME as updateTime,
		b.NAME as userName
		from ths_warnform a
		left join pqs_user b on a.USER_INDEX = b.USER_INDEX
		where a.STATE = 1 and a.grade = #{grade}
		order by a.UPDATETIME desc
	</select>

	<select id="getEarlyWarnAssBywfIndex" resultType="com.pqs9300.pojo.strategy.ThsAlarmFormAss">
		select WF_INDEX as wfIndex,
		WFASS_INDEX as wfassIndex,
		TYPE as type
		from ths_warnformass
		where WF_INDEX =#{wfIndex}
	</select>

    <select id="getEarlyWarnAssByGrade" resultType="com.pqs9300.pojo.strategy.ThsAlarmFormAss">
        select a.type type, a.wfass_index wfassIndex
        from ths_warnformass a, ths_warnform b
        where a.wf_index = b.wf_index
        and b.state = 1
        and b.grade = #{grade}
    </select>
</mapper>
