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

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.CHECKRECORD_ID,
		f.LISTMANAGER_ID,
		f.LISTCHECKITEM_ID,
		f.RISKUNITNAME,
		f.PARTSNAME,
		f.RISK_DESCR,
		f.POSITIONNAME,
		f.LEVEL_NAME,
		f.CHECK_CONTENT,
		f.ISNORMAL,
		f.RECORDITEM_ID,
		f.LONGITUDE,
		f.LATITUDE,
		f.CORPINFO_ID,
		f.RISKPOINT_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		CHECKRECORD_ID,
		LISTMANAGER_ID,
		LISTCHECKITEM_ID,
		RISKUNITNAME,
		PARTSNAME,
		RISK_DESCR,
		POSITIONNAME,
		LEVEL_NAME,
		CHECK_CONTENT,
		ISNORMAL,
		RECORDITEM_ID,
		LONGITUDE,
		LATITUDE,
		CORPINFO_ID,
		RISKPOINT_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CHECKRECORD_ID},
		#{LISTMANAGER_ID},
		#{LISTCHECKITEM_ID},
		#{RISKUNITNAME},
		#{PARTSNAME},
		#{RISK_DESCR},
		#{POSITIONNAME},
		#{LEVEL_NAME},
		#{CHECK_CONTENT},
		#{ISNORMAL},
		#{RECORDITEM_ID},
		#{LONGITUDE},
		#{LATITUDE},
		#{CORPINFO_ID},
		#{RISKPOINT_ID}
	</sql>

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

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

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISNORMAL = #{ISNORMAL},
			LONGITUDE=#{LONGITUDE},
			LATITUDE=#{LATITUDE}
		where
			RECORDITEM_ID = #{RECORDITEM_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			f.RECORDITEM_ID = #{RECORDITEM_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>,
		(select h.HIDDEN_ID from BUS_HIDDEN h where h.RECORDITEM_ID=f.RECORDITEM_ID and h.CHECKRECORD_ID=f.CHECKRECORD_ID and h.ISDELETE= '0') as HIDDEN_ID,
		( select count(1) from bus_imgfiles i where i.FOREIGN_KEY = f.RECORDITEM_ID and i.TYPE = '14' ) AS IMGCOUNT,
		r.IDENTIFICATION_ID
		from
		<include refid="tableName"></include> f
		left join BUS_RISKPOINT r on r.RISKPOINT_ID=f.RISKPOINT_ID
		left join bus_identificationparts m on r.IDENTIFICATION_ID = m.IDENTIFICATIONPARTS_ID
		where f.CHECKRECORD_ID=#{CHECKRECORD_ID}
		<if test="RISKPOINT_ID != null and RISKPOINT_ID != ''"><!-- 关键词检索 -->
			and
				f.RISKPOINT_ID=#{RISKPOINT_ID}
		</if>
		<if test="IDENTIFICATION_ID != null and IDENTIFICATION_ID != ''"><!-- 关键词检索 -->
			and
				r.IDENTIFICATION_ID=#{IDENTIFICATION_ID}
		</if>
		ORDER BY
		( SELECT o.ORDERBY FROM BUS_CHECKROUTE o WHERE o.LISTMANAGER_ID = f.LISTMANAGER_ID AND o.IDENTIFICATIONPARTS_ID = m.IDENTIFICATIONPARTS_ID )
	</select>

	<!-- 列表(全部) -->
	<select id="listAllForMap" parameterType="pd" resultType="pd">
		select
			f.PARTSNAME,
			f.LONGITUDE,
			f.LATITUDE
		from
		<include refid="tableName"></include> f
		where f.CHECKRECORD_ID=#{CHECKRECORD_ID}
		group by f.PARTSNAME,f.LONGITUDE,	f.LATITUDE
	</select>

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

	<!-- 修改状态 -->
	<update id="editIsnormal" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISNORMAL = #{ISNORMAL}
		where
			CHECKRECORD_ID = #{CHECKRECORD_ID}
			AND LISTMANAGER_ID = #{LISTMANAGER_ID}
			AND LISTCHECKITEM_ID = #{LISTCHECKITEM_ID}
	</update>

</mapper>
