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

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.COMMON_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_ITEM_STATUS,
		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.COMMON_ITEM_ID
	</sql>

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

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

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			<if test="COMMON_ID != null and COMMON_ID != ''">
				COMMON_ID = #{COMMON_ID},
			</if>
			<if test="CHECK_CATEGORY != null and CHECK_CATEGORY != ''">
				CHECK_CATEGORY = #{CHECK_CATEGORY},
			</if>
			<if test="CHECK_ITEM != null and CHECK_ITEM != ''">
				CHECK_ITEM = #{CHECK_ITEM},
			</if>
			<if test="CHECK_CONTENT != null and CHECK_CONTENT != ''">
				CHECK_CONTENT = #{CHECK_CONTENT},
			</if>
			<if test="CHECK_STANDARD != null and CHECK_STANDARD != ''">
				CHECK_STANDARD = #{CHECK_STANDARD},
			</if>
			<if test="REFERENCE_BASIS != null and REFERENCE_BASIS != ''">
				REFERENCE_BASIS = #{REFERENCE_BASIS},
			</if>
			<if test="CHECK_QUALIFIED != null and CHECK_QUALIFIED != ''">
				CHECK_QUALIFIED = #{CHECK_QUALIFIED},
			</if>
			<if test="CHECK_UNQUALIFIED != null and CHECK_UNQUALIFIED != ''">
				CHECK_UNQUALIFIED = #{CHECK_UNQUALIFIED},
			</if>
			<if test="OPERATION_TYPE != null and OPERATION_TYPE != ''">
				OPERATION_TYPE = #{OPERATION_TYPE},
			</if>
			<if test="COMMON_ITEM_SORT != null">
				COMMON_ITEM_SORT = #{COMMON_ITEM_SORT},
			</if>
			<if test="COMMON_ITEM_STATUS != null">
				COMMON_ITEM_STATUS = #{COMMON_ITEM_STATUS},
			</if>
			<if test="ISDELETE != null and ISDELETE != ''">
				ISDELETE = #{ISDELETE},
			</if>
			<if test="CREATOR_ID != null and CREATOR_ID != ''">
				CREATOR_ID = #{CREATOR_ID},
			</if>
			<if test="CREATOR_NAME != null and CREATOR_NAME != ''">
				CREATOR_NAME = #{CREATOR_NAME},
			</if>
			<if test="CREATE_TIME != null and CREATE_TIME != ''">
				CREATE_TIME = #{CREATE_TIME},
			</if>
			<if test="OPERATOR_ID != null and OPERATOR_ID != ''">
				OPERATOR_ID = #{OPERATOR_ID},
			</if>
			<if test="OPERATOR_NAME != null and OPERATOR_NAME != ''">
				OPERATOR_NAME = #{OPERATOR_NAME},
			</if>
			<if test="OPERATE_TIME != null and OPERATE_TIME != ''">
				OPERATE_TIME = #{OPERATE_TIME},
			</if>
			<if test="DELETEOR_ID != null and DELETEOR_ID != ''">
				DELETEOR_ID = #{DELETEOR_ID},
			</if>
			<if test="DELETEOR_NAME != null and DELETEOR_NAME != ''">
				DELETEOR_NAME = #{DELETEOR_NAME},
			</if>
			<if test="DELETE_TIME != null and DELETE_TIME != ''">
				DELETE_TIME = #{DELETE_TIME},
			</if>
			COMMON_ITEM_ID = COMMON_ITEM_ID
		where
			COMMON_ITEM_ID = #{COMMON_ITEM_ID}
	</update>


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

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
        select
		dca.DICTIONARY_NAME CHECK_CATEGORY_NAME,
		di.DICTIONARY_NAME CHECK_ITEM_NAME,
		(select count(*) from BUS_HIDDENDANGERCHECKSTANDARD_CUSTOM_ITEM where COMMON_ITEM_ID = f.COMMON_ITEM_ID) COUNT_CUSTOM,
		<include refid="Field"></include>
        from
        <include refid="tableName"></include> f
        LEFT JOIN BUS_HIDDENDANGERCHECKSTANDARD_DICTIONARY dca on dca.DICTIONARY_ID = f.CHECK_CATEGORY and dca.DICTIONARY_LEVEL = 1
		LEFT JOIN BUS_HIDDENDANGERCHECKSTANDARD_DICTIONARY di on di.DICTIONARY_ID = f.CHECK_ITEM and di.DICTIONARY_LEVEL = 2
		where f.ISDELETE = '0'
        <if test="pd.COMMON_ID != null and pd.COMMON_ID != ''">
			and f.COMMON_ID = #{pd.COMMON_ID}
		</if>
        <if test="pd.CHECK_CATEGORY != null and pd.CHECK_CATEGORY != ''">
			and f.CHECK_CATEGORY = #{pd.CHECK_CATEGORY}
		</if>
		<if test="pd.CHECK_ITEM != null and pd.CHECK_ITEM != ''">
			and f.CHECK_ITEM = #{pd.CHECK_ITEM}
		</if>
		<if test="pd.CHECK_CONTENT != null and pd.CHECK_CONTENT != ''">
			and f.CHECK_CONTENT like CONCAT(CONCAT('%', #{pd.CHECK_CONTENT}),'%')
		</if>
		<if test="pd.COMMON_ITEM_STATUS != null and pd.COMMON_ITEM_STATUS != ''">
			and f.COMMON_ITEM_STATUS = #{pd.COMMON_ITEM_STATUS}
		</if>
        order by dca.DICTIONARY_SORT asc, di.DICTIONARY_SORT asc, f.COMMON_ITEM_SORT asc
    </select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
			dca.DICTIONARY_NAME CHECK_CATEGORY_NAME,
			di.DICTIONARY_NAME CHECK_ITEM_NAME,
			<include refid="Field"></include>
		from
			<include refid="tableName"></include> f
			LEFT JOIN BUS_HIDDENDANGERCHECKSTANDARD_DICTIONARY dca on dca.DICTIONARY_ID = f.CHECK_CATEGORY and dca.DICTIONARY_LEVEL = 1
			LEFT JOIN BUS_HIDDENDANGERCHECKSTANDARD_DICTIONARY di on di.DICTIONARY_ID = f.CHECK_ITEM and di.DICTIONARY_LEVEL = 2
		where f.ISDELETE = '0'
		<if test="COMMON_ID != null and COMMON_ID != ''">
			and f.COMMON_ID = #{COMMON_ID}
		</if>
		<if test="COMMON_ITEM_STATUS != null and COMMON_ITEM_STATUS != ''">
			and f.COMMON_ITEM_STATUS = #{COMMON_ITEM_STATUS}
		</if>
		order by dca.DICTIONARY_SORT asc, di.DICTIONARY_SORT asc, f.COMMON_ITEM_SORT asc
	</select>

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

	<!--	* 获取符合条件数，+1后作为排序使用-->
	<select id="getCount" parameterType="pd" resultType="Integer" >
		SELECT
		COUNT(*)
		FROM
		<include refid="tableName"></include>
		WHERE 1 = 1
		<if test="ISDELETE != null and ISDELETE != ''">
			and ISDELETE = #{ISDELETE}
		</if>
		<if test="COMMON_ID != null and COMMON_ID != ''">
			 and COMMON_ID = #{COMMON_ID}
		</if>
	</select>

	<!-- 删除(根据标准删除所有标准项)-->
	<delete id="deleteByCommon" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		ISDELETE = #{ISDELETE},
		DELETEOR_ID = #{DELETEOR_ID},
		DELETEOR_NAME = #{DELETEOR_NAME},
		DELETE_TIME = #{DELETE_TIME}
		where ISDELETE = '0'
		and COMMON_ID = #{COMMON_ID}
	</delete>

</mapper>
