<?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.HiddenExamineApiMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.HIDDEN_ID,
		f.EXAMINE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.STATE,
		f.TYPE,
		f.DISPOSAL_PLAN,
		f.DISPOSAL_FILE,
		f.DELAY_TIME,
		f.spare1,
		f.spare2,
		f.spare3,
		f.spare4,
		f.spare5,
		f.HIDDENEXAMINE_ID,
		f.REPULSE_CAUSE
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		HIDDEN_ID,
		EXAMINE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		STATE,
		TYPE,
		DISPOSAL_PLAN,
		DISPOSAL_FILE,
		DELAY_TIME,
		spare1,
		spare2,
		spare3,
		spare4,
		spare5,
		HIDDENEXAMINE_ID,
		REPULSE_CAUSE
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{HIDDEN_ID},
		#{EXAMINE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{STATE},
		#{TYPE},
		#{DISPOSAL_PLAN},
		#{DISPOSAL_FILE},
		#{DELAY_TIME},
		#{spare1},
		#{spare2},
		#{spare3},
		#{spare4},
		#{spare5},
		#{HIDDENEXAMINE_ID},
		#{REPULSE_CAUSE}
	</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'
		where
			HIDDENEXAMINE_ID = #{HIDDENEXAMINE_ID}
	</delete>

	<delete id="deleteByHiddenId" parameterType="pd">
		delete from
		<include refid="tableName"></include>
		where
		HIDDEN_ID = #{HIDDEN_ID}
	</delete>
	<delete id="deleteByHiddenIdAndType" parameterType="pd">
		delete from
		<include refid="tableName"></include>
		where
		HIDDEN_ID = #{HIDDEN_ID}
		and TYPE = '4'
		and STATE = '2'
	</delete>
	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			STATE = #{STATE}
		where
			HIDDENEXAMINE_ID = #{HIDDENEXAMINE_ID}
	</update>

	<!-- 修改确认打回的信息 -->
	<update id="editrepulse" parameterType="pd">
		update
		<include refid="tableName"></include> f
		set
		f.spare5 = 1
		where
		HIDDENEXAMINE_ID = #{HIDDENEXAMINE_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			f.HIDDENEXAMINE_ID = #{HIDDENEXAMINE_ID}
	</select>

	<!-- 通过ID获取数据 -->
	<select id="findByHiddenId" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
		f.HIDDEN_ID = #{HIDDEN_ID}
		and f.TYPE = '6'
		and f.STATE = '16'
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
				<!--	根据需求自己加检索条件
					字段1 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					字段2 LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				-->
				)
		</if>
		order by f.STATE desc , CREATTIME desc
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		, user.name as editUserName
		, dept.name as editDeptName
		, LEVEL.name as editLevelName
		, users.NAME as NEWRECTIFICATIONOR
		from
		<include refid="tableName"></include> f
		LEFT JOIN sys_user user on user.USER_ID = f.spare2
		LEFT JOIN sys_user users on users.USER_ID = f.NEW_RECTIFICATIONOR
		LEFT JOIN oa_department dept on dept.DEPARTMENT_ID = f.spare4
		LEFT JOIN sys_dictionaries LEVEL on  LEVEL.BIANMA = f.spare1
		 where 1=1
		<if test="HIDDEN_ID != null and HIDDEN_ID != ''">
			and f.HIDDEN_ID = #{HIDDEN_ID}
		</if>
		<if test="TYPE != null and TYPE != ''">
			and f.TYPE = #{TYPE}
		</if>
		<if test="STATE != null and STATE != ''">
			and f.STATE = #{STATE}
		</if>
		order by  field (f.TYPE ,'0','4') desc ,CREATTIME
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		set
			ISDELETE = '1'
		where
			HIDDENEXAMINE_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>
	<update id="updateBySpecial" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME},
		STATE = #{STATE},
		DISPOSAL_PLAN = #{DISPOSAL_PLAN},
		DISPOSAL_FILE = #{DISPOSAL_FILE}
		<if test="spare1 != null and spare1 != ''">
			, spare1 = #{spare1}
		</if>
		<if test="spare2 != null and spare2 != ''">
			, spare2 = #{spare2}
		</if>
		<if test="spare3 != null and spare3 != ''">
			, spare3 = #{spare3}
		</if>
		<if test="spare4 != null and spare4 != ''">
			, spare4 = #{spare4}
		</if>
		<if test="NEW_RECTIFICATIONOR != null and NEW_RECTIFICATIONOR != ''">
			, NEW_RECTIFICATIONOR = #{NEW_RECTIFICATIONOR}
		</if>
		where
		HIDDENEXAMINE_ID = #{HIDDENEXAMINE_ID}
	</update>
	<select id="listAndHiddendatalistPage" parameterType="page" resultType="pd">
		select
		f.*
		,exa.DISPOSAL_PLAN
		,exa.HIDDENEXAMINE_ID
		,re.NAME  as RECTIFICATIONORNAME
		,confirmUser.NAME  as confirmUserName
		,hiddenType.NAME as hiddenTypeName
		,hregion.HIDDENREGION as hregionName
		,lv.NAME as HIDDENLEVELNAME
		,exa.STATE as exaSTATE
		from
		BUS_HIDDENEXAMINE exa
		left join BUS_HIDDEN f on exa.HIDDEN_ID =f.HIDDEN_ID
		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 sys_user re on re.USER_ID = f.RECTIFICATIONOR
		left join bus_hiddenregion hregion on hregion.HIDDENREGION_ID = f.HIDDENPART
		LEFT JOIN bus_hiddencheck hch on hch.HIDDEN_ID = f.HIDDEN_ID
		left join bus_hidden_user bhu on bhu.HIDDEN_ID = f.HIDDEN_ID
		where f.ISDELETE = '0'
		<if test='pd.roleLevel != null and pd.roleLevel != "" and pd.keyType !="2" and pd.keyIsSpecial !="2" '><!-- 权限显示 -->
			<choose>
				<when test='pd.roleLevel == "0" '>
				</when>
				<when test='pd.roleLevel == "1"'>
					and (confirmUser.DEPARTMENT_ID in (${pd.supDeparIds}) or bhu.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 (bhu.USER_ID =#{pd.loginUserId} or f.CONFIRM_USER = #{pd.loginUserId} or f.RECTIFICATIONOR = #{pd.loginUserId} or f.CHECKOR = #{pd.loginUserId} )
				</when>
			</choose>
		</if>
		and f.state != '0'
		and f.state not in ('100','101','102')
		and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		<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 != '' "><!-- 隐患来源 -->
			<choose>
				<when test="pd.keySource == 1">
					AND f.SOURCE = '1'
				</when>
				<when test="pd.keySource == 2">
					AND f.SOURCE = '2'
				</when>
				<when test="pd.keySource == 4">
					AND f.SOURCE = '4'
				</when>
				<when test="pd.keySource == 5">
					AND f.SOURCE = '5'
				</when>
			</choose>
		</if>
<!--		<if test="pd.SOURCE != null and pd.SOURCE != '' ">&lt;!&ndash; 隐患来源 &ndash;&gt;-->
<!--			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.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.keyHiddenPart != null and pd.keyHiddenPart != ''"><!-- 隐患部位 -->
			and f.HIDDENPART = #{pd.keyHiddenPart}
		</if>
		<if test="pd.keyHiddenlevel != null and pd.keyHiddenlevel != ''"><!-- 隐患级别 -->
			and f.HIDDENLEVEL = #{pd.keyHiddenlevel}
		</if>
		<if test='pd.CONFIRM_USER != null and pd.CONFIRM_USER != ""'><!-- 隐患确认人 -->
			and f.CONFIRM_USER = #{pd.CONFIRM_USER}
		</if>
<!--		<if test="pd.keyFindEndTime != null and pd.keyFindEndTime != ''">&lt;!&ndash; 隐患发现时间 &ndash;&gt;-->
<!--			and f.DISCOVERYTIME &lt;= #{pd.keyFindEndTime}-->
<!--		</if>-->
		<if test="pd.STARTTIME != null and pd.STARTTIME != ''"><!-- 隐患发现时间 -->
			and f.DISCOVERYTIME >= #{pd.STARTTIME}
		</if>
		<if test="pd.ENDTIME != null and pd.ENDTIME != ''">
			and f.DISCOVERYTIME &lt;= #{pd.ENDTIME}
		</if>
		<if test="pd.keyConfirmUser != null and pd.keyConfirmUser != ''"><!-- 确认人 -->
			and (confirmUser.NAME LIKE CONCAT(CONCAT('%', #{pd.keyConfirmUser}),'%'))
		</if>
		<if test="pd.keyPrincipal != null and pd.keyPrincipal != ''"><!-- 确认人 -->
			and (re.NAME LIKE CONCAT(CONCAT('%', #{pd.keyPrincipal}),'%'))
		</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.keyType != null and pd.keyType != "" '><!-- 是否是特殊处理隐患 -->
			and exa.TYPE = #{pd.keyType}
		</if>
		<if test='pd.keyIsSpecial != null and pd.keyIsSpecial != "" '><!-- 是否是特殊处理隐患 -->
			and exa.TYPE = #{pd.keyIsSpecial}
		</if>
		<if test='pd.keyHiddenStatus != null and pd.keyHiddenStatus != "" '><!-- 是否是特殊处理隐患 -->
			and f.STATE = #{pd.keyHiddenStatus}
		</if>
		GROUP BY exa.HIDDENEXAMINE_ID
	<!--order by exa.STATE  , exa.CREATTIME  desc -->
		order by 	exa.STATE, FIELD (f.STATE , '1','-2','7','11','12','13','14','15','101','102') desc,
		f.STATE + 0 asc,
		f.CREATTIME desc
	</select>

<!-- 通过ID获取数据 -->
	<select id="findByIdAndHidden" parameterType="pd" resultType="pd">
		select
		f.*,
		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,
		ht2.NAME HIDDENTYPENAME2,
		conUser.NAME as conUserName,
		hregion.HIDDENREGION as hregionName,
		exa.EXAMINE,
		exa.STATE AS exaState,
		exa.TYPE as exaType,
		exa.DISPOSAL_PLAN,
		exa.DISPOSAL_FILE,
		exa.DELAY_TIME,
		exa.HIDDENEXAMINE_ID
		from
		BUS_HIDDENEXAMINE exa
		left join BUS_HIDDEN f on exa.HIDDEN_ID =f.HIDDEN_ID
		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 conUser on conUser.USER_ID = f.CONFIRM_USER
		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 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> ht2 on f.HIDDENTYPE2 = ht2.BIANMA
		left join bus_hiddenregion hregion on hregion.HIDDENREGION_ID = f.HIDDENPART
		where
		exa.HIDDENEXAMINE_ID = #{HIDDENEXAMINE_ID}
	</select>

	<!-- -->
	<update id="adoptExamine" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME},
		STATE = #{STATE}
		where
		HIDDENEXAMINE_ID = #{HIDDENEXAMINE_ID}
	</update>

</mapper>
