<?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.AlarmStrategyMapper'>
	<insert id="addAlarm">
		insert into ths_alarm
		(
		ALARM_INDEX,
		ALARM_NAME,
		GRADE,
		OPERATION,
		USER_INDEX,
		UPDATETIME
		)values
		(
		#{alarmIndex},
		#{alarmName},
		#{grade},
		#{operation},
		#{userIndex},
		#{updateTime}
		)
	</insert>

	<update id="updateAlarm">
		update ths_alarm set
		ALARM_NAME=#{alarmName},
		GRADE=#{grade},
		USER_INDEX=#{userIndex},
		UPDATETIME=#{updateTime}
		where
		ALARM_INDEX=#{alarmIndex}
	</update>

	<!--是否启用-->
	<update id="statusController">
	  update ths_alarm set
	  STATE = #{state}
	  where
	  ALARM_INDEX=#{alarmIndex}
	</update>

	<!--指标类型与或修改-->
	<update id="updateAlarmAndOr">
		update ths_alarm set
		OPERATION=#{operation}
		where
		ALARM_INDEX=#{alarmIndex}
	</update>

	<update id="delAlarm">
		delete from ths_alarm
		where ALARM_INDEX = #{alarmIndex}
	</update>

	<select id="getAlarmList" resultType="com.pqs9300.pojo.strategy.ThsAlarmDTO">
		select
		a.ALARM_INDEX as alarmIndex,
		a.ALARM_NAME as alarmName,
		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_alarm 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="alarmIndex!=null and alarmIndex!=''">
			and a.ALARM_INDEX !=#{alarmIndex}
		</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="getAlarmById" resultType="com.pqs9300.pojo.strategy.ThsAlarmDTO">
		select * from ths_alarm
		where ALARM_INDEX=#{alarmIndex}
	</select>

	<select id="isSameName" resultType="int">
		select count(*) from ths_alarm
		where ALARM_NAME=#{alarmName}
		<if test="alarmIndex!=null and alarmIndex!=''">
			and ALARM_INDEX !=#{alarmIndex}
		</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="addAlarmAss">
		insert into ths_alarmass (ALARM_INDEX,ALARMASS_INDEX,TYPE)
		<foreach collection="list" item="item"  separator="UNION ALL">
			select
			#{item.wfIndex},
			#{item.wfassIndex},
			#{item.type}
			from DUAL
		</foreach>
	</insert>

	<delete id="delAlarmAss">
		delete from ths_alarmass
		where
		ALARM_INDEX=#{alarmIndex}
	</delete>


	<select id="getAlarmAss" resultType="com.pqs9300.pojo.strategy.ThsAlarmAss">
		select ALARM_INDEX as alarmIndex,
		ALARMASS_INDEX as alarmassIndex,
		TYPE as type
		from ths_alarmass
		where ALARM_INDEX =#{alarmIndex}
		<if test="type!=null">
		and TYPE = #{type}
		</if>
	</select>

	<select id="isExitBindEvent" resultType="int">
		select count(*) from ths_alarmass
		where ALARM_INDEX = #{alarmIndex}
	</select>

	<select id="isExitLine" resultType="int">
		select count(*) from ths_alarmass a
		left join ths_alarm b on a.ALARM_INDEX=b.ALARM_INDEX
		where a.ALARMASS_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.ALARM_INDEX !=#{alarmIndex}
	</select>

</mapper>
