<?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.check.ListCheckItemMapper">

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

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

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

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

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{LISTMANAGER_ID},
		#{RISKPOINT_ID},
		#{RISKCHECKITEM_ID},
		#{CORPINFO_ID},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{LISTCHECKITEM_ID},
		#{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
			LISTCHECKITEM_ID = #{LISTCHECKITEM_ID}
	</delete>

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

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		un.RISKUNITNAME,
		po.POSITIONNAME,
		id.PARTSNAME,
		d5.BIANMA BIANMA5,
		d5.NAME DNAME5,
		r.RISK_DESCR,
		chi.CHECK_CONTENT
		from
		<include refid="tableName"></include> f
		left join BUS_RISK_CHECK_ITEM chi on chi.RISKCHECKITEM_ID=f.RISKCHECKITEM_ID
		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
		left join bus_identificationparts id on id.IDENTIFICATIONPARTS_ID = r.IDENTIFICATION_ID
		left join SYS_DICTIONARIES d5 on r.LEVELID = d5.BIANMA
		where
			f.LISTCHECKITEM_ID = #{LISTCHECKITEM_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>,
		un.RISKUNITNAME,
		un.RISKUNIT_ID,
		id.IDENTIFICATIONPARTS_ID,
		id.PARTSNAME,
		id.ELECTRONIC_FENCE_NAME,
		id.ELECTRONIC_FENCE_ID,
		d5.BIANMA BIANMA5,
		d5.NAME DNAME5,
		r.RISK_DESCR,
		chi.CHECK_CONTENT,
		d.NAME as DEPARTMENT_NAME,
		u.NAME USER_NAME,
		r.USER_ID as USERNAME,

		(
			SELECT
				COUNT(h.HIDDEN_ID)
			FROM BUS_HIDDEN h
			WHERE
				h.RISKITEM_ID = f.LISTCHECKITEM_ID
				AND h.LISTMANAGER_ID = f.LISTMANAGER_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,
		btl.TERM_DESCRIBE as BAO_BAO_NUM_NAME
		from
		<include refid="tableName"></include> f
		left join BUS_RISK_CHECK_ITEM chi on chi.RISKCHECKITEM_ID=f.RISKCHECKITEM_ID
		left join bus_listmanager bl on bl.LISTMANAGER_ID = f.LISTMANAGER_ID
		left join sys_dictionaries blsd on blsd.DICTIONARIES_ID = bl.TASK_TYPE
		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 SYS_DICTIONARIES d5 on r.LEVELID = d5.BIANMA
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID=r.DEPARTMENT_ID
		left join sys_user u on u.USER_ID = r.USER_ID
		left join bus_term_library btl on btl.TASK_NUM = f.BAO_BAO_NUM and btl.WORK_TYPE = blsd.BZ
		where f.ISDELETE = '0'
		<if test="LISTMANAGER_ID != null and LISTMANAGER_ID != ''"><!-- 关键词检索 -->
			and f.LISTMANAGER_ID = #{LISTMANAGER_ID}
		</if>
		<if test="RISKPOINT_ID != null and RISKPOINT_ID != ''"><!-- 关键词检索 -->
			and f.RISKPOINT_ID = #{RISKPOINT_ID}
		</if>
		<if test="KEYWORDS != null and KEYWORDS != ''"><!-- 关键词检索 -->
			and
			(
			un.RISKUNITNAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
			or
			id.PARTSNAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
			)
		</if>
		<if test="IDENTIFICATIONPARTS_ID != null and IDENTIFICATIONPARTS_ID != ''"><!-- 关键词检索 -->
			and id.IDENTIFICATIONPARTS_ID = #{IDENTIFICATIONPARTS_ID}
		</if>
		order by (select o.ORDERBY from BUS_CHECKROUTE o where o.LISTMANAGER_ID = f.LISTMANAGER_ID and o.IDENTIFICATIONPARTS_ID = id.IDENTIFICATIONPARTS_ID)
	</select>

	<!-- 查询未对接数据 -->
	<select id="findHidden" parameterType="pd" resultType="pd" >
		SELECT
			h.HIDDEN_ID
		FROM BUS_HIDDEN h
		WHERE
			h.RISKITEM_ID = #{LISTCHECKITEM_ID}
		  AND h.LISTMANAGER_ID = #{LISTMANAGER_ID}
		  AND h.ISDELETE = '0' and h.SOURCE = '2'
		  AND h.STATE NOT IN ('4','0') limit 1
	</select>


	<select id="listByriskpointid" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		left join bus_listmanager bl on bl.LISTMANAGER_ID = f.LISTMANAGER_ID
		where f.ISDELETE = '0' and bl.ISDELETE != '-1'
		<if test="RISKPOINT_ID != null and RISKPOINT_ID != ''"><!-- 关键词检索 -->
			and f.RISKPOINT_ID = #{RISKPOINT_ID}
		</if>
		<if test="RISKPOINT_IDS != null"><!-- 关键词检索 -->
			and f.RISKPOINT_ID in
			<foreach item="item" index="index"
					 collection="RISKPOINT_IDS" open="(" separator="," close=")">
				'${item}'
			</foreach>
		</if>
	</select>


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

	<!-- 查询未对接数据 -->
	<select id="listForDocking" parameterType="pd" resultType="pd" >
		SELECT
			lc.*,
			ci.CHECK_CONTENT
		FROM
			BUS_LIST_CHECKITEM lc
			left join BUS_LISTMANAGER l on l.LISTMANAGER_ID = lc.LISTMANAGER_ID
			left join BUS_RISK_CHECK_ITEM ci on l.RISKCHECKITEM_ID = ci.RISKCHECKITEM_ID
		WHERE
			lc.CORPINFO_ID = #{CORPINFO_ID}
		  and lc.IS_DOCKING is null
		  and lc.ISDELETE = 0
		  and l.ISDELETE = 0
	</select>
    <select id="findByRiskPointIds" resultType="com.zcloud.entity.PageData">
		select distinct a.LISTMANAGER_ID
		from bus_list_checkitem a
		where a.RISKPOINT_ID in
		<foreach item="item" index="index" collection="ids" open="(" separator="," close=")">
			#{item}
		</foreach>
	</select>

	<select id="findByRiskPointId" resultType="com.zcloud.entity.PageData">
		select distinct a.LISTMANAGER_ID
		from bus_list_checkitem a
		where a.ISDELETE = '0'
		  <if test="RISKPOINT_ID != null and RISKPOINT_ID != ''">
			  and a.RISKPOINT_ID = #{RISKPOINT_ID}
		  </if>
		  <if test="riskPoints != null and riskPoints.size > 0">
			  and a.RISKPOINT_ID in
			  <foreach item="item" index="index" collection="riskPoints" open="(" separator="," close=")">
				  #{item}
			  </foreach>
		  </if>
	</select>

    <update id="updateDocking" parameterType="pd" >
		update
			BUS_LIST_CHECKITEM
		set
			IS_DOCKING = 1
		where
			LISTCHECKITEM_ID = #{LISTCHECKITEM_ID}
	</update>
</mapper>
