<?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.hiddenDangerCheckStandard.CustomItemMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.CUSTOM_ITEM_ID,
		f.CUSTOM_ID,
		f.CHECK_CATEGORY,
		f.CHECK_ITEM,
		f.CHECK_CONTENT,
		f.CHECK_STANDARD,
		f.REFERENCE_BASIS,
		f.CHECK_QUALIFIED,
		f.CHECK_UNQUALIFIED,
		f.OPERATION_TYPE,
		f.COMMON_ITEM_SORT,
		f.COMMON_ID,
		f.COMMON_ITEM_ID,
		f.ISDELETE,
		f.CREATOR_ID,
		f.CREATOR_NAME,
		f.CREATE_TIME,
		f.OPERATOR_ID,
		f.OPERATOR_NAME,
		f.OPERATE_TIME,
		f.DELETEOR_ID,
		f.DELETEOR_NAME,
		f.DELETE_TIME,
		f.BAO_BAO_NUM
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		CUSTOM_ITEM_ID,
		CUSTOM_ID,
		CHECK_CATEGORY,
		CHECK_ITEM,
		CHECK_CONTENT,
		CHECK_STANDARD,
		REFERENCE_BASIS,
		CHECK_QUALIFIED,
		CHECK_UNQUALIFIED,
		OPERATION_TYPE,
		COMMON_ITEM_SORT,
		COMMON_ID,
		COMMON_ITEM_ID,
		ISDELETE,
		CREATOR_ID,
		CREATOR_NAME,
		CREATE_TIME,
		OPERATOR_ID,
		OPERATOR_NAME,
		OPERATE_TIME,
		DELETEOR_ID,
		DELETEOR_NAME,
		DELETE_TIME,
		BAO_BAO_NUM
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CUSTOM_ITEM_ID},
		#{CUSTOM_ID},
		#{CHECK_CATEGORY},
		#{CHECK_ITEM},
		#{CHECK_CONTENT},
		#{CHECK_STANDARD},
		#{REFERENCE_BASIS},
		#{CHECK_QUALIFIED},
		#{CHECK_UNQUALIFIED},
		#{OPERATION_TYPE},
		#{COMMON_ITEM_SORT},
		#{COMMON_ID},
		#{COMMON_ITEM_ID},
		#{ISDELETE},
		#{CREATOR_ID},
		#{CREATOR_NAME},
		#{CREATE_TIME},
		#{OPERATOR_ID},
		#{OPERATOR_NAME},
		#{OPERATE_TIME},
		#{DELETEOR_ID},
		#{DELETEOR_NAME},
		#{DELETE_TIME},
		#{BAO_BAO_NUM}
	</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
		CUSTOM_ITEM_ID = #{CUSTOM_ITEM_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		CHECK_CATEGORY = #{CHECK_CATEGORY},
		CHECK_ITEM = #{CHECK_ITEM},
		CHECK_CONTENT = #{CHECK_CONTENT},
		CHECK_STANDARD = #{CHECK_STANDARD},
		REFERENCE_BASIS = #{REFERENCE_BASIS},
		CHECK_QUALIFIED = #{CHECK_QUALIFIED},
		CHECK_UNQUALIFIED = #{CHECK_UNQUALIFIED},
		OPERATION_TYPE = #{OPERATION_TYPE},
		BAO_BAO_NUM = #{BAO_BAO_NUM},
		CUSTOM_ITEM_ID = CUSTOM_ITEM_ID
		where
		CUSTOM_ITEM_ID = #{CUSTOM_ITEM_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		bhd1.DICTIONARY_NAME as CHECK_CATEGORY_NAME,
		bhd2.DICTIONARY_NAME as CHECK_ITEM_NAME
		from
		<include refid="tableName"></include> f
		left join bus_hiddendangercheckstandard_dictionary bhd1 on bhd1.DICTIONARY_ID = f.CHECK_CATEGORY and bhd1.ISDELETE = 0
		left join bus_hiddendangercheckstandard_dictionary bhd2 on bhd2.DICTIONARY_ID = f.CHECK_ITEM  and bhd2.ISDELETE = 0
		where
		f.CUSTOM_ITEM_ID = #{CUSTOM_ITEM_ID}
	</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>
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		d.NAME as DEPARTMENT_NAME,
		u.NAME USER_NAME,
		(
		SELECT
		COUNT(h.HIDDEN_ID)
		FROM bus_hiddendangercheckstandard_hidden h
		WHERE
		h.CUSTOM_ITEM_ID = f.CUSTOM_ITEM_ID
		AND h.CUSTOM_ID = f.CUSTOM_ID
		AND h.ISDELETE = '0' and h.SOURCE = '2'
		AND h.STATE NOT IN ('4','0')
		) as HASHIDDEN,
		bl.TASK_TYPE,
		blsd.NAME as TASK_TYPE_NAME,
		blsd.BZ as TASK_TYPE_NUM,
		bhd1.DICTIONARY_NAME as CHECK_CATEGORY_NAME,
		bhd2.DICTIONARY_NAME as CHECK_ITEM_NAME,
		btl.TERM_DESCRIBE as BAO_BAO_NUM_NAME
		from
		<include refid="tableName"></include> f
		left join bus_hiddendangercheckstandard_custom bl on bl.CUSTOM_ID = f.CUSTOM_ID
		left join sys_dictionaries blsd on blsd.DICTIONARIES_ID = bl.TASK_TYPE
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID = bl.DEPARTMENT_ID
		left join sys_user u on u.USER_ID = bl.USER_ID
		left join bus_hiddendangercheckstandard_dictionary bhd1 on bhd1.DICTIONARY_ID = f.CHECK_CATEGORY and bhd1.ISDELETE = 0
		left join bus_hiddendangercheckstandard_dictionary bhd2 on bhd2.DICTIONARY_ID = f.CHECK_ITEM  and bhd2.ISDELETE = 0
		left join bus_hiddendangercheckstandard_term_library btl on btl.TASK_NUM = f.BAO_BAO_NUM and btl.WORK_TYPE = blsd.BZ
		where f.ISDELETE = '0'
		<if test="CUSTOM_ID != null and CUSTOM_ID != ''"><!-- 检查表id -->
			and f.CUSTOM_ID = #{CUSTOM_ID}
		</if>
		<if test="CHECK_ITEM != null and CHECK_ITEM != ''"><!-- 检查项目id -->
			and f.CHECK_ITEM = #{CHECK_ITEM}
		</if>
		<if test="KEYWORDS != null and KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			f.CHECK_CONTENT LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
			or
			f.CHECK_STANDARD LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
			or
			f.REFERENCE_BASIS LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
			)
		</if>
		order by f.COMMON_ITEM_SORT ASC
	</select>

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

	<!-- 获取用户检查清单的检查项目 -->
	<select id="getCheckItem" parameterType="pd" resultType="pd">
		select
			f.CUSTOM_ID,
			f.CHECK_ITEM,
			d.DICTIONARY_NAMES
		from
			<include refid="tableName"></include> f
			left join BUS_HIDDENDANGERCHECKSTANDARD_DICTIONARY d on d.DICTIONARY_ID = f.CHECK_ITEM
		where f.ISDELETE = '0' and d.ISDELETE = '0'
			<if test="CUSTOM_ID != null and CUSTOM_ID != ''"><!-- 关键词检索 -->
				and f.CUSTOM_ID = #{CUSTOM_ID}
			</if>
		GROUP BY f.CHECK_ITEM
		ORDER BY d.DICTIONARY_SORT ASC
	</select>
	<!-- 查询未对接数据 -->
	<select id="findHidden" parameterType="pd" resultType="pd" >
		SELECT
			h.HIDDEN_ID
		FROM bus_hiddendangercheckstandard_hidden h
		WHERE
			h.CUSTOM_ITEM_ID = #{CUSTOM_ITEM_ID}
		  AND h.CUSTOM_ID = #{CUSTOM_ID}
		  AND h.ISDELETE = '0' and h.SOURCE = '2'
		  AND h.STATE NOT IN ('4','0') limit 1
	</select>
</mapper>
