<?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.zcloud.mapper.datasource.majordangersource.MajordangersourceDisposalMapper">

	<!--表名 -->
	<sql id="tableName">
		BUS_MAJORDANGERSOURCE_DISPOSAL
	</sql>

	<!--数据字典表名 -->
	<sql id="dicTableName">
		SYS_DICTIONARIES
	</sql>

	<!-- 字段 -->
	<sql id="Field">
		f.MAJORDANGERSOURCE_ID,
		f.ALARM_TYPE,
		f.ALARM_VALUE,
		f.ALARM_LEVEL,
		f.ALARM_RANGE_MIN,
		f.ALARM_RANGE_MAX,
		f.DISPOSAL_PERSON,
		f.DISPOSAL_METHOD,
		f.DISPOSAL_DESC,
		f.DISPOSAL_TIME,
		f.DISPOSAL_STATUS,
		f.DISPOSAL_TIME_OVER,
		f.ISDELETE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.CORPINFO_ID,
		f.MAJORDANGERSOURCE_DISPOSAL_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		MAJORDANGERSOURCE_ID,
		ALARM_TYPE,
		ALARM_VALUE,
		ALARM_LEVEL,
		ALARM_RANGE_MIN,
		ALARM_RANGE_MAX,
		DISPOSAL_PERSON,
		DISPOSAL_METHOD,
		DISPOSAL_DESC,
		DISPOSAL_TIME,
		DISPOSAL_STATUS,
		DISPOSAL_TIME_OVER,
		ISDELETE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		CORPINFO_ID,
		MAJORDANGERSOURCE_DISPOSAL_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{MAJORDANGERSOURCE_ID},
		#{ALARM_TYPE},
		#{ALARM_VALUE},
		#{ALARM_LEVEL},
		#{ALARM_RANGE_MIN},
		#{ALARM_RANGE_MAX},
		#{DISPOSAL_PERSON},
		#{DISPOSAL_METHOD},
		#{DISPOSAL_DESC},
		#{DISPOSAL_TIME},
		#{DISPOSAL_STATUS},
		#{DISPOSAL_TIME_OVER},
		#{ISDELETE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{CORPINFO_ID},
		#{MAJORDANGERSOURCE_DISPOSAL_ID}
	</sql>

	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into
	<include refid="tableName"></include>
		(
	<include refid="Field2"></include>
		) values (
	<include refid="FieldValue"></include>
		)
	</insert>

	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1',
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME}
		where
			MAJORDANGERSOURCE_DISPOSAL_ID = #{MAJORDANGERSOURCE_DISPOSAL_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ALARM_TYPE = #{ALARM_TYPE},
			ALARM_VALUE = #{ALARM_VALUE},
			ALARM_LEVEL = #{ALARM_LEVEL},
			ALARM_RANGE_MIN = #{ALARM_RANGE_MIN},
			ALARM_RANGE_MAX = #{ALARM_RANGE_MAX},
			DISPOSAL_STATUS = #{DISPOSAL_STATUS},
			DISPOSAL_PERSON = #{DISPOSAL_PERSON},
			DISPOSAL_METHOD = #{DISPOSAL_METHOD},
			DISPOSAL_DESC = #{DISPOSAL_DESC},
			DISPOSAL_TIME = #{DISPOSAL_TIME},
			DISPOSAL_TIME_OVER = #{DISPOSAL_TIME_OVER},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			MAJORDANGERSOURCE_DISPOSAL_ID = MAJORDANGERSOURCE_DISPOSAL_ID
		where
			MAJORDANGERSOURCE_DISPOSAL_ID = #{MAJORDANGERSOURCE_DISPOSAL_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		m.MAJORDANGERSOURCE_NAME,
		m.MAJORDANGERSOURCE_NUMBER,
		m.MAJORDANGERSOURCE_ADDRESS,
		m.MAJORDANGERSOURCE_LONGITUDE,
		m.MAJORDANGERSOURCE_LATITUDE,
		d2.NAME ALARM_TYPE_NAME,
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		 left join BUS_MAJORDANGERSOURCE m on m.MAJORDANGERSOURCE_ID = f.MAJORDANGERSOURCE_ID
		 left join SYS_DICTIONARIES_CORP d2 on f.ALARM_TYPE = d2.BIANMA and d2.PARENT_ID = '6a724d36c3ad416fad22049d932c1987'
		where
			f.MAJORDANGERSOURCE_DISPOSAL_ID = #{MAJORDANGERSOURCE_DISPOSAL_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		m.MAJORDANGERSOURCE_NAME,
		m.MAJORDANGERSOURCE_NUMBER,
		m.MAJORDANGERSOURCE_ADDRESS,
		m.MAJORDANGERSOURCE_LONGITUDE,
		m.MAJORDANGERSOURCE_LATITUDE,
		d2.NAME ALARM_TYPE_NAME,
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		left join BUS_MAJORDANGERSOURCE m on m.MAJORDANGERSOURCE_ID = f.MAJORDANGERSOURCE_ID
		left join SYS_DICTIONARIES_CORP d2 on f.ALARM_TYPE = d2.BIANMA and d2.PARENT_ID = '6a724d36c3ad416fad22049d932c1987'
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and 1 = 1
		</if>
		<if test="pd.DISPOSAL_STATUS != null and pd.DISPOSAL_STATUS != ''"><!-- 处置状态 -->
			and f.DISPOSAL_STATUS = #{pd.DISPOSAL_STATUS}
		</if>
		<if test="pd.ALARM_LEVEL != null and pd.ALARM_LEVEL != ''"><!-- 处置状态 -->
			and f.ALARM_LEVEL = #{pd.ALARM_LEVEL}
		</if>
		<if test="pd.MAJORDANGERSOURCE_ID != null and pd.MAJORDANGERSOURCE_ID != ''"><!-- 所属重大危险源 -->
			and f.MAJORDANGERSOURCE_ID = #{pd.MAJORDANGERSOURCE_ID}
		</if>
		ORDER BY f.CREATOR DESC
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		d2.NAME ALARM_TYPE_NAME,
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		left join SYS_DICTIONARIES_CORP d2 on f.ALARM_TYPE = d2.BIANMA and d2.PARENT_ID = '6a724d36c3ad416fad22049d932c1987'
		where f.ISDELETE = '0' and f.DISPOSAL_STATUS = '-1'
		<if test="MAJORDANGERSOURCE_ID != null and MAJORDANGERSOURCE_ID != ''"><!-- 所属重大危险源 -->
			and f.MAJORDANGERSOURCE_ID = #{MAJORDANGERSOURCE_ID}
		</if>
		ORDER BY f.CREATOR DESC
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1',
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME}
		where
			MAJORDANGERSOURCE_DISPOSAL_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

	<!-- 列表(根据所选ID获取数据) -->
	<select id="findByIds" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			MAJORDANGERSOURCE_DISPOSAL_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
				#{item}
		</foreach>
	</select>

	<!-- 报警升级 -->
	<update id="upLevel" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		DISPOSAL_STATUS = '0',
		OPERATTIME = #{OPERATTIME}
		where
		MAJORDANGERSOURCE_DISPOSAL_ID = #{MAJORDANGERSOURCE_DISPOSAL_ID}
	</update>

	<!-- 根据重大危险源分组统计报警处置情况 -->
	<select id="statisticsGroupMajordangersource" parameterType="pd" resultType="pd">
		SELECT m.MAJORDANGERSOURCE_NAME
			, COUNT(case when f.DISPOSAL_STATUS != '0' AND  f.ISDELETE = '0' then f.MAJORDANGERSOURCE_DISPOSAL_ID END) COUNTALARM
			, COUNT(case when f.DISPOSAL_STATUS = '1' AND  f.ISDELETE = '0' then f.MAJORDANGERSOURCE_DISPOSAL_ID END) COUNTDISPOSED
			, COUNT(case when f.DISPOSAL_STATUS = '-1' AND  f.ISDELETE = '0' then f.MAJORDANGERSOURCE_DISPOSAL_ID END) COUNTDISPOSAL
		  FROM bus_majordangersource m
		 LEFT JOIN bus_majordangersource_disposal f ON m.MAJORDANGERSOURCE_ID = f.MAJORDANGERSOURCE_ID
		WHERE m.CORPINFO_ID = #{CORPINFO_ID}
			 GROUP BY m.MAJORDANGERSOURCE_ID
			 ORDER BY m.MAJORDANGERSOURCE_NUMBER
	</select>

	<!-- 重大危险源处置统计数据（报警数【COUNTALARM】，已处置【COUNTDISPOSED】，处置中【COUNTDISPOSAL】） -->
	<select id="statisticsDisposal" parameterType="pd" resultType="pd">
		SELECT
			COUNT(case when f.DISPOSAL_STATUS != '0' then f.MAJORDANGERSOURCE_DISPOSAL_ID END) COUNTALARM,
			COUNT(case when f.DISPOSAL_STATUS = '1' then f.MAJORDANGERSOURCE_DISPOSAL_ID END) COUNTDISPOSED,
			COUNT(case when f.DISPOSAL_STATUS = '-1' then f.MAJORDANGERSOURCE_DISPOSAL_ID END) COUNTDISPOSAL
		FROM bus_majordangersource_disposal f
			WHERE f.ISDELETE = '0' AND f.CORPINFO_ID = #{CORPINFO_ID}
	</select>

	<!-- 根据处置状态按日期分组统计处置数据 -->
	<select id="statisticsGroupDateByState" parameterType="pd" resultType="pd">
		SELECT f.CREATDATE,
			COUNT(case when f.DISPOSAL_STATUS != '0' then f.MAJORDANGERSOURCE_DISPOSAL_ID END) COUNTALARM,
			COUNT(case when f.DISPOSAL_STATUS = '1' then f.MAJORDANGERSOURCE_DISPOSAL_ID END) COUNTDISPOSED,
			COUNT(case when f.DISPOSAL_STATUS = '-1' then f.MAJORDANGERSOURCE_DISPOSAL_ID END) COUNTDISPOSAL
		FROM (SELECT f.MAJORDANGERSOURCE_DISPOSAL_ID, f.DISPOSAL_STATUS, SUBSTR(f.CREATTIME, 1, 10) CREATDATE
				FROM bus_majordangersource_disposal f
					WHERE f.ISDELETE = '0' AND f.CORPINFO_ID = #{CORPINFO_ID}
					 AND f.CREATTIME >= CONCAT_WS(" ", DATE_FORMAT(DATE_SUB(CURDATE(),INTERVAL 5 DAY),'%Y-%m-%d'), '00:00:00')
		) f GROUP BY f.CREATDATE
	</select>

</mapper>
