<?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.risk.RiskCheckItemMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.RISKPOINT_ID,
		f.CHECK_CONTENT,
		f.ISDELETE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.RISKCHECKITEM_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		RISKPOINT_ID,
		CHECK_CONTENT,
		ISDELETE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		RISKCHECKITEM_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{RISKPOINT_ID},
		#{CHECK_CONTENT},
		#{ISDELETE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{RISKCHECKITEM_ID}
	</sql>

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

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

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			RISKPOINT_ID = #{RISKPOINT_ID},
			CHECK_CONTENT = #{CHECK_CONTENT},
			RISKCHECKITEM_ID = RISKCHECKITEM_ID
		where
			RISKCHECKITEM_ID = #{RISKCHECKITEM_ID}
	</update>

	<!-- 修改 -->
	<update id="editByPoint" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			CHECK_CONTENT = #{CHECK_CONTENT}
		where
			RISKPOINT_ID = #{RISKPOINT_ID}
	</update>

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

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
			d5.BIANMA BIANMA5,
			d5.NAME DNAME5,
		<include refid="Field"></include>,
		un.RISKUNITNAME,
		un.SOURCE_ID,
		un.HIGH_RISK_PROCESS_ID,
<!-- 		po.POSITIONNAME, -->
		id.IDENTIFICATIONPARTS_ID,
		id.PARTSNAME,
		r.LEVELID,
		r.RISK_DESCR,
		r.USER_ID USERNAME
		from
		<include refid="tableName"></include> f
		left join bus_riskpoint r on r.RISKPOINT_ID = f.RISKPOINT_ID
		left join bus_riskunit un on un.RISKUNIT_ID = r.RISK_UNIT_ID
<!-- 		left join bus_riskpointposition po on po.RISKPOINTPOSITION_ID = r.RISK_POSITION_ID and po.ISDELETE = '0' -->
		left join bus_identificationparts id on id.IDENTIFICATIONPARTS_ID = r.IDENTIFICATION_ID
		left join
			<include refid="dicTableName"></include> d5
			on r.LEVELID = d5.BIANMA
		where f.ISDELETE = '0'
		and r.ISDELETE = '0'
		and un.ISDELETE = '0'
<!-- 		and po.ISDELETE = '0' -->
		and id.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					un.RISKUNITNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					id.PARTSNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					or
					r.USER_ID LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.RISKPOINT_ID != null and pd.RISKPOINT_ID != ''"><!-- 关键词检索 -->
			AND
				f.RISKPOINT_ID = #{pd.RISKPOINT_ID}
		</if>
		<if test="pd.FIND_USER_ID != null and pd.FIND_USER_ID != ''">
			and
					r.USER_ID=#{pd.FIND_USER_ID}
		</if>
		<if test="pd.OTHERLEVELID != null and pd.OTHERLEVELID != ''">
			and
					r.LEVELID=#{pd.OTHERLEVELID}
		</if>
		<if test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">
			and
					r.DEPARTMENT_ID=#{pd.DEPARTMENT_ID}
		</if>
		<if test="pd.LEVELID != null and pd.LEVELID != ''">
			and
					r.LEVELID=#{pd.LEVELID}
		</if>
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''">
			and
					r.CORPINFO_ID=#{pd.CORPINFO_ID}
		</if>
		ORDER by   un.RISKUNITNAME ,id.PARTSNAME,f.CREATTIME desc
	</select>

		<!-- 列表 -->
	<select id="itemlistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		un.RISKUNITNAME,
<!-- 		po.POSITIONNAME, -->
		id.PARTSNAME,
		d5.BIANMA BIANMA5,
		d5.NAME DNAME5,
		r.RISK_DESCR
		from
		<include refid="tableName"></include> f
		left join bus_riskpoint r on r.RISKPOINT_ID = f.RISKPOINT_ID
		left join bus_riskunit un on un.RISKUNIT_ID = r.RISK_UNIT_ID
<!-- 		left join bus_riskpointposition po on po.RISKPOINTPOSITION_ID = r.RISK_POSITION_ID and po.ISDELETE = '0' -->
		left join bus_identificationparts id on id.IDENTIFICATIONPARTS_ID = r.IDENTIFICATION_ID
			left join
			<include refid="dicTableName"></include> d5
			on r.LEVELID = d5.BIANMA
		where f.ISDELETE = '0'
		and r.ISDELETE = '0'
		and un.ISDELETE = '0'
		and po.ISDELETE = '0'
		and id.ISDELETE = '0'
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					f.CHECK_CONTENT LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.RISKPOINT_ID != null and pd.RISKPOINT_ID != ''"><!-- 关键词检索 -->
			AND
				f.RISKPOINT_ID = #{pd.RISKPOINT_ID}
		</if>
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
			and r.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		order by f.RISKPOINT_ID, f.OPERATTIME desc
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		un.RISKUNITNAME,
		un.SOURCE_ID,
		un.HIGH_RISK_PROCESS_ID,
<!-- 		po.POSITIONNAME, -->
		id.IDENTIFICATIONPARTS_ID,
		id.PARTSNAME,
		d5.BIANMA BIANMA5,
		d5.NAME DNAME5,
		r.RISK_DESCR,
		r.USER_ID USERNAME
		from
		<include refid="tableName"></include> f
		left join bus_riskpoint r on r.RISKPOINT_ID = f.RISKPOINT_ID
		left join bus_identificationparts id on id.IDENTIFICATIONPARTS_ID = r.IDENTIFICATION_ID
		left join bus_riskunit un on un.RISKUNIT_ID = id.RISK_UNIT_ID

			left join
			<include refid="dicTableName"></include> d5
			on r.LEVELID = d5.BIANMA
		where f.ISDELETE = '0'
		and r.ISDELETE = '0'
		and un.ISDELETE = '0'
<!-- 		and po.ISDELETE = '0' -->
		and id.ISDELETE = '0'
		and ( (
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			r.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''"><!-- 关键词检索 -->
			and r.DEPARTMENT_ID = #{DEPARTMENT_ID}
		</if>
		<if test="USER_ID != null and USER_ID != ''"><!-- 关键词检索 -->
			and r.USER_ID = #{USER_ID}
		</if>)

		<if test="idsArray != null and idsArray != ''"><!-- 关键词检索 -->
			or
				RISKCHECKITEM_ID in
				<foreach item="item" index="index" collection="idsArray" open="(" separator="," close=")">
		        	#{item}
				</foreach>
		</if>
		)
		<if test="KEYWORDS != null and KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					un.RISKUNITNAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					or
					id.PARTSNAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
				)
		</if>
		order by un.RISKUNIT_ID,id.IDENTIFICATIONPARTS_ID
	</select>

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

	<!-- 列表(全部) -->
	<select id="queryRiskCheck" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		un.RISKUNITNAME,
<!-- 		po.POSITIONNAME, -->
		id.PARTSNAME,
		d5.BIANMA BIANMA5,
		d5.NAME DNAME5,
		r.RISK_DESCR
		from
		<include refid="tableName"></include> f
		left join bus_riskpoint r on r.RISKPOINT_ID = f.RISKPOINT_ID
		left join bus_riskunit un on un.RISKUNIT_ID = r.RISK_UNIT_ID
<!-- 		left join bus_riskpointposition po on po.RISKPOINTPOSITION_ID = r.RISK_POSITION_ID and po.ISDELETE = '0' -->
		left join bus_identificationparts id on id.IDENTIFICATIONPARTS_ID = r.IDENTIFICATION_ID
			left join
			<include refid="dicTableName"></include> d5
			on r.LEVELID = d5.BIANMA
		where f.ISDELETE = '0'
		and r.ISDELETE = '0'
		and un.ISDELETE = '0'
<!-- 		and po.ISDELETE = '0' -->
		and id.ISDELETE = '0'
		<if test="KEYWORDS != null and KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					un.RISKUNITNAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
					or
					id.PARTSNAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
				)
		</if>
		and RISKCHECKITEM_ID in
		<foreach item="item" index="index" collection="idsArray" open="(" separator="," close=")">
        	#{item}
		</foreach>
	</select>

</mapper>
