<?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.keyProjects.KeyprojectHiddenMapper">

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

	<!--数据字典表名 -->
	<sql id="dicTableName">
		sys_dictionaries
	</sql>
	<!-- 字段 -->
	<sql id="Field">
		f.SOURCE,
		f.RISK_UNIT,
		f.IDENTIFICATION,
		f.RISK_DESCR,
		f.RISK_POSITION,
		f.LEVEL,
		f.CHECK_CONTENT,
		f.HIDDENDESCR,
		f.HIDDENPART,
		f.CREATOR,
		f.CREATTIME,
		f.RECTIFYDESCR,
		f.RECTIFICATIONTYPE,
		f.RECTIFICATIONOR,
		f.RECTIFICATIONTIME,
		f.HIDDENLEVEL,
		f.STATE,
		f.CHECKDEPT,
		f.CHECKOR,
		f.CHECKTIME,
		f.CHECKDESCR,
		f.ISQUALIFIED,
		f.ISDELETE,
		f.CORPINFO_ID,
		f.HIDDENFINDDEPT,
		f.RECTIFICATIONDEADLINE,
		f.RECTIFICATIONDEPT,
		f.HIDDEN_ID,
		f.CHECKRECORD_ID,
		f.RECORDITEM_ID,
		f.RISKITEM_ID,
		f.REVIEWOR,
		f.REVIEWTIME,
		f.REVIEWDEPT,
		f.LONGITUDE,
		f.LATITUDE,
		f.HAVESCHEME,
		f.HIDDENTYPE,
		f.ISCONFIRM,
		f.CONFIRM_USER,
		f.DISCOVERYTIME,
		f.CONFIRM_TIME,
		f.HIDDENTYPE2,
		f.INVESTMENT_FUNDS,
		f.LISTMANAGER_ID,
		f.FOREIGN_ID,
		f.FINAL_CHECK,
		f.FINAL_CHECKOR,
		f.FINAL_CHECKTIME,
		f.FINAL_CHECKDESCR,
		f.ISRELEVANT,
		f.ISPUNISH,
		f.POSITIONDESC,
		f.HIDDENTYPE_NAME
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		ISPUNISH,
        ISRELEVANT,
		SOURCE,
		RISK_UNIT,
		IDENTIFICATION,
		RISK_DESCR,
		RISK_POSITION,
		LEVEL,
		CHECK_CONTENT,
		HIDDENDESCR,
		HIDDENPART,
		CREATOR,
		CREATTIME,
		RECTIFYDESCR,
		RECTIFICATIONTYPE,
		RECTIFICATIONOR,
		RECTIFICATIONTIME,
		HIDDENLEVEL,
		STATE,
		CHECKDEPT,
		CHECKOR,
		CHECKTIME,
		CHECKDESCR,
		ISQUALIFIED,
		ISDELETE,
		CORPINFO_ID,
		HIDDENFINDDEPT,
		RECTIFICATIONDEADLINE,
		RECTIFICATIONDEPT,
		HIDDEN_ID,
		CHECKRECORD_ID,
		RECORDITEM_ID,
		RISKITEM_ID,
		REVIEWOR,
		REVIEWTIME,
		REVIEWDEPT,
		LONGITUDE,
		LATITUDE,
		HAVESCHEME,
		HIDDENTYPE,
		ISCONFIRM,
		CONFIRM_USER,
		DISCOVERYTIME,
		HIDDENTYPE2,
		INVESTMENT_FUNDS,
		LISTMANAGER_ID,
		FOREIGN_ID,
		FINAL_CHECK,
		FINAL_CHECKOR,
		FINAL_CHECKTIME,
		FINAL_CHECKDESCR,
		POSITIONDESC,
		HIDDENTYPE_NAME
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{ISPUNISH},
		#{ISRELEVANT},
		#{SOURCE},
		#{RISK_UNIT},
		#{IDENTIFICATION},
		#{RISK_DESCR},
		#{RISK_POSITION},
		#{LEVEL},
		#{CHECK_CONTENT},
		#{HIDDENDESCR},
		#{HIDDENPART},
		#{CREATOR},
		#{CREATTIME},
		#{RECTIFYDESCR},
		#{RECTIFICATIONTYPE},
		#{RECTIFICATIONOR},
		#{RECTIFICATIONTIME},
		#{HIDDENLEVEL},
		#{STATE},
		#{CHECKDEPT},
		#{CHECKOR},
		#{CHECKTIME},
		#{CHECKDESCR},
		#{ISQUALIFIED},
		#{ISDELETE},
		#{CORPINFO_ID},
		#{HIDDENFINDDEPT},
		#{RECTIFICATIONDEADLINE},
		#{RECTIFICATIONDEPT},
		#{HIDDEN_ID},
		#{CHECKRECORD_ID},
		#{RECORDITEM_ID},
		#{RISKITEM_ID},
		#{REVIEWOR},
		#{REVIEWTIME},
		#{REVIEWDEPT},
		#{LONGITUDE},
		#{LATITUDE},
		#{HAVESCHEME},
		#{HIDDENTYPE},
		#{ISCONFIRM},
		#{CONFIRM_USER},
		#{DISCOVERYTIME},
		#{HIDDENTYPE2},
		#{INVESTMENT_FUNDS},
		#{LISTMANAGER_ID},
		#{FOREIGN_ID},
		#{FINAL_CHECK},
		#{FINAL_CHECKOR},
		#{FINAL_CHECKTIME},
		#{FINAL_CHECKDESCR},
		#{POSITIONDESC},
		#{HIDDENTYPE_NAME}
	</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">
		delete from
		<include refid="tableName"></include>
		where
			HIDDEN_ID = #{HIDDEN_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISPUNISH = #{ISPUNISH},
		    ISRELEVANT = #{ISRELEVANT},
			SOURCE = #{SOURCE},
			RISK_UNIT = #{RISK_UNIT},
			IDENTIFICATION = #{IDENTIFICATION},
			RISK_DESCR = #{RISK_DESCR},
			RISK_POSITION = #{RISK_POSITION},
			LEVEL = #{LEVEL},
			CHECK_CONTENT = #{CHECK_CONTENT},
			HIDDENDESCR = #{HIDDENDESCR},
			HIDDENPART = #{HIDDENPART},
			RECTIFYDESCR = #{RECTIFYDESCR},
			RECTIFICATIONTYPE = #{RECTIFICATIONTYPE},
			RECTIFICATIONDEPT = #{RECTIFICATIONDEPT},
			RECTIFICATIONOR = #{RECTIFICATIONOR},
			RECTIFICATIONTIME = #{RECTIFICATIONTIME},
			HIDDENLEVEL = #{HIDDENLEVEL},
			STATE = #{STATE},
			CHECKDEPT = #{CHECKDEPT},
			CHECKOR = #{CHECKOR},
			CHECKDESCR = #{CHECKDESCR},
			ISQUALIFIED = #{ISQUALIFIED},
			ISDELETE = #{ISDELETE},
			CORPINFO_ID = #{CORPINFO_ID},
			HIDDENFINDDEPT = #{HIDDENFINDDEPT},
			CHECKRECORD_ID = #{CHECKRECORD_ID},
			RECORDITEM_ID = #{RECORDITEM_ID},
			RISKITEM_ID = #{RISKITEM_ID},
			REJECTREASON = #{REJECTREASON},
			REVIEWOR = #{REVIEWOR},
			REVIEWTIME = #{REVIEWTIME},
			REVIEWDEPT = #{REVIEWDEPT},
			HAVESCHEME = #{HAVESCHEME},
			LONGITUDE = #{LONGITUDE},
			LATITUDE = #{LATITUDE},
			HIDDEN_ID = HIDDEN_ID
		where
			HIDDEN_ID = #{HIDDEN_ID}
	</update>

	<!-- 只用与修改隐患状态 -->
	<update id="editstate" parameterType="pd">
		update
		<include refid="tableName"></include>
		<trim prefix="SET" suffixOverrides=",">
			<if test="STATE != null and STATE != ''">
				STATE = #{STATE},
			</if>
			<if test="RECTIFICATIONTIME != null and RECTIFICATIONTIME != ''">
				RECTIFICATIONTIME = #{RECTIFICATIONTIME},
			</if>
			<if test="RECTIFICATIONOR != null and RECTIFICATIONOR != ''">
				RECTIFICATIONOR = #{RECTIFICATIONOR},
			</if>
			<if test="RECTIFICATIONDEPT != null and RECTIFICATIONDEPT != ''">
				RECTIFICATIONDEPT = #{RECTIFICATIONDEPT},
			</if>
			<if test="CHECKOR != null and CHECKOR != ''">
				CHECKOR = #{CHECKOR},
			</if>
			<if test="ISPUNISH != null and ISPUNISH != ''">
				ISPUNISH = #{ISPUNISH},
			</if>
			<if test="PUNISH_PERSON != null and PUNISH_PERSON != ''">
				PUNISH_PERSON = #{PUNISH_PERSON},
			</if>
			<if test="HIDDENLEVEL != null and HIDDENLEVEL != ''">
				HIDDENLEVEL = #{HIDDENLEVEL},
			</if>
			<if test="CHECKTIME != null and CHECKTIME != ''">
				CHECKTIME = #{CHECKTIME},
			</if>
		</trim>
		where
		HIDDEN_ID = #{HIDDEN_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		f.*,
		hr.HIDDENREGION HIDDENPART_NAME,
		lv.NAME HIDDENLEVEL_NAME,
		ht2.NAME HIDDENTYPE2_NAME,
		crd.NAME HIDDENFINDDEPT_NAME,
		cr.NAME CREATOR_NAME,
		u.UNITS_NAME as RECTIFICATIONDEPT_NAME,
		p.NAME as RECTIFICATIONOR_NAME
		from
		<include refid="tableName"></include> f
		left join bus_hiddenregion hr on hr.HIDDENREGION_ID = f.HIDDENPART
		left join <include refid="dicTableName"></include> lv on f.HIDDENLEVEL = lv.BIANMA
		left join <include refid="dicTableName"></include> ht on f.HIDDENTYPE = ht.BIANMA
		left join <include refid="dicTableName"></include> ht2 on f.HIDDENTYPE2 = ht2.BIANMA and ht2.PARENT_ID = '18c0a9aea6e54feab9f4ab3f46e0bc86'
		left join oa_department crd on crd.DEPARTMENT_ID = f.HIDDENFINDDEPT
		left join vi_user_all cr on cr.USER_ID = f.CREATOR
		left join BUS_UNITS u on u.UNITS_ID = f.RECTIFICATIONDEPT
		left join BUS_PERSONNELMANAGEMENT p on p.PERSONNELMANAGEMENT_ID = f.RECTIFICATIONOR
		where
		f.HIDDEN_ID = #{HIDDEN_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		f.*,
		hr.HIDDENREGION HIDDENPART_NAME,
		lv.NAME HIDDENLEVEL_NAME,
		ht2.NAME HIDDENTYPE2_NAME,
		crd.NAME HIDDENFINDDEPT_NAME,
		cr.NAME CREATOR_NAME,
		u.UNITS_NAME as RECTIFICATIONDEPT_NAME,
		p.NAME as RECTIFICATIONOR_NAME,
		bo.OUTSOURCED_NAME,
		bo.PUNISH_THE_PERSON,
		bod.OUTSOURCED_NAME AIOUTSOURCED_NAME,
		pu.KEYPROJECTPUNISH_ID
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_KEYPROJECTCHECK ch ON ch.OUTSOURCED_ID = f.FOREIGN_ID or  f.FOREIGN_ID = ch.KEYPROJECTCHECK_ID
		LEFT JOIN BUS_OUTSOURCED bo ON bo.OUTSOURCED_ID = ch.OUTSOURCED_ID <!-- 用于安全环保检查隐患的重点工程名称 -->
		left join bus_hiddenregion hr on hr.HIDDENREGION_ID = f.HIDDENPART
		left join <include refid="dicTableName"></include> lv on f.HIDDENLEVEL = lv.BIANMA
		left join <include refid="dicTableName"></include> ht on f.HIDDENTYPE = ht.BIANMA
		left join <include refid="dicTableName"></include> ht2 on f.HIDDENTYPE2 = ht2.BIANMA and ht2.PARENT_ID = '18c0a9aea6e54feab9f4ab3f46e0bc86'
		left join oa_department crd on crd.DEPARTMENT_ID = f.HIDDENFINDDEPT
		left join BUS_UNITS u on u.UNITS_ID = f.RECTIFICATIONDEPT
		left join vi_user_all cr on cr.USER_ID = f.CREATOR
		left join BUS_OUTSOURCED o on o.UNITS_ID = u.UNITS_ID
		LEFT JOIN BUS_OUTSOURCED bod ON bod.OUTSOURCED_ID = f.FOREIGN_ID <!-- 用于AI隐患的重点工程名称 -->
		left join BUS_PERSONNELMANAGEMENT p on p.PERSONNELMANAGEMENT_ID = f.RECTIFICATIONOR
		left join BUS_KEYPROJECTPUNISH pu on pu.HIDDEN_ID = f.HIDDEN_ID
		<if test="pd.CREATOR != null and pd.CREATOR !=''" >
		left join (
		SELECT
		f.HIDDEN_ID
		FROM
		BUS_KEYPROJECT_HIDDEN f
		LEFT JOIN bus_keyprojectcheck keycheck ON keycheck.KEYPROJECTCHECK_ID = f.FOREIGN_ID
		LEFT JOIN BUS_OUTSOURCED bo ON keycheck.OUTSOURCED_ID = bo.OUTSOURCED_ID
		LEFT JOIN BUS_OUTSOURCED boai ON f.FOREIGN_ID = boai.OUTSOURCED_ID
		LEFT JOIN BUS_KEYPROJECTPUNISH pu ON pu.HIDDEN_ID = f.HIDDEN_ID
		WHERE
		f.ISDELETE = '0'
		AND f.SOURCE IN ( '4', '5', '1' )
		AND f.STATE = '2'
		AND f.CREATOR = #{pd.CREATOR}
		AND ifnull(bo.OUTSOURCED_ID,boai.OUTSOURCED_ID) IS NOT NULL
		) t on t.HIDDEN_ID = f.HIDDEN_ID
		</if>
		where f.ISDELETE = '0'
		<if test="pd.OUTSOURCED_ID != null and pd.OUTSOURCED_ID != ''">
			and bo.OUTSOURCED_ID = #{pd.OUTSOURCED_ID}
           </if>
           <if test="pd.ISRECTIFY != null and pd.ISRECTIFY !='' and pd.ISRECTIFY == 1" >
               and f.STATE = 1
           </if>
           <if test="pd.ISRECTIFY != null and pd.ISRECTIFY !='' and pd.ISRECTIFY == 2" >
               and f.STATE &gt; 1
           </if>
			<if test="pd.ISCHECK != null and pd.ISCHECK !='' and pd.ISCHECK == 1" >
				and f.STATE = 2
				<if test="pd.CREATOR != null and pd.CREATOR !=''" >
					and (f.CREATOR = #{pd.CREATOR} or bo.PUNISH_THE_PERSON like CONCAT(CONCAT('%', #{pd.CREATOR}),'%'))
				</if>
			</if>
           <if test="pd.ISCHECK != null and pd.ISCHECK !='' and pd.ISCHECK == 2" >
               and f.STATE = 4
           </if>
           <if test="pd.HIDDENLEVEL != null and pd.HIDDENLEVEL !=''" >
               and f.HIDDENLEVEL = #{pd.HIDDENLEVEL}
           </if>
           <if test="pd.RECTIFICATIONOR != null and pd.RECTIFICATIONOR !=''" >
               and f.RECTIFICATIONOR = #{pd.RECTIFICATIONOR}
           </if>
           <if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			<!--	根据需求自己加检索条件-->
			f.HIDDENDESCR LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		GROUP BY f.HIDDEN_ID
		order by
		<if test="pd.CREATOR != null and pd.CREATOR !=''" >
		t.HIDDEN_ID desc,
		</if>
		f.CREATTIME  desc, f.STATE desc
	</select>

	<!-- 隐患导出EXCEL（企业隐患管理使用） -->
	<select id="listExcelHidden" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		c.CORP_NAME,
		type.NAME as TYPE_NAME,
		city.NAME as CITY_NAME,
		country.NAME as COUNTRY_NAME,
		village.NAME as VILLAGE_NAME,
		cr.NAME CREATORNAME,
		re.NAME RECTIFICATIONORNAME,
		ch.NAME CHECKORNAME,
		rev.NAME REVIEWORNAME,
		crd.NAME HIDDENFINDDEPTNAME,
		red.NAME RECTIFICATIONDEPTNAME,
		chd.NAME CHECKDEPTNAME,
		lv.NAME HIDDENLEVELNAME,
		(select group_concat(chr.NAME) from BUS_HIDDENCHECK bh left join sys_user chr on chr.USER_ID=bh.USER_ID where bh.HIDDEN_ID=f.HIDDEN_ID ) CHECKNAME
		from
		<include refid="tableName"></include> f
		left join BUS_CORP_INFO c on c.CORPINFO_ID = f.CORPINFO_ID
		left join BUS_CORPTYPE type on type.CORPTYPE_ID = c.CORP_OF_TYPE
		left join sys_dictionaries city on city.BIANMA = c.CITY
		left join sys_dictionaries country on country.BIANMA = c.COUNTRY
		left join sys_dictionaries village on village.BIANMA = c.VILLAGE

		left join sys_user cr on cr.USER_ID = f.CREATOR
		left join sys_user re on re.USER_ID = f.RECTIFICATIONOR
		left join sys_user ch on ch.USER_ID = f.CHECKOR
		left join sys_user rev on rev.USER_ID = f.REVIEWOR

		left join oa_department crd on crd.DEPARTMENT_ID = f.HIDDENFINDDEPT
		left join oa_department red on red.DEPARTMENT_ID = f.RECTIFICATIONDEPT
		left join oa_department chd on chd.DEPARTMENT_ID = f.CHECKDEPT

		left join <include refid="dicTableName"></include> lv on f.HIDDENLEVEL = lv.BIANMA
		where f.ISDELETE = '0' and f.STATE != '0'
		<if test="IDS != null and IDS != ''">
			and f.HIDDEN_ID in
	        <foreach item="item" index="index" collection="IDS" open="(" separator="," close=")">
	            #{item}
	        </foreach>
		</if>
		order by f.CREATTIME  desc, f.STATE desc
	</select>

	<!-- 列表 -->
	<select id="majorlistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		c.CORP_NAME,
		type.NAME as TYPE_NAME,
		city.NAME as CITY_NAME,
		country.NAME as COUNTRY_NAME,
		village.NAME as VILLAGE_NAME,
		cr.NAME CREATORNAME,
		re.NAME RECTIFICATIONORNAME,
		ch.NAME CHECKORNAME,
		rev.NAME REVIEWORNAME,
		crd.NAME HIDDENFINDDEPTNAME,
		red.NAME RECTIFICATIONDEPTNAME,
		chd.NAME CHECKDEPTNAME,
		lv.NAME HIDDENLEVELNAME,
		(select group_concat(chr.NAME) from BUS_HIDDENCHECK bh left join sys_user chr on chr.USER_ID=bh.USER_ID where bh.HIDDEN_ID=f.HIDDEN_ID ) CHECKNAME,
		ma.IS_LISTING,
		ma.STATE as MA_STATE,
		ma.MAJORHIDDEN_ID
		from
		<include refid="tableName"></include> f
		left join BUS_CORP_INFO c on c.CORPINFO_ID = f.CORPINFO_ID
		left join BUS_CORPTYPE type on type.CORPTYPE_ID = c.CORP_OF_TYPE
		left join sys_dictionaries city on city.BIANMA = c.CITY
		left join sys_dictionaries country on country.BIANMA = c.COUNTRY
		left join sys_dictionaries village on village.BIANMA = c.VILLAGE

		left join sys_user cr on cr.USER_ID = f.CREATOR
		left join sys_user re on re.USER_ID = f.RECTIFICATIONOR
		left join sys_user ch on ch.USER_ID = f.CHECKOR
		left join sys_user rev on rev.USER_ID = f.REVIEWOR

		left join oa_department crd on crd.DEPARTMENT_ID = f.HIDDENFINDDEPT
		left join oa_department red on red.DEPARTMENT_ID = f.RECTIFICATIONDEPT
		left join oa_department chd on chd.DEPARTMENT_ID = f.CHECKDEPT

		left join 	<include refid="dicTableName"></include> lv on f.HIDDENLEVEL = lv.BIANMA

		left join BUS_MAJORHIDDEN ma on ma.HIDDEN_ID = f.HIDDEN_ID
		where f.ISDELETE = '0' and f.STATE != '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					f.RISK_UNIT LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					or
					f.RISK_POSITION LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					or
					f.CHECK_CONTENT LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					or
					f.HIDDENDESCR LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					or
					c.CORP_NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.IS_COUNTYR != null and pd.IS_COUNTYR != ''">
			and ma.STATE = 2 or (ma.STATE&gt;2 and ma.DEPARTMENT_ID = pd.MA_DEPARTMENT_ID)
		</if>

		<if test="pd.IS_STREET != null and pd.IS_STREET != ''">
			and ma.STATE = 1 or (ma.STATE&gt;2 and ma.DEPARTMENT_ID = pd.MA_DEPARTMENT_ID)
		</if>



		<if test="pd.DEPT_PROVINCE != null and pd.DEPT_PROVINCE != ''">
			and c.PROVINCE = #{pd.DEPT_PROVINCE}
		</if>
		<if test="pd.DEPT_CITY != null and pd.DEPT_CITY != ''">
			and c.CITY = #{pd.DEPT_CITY}
		</if>
		<if test="pd.DEPT_COUNTRY != null and pd.DEPT_COUNTRY != ''">
			and c.COUNTRY = #{pd.DEPT_COUNTRY}
		</if>
		<if test="pd.DEPT_VILLAGE != null and pd.DEPT_VILLAGE != ''">
			and c.VILLAGE = #{pd.DEPT_VILLAGE}
		</if>
		<if test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">
			and c.CORP_OF_TYPE in (select CORPTYPE_ID from BUS_CORPTYPETODEPT ctod where ctod.DEPARTMENT_ID=#{pd.DEPARTMENT_ID})
		</if>

		<if test="pd.COUNTRY != null and pd.COUNTRY != ''">
			and c.COUNTRY = #{pd.COUNTRY}
		</if>
		<if test="pd.VILLAGE != null and pd.VILLAGE != ''">
			and c.VILLAGE = #{pd.VILLAGE}
		</if>
		<if test="pd.CORP_OF_TYPE != null and pd.CORP_OF_TYPE != ''">
			and c.CORP_OF_TYPE = #{pd.CORP_OF_TYPE}
		</if>

		<if test="pd.STARTTIME != null and pd.STARTTIME != ''"><!-- 关键词检索 -->
			and f.RECTIFICATIONDEADLINE &gt;= #{pd.STARTTIME}
		</if>
		<if test="pd.ENDTIME != null and pd.ENDTIME != ''">
			and f.RECTIFICATIONDEADLINE &lt;= #{pd.ENDTIME}
		</if>
		<if test="pd.STATE != null and pd.STATE !='' and pd.STATE != 0" >
			and f.STATE = #{pd.STATE}
		</if>
		<if test="pd.MAJOR_STATE != null and pd.MAJOR_STATE !='' and pd.MAJOR_STATE != 0" >
			and ma.STATE = #{pd.MAJOR_STATE}
		</if>
		<if test="pd.HIDDENLEVEL != null and pd.HIDDENLEVEL !=''" >
			and f.HIDDENLEVEL = #{pd.HIDDENLEVEL}
		</if>
		order by f.CREATTIME  desc
	</select>

	<!-- 隐患统计 -->
	<select id="reportedChart" parameterType="pd" resultType="pd">
		select
		count(c.PROVINCE) value,
		<if test="CHARTTYPE != null and CHARTTYPE != ''"><!-- 关键词检索 -->
			<choose>
				<when test="CHARTTYPE==1"><!-- 按省统计 -->
					c.PROVINCE as name
				</when>
				<when test="CHARTTYPE=='2'"><!-- 按市统计 -->
					c.CITY as name
				</when>
				<when test="CHARTTYPE=='3'"><!-- 按区县统计 -->
					c.COUNTY as name
				</when>
			</choose>
		</if>

		from
		<include refid="tableName"></include> f
		left join bus_corp_info c on c.CORPINFO_ID = f.CORPINFO_ID
		where f.ISDELETE  = '0' and  c.ISDELETE = '0'
		<if test = "STATE != null and STATE != ''">
			and f.STATE = #{STATE}
		</if>
		<if test="CHARTTYPE != null and CHARTTYPE != ''"><!-- 关键词检索 -->
			<choose>
				<when test="CHARTTYPE==1"><!-- 按省统计 -->
					GROUP BY c.PROVINCE
					ORDER BY c.PROVINCE
				</when>
				<when test="CHARTTYPE=='2'"><!-- 按市统计 -->
					GROUP BY c.CITY
					ORDER BY c.CITY
				</when>
				<when test="CHARTTYPE=='3'"><!-- 按区县统计 -->
					GROUP BY c.COUNTY
					ORDER BY c.COUNTY
				</when>
			</choose>
		</if>
	</select>

		<!-- 隐患统计（折线图） -->
	<select id="lineChart" parameterType="pd" resultType="pd">
		SELECT
			d.NAME,
			IFNULL( h.NUM1, 0 ) NUM1,
			IFNULL( h.NUM2, 0 ) NUM2
		FROM
			sys_dictionaries d
			LEFT JOIN (
				SELECT
					COUNT( f.STATE = 4 OR NULL ) AS NUM1,
					COUNT( f.STATE = 1 OR NULL ) AS NUM2,
					c.CORP_OF_TYPE
				FROM
					BUS_HIDDEN f
					LEFT JOIN bus_corp_info c ON c.CORPINFO_ID = f.CORPINFO_ID
				WHERE
					f.ISDELETE = '0'
					AND c.ISDELETE = '0'
				GROUP BY
					c.CORP_OF_TYPE
				ORDER BY
					c.CORP_OF_TYPE
			) h ON h.CORP_OF_TYPE = d.DICTIONARIES_ID
		WHERE
			d.PARENT_ID = #{DICTIONARIES_ID}
			and d.YNDEL = 'no'
	</select>


	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		c.CORP_NAME,
		type.NAME as TYPE_NAME,
		city.NAME as CITY_NAME,
		country.NAME as COUNTRY_NAME,
		village.NAME as VILLAGE_NAME,
		cr.NAME CREATORNAME,
		re.NAME RECTIFICATIONORNAME,
		ch.NAME CHECKORNAME,
		rev.NAME REVIEWORNAME,
		crd.NAME HIDDENFINDDEPTNAME,
		red.NAME RECTIFICATIONDEPTNAME,
		chd.NAME CHECKDEPTNAME,
		lv.NAME HIDDENLEVELNAME,
		(select group_concat(chr.NAME) from BUS_HIDDENCHECK bh left join sys_user chr on chr.USER_ID=bh.USER_ID where bh.HIDDEN_ID=f.HIDDEN_ID ) CHECKNAME
		from
		<include refid="tableName"></include> f
		left join BUS_CORP_INFO c on c.CORPINFO_ID = f.CORPINFO_ID
		left join BUS_CORPTYPE type on type.CORPTYPE_ID = c.CORP_OF_TYPE
		left join sys_dictionaries city on city.BIANMA = c.CITY
		left join sys_dictionaries country on country.BIANMA = c.COUNTRY
		left join sys_dictionaries village on village.BIANMA = c.VILLAGE

		left join sys_user cr on cr.USER_ID = f.CREATOR
		left join sys_user re on re.USER_ID = f.RECTIFICATIONOR
		left join sys_user ch on ch.USER_ID = f.CHECKOR
		left join sys_user rev on rev.USER_ID = f.REVIEWOR

		left join oa_department crd on crd.DEPARTMENT_ID = f.HIDDENFINDDEPT
		left join oa_department red on red.DEPARTMENT_ID = f.RECTIFICATIONDEPT
		left join oa_department chd on chd.DEPARTMENT_ID = f.CHECKDEPT

		left join 	<include refid="dicTableName"></include> lv on f.HIDDENLEVEL = lv.BIANMA
		where f.ISDELETE = '0' and f.STATE != '0'
		<if test="KEYWORDS != null and KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					f.RISK_UNIT LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					or
					f.RISK_POSITION LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					or
					f.CHECK_CONTENT LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					or
					f.HIDDENDESCR LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					or
					c.CORP_NAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
				)
		</if>

		<if test="DEPT_PROVINCE != null and DEPT_PROVINCE != ''">
			and c.PROVINCE = #{DEPT_PROVINCE}
		</if>
		<if test="DEPT_CITY != null and DEPT_CITY != ''">
			and c.CITY = #{DEPT_CITY}
		</if>
		<if test="DEPT_COUNTRY != null and DEPT_COUNTRY != ''">
			and c.COUNTRY = #{DEPT_COUNTRY}
		</if>
		<if test="DEPT_VILLAGE != null and DEPT_VILLAGE != ''">
			and c.VILLAGE = #{DEPT_VILLAGE}
		</if>
		<if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">
			and c.CORP_OF_TYPE in (select CORPTYPE_ID from BUS_CORPTYPETODEPT ctod where ctod.DEPARTMENT_ID=#{DEPARTMENT_ID})
		</if>

		<if test="COUNTRY != null and COUNTRY != ''">
			and c.COUNTRY = #{COUNTRY}
		</if>
		<if test="VILLAGE != null and VILLAGE != ''">
			and c.VILLAGE = #{VILLAGE}
		</if>
		<if test="CORP_OF_TYPE != null and CORP_OF_TYPE != ''">
			and c.CORP_OF_TYPE = #{CORP_OF_TYPE}
		</if>

		<if test="STARTTIME != null and STARTTIME != ''"><!-- 关键词检索 -->
			and f.RECTIFICATIONDEADLINE &gt;= #{STARTTIME}
		</if>
		<if test="ENDTIME != null and ENDTIME != ''">
			and f.RECTIFICATIONDEADLINE &lt;= #{ENDTIME}
		</if>
		<if test="STATE != null and STATE !='' and STATE != 0" >
			and f.STATE = #{STATE}
		</if>

		<if test="HIDDENLEVEL != null and HIDDENLEVEL !=''" >
			and f.HIDDENLEVEL = #{HIDDENLEVEL}
		</if>
		order by f.CREATTIME  desc
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		delete from
		<include refid="tableName"></include>
		where
			HIDDEN_ID in
		<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>
    <!-- 隐患统计 手机端 -->
    <select id="yhpcCountForApp" parameterType="pd" resultType="pd">
        select
        count(1) as count ,
         SUM(case when  h.HIDDENLEVEL ='hiddenLevel0001' THEN 1 ELSE 0 end )  as ybCount ,
         SUM(case when h.STATE !='4' THEN 1 ELSE 0 end )  as wzgCount
        from BUS_HIDDEN h
        LEFT JOIN bus_corp_info c on c.CORPINFO_ID = h.CORPINFO_ID
        where h.ISDELETE ='0'
        <if test="DEPT_PROVINCE != null and DEPT_PROVINCE != ''">
            and c.PROVINCE = #{DEPT_PROVINCE}
        </if>
        <if test="DEPT_CITY != null and DEPT_CITY != ''">
            and c.CITY = #{DEPT_CITY}
        </if>
        <if test="DEPT_COUNTRY != null and DEPT_COUNTRY != ''">
            and c.COUNTRY = #{DEPT_COUNTRY}
        </if>
        <if test="DEPT_VILLAGE != null and DEPT_VILLAGE != ''">
            and c.VILLAGE = #{DEPT_VILLAGE}
        </if>
        <if test="toTime != null and toTime != ''">
            and h.CREATTIME >= #{toTime}
            and h.CREATTIME &lt;= #{fromTime}
        </if>
	</select>
    <!-- 清单其他隐患 -->
    <select id="getOtherHidden" parameterType="pd" resultType="pd">
        SELECT
        <include refid="Field"></include>
        FROM
        BUS_HIDDEN f
        WHERE f.ISDELETE = '0'
			and f.SOURCE = '3'
        <if test="STATE != null and STATE != ''"><!-- 关键词检索 -->
            and
            f.STATE = #{STATE}
        </if>
        <if test="USER_ID != null and USER_ID != ''"><!-- 关键词检索 -->
            and
            f.CREATOR = #{USER_ID}
        </if>
        <if test="LISTMANAGER_ID != null and LISTMANAGER_ID != ''"><!-- 关键词检索 -->
            and
            f.LISTMANAGER_ID = #{LISTMANAGER_ID}
        </if>
        <if test="CHECKRECORD_ID != null and CHECKRECORD_ID != ''"><!-- 关键词检索 -->
            and
            f.CHECKRECORD_ID = #{CHECKRECORD_ID}
        </if>
        <if test="IS_DAY != null and IS_DAY != ''"><!-- 关键词检索 -->
            and
            TO_DAYS(f.CREATTIME) = TO_DAYS(NOW())
        </if>
        <if test="IS_WEEK != null and IS_WEEK != ''"><!-- 关键词检索 -->
            and
            YEARWEEK(DATE_FORMAT(f.CREATTIME,'%Y-%m-%d')) = YEARWEEK(NOW())
        </if>
        <if test="IS_XUN != null and IS_XUN != ''"><!-- 关键词检索 -->
            <if test="BEGINTIME != null and BEGINTIME != ''"><!-- 关键词检索 -->
                and f.CREATTIME &gt;= #{BEGINTIME}
            </if>
            <if test="ENDTIME != null and ENDTIME != ''">
                and f.CREATTIME &lt;= #{ENDTIME}
            </if>
        </if>

        <if test="IS_MONTH != null and IS_MONTH != ''"><!-- 关键词检索 -->
            and
            DATE_FORMAT(f.CREATTIME,'%Y%m') = DATE_FORMAT(CURDATE(),'%Y%m')
        </if>
        <if test="IS_QUARTER != null and IS_QUARTER != ''"><!-- 关键词检索 -->
            and
            QUARTER(f.CREATTIME) = QUARTER(NOW())
        </if>
        <if test="IS_HALFYEAR != null and IS_HALFYEAR != ''">
            AND
            CONVERT(concat(DATE_FORMAT(f.CREATTIME, '%Y'), INTERVAL(CONVERT(MONTH(f.CREATTIME),SIGNED),7,13) ),SIGNED) =
            CONVERT(concat(DATE_FORMAT(NOW(), '%Y'), INTERVAL(CONVERT(MONTH(NOW()),SIGNED),7,13) ),SIGNED)
        </if>
        <if test="IS_YEAR != null and IS_YEAR != ''"><!-- 关键词检索 -->
            and
            YEAR(f.CREATTIME)=YEAR(NOW())
        </if>
        ORDER BY f.CREATTIME
    </select>
    <!-- 列表 -->
    <select id="getListForApplistPage" parameterType="page" resultType="pd">
        select
        <include refid="Field"></include>,
        cr.NAME CREATORNAME,
        re.NAME RECTIFICATIONORNAME,
        ch.NAME CHECKORNAME,
        rev.NAME REVIEWORNAME,
        crd.NAME HIDDENFINDDEPTNAME,
        red.NAME RECTIFICATIONDEPTNAME,
        chd.NAME CHECKDEPTNAME,
        lv.NAME HIDDENLEVELNAME,
        (select group_concat(chr.NAME) from BUS_HIDDENCHECK bh left join sys_user chr on chr.USER_ID=bh.USER_ID where bh.HIDDEN_ID=f.HIDDEN_ID ) CHECKNAME,
        ma.STATE as MA_STATE,
        ma.MAJORHIDDEN_ID
        from
        <include refid="tableName"></include> f
        left join sys_user cr on cr.USER_ID = f.CREATOR
        left join sys_user re on re.USER_ID = f.RECTIFICATIONOR
        left join sys_user ch on ch.USER_ID = f.CHECKOR
        left join sys_user rev on rev.USER_ID = f.REVIEWOR
        left join oa_department crd on crd.DEPARTMENT_ID = f.HIDDENFINDDEPT
        left join oa_department red on red.DEPARTMENT_ID = f.RECTIFICATIONDEPT
        left join oa_department chd on chd.DEPARTMENT_ID = f.CHECKDEPT
        left join  <include refid="dicTableName"></include> lv on f.HIDDENLEVEL = lv.BIANMA
        left join BUS_MAJORHIDDEN ma on ma.HIDDEN_ID = f.HIDDEN_ID
        where f.ISDELETE = '0' and f.STATE != '0'
        <if test="pd.HIDDENLEVEL != null and pd.HIDDENLEVEL !=''" >
            and f.HIDDENLEVEL = #{pd.HIDDENLEVEL}
        </if>
        <if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
            and
            (
            f.RISK_UNIT LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            or
            f.RISK_POSITION LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            or
            f.CHECK_CONTENT LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            or
            f.HIDDENDESCR LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
            )
        </if>

        <if test="pd.HIDDENDESCR != null and pd.HIDDENDESCR != ''"><!-- 关键词检索 -->
            and f.HIDDENDESCR LIKE CONCAT(CONCAT('%', #{pd.HIDDENDESCR}),'%')
        </if>
        <if test="pd.ISQUALIFIED != null and pd.ISQUALIFIED != ''"><!-- 关键词检索 -->
            and f.ISQUALIFIED = #{pd.ISQUALIFIED}
        </if>
        <if test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''"><!-- 关键词检索 -->
            and f.HIDDENFINDDEPT = #{pd.DEPARTMENT_ID}
        </if>
        <if test="pd.HIDDENLEVEL != null and pd.HIDDENLEVEL != ''"><!-- 关键词检索 -->
            and f.HIDDENLEVEL = #{pd.HIDDENLEVEL}
        </if>
        <if test="pd.STARTTIME != null and pd.STARTTIME != ''"><!-- 关键词检索 -->
            and f.CREATTIME &gt;= #{pd.STARTTIME}
        </if>
        <if test="pd.ENDTIME != null and pd.ENDTIME != ''">
            and f.CREATTIME &lt;= #{pd.ENDTIME}
        </if>
        <if test="pd.STATE != null and pd.STATE !='' and pd.STATE != 0" >
            and f.STATE = #{pd.STATE}
        </if>
        <if test="pd.SELF_USER_ID != null and pd.SELF_USER_ID != ''">
            and f.CREATOR = #{pd.SELF_USER_ID}
        </if>
        <if test="pd.DISPOSESTATE != null and pd.DISPOSESTATE != ''"><!-- 关键词检索 -->
            <if test="pd.DISPOSESTATE==2"><!-- 整改 -->
                <if test="pd.ISMAIN != null and pd.ISMAIN != ''">
                    <choose>
                        <when test="pd.ISMAIN == 1">
                            AND f.CORPINFO_ID = #{pd.CORPINFO_ID}
                        </when>
                        <otherwise>
                            and f.RECTIFICATIONOR = #{pd.SELFUSERID}
                        </otherwise>
                    </choose>
                </if>

                and f.STATE in('1','-1')
            </if>
            <if test="pd.DISPOSESTATE==3"><!-- 验收 -->
                and f.STATE = '3'
                <if test="pd.ISMAIN != null and pd.ISMAIN != ''">
                    <choose>
                        <when test="pd.ISMAIN == 1">
                            AND f.CORPINFO_ID = #{pd.CORPINFO_ID}
                        </when>
                        <otherwise>
                            and
                            (
                            select count(1)
                            from BUS_HIDDENCHECK bh
                            where
                            bh.HIDDEN_ID=f.HIDDEN_ID
                            and bh.USER_ID=#{pd.SELFUSERID}
                            and bh.STATUS=0
                            )>0
                        </otherwise>
                    </choose>
                </if>

            </if>
            <if test="pd.DISPOSESTATE==4"><!-- 已验收 -->
                and f.STATE = '4'

                <choose>
                    <when test="pd.ISMAIN == 1">
                        AND f.CORPINFO_ID = #{pd.CORPINFO_ID}
                    </when>
                    <otherwise>
                        and (
                        f.RECTIFICATIONOR = #{pd.SELFUSERID}
                        or f.CREATOR = #{pd.SELFUSERID}
                        or (
                        select count(1)
                        from BUS_HIDDENCHECK bh
                        where
                        bh.HIDDEN_ID=f.HIDDEN_ID
                        and bh.USER_ID=#{pd.SELFUSERID}
                        )>0
                        )
                    </otherwise>
                </choose>

            </if>
            <if test="pd.DISPOSESTATE==-1"><!-- 所有 -->
                <if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
                    and
                    f.CORPINFO_ID = #{pd.CORPINFO_ID}
                </if>
                <if test="pd.STATE != null and pd.STATE != ''"><!-- 关键词检索 -->
                    and
                    f.STATE = #{pd.STATE}
                </if>
            </if>
        </if>
        order by f.CREATTIME  desc
    </select>
    <!-- 首页统计 -->
    <select id="mainStatistics" parameterType="pd" resultType="pd">
        SELECT
        <if test="DEPT_COUNTRY == null or DEPT_COUNTRY == ''">
            country.NAME as GROUP_NAME,
        </if>
        <if test="DEPT_COUNTRY != null and DEPT_COUNTRY != ''">
            village.NAME as GROUP_NAME,
        </if>
        count(CASE WHEN h.HIDDEN_ID != '' THEN 1 ELSE NULL END) as SBHD_NUM,
        count(case when h.STATE=4 then 1 else null end) as ZGHD_NUM
        <if test="DEPT_COUNTRY == null or DEPT_COUNTRY == ''">
            from sys_dictionaries country
            LEFT join bus_corp_info f on country.BIANMA = f.COUNTRY and f.ISDELETE='0'
        </if>
        <if test="DEPT_COUNTRY != null and DEPT_COUNTRY != ''">
            from sys_dictionaries village
            LEFT join bus_corp_info f on village.BIANMA = f.VILLAGE and f.ISDELETE='0'
        </if>
        <if test="DEPT_PROVINCE != null and DEPT_PROVINCE != ''">
            and f.PROVINCE = #{DEPT_PROVINCE}
        </if>
        <if test="DEPT_CITY != null and DEPT_CITY != ''">
            and f.CITY = #{DEPT_CITY}
        </if>
        <if test="DEPT_COUNTRY != null and DEPT_COUNTRY != ''">
            and f.COUNTRY = #{DEPT_COUNTRY}
        </if>
        <if test="DEPT_VILLAGE != null and DEPT_VILLAGE != ''">
            and f.VILLAGE = #{DEPT_VILLAGE}
        </if>
        <if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">
            and f.CORP_OF_TYPE in (select CORPTYPE_ID from BUS_CORPTYPETODEPT ctod where ctod.DEPARTMENT_ID=#{DEPARTMENT_ID})
        </if>

        left join BUS_HIDDEN h on f.CORPINFO_ID=h.CORPINFO_ID and h.ISDELETE='0'
        where 1=1

        <if test="DEPT_COUNTRY == null or DEPT_COUNTRY == ''">
            and country.PARENT_ID = #{CITY_CODE}
        </if>
        <if test="DEPT_COUNTRY != null and DEPT_COUNTRY != ''">
            and village.PARENT_ID in (select DICTIONARIES_ID from sys_dictionaries dic where dic.BIANMA=#{DEPT_COUNTRY})
        </if>

        <if test="DEPT_COUNTRY == null or DEPT_COUNTRY == ''">
            group by f.COUNTRY,country.NAME
        </if>
        <if test="DEPT_COUNTRY != null and DEPT_COUNTRY != ''">
            group by f.VILLAGE,village.NAME
        </if>
    </select>

	<!-- 重大隐患核实列表 -->
	<select id="verifydatalistPage" parameterType="page" resultType="pd">
		select
		f.*
		,lv.NAME HIDDENLEVELNAME
		,re.NAME  as RECTIFICATIONORNAME
		,confirmUser.NAME  as confirmUserName
		,hiddenType.NAME as hiddenTypeName
		,hiddenType2.NAME as hiddenTypeName2
		,hregion.HIDDENREGION as hregionName
		,finduser.NAME as findUserNames
		,od.NAME as findDeptNames
		,(select count(1) from BUS_HIDDENEXAMINE eax where eax.HIDDEN_ID = f.HIDDEN_ID and eax.STATE = '-1') as exaCount
		from
		<include refid="tableName"></include> f
		LEFT JOIN sys_user confirmUser on confirmUser.USER_ID = f.CONFIRM_USER  <!-- 隐患确认人 -->
		left join <include refid="dicTableName"></include> lv on f.HIDDENLEVEL = lv.BIANMA
		left join <include refid="dicTableName"></include> hiddenType on f.HIDDENTYPE = hiddenType.BIANMA  <!-- 隐患类型-->
		left join <include refid="dicTableName"></include> hiddenType2 on f.HIDDENTYPE2 = hiddenType2.BIANMA  <!-- 隐患类型-->
		left join sys_user re on re.USER_ID = f.RECTIFICATIONOR
		left join sys_user finduser on finduser.USER_ID = f.CREATOR
		left join bus_hiddenregion hregion on hregion.HIDDENREGION_ID = f.HIDDENPART
		left join bus_listmanager list on list.LISTMANAGER_ID = f.LISTMANAGER_ID
		left join OA_DEPARTMENT od on od.DEPARTMENT_ID = f.HIDDENFINDDEPT
		where f.ISDELETE = '0'
		and f.state not in ('0')
		<if test='pd.approvedstate != null and pd.approvedstate != ""'>
			and f.STATE = '12'
		</if>
		<if test='pd.CORPINFO_ID != null and pd.CORPINFO_ID != "" '>
			and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<if test='pd.majorhiddenstate != null and pd.majorhiddenstate != ""'>
			and f.STATE <![CDATA[ >= ]]> 13
		</if>
		<if test="pd.STATE != null and pd.STATE != ''">
			and f.STATE = #{pd.STATE}
		</if>
		<if test='pd.stateConfirm != null and pd.stateConfirm != "" and pd.stateConfirm == "1"'><!-- 未确认确认隐患 -->
			and f.state = '-2'
		</if>
		<if test='pd.stateConfirm != null and pd.stateConfirm != "" and pd.stateConfirm == "2"'><!-- 确认确认隐患 -->
			and f.state != '-2'
		</if>
		<if test="pd.keySource != null and pd.keySource != '' "><!-- 隐患来源 -->
			<if test='pd.keySource == "2"'>
				and f.SOURCE in ('2','3')
			</if>
			<if test='pd.keySource != "2"'>
				and f.SOURCE = #{pd.keySource}
			</if>
		</if>
		<if test="pd.SOURCE != null and pd.SOURCE != '' "><!-- 隐患来源 -->
			and f.SOURCE in ('2','3')
		</if>
		<if test="pd.keyHiddenDescr != null and pd.keyHiddenDescr != ''"><!-- 隐患描述 -->
			and (f.HIDDENDESCR LIKE CONCAT(CONCAT('%', #{pd.keyHiddenDescr}),'%'))
		</if>
		<if test="pd.HIDDENDESCR != null and pd.HIDDENDESCR != ''"><!-- 隐患描述 -->
			and (f.HIDDENDESCR LIKE CONCAT(CONCAT('%', #{pd.HIDDENDESCR}),'%'))
		</if>
		<if test="pd.keyFindUserName != null and pd.keyFindUserName != ''"><!-- 隐患发现人 -->
			and f.HIDDEN_ID in (select hfu.HIDDEN_ID from BUS_HIDDEN_USER  hfu LEFT JOIN sys_user user on user.USER_ID = hfu.USER_ID where user.NAME  LIKE CONCAT(CONCAT('%', #{pd.keyFindUserName}),'%') )
		</if>
		<if test="pd.keyFindUserDept != null and pd.keyFindUserDept != ''"><!-- 隐患发现部门 -->
			and f.HIDDEN_ID in (select hfu.HIDDEN_ID from BUS_HIDDEN_USER  hfu LEFT JOIN sys_user user on user.USER_ID = hfu.USER_ID where user.DEPARTMENT_ID  = #{pd.keyFindUserDept} )
		</if>
		<if test="pd.keyHiddenType != null and pd.keyHiddenType != ''"><!-- 隐患类型 -->
			and f.HIDDENTYPE = #{pd.keyHiddenType}
		</if>
		<if test="pd.keyHiddenType2 != null and pd.keyHiddenType2 != ''"><!-- 隐患类型 -->
			and f.HIDDENTYPE2 = #{pd.keyHiddenType2}
		</if>
		<if test="pd.keyHiddenPart != null and pd.keyHiddenPart != ''"><!-- 隐患部位 -->
			and f.HIDDENPART = #{pd.keyHiddenPart}
		</if>
		<if test="pd.keyHiddenlevel = 'hiddenLevel2002'"><!-- 隐患级别 -->
			and f.HIDDENLEVEL in ('jdyh001','hiddenLevel2002')
		</if>
		<if test="pd.CONFIRM_USER != null and pd.CONFIRM_USER != ''"><!-- 隐患确认人 -->
			and f.CONFIRM_USER = #{pd.CONFIRM_USER}
		</if>
		<if test="pd.keyFindStateTime != null and pd.keyFindStateTime != ''"><!-- 隐患发现时间 -->
			and f.DISCOVERYTIME >= #{pd.keyFindStateTime}
		</if>
		<if test="pd.keyFindEndTime != null and pd.keyFindEndTime != ''"><!-- 隐患发现时间 -->
			and f.DISCOVERYTIME &lt;= #{pd.keyFindEndTime}
		</if>
		<if test="pd.keyConfirmUser != null and pd.keyConfirmUser != ''"><!-- 确认人 -->
			and (confirmUser.NAME LIKE CONCAT(CONCAT('%', #{pd.keyConfirmUser}),'%'))
		</if>
		<if test="pd.keyMajor != null and pd.keyMajor != ''"><!-- 重大隐患 -->
			and (lv.PARENT_ID = '5ff9daf78e9a4fb1b40d77980656799d' or lv.DICTIONARIES_ID='5ff9daf78e9a4fb1b40d77980656799d' )
		</if>
		<if test="pd.keyHiddenSpecialType != null and pd.keyHiddenSpecialType != ''">
			and f.HIDDEN_ID in (select hex.HIDDEN_ID from bus_hiddenexamine hex
			where hex.TYPE = #{pd.keyHiddenSpecialType} and hex.STATE ='1' )
		</if>
		<if test="pd.RECTIFICATIONOR != null and pd.RECTIFICATIONOR != ''"><!-- 整改人 -->
			and f.RECTIFICATIONOR = #{pd.RECTIFICATIONOR}}
		</if>
		<!--<if test="pd.ArrayDATA_IDS != null and pd.ArrayDATA_IDS != ''">
			or (list.DEPARTMENT_ID in (${pd.ArrayDATA_IDS})
			)
		</if>-->
		order by  f.CREATTIME  desc,f.STATE + 0 asc
	</select>


	<!-- 列表(全部) -->
	<select id="listAllInspection" parameterType="pd" resultType="pd">
		select
		f.*,
		hr.HIDDENREGION HIDDENPART_NAME,
		lv.NAME HIDDENLEVEL_NAME,
		ht2.NAME HIDDENTYPE2_NAME,
		crd.NAME HIDDENFINDDEPT_NAME,
		cr.NAME CREATOR_NAME,
		cre.NAME CREATOR_NAMES,
		u.UNITS_NAME as RECTIFICATIONDEPT_NAME,
		p.NAME as RECTIFICATIONOR_NAME
		from
		<include refid="tableName"></include> f
		left join bus_hiddenregion hr on hr.HIDDENREGION_ID = f.HIDDENPART
		left join <include refid="dicTableName"></include> lv on f.HIDDENLEVEL = lv.BIANMA
		left join <include refid="dicTableName"></include> ht on f.HIDDENTYPE = ht.BIANMA
		left join <include refid="dicTableName"></include> ht2 on f.HIDDENTYPE2 = ht2.BIANMA and ht2.PARENT_ID = '18c0a9aea6e54feab9f4ab3f46e0bc86'
		left join oa_department crd on crd.DEPARTMENT_ID = f.HIDDENFINDDEPT
		left join vi_user_all cr on cr.USER_ID = f.CREATOR
		left join sys_user cre on cre.USER_ID = f.CREATOR
		left join BUS_UNITS u on u.UNITS_ID = f.RECTIFICATIONDEPT
		left join BUS_PERSONNELMANAGEMENT p on p.PERSONNELMANAGEMENT_ID = f.RECTIFICATIONOR
		where f.ISDELETE = '0'
		<if test="FOREIGN_ID != null and FOREIGN_ID !=''" >
			and f.FOREIGN_ID = #{FOREIGN_ID}
		</if>
		<!-- Ai报警信息专查 -->
		<if test="AI_WARNING != null and AI_WARNING !=''" >
			and f.HIDDEN_ID in
			    <foreach collection="array" item="item" separator="," open="(" close = ")">
					#{item}
				</foreach>
		</if>
		<if test="KEYWORDS != null and KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			f.RISK_UNIT LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
			or
			f.RISK_POSITION LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
			or
			f.CHECK_CONTENT LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
			or
			f.HIDDENDESCR LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
			or
			c.CORP_NAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
			)
		</if>

		<if test="DEPT_PROVINCE != null and DEPT_PROVINCE != ''">
			and c.PROVINCE = #{DEPT_PROVINCE}
		</if>
		<if test="DEPT_CITY != null and DEPT_CITY != ''">
			and c.CITY = #{DEPT_CITY}
		</if>
		<if test="DEPT_COUNTRY != null and DEPT_COUNTRY != ''">
			and c.COUNTRY = #{DEPT_COUNTRY}
		</if>
		<if test="DEPT_VILLAGE != null and DEPT_VILLAGE != ''">
			and c.VILLAGE = #{DEPT_VILLAGE}
		</if>
		<if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">
			and c.CORP_OF_TYPE in (select CORPTYPE_ID from BUS_CORPTYPETODEPT ctod where ctod.DEPARTMENT_ID=#{DEPARTMENT_ID})
		</if>

		<if test="COUNTRY != null and COUNTRY != ''">
			and c.COUNTRY = #{COUNTRY}
		</if>
		<if test="VILLAGE != null and VILLAGE != ''">
			and c.VILLAGE = #{VILLAGE}
		</if>
		<if test="CORP_OF_TYPE != null and CORP_OF_TYPE != ''">
			and c.CORP_OF_TYPE = #{CORP_OF_TYPE}
		</if>

		<if test="STARTTIME != null and STARTTIME != ''"><!-- 关键词检索 -->
			and f.RECTIFICATIONDEADLINE &gt;= #{STARTTIME}
		</if>
		<if test="ENDTIME != null and ENDTIME != ''">
			and f.RECTIFICATIONDEADLINE &lt;= #{ENDTIME}
		</if>
		<if test="STATE != null and STATE !='' and STATE != 0" >
			and f.STATE = #{STATE}
		</if>

		<if test="HIDDENLEVEL != null and HIDDENLEVEL !=''" >
			and f.HIDDENLEVEL = #{HIDDENLEVEL}
		</if>
		order by f.CREATTIME ASC
	</select>

	<!-- 只用与修改隐患状态 -->
	<update id="editStateInspection" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		STATE = #{STATE}
		where
		FOREIGN_ID = #{INSPECTION_ID}
	</update>

	<!-- 安全环保检查归档后的隐患列表 -->
	<select id="hiddenInspectionlistPage" parameterType="page" resultType="pd">
		select
		iou.NAME INSPECTION_ORIGINATOR_NAME,
		ild.NAME INSPECTION_LEADDEPARTMENT_NAME,
		iod.NAME INSPECTION_DEPARTMENT_NAME,
		isd.NAME INSPECTED_DEPARTMENT_NAME,
		isda.NAME INSPECTED_CORPINFO_ID_NAME,
		isu.NAME INSPECTED_SITEUSER_NAME,
		se.INSPECTION_STATUS,
		se.INSPECTION_ID,
		f.HIDDENDESCR,
		f.CREATOR,
		c.NAME CREATOR_NAME,
		f.CONFIRM_USER,
		hcu.NAME CONFIRM_USER_NAME,
		f.RECTIFICATIONOR,
		f.CHECKOR,
		f.STATE HIDDEN_STATUS,
		f.FINAL_CHECK,
		f.FINAL_CHECKOR,
		f.FINAL_CHECKTIME,
		f.FINAL_CHECKDESCR,
		isa.INSPECTION_ASSESS_ID,
		isa.ASSESS_SCORE,
		isa.ASSESS_PENALTY_AMOUNT,
		f.HIDDEN_ID
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_INSPECTION_SAFETYENVIRONMENTAL se ON se.INSPECTION_ID = f.FOREIGN_ID
		LEFT JOIN VI_USER_ALL iou ON iou.USER_ID = se.INSPECTION_ORIGINATOR_ID
		LEFT JOIN VI_DEPARTMENT_ALL iod ON iod.DEPARTMENT_ID = iou.DEPARTMENT_ID
		LEFT JOIN VI_DEPARTMENT_ALL ild ON ild.DEPARTMENT_ID = se.INSPECTION_LEADDEPARTMENT_ID
		LEFT JOIN OA_DEPARTMENT isd ON isd.DEPARTMENT_ID = se.INSPECTED_DEPARTMENT_ID
		LEFT JOIN OA_DEPARTMENT isda ON isda.DEPARTMENT_ID = isd.CORPINFO_ID
		LEFT JOIN vi_user_all isu ON isu.USER_ID = se.INSPECTED_SITEUSER_ID
		LEFT JOIN vi_user_all c on c.USER_ID = f.CREATOR
		left join <include refid="dicTableName"></include> lv on f.HIDDENLEVEL = lv.BIANMA
		LEFT JOIN sys_user hcu on hcu.USER_ID = f.CONFIRM_USER
		LEFT JOIN BUS_INSPECTION_SAFETYENVIRONMENTAL_ASSESS isa on isa.HIDDEN_ID = f.HIDDEN_ID and isa.ISDELETE = '0'
		<!--
                left join <include refid="dicTableName"></include> hiddenType on f.HIDDENTYPE = hiddenType.BIANMA  &lt;!&ndash; 隐患类型&ndash;&gt;
                left join <include refid="dicTableName"></include> hiddenType2 on f.HIDDENTYPE2 = hiddenType2.BIANMA  &lt;!&ndash; 隐患类型&ndash;&gt;
                left join sys_user re on re.USER_ID = f.RECTIFICATIONOR
                left join bus_hiddenregion hregion on hregion.HIDDENREGION_ID = f.HIDDENPART
                left join bus_listmanager list on list.LISTMANAGER_ID = f.LISTMANAGER_ID
        -->
		where f.ISDELETE = '0' and f.SOURCE IN ('4','5') and f.STATE != '100'
		<if test='pd.CORPINFO_ID != null and pd.CORPINFO_ID != "" '>
			and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<if test="pd.INSPECTED_DEPARTMENT_NAME != null and pd.INSPECTED_DEPARTMENT_NAME != ''"><!-- 被检查单位 -->
			and isd.NAME LIKE CONCAT(CONCAT('%', #{pd.INSPECTED_DEPARTMENT_NAME}),'%')
		</if>
		<if test="pd.INSPECTION_DEPARTMENT_NAME != null and pd.INSPECTION_DEPARTMENT_NAME != ''"><!-- 检查部门 -->
			and iod.NAME LIKE CONCAT(CONCAT('%', #{pd.INSPECTION_DEPARTMENT_NAME}),'%')
		</if>
		<if test="pd.INSPECTION_ORIGINATOR_NAME != null and pd.INSPECTION_ORIGINATOR_NAME != ''"><!-- 检查发起人 -->
			and iou.NAME LIKE CONCAT(CONCAT('%', #{pd.INSPECTION_ORIGINATOR_NAME}),'%')
		</if>
		<if test="pd.HIDDENDESCR != null and pd.HIDDENDESCR != ''"><!-- 隐患描述 -->
			and f.HIDDENDESCR LIKE CONCAT(CONCAT('%', #{pd.HIDDENDESCR}),'%')
		</if>
		<if test='pd.HIDDEN_STATUS != null and pd.HIDDEN_STATUS != ""'><!-- 分公司内隐患处理状态 -->
			and f.STATE = #{pd.HIDDEN_STATUS}
		</if>
		<if test='pd.INSPECTION_ID != null and pd.INSPECTION_ID != ""'><!-- 安全环保检查ID -->
			and f.FOREIGN_ID = #{pd.INSPECTION_ID}
		</if>
		<if test='pd.INSPECTION_STATUS != null and pd.INSPECTION_STATUS != ""'> <!-- 安全、环保检查隐患处理状态 -->
			<choose>
				<when test="pd.INSPECTION_STATUS == '3-7'">
					and se.INSPECTION_STATUS in ('3','4','5','6','7')
				</when>
				<otherwise>
					and se.INSPECTION_STATUS = #{pd.INSPECTION_STATUS}
				</otherwise>
			</choose>
		</if>
		<if test='pd.INSPECTION_ASSESS_ID != null and pd.INSPECTION_ASSESS_ID != ""'> <!-- 安全、环保检查隐患考评状态 -->
			<choose>
				<when test="pd.INSPECTION_ASSESS_ID == 1">
					and isa.INSPECTION_ASSESS_ID IS NOT NULL
				</when>
				<otherwise>
					and isa.INSPECTION_ASSESS_ID IS NULL
				</otherwise>
			</choose>
		</if>
		<!--
                <if test='pd.stateConfirm != null and pd.stateConfirm != "" and pd.stateConfirm == "1"'>&lt;!&ndash; 未确认确认隐患 &ndash;&gt;
                    and f.state = '-2'
                </if>
                <if test='pd.stateConfirm != null and pd.stateConfirm != "" and pd.stateConfirm == "2"'>&lt;!&ndash; 确认确认隐患 &ndash;&gt;
                    and f.state != '-2'
                </if>
                <if test="pd.keyFindUserName != null and pd.keyFindUserName != ''">&lt;!&ndash; 隐患发现人 &ndash;&gt;
                    and f.HIDDEN_ID in (select hfu.HIDDEN_ID from BUS_HIDDEN_USER  hfu LEFT JOIN sys_user user on user.USER_ID = hfu.USER_ID where user.NAME  LIKE CONCAT(CONCAT('%', #{pd.keyFindUserName}),'%') )
                </if>
                <if test="pd.keyFindUserDept != null and pd.keyFindUserDept != ''">&lt;!&ndash; 隐患发现部门 &ndash;&gt;
                    and f.HIDDEN_ID in (select hfu.HIDDEN_ID from BUS_HIDDEN_USER  hfu LEFT JOIN sys_user user on user.USER_ID = hfu.USER_ID where user.DEPARTMENT_ID  = #{pd.keyFindUserDept} )
                </if>
                <if test="pd.keyHiddenType != null and pd.keyHiddenType != ''">&lt;!&ndash; 隐患类型 &ndash;&gt;
                    and f.HIDDENTYPE = #{pd.keyHiddenType}
                </if>
                <if test="pd.keyHiddenType2 != null and pd.keyHiddenType2 != ''">&lt;!&ndash; 隐患类型 &ndash;&gt;
                    and f.HIDDENTYPE2 = #{pd.keyHiddenType2}
                </if>
                <if test="pd.keyHiddenPart != null and pd.keyHiddenPart != ''">&lt;!&ndash; 隐患部位 &ndash;&gt;
                    and f.HIDDENPART = #{pd.keyHiddenPart}
                </if>
                <if test="pd.keyHiddenlevel = 'hiddenLevel2002'">&lt;!&ndash; 隐患级别 &ndash;&gt;
                    and f.HIDDENLEVEL in ('hiddenLevel2001','hiddenLevel2002')
                </if>
                <if test="pd.CONFIRM_USER != null and pd.CONFIRM_USER != ''">&lt;!&ndash; 隐患确认人 &ndash;&gt;
                    and f.CONFIRM_USER = #{pd.CONFIRM_USER}
                </if>
                <if test="pd.keyFindStateTime != null and pd.keyFindStateTime != ''">&lt;!&ndash; 隐患发现时间 &ndash;&gt;
                    and f.DISCOVERYTIME >= #{pd.keyFindStateTime}
                </if>
                <if test="pd.keyFindEndTime != null and pd.keyFindEndTime != ''">&lt;!&ndash; 隐患发现时间 &ndash;&gt;
                    and f.DISCOVERYTIME &lt;= #{pd.keyFindEndTime}
                </if>
                <if test="pd.keyConfirmUser != null and pd.keyConfirmUser != ''">&lt;!&ndash; 确认人 &ndash;&gt;
                    and (confirmUser.NAME LIKE CONCAT(CONCAT('%', #{pd.keyConfirmUser}),'%'))
                </if>
                <if test="pd.keyMajor != null and pd.keyMajor != ''">&lt;!&ndash; 重大隐患 &ndash;&gt;
                    and (lv.PARENT_ID = '5ff9daf78e9a4fb1b40d77980656799d' or lv.DICTIONARIES_ID='5ff9daf78e9a4fb1b40d77980656799d' )
                </if>
                <if test="pd.keyHiddenSpecialType != null and pd.keyHiddenSpecialType != ''">
                    and f.HIDDEN_ID in (select hex.HIDDEN_ID from bus_hiddenexamine hex
                    where hex.TYPE = #{pd.keyHiddenSpecialType} and hex.STATE ='1' )
                </if>
                <if test="pd.RECTIFICATIONOR != null and pd.RECTIFICATIONOR != ''">&lt;!&ndash; 整改人 &ndash;&gt;
                    and f.RECTIFICATIONOR = #{pd.RECTIFICATIONOR}}
                </if>
        -->
		GROUP BY f.HIDDEN_ID
		ORDER BY f.CREATTIME desc,f.STATE + 0 asc
	</select>

	<!-- 重大隐患核实列表 -->
	<select id="checkandacceptdatalistPage" parameterType="page" resultType="pd">
		select
		f.*
		,lv.NAME HIDDENLEVELNAME
		,re.NAME  as RECTIFICATIONORNAME
		,confirmUser.NAME  as confirmUserName
		,hiddenType.NAME as hiddenTypeName
		,hiddenType2.NAME as hiddenTypeName2
		,hregion.HIDDENREGION as hregionName
		,finduser.NAME as findUserNames
		,od.NAME as findDeptNames
		,(select count(1) from BUS_HIDDENEXAMINE eax where eax.HIDDEN_ID = f.HIDDEN_ID and eax.STATE = '-1') as exaCount
		from
		<include refid="tableName"></include> f
		LEFT JOIN sys_user confirmUser on confirmUser.USER_ID = f.CONFIRM_USER  <!-- 隐患确认人 -->
		left join <include refid="dicTableName"></include> lv on f.HIDDENLEVEL = lv.BIANMA
		left join <include refid="dicTableName"></include> hiddenType on f.HIDDENTYPE = hiddenType.BIANMA  <!-- 隐患类型-->
		left join <include refid="dicTableName"></include> hiddenType2 on f.HIDDENTYPE2 = hiddenType2.BIANMA  <!-- 隐患类型-->
		left join sys_user re on re.USER_ID = f.RECTIFICATIONOR
		left join sys_user finduser on finduser.USER_ID = f.CREATOR
		left join bus_hiddenregion hregion on hregion.HIDDENREGION_ID = f.HIDDENPART
		left join bus_listmanager list on list.LISTMANAGER_ID = f.LISTMANAGER_ID
		left join OA_DEPARTMENT od on od.DEPARTMENT_ID = f.HIDDENFINDDEPT
		where f.ISDELETE = '0'
		and f.state not in ('0')
		and f.STATE in ('14','15')
		<if test="pd.STATE != null and pd.STATE != ''">
			and f.STATE = #{pd.STATE}
		</if>
		<if test='pd.CORPINFO_ID != null and pd.CORPINFO_ID != "" '>
			and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<if test='pd.majorhiddenstate != null and pd.majorhiddenstate != ""'>
			and f.STATE = '13'
		</if>
		<if test='pd.stateConfirm != null and pd.stateConfirm != "" and pd.stateConfirm == "1"'><!-- 未确认确认隐患 -->
			and f.state = '-2'
		</if>
		<if test='pd.stateConfirm != null and pd.stateConfirm != "" and pd.stateConfirm == "2"'><!-- 确认确认隐患 -->
			and f.state != '-2'
		</if>
		<if test="pd.keySource != null and pd.keySource != '' "><!-- 隐患来源 -->
			<if test='pd.keySource == "2"'>
				and f.SOURCE in ('2','3')
			</if>
			<if test='pd.keySource != "2"'>
				and f.SOURCE = #{pd.keySource}
			</if>
		</if>
		<if test="pd.SOURCE != null and pd.SOURCE != '' "><!-- 隐患来源 -->
			and f.SOURCE in ('2','3')
		</if>
		<if test="pd.keyHiddenDescr != null and pd.keyHiddenDescr != ''"><!-- 隐患描述 -->
			and (f.HIDDENDESCR LIKE CONCAT(CONCAT('%', #{pd.keyHiddenDescr}),'%'))
		</if>
		<if test="pd.HIDDENDESCR != null and pd.HIDDENDESCR != ''"><!-- 隐患描述 -->
			and (f.HIDDENDESCR LIKE CONCAT(CONCAT('%', #{pd.HIDDENDESCR}),'%'))
		</if>
		<if test="pd.keyFindUserName != null and pd.keyFindUserName != ''"><!-- 隐患发现人 -->
			and f.HIDDEN_ID in (select hfu.HIDDEN_ID from BUS_HIDDEN_USER  hfu LEFT JOIN sys_user user on user.USER_ID = hfu.USER_ID where user.NAME  LIKE CONCAT(CONCAT('%', #{pd.keyFindUserName}),'%') )
		</if>
		<if test="pd.keyFindUserDept != null and pd.keyFindUserDept != ''"><!-- 隐患发现部门 -->
			and f.HIDDEN_ID in (select hfu.HIDDEN_ID from BUS_HIDDEN_USER  hfu LEFT JOIN sys_user user on user.USER_ID = hfu.USER_ID where user.DEPARTMENT_ID  = #{pd.keyFindUserDept} )
		</if>
		<if test="pd.keyHiddenType != null and pd.keyHiddenType != ''"><!-- 隐患类型 -->
			and f.HIDDENTYPE = #{pd.keyHiddenType}
		</if>
		<if test="pd.keyHiddenType2 != null and pd.keyHiddenType2 != ''"><!-- 隐患类型 -->
			and f.HIDDENTYPE2 = #{pd.keyHiddenType2}
		</if>
		<if test="pd.keyHiddenPart != null and pd.keyHiddenPart != ''"><!-- 隐患部位 -->
			and f.HIDDENPART = #{pd.keyHiddenPart}
		</if>
		<if test="pd.keyHiddenlevel = 'hiddenLevel2002'"><!-- 隐患级别 -->
			and f.HIDDENLEVEL in ('jdyh001','hiddenLevel2002')
		</if>
		<if test="pd.CONFIRM_USER != null and pd.CONFIRM_USER != ''"><!-- 隐患确认人 -->
			and f.CONFIRM_USER = #{pd.CONFIRM_USER}
		</if>
		<if test="pd.keyFindStateTime != null and pd.keyFindStateTime != ''"><!-- 隐患发现时间 -->
			and f.DISCOVERYTIME >= #{pd.keyFindStateTime}
		</if>
		<if test="pd.keyFindEndTime != null and pd.keyFindEndTime != ''"><!-- 隐患发现时间 -->
			and f.DISCOVERYTIME &lt;= #{pd.keyFindEndTime}
		</if>
		<if test="pd.keyConfirmUser != null and pd.keyConfirmUser != ''"><!-- 确认人 -->
			and (confirmUser.NAME LIKE CONCAT(CONCAT('%', #{pd.keyConfirmUser}),'%'))
		</if>
		<if test="pd.keyMajor != null and pd.keyMajor != ''"><!-- 重大隐患 -->
			and (lv.PARENT_ID = '5ff9daf78e9a4fb1b40d77980656799d' or lv.DICTIONARIES_ID='5ff9daf78e9a4fb1b40d77980656799d' )
		</if>
		<if test="pd.keyHiddenSpecialType != null and pd.keyHiddenSpecialType != ''">
			and f.HIDDEN_ID in (select hex.HIDDEN_ID from bus_hiddenexamine hex
			where hex.TYPE = #{pd.keyHiddenSpecialType} and hex.STATE ='1' )
		</if>
		<if test="pd.RECTIFICATIONOR != null and pd.RECTIFICATIONOR != ''"><!-- 整改人 -->
			and f.RECTIFICATIONOR = #{pd.RECTIFICATIONOR}}
		</if>
		<!--<if test="pd.ArrayDATA_IDS != null and pd.ArrayDATA_IDS != ''">
			or (list.DEPARTMENT_ID in (${pd.ArrayDATA_IDS})
			)
		</if>-->
		order by  f.CREATTIME  desc,f.STATE + 0 asc
	</select>

	<!-- 验收 -->
	<update id="finalcheck" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		FINAL_CHECK = #{FINAL_CHECK},
		FINAL_CHECKOR = #{FINAL_CHECKOR},
		FINAL_CHECKTIME = #{FINAL_CHECKTIME},
		FINAL_CHECKDESCR = #{FINAL_CHECKDESCR}
		where
		HIDDEN_ID = #{HIDDEN_ID}
	</update>

	<select id="listOtherNotAccept" parameterType="pd" resultType="pd">
		select
		f.HIDDEN_ID
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		<if test="ACCEPTSTEP == 6">
			and f.STATE != '4'
		</if>
		<if test="ACCEPTSTEP == 7">
			and (f.STATE != '4' OR (f.STATE = '4' AND f.FINAL_CHECK IS NULL))
		</if>
		and f.FOREIGN_ID = #{INSPECTION_ID} and f.HIDDEN_ID != #{HIDDEN_ID}
	</select>

	<update id="changeState" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		STATE = #{STATE}

		<if test="BUHEGE != null and BUHEGE != ''"><!-- 关键词检索 -->
			,RECTIFYDESCR = null
			,RECTIFICATIONTIME = null
		</if>
		where
		HIDDEN_ID = #{HIDDEN_ID}
	</update>

	<!-- 删除-->
	<delete id="deleteByKey" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		ISDELETE = '1'
		where
		FOREIGN_ID = #{FOREIGN_ID}
		and SOURCE = #{SOURCE}
	</delete>

	<select id="keyprojectcount" parameterType="pd" resultType="pd">
		select count(1)                                                                as AI_COUNT,
			   (		SELECT COUNT(t.HIDDEN_ID) FROM (
		SELECT
		f.HIDDEN_ID,ifnull(bo.OUTSOURCED_ID,boai.OUTSOURCED_ID) OUTSOURCED_ID
		FROM
		BUS_KEYPROJECT_HIDDEN f
		LEFT JOIN bus_keyprojectcheck keycheck ON keycheck.KEYPROJECTCHECK_ID = f.FOREIGN_ID
		LEFT JOIN BUS_OUTSOURCED bo ON keycheck.OUTSOURCED_ID = bo.OUTSOURCED_ID
		LEFT JOIN BUS_OUTSOURCED boai ON f.FOREIGN_ID = boai.OUTSOURCED_ID
		WHERE
		f.ISDELETE = '0'
		AND f.SOURCE IN ( '4', '5', '1' )
		AND ifnull(bo.OUTSOURCED_ID,boai.OUTSOURCED_ID) IS NOT NULL
		AND (bo.ISDELETE = '0' or boai.ISDELETE = '0')
		and f.ISPUNISH is null
		and (
		bo.PUNISH_THE_PERSON LIKE CONCAT( CONCAT( '%', #{loginUserId} ), '%' )
		OR boai.PUNISH_THE_PERSON LIKE CONCAT( CONCAT( '%', #{loginUserId} ), '%' )
		)
	) t ) as HIDDEN_COUNT,
		(	SELECT COUNT(DISTINCT t.HIDDEN_ID) FROM (
		SELECT
		f.HIDDEN_ID
		FROM
		BUS_KEYPROJECT_HIDDEN f
		LEFT JOIN bus_keyprojectcheck keycheck ON keycheck.KEYPROJECTCHECK_ID = f.FOREIGN_ID
		LEFT JOIN BUS_OUTSOURCED bo ON keycheck.OUTSOURCED_ID = bo.OUTSOURCED_ID
		LEFT JOIN BUS_OUTSOURCED boai ON f.FOREIGN_ID = boai.OUTSOURCED_ID
		LEFT JOIN BUS_KEYPROJECTPUNISH pu ON pu.HIDDEN_ID = f.HIDDEN_ID
		WHERE
		f.ISDELETE = '0'
		AND f.SOURCE IN ( '4', '5', '1' )
		AND f.STATE = '2'
		AND f.CREATOR = #{loginUserId}
		AND ifnull(bo.OUTSOURCED_ID,boai.OUTSOURCED_ID) IS NOT NULL
		AND (bo.ISDELETE = '0' or boai.ISDELETE = '0')
		) t)CF_COUNT,
			(SELECT
	COUNT( DISTINCT t.OUTSOURCED_ID )
FROM
	(
	SELECT
		b.OUTSOURCED_ID
	FROM
		BUS_OUTSOURCED b
	WHERE
		b.ISDELETE = '0'
		 AND ( b.STATE = '-1' OR b.STATE = '-2' )
	AND b.CREATOR =  #{loginUserId}
	) t) GC_COUNT
		from bus_aiwarning f
		where f.ISDELETE = '0'
		  and f.STATUS = 0
		  and VIDEOMANAGER_ID in (
		      select vi.VIDEOMANAGER_ID
								from bus_aiwarning_people aip
		      left join bus_videomanager vi on vi.OUTSOURCED_ID = aip.OUTSOURCED_ID
								where aip.AIWARNING_PEOPLE = #{loginUserId}
								  and aip.ISDELETE = 0
		      )
	</select>

	<select id="getpunishlistPage" parameterType="page" resultType="pd">
		SELECT
		u.UNITS_NAME,
		per.`NAME` as PERSON_NAME,
		ifnull(v.`NAME`,vv.`NAME`) as CREATOR_NAME,
		c.KEYPROJECTCHECK_ID,
		p.KEYPROJECTPUNISH_ID,
		p.HANDLED,
		p.REASON,
		vi.`NAME` as RECTIFICATIONOR_NAME,
		h.*
		FROM
		bus_keyproject_hidden h
		LEFT JOIN bus_keyprojectcheck c ON h.FOREIGN_ID = c.KEYPROJECTCHECK_ID
		LEFT JOIN bus_keyprojectpunish p ON p.HIDDEN_ID = h.HIDDEN_ID
		LEFT JOIN bus_units u ON h.RECTIFICATIONDEPT = u.UNITS_ID
		LEFT JOIN bus_personnelmanagement per on per.PERSONNELMANAGEMENT_ID = p.PERSONNELMANAGEMENT_ID
		LEFT JOIN vi_user_all v ON v.USER_ID = p.CREATOR
		LEFT JOIN vi_user_all vv ON vv.USER_ID = h.PUNISH_PERSON
		LEFT JOIN bus_personnelmanagement vi ON vi.PERSONNELMANAGEMENT_ID = h.RECTIFICATIONOR
		WHERE
		h.ISDELETE = '0'
		<if test="pd.HANDLED==1">
			and (P.HANDLED = '0' OR  P.HANDLED IS NULL)
		</if>
		<if test="pd.HANDLED==2">
			and P.HANDLED = '1'
		</if>
		AND ( c.OUTSOURCED_ID = #{pd.OUTSOURCED_ID} OR h.FOREIGN_ID =  #{pd.OUTSOURCED_ID} )
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			h.HIDDENDESCR LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		ORDER BY
		h.ISPUNISH ASC,
		p.HANDLED DESC
	</select>

	<select id="getSmsList" parameterType="pd" resultType="pd">
		SELECT IFNULL(ry.PHONE,ry.USERNAME) PHONE, t.HIDDENDESCR HIDDENDESCR FROM BUS_KEYPROJECT_HIDDEN t
		LEFT JOIN bus_keyprojectcheck jc on t.FOREIGN_ID = jc.KEYPROJECTCHECK_ID
		LEFT JOIN BUS_OUTSOURCED gc on gc.OUTSOURCED_ID = jc.OUTSOURCED_ID
		LEFT JOIN vi_user_all ry on ry.USER_ID = jc.CREATOR
		WHERE t.HIDDEN_ID=#{HIDDEN_ID}
		AND gc.IS_SMS = '0'
	</select>

	<select id="getPUNISHTHEPERSON" parameterType="pd" resultType="string">
		SELECT gc.PUNISH_THE_PERSON PUNISH_THE_PERSON FROM BUS_OUTSOURCED gc
		WHERE gc.OUTSOURCED_ID=#{OUTSOURCED_ID}
		AND gc.IS_SMS = '0'
	</select>

	<select id="getPhone" parameterType="pd" resultType="string">
		SELECT
		IFNULL(PHONE,USERNAME) PHONE  FROM vi_user_all WHERE
		<if test="ids!= null and ids.length>0">
			USER_ID IN
			<foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
				#{item}
			</foreach>
		</if>
	</select>
	<select id="hiddenlistPage" resultType="com.zcloud.entity.PageData">
		select
		f.*,
		hr.HIDDENREGION HIDDENPART_NAME,
		lv.NAME HIDDENLEVEL_NAME,
		ht2.NAME HIDDENTYPE2_NAME,
		crd.NAME HIDDENFINDDEPT_NAME,
		cr.NAME CREATOR_NAME,
		u.UNITS_NAME as RECTIFICATIONDEPT_NAME,
		p.NAME as RECTIFICATIONOR_NAME,
		bo.OUTSOURCED_NAME,
		bo.PUNISH_THE_PERSON,
		pu.KEYPROJECTPUNISH_ID,
		pu.HANDLED,
		pu.REASON,
		bu.UNITS_NAME,
		bp.NAME AS PERSON_NAME
		from
		<include refid="tableName"></include> f
		left join `qa-gwj-prevention`.bus_hiddenregion hr on hr.HIDDENREGION_ID = f.HIDDENPART
		left join <include refid="dicTableName"></include> lv on f.HIDDENLEVEL = lv.BIANMA
		left join <include refid="dicTableName"></include> ht on f.HIDDENTYPE = ht.BIANMA
		left join <include refid="dicTableName"></include> ht2 on f.HIDDENTYPE2 = ht2.BIANMA and ht2.PARENT_ID = '18c0a9aea6e54feab9f4ab3f46e0bc86'
		left join oa_department crd on crd.DEPARTMENT_ID = f.HIDDENFINDDEPT
		left join vi_user_all cr on cr.USER_ID = f.CREATOR
		left join BUS_UNITS u on u.UNITS_ID = f.RECTIFICATIONDEPT
		left join BUS_OUTSOURCED o on o.UNITS_ID = u.UNITS_ID
		left join BUS_PERSONNELMANAGEMENT p on p.PERSONNELMANAGEMENT_ID = f.RECTIFICATIONOR
		left join bus_keyprojectcheck keycheck on keycheck.OUTSOURCED_ID = f.FOREIGN_ID or  f.FOREIGN_ID = keycheck.KEYPROJECTCHECK_ID
		left join BUS_OUTSOURCED bo on keycheck.OUTSOURCED_ID = bo.OUTSOURCED_ID
		left join BUS_KEYPROJECTPUNISH pu on pu.HIDDEN_ID = f.HIDDEN_ID
		left join BUS_UNITS bu on bu.UNITS_ID = pu.UNITS_ID
		left join BUS_PERSONNELMANAGEMENT bp on bp.PERSONNELMANAGEMENT_ID = pu.PERSONNELMANAGEMENT_ID
		where f.ISDELETE = '0' and (f.SOURCE in ('4','5') or  f.SOURCE ='1')  and f.ISPUNISH = '1'
		<if test="pd.HANDLED != null and pd.HANDLED != ''">
			and o.HANDLED = #{pd.HANDLED}
		</if>
		<if test="pd.OUTSOURCED_ID != null and pd.OUTSOURCED_ID != ''">
			and f.FOREIGN_ID in
			(select k.KEYPROJECTCHECK_ID from BUS_KEYPROJECTCHECK k left join BUS_KEYPROJECTPUNISH sh on k.KEYPROJECTCHECK_ID = sh.KEYPROJECTCHECK_ID where k.OUTSOURCED_ID = #{pd.OUTSOURCED_ID} and sh.ISDELETE = '0')
		</if>
		group by f.HIDDEN_ID
		order by f.CREATTIME  desc, f.STATE desc
	</select>

	<select id="getCorpInfoIdByHiddenId" parameterType="pd" resultType="string">
		select ry.CORPINFO_ID from
		<include refid="tableName"></include> f
		left join bus_keyprojectcheck keycheck on keycheck.OUTSOURCED_ID = f.FOREIGN_ID or  f.FOREIGN_ID = keycheck.KEYPROJECTCHECK_ID
		left join BUS_OUTSOURCED bo on keycheck.OUTSOURCED_ID = bo.OUTSOURCED_ID
		LEFT JOIN vi_user_all ry on ry.USER_ID = bo.CREATOR
		where  f.HIDDEN_ID = #{HIDDEN_ID}
	</select>
</mapper>
