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

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.RISK_UNIT_ID,
		f.CORPINFO_ID,
		f.IDENTIFICATION_ID,
		f.RISK_DESCR,
		f.RISK_POSITION_ID,
		f.LEVELID,
		f.MEASURES,
		f.ACCIDENTS,
		f.ACCIDENTS_NAME,
		f.EME_MEASURES,
		f.DEPARTMENT_ID,
		f.USER_ID,
		f.ISDELETE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.LONGITUDE,
		f.LATITUDE,
		f.RISKPOINT_ID,
		SORT
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		RISK_UNIT_ID,
		CORPINFO_ID,
		IDENTIFICATION_ID,
		RISK_DESCR,
		RISK_POSITION_ID,
		LEVELID,
		MEASURES,
		ACCIDENTS,
		ACCIDENTS_NAME,
		EME_MEASURES,
		DEPARTMENT_ID,
		USER_ID,
		ISDELETE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		LONGITUDE,
		LATITUDE,
		RISKPOINT_ID,
		SORT
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{RISK_UNIT_ID},
		#{CORPINFO_ID},
		#{IDENTIFICATION_ID},
		#{RISK_DESCR},
		#{RISK_POSITION_ID},
		#{LEVELID},
		#{MEASURES},
		#{ACCIDENTS},
		#{ACCIDENTS_NAME},
		#{EME_MEASURES},
		#{DEPARTMENT_ID},
		#{USER_ID},
		#{ISDELETE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{LONGITUDE},
		#{LATITUDE},
		#{RISKPOINT_ID},
		#{SORT}
	</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
			RISKPOINT_ID = #{RISKPOINT_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			RISK_UNIT_ID = #{RISK_UNIT_ID},
			IDENTIFICATION_ID = #{IDENTIFICATION_ID},
			RISK_DESCR = #{RISK_DESCR},
			RISK_POSITION_ID = #{RISK_POSITION_ID},
			LEVELID = #{LEVELID},
			MEASURES = #{MEASURES},
			ACCIDENTS = #{ACCIDENTS},
			ACCIDENTS_NAME=#{ACCIDENTS_NAME},
			EME_MEASURES = #{EME_MEASURES},
			DEPARTMENT_ID = #{DEPARTMENT_ID},
			USER_ID = #{USER_ID},
			LONGITUDE = #{LONGITUDE},
			LATITUDE = #{LATITUDE},
			RISKPOINT_ID = RISKPOINT_ID
		where
			RISKPOINT_ID = #{RISKPOINT_ID}
	</update>

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

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
			d5.BIANMA BIANMA5,
			d5.NAME DNAME5,
		<include refid="Field"></include>,
			un.RISKUNITNAME,
			po.POSITIONNAME,
			id.PARTSNAME,
			de.NAME as DEPT_NAME,
			u.NAME as USER_NAME
		from
		<include refid="tableName"></include> f
			left join
			<include refid="dicTableName"></include> d5
			on f.LEVELID = d5.BIANMA
			left join bus_riskunit un on un.RISKUNIT_ID = f.RISK_UNIT_ID and un.ISDELETE = '0'
			left join bus_riskpointposition po on po.RISKPOINTPOSITION_ID = f.RISK_POSITION_ID and po.ISDELETE = '0'
			left join bus_identificationparts id on id.IDENTIFICATIONPARTS_ID = f.IDENTIFICATION_ID and id.ISDELETE = '0'
			left join OA_DEPARTMENT de on de.DEPARTMENT_ID = f.DEPARTMENT_ID
			left join SYS_USER u on u.USER_ID = f.USER_ID
		where
			f.RISKPOINT_ID = #{RISKPOINT_ID}
	</select>


	<!-- 通过ID获取数据 -->
	<select id="findByIdRes" parameterType="pd" resultType="pd">
		select
			un.RISKUNITNAME RISKUNIT,
			id.PARTSNAME IDENTIFICATION,
			f.RISK_DESCR,
			f.LEVELID,
			f.MEASURES,
			f.ACCIDENTS,
			f.EME_MEASURES,
			f.CREATOR,
			f.CREATTIME,
			f.OPERATOR,
			f.OPERATTIME,
			f.ACCIDENTS_NAME,
			f.RISKPOINT_ID IDEREPOSITORY_ID
		from
		<include refid="tableName"></include> f
			left join bus_riskunit un on un.RISKUNIT_ID = f.RISK_UNIT_ID and un.ISDELETE = '0'
			left join bus_identificationparts id on id.IDENTIFICATIONPARTS_ID = f.IDENTIFICATION_ID and id.ISDELETE = '0'
		where
			f.RISKPOINT_ID = #{RISKPOINT_ID}
	</select>

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
			d5.BIANMA BIANMA5,
			d5.NAME DNAME5,
		<include refid="Field"></include>,
		un.RISKUNITNAME,
		po.POSITIONNAME,
		id.PARTSNAME,
		de.NAME as DEPT_NAME,
		u.NAME as USER_NAME
		from
		<include refid="tableName"></include> f
			left join
			<include refid="dicTableName"></include> d5
			on f.LEVELID = d5.BIANMA
			left join bus_riskunit un on un.RISKUNIT_ID = f.RISK_UNIT_ID and un.ISDELETE = '0'
			left join bus_riskpointposition po on po.RISKPOINTPOSITION_ID = f.RISK_POSITION_ID and po.ISDELETE = '0'
			left join bus_identificationparts id on id.IDENTIFICATIONPARTS_ID = f.IDENTIFICATION_ID and id.ISDELETE = '0'
			left join OA_DEPARTMENT de on de.DEPARTMENT_ID = f.DEPARTMENT_ID
			left join SYS_USER u on u.USER_ID = f.USER_ID
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and
				(
					un.RISKUNITNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
					 or
					id.PARTSNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
				)
		</if>
		<if test="pd.DEPARTMENT_IDS != null and pd.DEPARTMENT_IDS != ''"><!-- 关键词检索 -->
			and
				(
					f.DEPARTMENT_ID in
					<foreach item="item" index="index" collection="pd.DEPARTMENT_IDS" open="(" separator="," close=")">
			                '${item}'
					</foreach>
					or f.USER_ID = #{pd.USER_ID}
				)
		</if>
		<if test="pd.LEVELID != null and pd.LEVELID != ''">
			and
					f.LEVELID=#{pd.LEVELID}
		</if>
		<if test="pd.FIND_USER_ID != null and pd.FIND_USER_ID != ''">
			and
					f.USER_ID=#{pd.FIND_USER_ID}
		</if>
		<if test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">
			and
					f.DEPARTMENT_ID=#{pd.DEPARTMENT_ID}
		</if>
		<if test="pd.RISK_UNIT_ID != null and pd.RISK_UNIT_ID != ''">
			and
					f.RISK_UNIT_ID=#{pd.RISK_UNIT_ID}
		</if>
		<if test="pd.BIANMA != null and pd.BIANMA != ''">
			and 	f.ACCIDENTS LIKE CONCAT(CONCAT('%', #{pd.BIANMA}),'%')
		</if>
		<if test="pd.gkzrName != null and pd.gkzrName != ''">
			and 	f.USER_ID LIKE CONCAT(CONCAT('%', #{pd.gkzrName}),'%')
		</if>
		order by f.SORT asc,f.OPERATTIME asc ,RISKUNITNAME desc ,PARTSNAME desc
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		un.RISKUNITNAME,
		id.PARTSNAME
		from
		<include refid="tableName"></include> f
		left join bus_riskunit un on un.RISKUNIT_ID = f.RISK_UNIT_ID
		left join bus_identificationparts id on id.IDENTIFICATIONPARTS_ID = f.IDENTIFICATION_ID
		where f.ISDELETE = '0'
		<if test="CHECK_IDENTIFICATION_ID != null and CHECK_IDENTIFICATION_ID != ''">
		 	 and f.IDENTIFICATION_ID=#{CHECK_IDENTIFICATION_ID}
	 	</if>
	</select>


	<select id="listAllByDc" parameterType="pd" resultType="pd">
		select b.*
		,(select RISKUNITNAME from bus_riskunit where r.RISK_UNIT_ID=RISKUNIT_ID) as RISKUNITNAME
		,(select PARTSNAME from bus_identificationparts where r.IDENTIFICATION_ID=IDENTIFICATIONPARTS_ID) as PARTSNAME
		,r.RISK_DESCR
		,r.RISK_POSITION_ID
		,(select NAME from SYS_DICTIONARIES where r.LEVELID=BIANMA) as LEVELID
		,r.MEASURES
		,r.ACCIDENTS
		,r.EME_MEASURES
		,(select NAME from oa_department where r.DEPARTMENT_ID=DEPARTMENT_ID) as DEPARTMENT_ID
		,(select NAME from sys_user where r.USER_ID=USER_ID) as USER_ID
		,(select CORP_NAME from bus_corp_info where r.CORPINFO_ID=CORPINFO_ID) as CORP_NAME
		,r.LONGITUDE
		,r.LATITUDE
		,r.ACCIDENTS_NAME
		 from bus_risk_check_item b LEFT JOIN bus_riskpoint r on b.RISKPOINT_ID=r.RISKPOINT_ID where r.ISDELETE='0'
		 	<if test="CORPINFO_ID != null and CORPINFO_ID != ''">
		 	 and r.CORPINFO_ID=#{CORPINFO_ID}
		 	</if>
		   order by RISKUNITNAME,r.RISKPOINT_ID
	</select>
	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			RISKPOINT_ID in
		<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

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

	<!-- 总数统计 -->
	<select id="statisticsAll" parameterType="pd" resultType="pd">
        SELECT
				t.LEVELID,
				count(1) as COUNT
			FROM
				BUS_RISKPOINT t
			WHERE
				  t.CORPINFO_ID=#{CORPINFO_ID} and t.ISDELETE='0'
			  <if test="DEPARTMENT_IDS != null and DEPARTMENT_IDS != ''"><!-- 关键词检索 -->
				and
					(
						t.DEPARTMENT_ID in
						<foreach item="item" index="index" collection="DEPARTMENT_IDS" open="(" separator="," close=")">
				                 #{item}
						</foreach>
						or t.USER_ID = #{USER_ID}
					)
			</if>
			GROUP BY
				t.LEVELID
   	</select>

	<!-- 列表 -->
	<select id="datalistPageForNameLikes" parameterType="page" resultType="pd">
		select
			d5.BIANMA BIANMA5,
			d5.NAME DNAME5,
		<include refid="Field"></include>,
		un.RISKUNITNAME,
		po.POSITIONNAME,
		id.PARTSNAME,
		de.NAME as DEPT_NAME,
		u.NAME as USER_NAME
		from
		<include refid="tableName"></include> f
			left join
			<include refid="dicTableName"></include> d5
			on f.LEVELID = d5.BIANMA

			left join bus_riskunit un on un.RISKUNIT_ID = f.RISK_UNIT_ID and un.ISDELETE = '0'
			left join bus_riskpointposition po on po.RISKPOINTPOSITION_ID = f.RISK_POSITION_ID and po.ISDELETE = '0'
			left join bus_identificationparts id on id.IDENTIFICATIONPARTS_ID = f.IDENTIFICATION_ID and id.ISDELETE = '0'
			left join OA_DEPARTMENT de on de.DEPARTMENT_ID = f.DEPARTMENT_ID
			left join SYS_USER u on u.USER_ID = f.USER_ID
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		<if test="pd.NameLiskes != null and pd.NameLiskes != ''"><!-- 关键词检索 -->
			and
				(
					f.ACCIDENTS_NAME LIKE CONCAT(CONCAT('%', #{pd.NameLiskes}),'%')
 					 or
					id.PARTSNAME  LIKE CONCAT(CONCAT('%', #{pd.NameLiskes}),'%')
					or
					f.RISK_DESCR LIKE CONCAT(CONCAT('%', #{pd.NameLiskes}),'%')
				)
		</if>
		<if test="pd.RISK_UNIT_ID != null and pd.RISK_UNIT_ID != ''">
			and
					f.RISK_UNIT_ID=#{pd.RISK_UNIT_ID}
		</if>
		order by f.OPERATTIME desc
	</select>
	<!-- 修改 -->
	<update id="editLonAndLat" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			LONGITUDE = #{LONGITUDE},
			LATITUDE = #{LATITUDE}
		where
			RISKPOINT_ID = #{RISKPOINT_ID}
	</update>
	<!-- 总数统计 -->
	<select id="countByCorpInfo" parameterType="pd" resultType="String">
        SELECT
				count(1) as count
			FROM
				BUS_RISKPOINT t
			WHERE
				  t.CORPINFO_ID=#{CORPINFO_ID}

   	</select>
</mapper>
