<?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.hiddenApi.HiddenApiMapper">

	<!--表名 -->
	<sql id="tableName">
		BUS_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.POSITIONDESC,
		f.ISRELEVANT

	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		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,
		ISRELEVANT
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{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},
		getHiddenType(#{HIDDENTYPE}),
		#{INVESTMENT_FUNDS},
		#{LISTMANAGER_ID},
		#{FOREIGN_ID},
		#{FINAL_CHECK},
		#{FINAL_CHECKOR},
		#{FINAL_CHECKTIME},
		#{FINAL_CHECKDESCR},
		#{POSITIONDESC},
		#{ISRELEVANT}
	</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="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			HIDDEN_ID = #{HIDDEN_ID}
	</delete>
	<!-- 修改 -->
	<update id="confirmHidden" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		STATE = #{STATE},
		CONFIRM_USER = #{CONFIRM_USER},
		CONFIRM_TIME = #{CONFIRM_TIME},
		RECTIFICATIONDEPT = #{RECTIFICATIONDEPT},
		RECTIFICATIONOR = #{RECTIFICATIONOR},
		RECTIFICATIONDEADLINE = #{RECTIFICATIONDEADLINE},
		HIDDENLEVEL = #{HIDDENLEVEL}
		where
		HIDDEN_ID = #{HIDDEN_ID}
	</update>
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		cr.NAME CREATORNAME,
		re.NAME RECTIFICATIONORNAME,
		ch.NAME CHECKORNAME,
		crd.NAME HIDDENFINDDEPTNAME,
		red.NAME RECTIFICATIONDEPTNAME,
		chd.NAME CHECKDEPTNAME,
		lv.NAME HIDDENLEVELNAME,
		bc.LIST_NAME,
		lv.NAME HIDDENLEVELNAME,
		ht.NAME HIDDENTYPENAME,
		hiddenType2.NAME HIDDENTYPENAME2,
		conUser.NAME as conUserName,
		hregion.HIDDENREGION as hregionName,
		hregionDept.NAME as hregionDeptName,
		hregion.HIDDENREGION as hregionName,
		od.DEPARTMENT_ID as confirmDept,
		fc.NAME FINAL_CHECKOR_NAME,
		isa.INSPECTION_ASSESS_ID,
		siu.NAME as acceptName,
		isa.ASSESS_SCORE,
		isa.ASSESS_PENALTY_AMOUNT
		from
		<include refid="tableName"></include> f
		left join vi_user_all 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 conUser on conUser.USER_ID = f.CONFIRM_USER
		left join vi_department_all 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 bus_checkrecord bc on bc.CHECKRECORD_ID = f.CHECKRECORD_ID and bc.ISDELETE = 0
		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> hiddenType2 on f.HIDDENTYPE2 = hiddenType2.BIANMA  <!-- 隐患类型-->
		left join bus_hiddenregion hregion on hregion.HIDDENREGION_ID = f.HIDDENPART
		left join oa_department hregionDept on hregionDept.DEPARTMENT_ID = conUser.DEPARTMENT_ID
		left join sys_user su on su.USER_ID = f.CONFIRM_USER
		left join oa_department od on od.DEPARTMENT_ID = su.DEPARTMENT_ID
		LEFT JOIN vi_user_all fc ON fc.USER_ID = f.FINAL_CHECKOR
		LEFT JOIN vi_user_all siu ON siu.USER_ID = f.CHECKOR
		LEFT JOIN BUS_INSPECTION_SAFETYENVIRONMENTAL_ASSESS isa ON isa.HIDDEN_ID = f.HIDDEN_ID and isa.ISDELETE = '0'
		where
		f.HIDDEN_ID = #{HIDDEN_ID}
	</select>
	<!-- 整改 -->
	<update id="rectify" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		RECTIFYDESCR = #{RECTIFYDESCR},
		RECTIFICATIONTIME = #{RECTIFICATIONTIME},
		HAVESCHEME = #{HAVESCHEME},
		INVESTMENT_FUNDS = #{INVESTMENT_FUNDS},
		STATE = #{STATE},
		ISEXPIREREPAIR = #{ISEXPIREREPAIR}
		where
		HIDDEN_ID = #{HIDDEN_ID}
	</update>
	<!-- 验收 -->
	<update id="check" parameterType="String">
		update
		<include refid="tableName"></include>
		set
		CHECKTIME = #{CHECKTIME},
		STATE = '4'
		where
		HIDDEN_ID = #{HIDDEN_ID}
	</update>
	<!-- 验收打回 -->
	<update id="repulseCheck" parameterType="String">
		update
		<include refid="tableName"></include>
		set
		STATE = '1'
		where
		HIDDEN_ID = #{HIDDEN_ID}
	</update>
	<insert id="saveForEmis" parameterType="pd">
		INSERT INTO bus_hidden_emis(
			HIDDEN_ID,SOURCE,RISK_UNIT,IDENTIFICATION,RISK_DESCR,RISK_POSITION,LEVEL,CHECK_CONTENT,HIDDENDESCR,HIDDENPART,CREATOR,
			CREATTIME,RECTIFYDESCR,RECTIFICATIONTYPE,RECTIFICATIONDEPT,RECTIFICATIONOR,RECTIFICATIONDEADLINE,
			RECTIFICATIONTIME,HIDDENLEVEL,STATE,CHECKDEPT,CHECKOR,CHECKTIME,CHECKDESCR,ISQUALIFIED,ISDELETE,CORPINFO_ID,
			HIDDENFINDDEPT,CHECKRECORD_ID,RECORDITEM_ID,RISKITEM_ID,REJECTREASON,REVIEWOR,REVIEWTIME,REVIEWDEPT,HAVESCHEME,
			LONGITUDE,LATITUDE,LISTMANAGER_ID,HIDDENTYPE,ISCONFIRM,CONFIRM_USER,CONFIRM_TIME,DISCOVERYTIME,INVESTMENT_FUNDS,
			HIDDENTYPE2,POSITIONDESC,ISRELEVANT,EMIS_RES
		) SELECT
			  HIDDEN_ID,SOURCE,RISK_UNIT,IDENTIFICATION,RISK_DESCR,RISK_POSITION,LEVEL,CHECK_CONTENT,HIDDENDESCR,HIDDENPART,CREATOR,CREATTIME,RECTIFYDESCR,
			  RECTIFICATIONTYPE,RECTIFICATIONDEPT,RECTIFICATIONOR,RECTIFICATIONDEADLINE,RECTIFICATIONTIME,HIDDENLEVEL,STATE,CHECKDEPT,CHECKOR,CHECKTIME,
			  CHECKDESCR,ISQUALIFIED,ISDELETE,CORPINFO_ID,HIDDENFINDDEPT,CHECKRECORD_ID,RECORDITEM_ID,RISKITEM_ID,REJECTREASON,REVIEWOR,REVIEWTIME,REVIEWDEPT,
			  HAVESCHEME,LONGITUDE,LATITUDE,LISTMANAGER_ID,HIDDENTYPE,ISCONFIRM,CONFIRM_USER,CONFIRM_TIME,DISCOVERYTIME,INVESTMENT_FUNDS,HIDDENTYPE2,
			  POSITIONDESC,ISRELEVANT,#{resXML}
		FROM bus_hidden where
			HIDDEN_ID  = #{HIDDEN_ID}
	</insert>
	<delete id="deleteForEmis" parameterType="pd">
		delete from
		<include refid="tableName"></include>
		where
		HIDDEN_ID  = #{HIDDEN_ID}
	</delete>

	<!-- 确认打回 -->
	<update id="repulseConfirm" parameterType="String">
		update
		<include refid="tableName"></include>
		set
		STATE = '16'
		where
		HIDDEN_ID = #{HIDDEN_ID}
	</update>
	<!-- 隐患无法整改 -->
	<update id="unableRectify" parameterType="String">
		update
		<include refid="tableName"></include>
		set
		STATE = '7'
		where
		HIDDEN_ID = #{HIDDEN_ID}
	</update>

	<!-- 列表 发现人没有在这里 -->
	 <select id="getListdatalistPage" parameterType="page" resultType="pd">
		SELECT
		f.HIDDEN_ID,
		f.SOURCE,
		f.HIDDENDESCR,
		f.RECTIFICATIONTYPE,
		f.HIDDENLEVEL,	lv.NAME as HIDDENLEVELNAME,
		hreg.HIDDENREGION as HREGIONNAME,
		f.HIDDENPART,
		rect.NAME as RECTIFICATIONOR_NAME,
		f.DISCOVERYTIME,
		f.STATE,
		f.CONFIRM_USER, confirmUser.NAME as CONFIRM_USER_NAME ,
		f.RECTIFICATIONTIME,
		f.RECTIFICATIONOR,
		f.CREATOR,
		(select count(1) from BUS_HIDDENEXAMINE eax where eax.HIDDEN_ID = f.HIDDEN_ID and eax.STATE = '-1' and eax.type in ('1','2')) as exaCount
		FROM
		bus_hidden f
		left join SYS_DICTIONARIES lv on f.HIDDENLEVEL = lv.BIANMA
		LEFT JOIN sys_user confirmUser on confirmUser.USER_ID = f.CONFIRM_USER  <!-- 隐患确认人 -->
		LEFT JOIN bus_hiddenregion hreg on hreg.HIDDENREGION_ID = f.HIDDENPART  <!-- 隐患部位 -->
		LEFT JOIN sys_user rect on rect.USER_ID = f.RECTIFICATIONOR  <!-- 整改人 -->
		left join bus_hidden_user bhu on bhu.HIDDEN_ID = f.HIDDEN_ID
		left join sys_user bhuUser on bhuUser.user_id = bhu.user_id
		LEFT JOIN bus_hiddencheck hch on hch.HIDDEN_ID = f.HIDDEN_ID
		WHERE f.ISDELETE = '0'
		and f.STATE not in ('0','7','8','100','101','102')
		 AND f.HIDDENLEVEL != 'hiddenLevel2002'
		 AND f.HIDDENLEVEL != 'hiddenLevel1001'
		 AND f.HIDDENLEVEL != 'jdyh001'
		<if test="pd.roleLevel != null and pd.roleLevel != ''"><!-- 权限显示 -->
			<choose>
				<when test='pd.roleLevel == "0"'>
				</when>
				<when test='pd.roleLevel == "1"'>
					and (confirmUser.DEPARTMENT_ID in (${pd.supDeparIds}) or bhuUser.DEPARTMENT_ID in (${pd.supDeparIds}) or
					     f.RECTIFICATIONDEPT in (${pd.supDeparIds})  or hch.DEPARTMENT_ID in (${pd.supDeparIds}))
				</when>
				<when test='pd.roleLevel == "2"'>
					and (f.CREATOR = #{pd.loginUserId} or f.RECTIFICATIONOR = #{pd.loginUserId} or
					     f.CONFIRM_USER= #{pd.loginUserId} or bhu.USER_ID =#{pd.loginUserId} or hch.USER_ID = #{pd.loginUserId} )
				</when>
			</choose>
		</if>
		<if test='pd.hiddenVueType != null and pd.hiddenVueType != "confirm"'><!--  -->
			AND f.HIDDENLEVEL NOT IN ( 'hiddenLevel2002','hiddenLevel1001','jdyh001'  )
		</if>
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 企业ID -->
			and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<if test="pd.hiddendescr != null and pd.hiddendescr != ''"><!-- 隐患描述 -->
			and (f.HIDDENDESCR LIKE CONCAT(CONCAT('%', #{pd.hiddendescr}),'%'))
		</if>
		<if test="pd.source != null and pd.source != '' "><!-- 隐患来源 -->
			<if test='pd.source == "2"'>
				and f.SOURCE in ('2','3')
			</if>
			<if test='pd.source != "2"'>
				and f.SOURCE = #{pd.source}
			</if>
		</if>
		<if test="pd.creatorUserDept != null and pd.creatorUserDept != ''"><!-- 隐患发现人 部门-->
			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.creatorUserDept} )
		</if>
		<if test="pd.creatorName != null and pd.creatorName != ''"><!-- 隐患发现人 -->
			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.USERNAME LIKE CONCAT(CONCAT('%', #{pd.creatorName}),'%') or
					user.NUMBER LIKE CONCAT(CONCAT('%', #{pd.creatorName}),'%') or
				user.PHONE LIKE CONCAT(CONCAT('%', #{pd.creatorName}),'%')-->

			user.NAME LIKE CONCAT(CONCAT('%', #{pd.creatorName}),'%')
			)
		</if>
		<if test="pd.STARTTIME != null and pd.STARTTIME != ''"><!-- 隐患发现时间 开始时间 -->
			and f.DISCOVERYTIME <![CDATA[ >= ]]> CONCAT_WS(' ', #{pd.STARTTIME}, '00:00')
		</if>
		<if test="pd.ENDTIME != null and pd.ENDTIME != ''"><!-- 隐患发现时间 结束时间 -->
			and f.DISCOVERYTIME <![CDATA[ <= ]]> CONCAT_WS(' ', #{pd.ENDTIME}, '23:59')
		</if>
		<if test="pd.hiddenlevel != null and pd.hiddenlevel != ''"> <!-- 隐患级别-->
			and f.HIDDENLEVEL = #{pd.hiddenlevel}
		</if>
		<if test="pd.state != null and pd.state != ''"> <!-- 隐患状态-->
			and f.STATE = #{pd.state}
		</if>
		<if test="pd.hiddenType != null and pd.hiddenType != ''"><!-- 隐患类型 -->
			and f.HIDDENTYPE = #{pd.hiddenType}
		</if>
		<if test="pd.confirmUserName != null and pd.confirmUserName != ''"><!-- 确认人 -->
			and (

			<!-- 	confirmUser.USERNAME LIKE CONCAT(CONCAT('%', #{pd.confirmUserName}),'%') or}),'%') -->
			<!-- 	confirmUser.NUMBER LIKE CONCAT(CONCAT('%', #{pd.confirmUserName}),'%') or}),'%') -->
				confirmUser.NAME LIKE CONCAT(CONCAT('%', #{pd.confirmUserName}),'%')
			<!-- confirmUser.PHONE LIKE CONCAT(CONCAT('%', #{pd.confirmUserName}),'%'因为搜索框搜1或者2也能出来数据，但是不应该有数据，暂时注释) -->
			)
		</if>
		<if test="pd.rectificationorName != null and pd.rectificationorName != ''"><!-- 整改人 -->
			and (
			rect.NAME LIKE CONCAT(CONCAT('%', #{pd.rectificationorName}),'%')
			<!-- 	rect.PHONE LIKE CONCAT(CONCAT('%', #{pd.rectificationorName}),'%'
			rect.USERNAME LIKE CONCAT(CONCAT('%', #{pd.rectificationorName}),'%') or
			rect.NUMBER LIKE CONCAT(CONCAT('%', #{pd.rectificationorName}),'%') or因为搜索框搜1或者2也能出来数据，但是不应该有数据，暂时注释) -->
			)
		</if>
		<if test="pd.checkrecordName != null and pd.checkrecordName != ''"><!-- 验收人 -->
			and f.HIDDEN_ID in (
			SELECT
			yhc.HIDDEN_ID
			FROM
			bus_hiddencheck yhc
			LEFT JOIN sys_user yuser ON yuser.USER_ID = yhc.USER_ID	AND yuser.CORPINFO_ID = #{pd.CORPINFO_ID}
			where (
			<!-- 	yuser.USERNAME LIKE CONCAT(CONCAT('%',#{pd.checkrecordName}),'%')
			or) or yuser.PHONE LIKE CONCAT(CONCAT('%', #{pd.checkrecordName}),'%') 因为搜索框搜1或者2也能出来数据，但是不应该有数据，暂时注释-->
			yuser.NAME LIKE CONCAT(CONCAT('%', #{pd.checkrecordName}),'%')
			)
			)
		</if>
		 <if test="pd.checkrecordId != null and pd.checkrecordId != ''"><!-- 验收人 -->
			 and f.STATE = '2'
			 and (select count(1) from BUS_HIDDENCHECK bh  where bh.HIDDEN_ID=f.HIDDEN_ID and bh.USER_ID=#{pd.SELFUSERID} )>0
		 </if>


		<if test="pd.hiddenpart != null and pd.hiddenpart != ''"><!-- 隐患部位 -->
			and hreg.HIDDENREGION_ID  LIKE CONCAT(CONCAT('%', #{pd.hiddenpart}),'%')
		</if>
		<if test='pd.CONFIRM_USER != null and pd.CONFIRM_USER != ""'><!-- 隐患确认人 -->
			and f.CONFIRM_USER = #{pd.CONFIRM_USER}
		</if>
		<if test='pd.hiddenVueType != null and pd.hiddenVueType == "rectify"'><!--整改隐患信息  -->
			and f.STATE in('1','-1','10')
		</if>
		<if test='pd.SELFUSERID != null and pd.SELFUSERID != ""'><!-- 整改隐患信息 -->
			and f.RECTIFICATIONOR = #{pd.SELFUSERID}
		</if>
		<if test='pd.hiddenVueType != null and pd.hiddenVueType == "check"'><!--验收隐患  -->
			and f.STATE ='2'
		</if>
		<if test='pd.checkUser != null and pd.checkUser != ""'><!-- 验收隐患 -->
			and f.STATE = '2'
			and (select count(1) from BUS_HIDDENCHECK bh where bh.HIDDEN_ID=f.HIDDEN_ID and bh.USER_ID=#{pd.checkUser} )>0
		</if>
		 GROUP BY f.HIDDEN_ID
		order by FIELD ( f.STATE, '-2' ) desc,<!-- 因为这是隐患确认列表有确认按钮的排在前面 -->
		    f.DISCOVERYTIME desc  <!-- 剩下的按隐患发现时间排序 -->
	</select>




	<!-- 列表 发现人没有在这里 -->
	<select id="getListAll" parameterType="pd" resultType="pd">
		SELECT
		bhuUser.name as fxUserName,
		bhuUserDept.name  as fxUserDeptName,
		type1.name as type1Name,
		confirmUser.name as conUsername,

		zgUserDept.name as zgUserDeptName,
		zgUser.name as zgUserName,
		hreg.HIDDENREGION as HIDDENPARTNAME,
		f.*
		FROM
		bus_hidden f
		left join bus_hidden_user bhu on bhu.HIDDEN_ID = f.HIDDEN_ID

		left join `qa-gwj-prevention`.vi_user_all zgUser on zgUser.user_id = f.RECTIFICATIONOR
		LEFT JOIN `qa-gwj-prevention`.vi_user_all confirmUser on confirmUser.USER_ID = f.CONFIRM_USER  <!-- 隐患确认人 -->
		left join `qa-gwj-prevention`.vi_user_all bhuUser on bhuUser.user_id = bhu.user_id
		left join `qa-gwj-prevention`.vi_department_all bhuUserDept on bhuUser.DEPARTMENT_ID = bhuUserDept.DEPARTMENT_ID

		LEFT JOIN bus_hiddencheck hch on hch.HIDDEN_ID = f.HIDDEN_ID
		left join sys_dictionaries type1 on type1.bianma = f.HIDDENTYPE
		left join oa_department zgUserDept on zgUserDept.DEPARTMENT_ID = f.RECTIFICATIONDEPT
		left join bus_hiddenregion hreg on hreg.HIDDENREGION_ID = f.HIDDENPART


		WHERE f.ISDELETE = '0'
		and f.STATE not in ('0','7','8','100','101','102')
		AND f.HIDDENLEVEL != 'hiddenLevel2002'
		AND f.HIDDENLEVEL != 'hiddenLevel1001'
		AND f.HIDDENLEVEL != 'jdyh001'
		<if test="roleLevel != null and roleLevel != ''"><!-- 权限显示 -->
			<choose>
				<when test='roleLevel == "0"'>
				</when>
				<when test='roleLevel == "1"'>
					and (confirmUser.DEPARTMENT_ID in (${supDeparIds}) or bhuUser.DEPARTMENT_ID in (${supDeparIds}) or
					f.RECTIFICATIONDEPT in (${supDeparIds})  or hch.DEPARTMENT_ID in (${supDeparIds}))
				</when>
				<when test='roleLevel == "2"'>
					and (f.CREATOR = #{loginUserId} or f.RECTIFICATIONOR = #{loginUserId} or
					f.CONFIRM_USER= #{loginUserId} or bhu.USER_ID =#{loginUserId} or hch.USER_ID = #{loginUserId} )
				</when>
			</choose>
		</if>
		<if test='hiddenVueType != null and hiddenVueType != "confirm"'><!--  -->
			AND f.HIDDENLEVEL NOT IN ( 'hiddenLevel2002','hiddenLevel1001','jdyh001'  )
		</if>
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 企业ID -->
			and f.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="hiddendescr != null and hiddendescr != ''"><!-- 隐患描述 -->
			and (f.HIDDENDESCR LIKE CONCAT(CONCAT('%', #{hiddendescr}),'%'))
		</if>
		<if test="source != null and source != '' "><!-- 隐患来源 -->
			<if test='source == "2"'>
				and f.SOURCE in ('2','3')
			</if>
			<if test='source != "2"'>
				and f.SOURCE = #{source}
			</if>
		</if>
		<if test="creatorUserDept != null and creatorUserDept != ''"><!-- 隐患发现人 部门-->
			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 = #{creatorUserDept} )
		</if>
		<if test="creatorName != null and creatorName != ''"><!-- 隐患发现人 -->
			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.USERNAME LIKE CONCAT(CONCAT('%', #{creatorName}),'%') or
            user.NUMBER LIKE CONCAT(CONCAT('%', #{creatorName}),'%') or
        user.PHONE LIKE CONCAT(CONCAT('%', #{creatorName}),'%')-->

			user.NAME LIKE CONCAT(CONCAT('%', #{creatorName}),'%')
			)
		</if>
		<if test='STARTTIME != null and STARTTIME != "" and STARTTIME != "undefined"'><!-- 隐患发现时间 开始时间 -->
			and f.DISCOVERYTIME <![CDATA[ >= ]]> CONCAT_WS(' ', #{STARTTIME}, '00:00')
		</if>
		<if test='ENDTIME != null and ENDTIME != "" and ENDTIME != "undefined" '><!-- 隐患发现时间 结束时间 -->
			and f.DISCOVERYTIME <![CDATA[ <= ]]> CONCAT_WS(' ', #{ENDTIME}, '23:59')
		</if>
		<if test="hiddenlevel != null and hiddenlevel != ''"> <!-- 隐患级别-->
			and f.HIDDENLEVEL = #{hiddenlevel}
		</if>
		<if test="state != null and state != ''"> <!-- 隐患状态-->
			and f.STATE = #{state}
		</if>
		<if test="hiddenType != null and hiddenType != ''"><!-- 隐患类型 -->
			and f.HIDDENTYPE = #{hiddenType}
		</if>
		<if test="confirmUserName != null and confirmUserName != ''"><!-- 确认人 -->
			and (

			<!-- 	confirmUser.USERNAME LIKE CONCAT(CONCAT('%', #{confirmUserName}),'%') or}),'%') -->
			<!-- 	confirmUser.NUMBER LIKE CONCAT(CONCAT('%', #{confirmUserName}),'%') or}),'%') -->
			confirmUser.NAME LIKE CONCAT(CONCAT('%', #{confirmUserName}),'%')
			<!-- confirmUser.PHONE LIKE CONCAT(CONCAT('%', #{confirmUserName}),'%'因为搜索框搜1或者2也能出来数据，但是不应该有数据，暂时注释) -->
			)
		</if>
		<if test="rectificationorName != null and rectificationorName != ''"><!-- 整改人 -->
			and (
			rect.NAME LIKE CONCAT(CONCAT('%', #{rectificationorName}),'%')
			<!-- 	rect.PHONE LIKE CONCAT(CONCAT('%', #{rectificationorName}),'%'
			rect.USERNAME LIKE CONCAT(CONCAT('%', #{rectificationorName}),'%') or
			rect.NUMBER LIKE CONCAT(CONCAT('%', #{rectificationorName}),'%') or因为搜索框搜1或者2也能出来数据，但是不应该有数据，暂时注释) -->
			)
		</if>
		<if test="checkrecordName != null and checkrecordName != ''"><!-- 验收人 -->
			and f.HIDDEN_ID in (
			SELECT
			yhc.HIDDEN_ID
			FROM
			bus_hiddencheck yhc
			LEFT JOIN sys_user yuser ON yuser.USER_ID = yhc.USER_ID	AND yuser.CORPINFO_ID = #{CORPINFO_ID}
			where (
			<!-- 	yuser.USERNAME LIKE CONCAT(CONCAT('%',#{checkrecordName}),'%')
			or) or yuser.PHONE LIKE CONCAT(CONCAT('%', #{checkrecordName}),'%') 因为搜索框搜1或者2也能出来数据，但是不应该有数据，暂时注释-->
			yuser.NAME LIKE CONCAT(CONCAT('%', #{checkrecordName}),'%')
			)
			)
		</if>
		<if test="checkrecordId != null and checkrecordId != ''"><!-- 验收人 -->
			and f.STATE = '2'
			and (select count(1) from BUS_HIDDENCHECK bh  where bh.HIDDEN_ID=f.HIDDEN_ID and bh.USER_ID=#{SELFUSERID} )>0
		</if>


		<if test="hiddenpart != null and hiddenpart != ''"><!-- 隐患部位 -->
			and hreg.HIDDENREGION_ID  LIKE CONCAT(CONCAT('%', #{hiddenpart}),'%')
		</if>
		<if test='CONFIRM_USER != null and CONFIRM_USER != ""'><!-- 隐患确认人 -->
			and f.CONFIRM_USER = #{CONFIRM_USER}
		</if>
		<if test='hiddenVueType != null and hiddenVueType == "rectify"'><!--整改隐患信息  -->
			and f.STATE in('1','-1','10')
		</if>
		<if test='SELFUSERID != null and SELFUSERID != ""'><!-- 整改隐患信息 -->
			and f.RECTIFICATIONOR = #{SELFUSERID}
		</if>
		<if test='hiddenVueType != null and hiddenVueType == "check"'><!--验收隐患  -->
			and f.STATE ='2'
		</if>
		<if test='checkUser != null and checkUser != ""'><!-- 验收隐患 -->
			and f.STATE = '2'
			and (select count(1) from BUS_HIDDENCHECK bh where bh.HIDDEN_ID=f.HIDDEN_ID and bh.USER_ID=#{checkUser} )>0
		</if>
		GROUP BY f.HIDDEN_ID
		order by FIELD ( f.STATE, '-2' ) desc,<!-- 因为这是隐患确认列表有确认按钮的排在前面 -->
		f.DISCOVERYTIME desc  <!-- 剩下的按隐患发现时间排序 -->
	</select>
</mapper>


