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

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.RISKUNITNAME,
		f.CORPINFO_ID,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.ISDELETE,
		f.RISKUNIT_ID,
		f.DEPARTMENT_ID,
		f.SOURCE_ID,
		f.HIGH_RISK_PROCESS_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		RISKUNITNAME,
		CORPINFO_ID,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		ISDELETE,
		RISKUNIT_ID,
		DEPARTMENT_ID,
		SOURCE_ID,
		HIGH_RISK_PROCESS_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{RISKUNITNAME},
		#{CORPINFO_ID},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{ISDELETE},
		#{RISKUNIT_ID},
		#{DEPARTMENT_ID},
		#{SOURCE_ID},
		#{HIGH_RISK_PROCESS_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',
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME}
		where
			RISKUNIT_ID = #{RISKUNIT_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			RISKUNITNAME = #{RISKUNITNAME},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			DEPARTMENT_ID=#{DEPARTMENT_ID},
			RISKUNIT_ID = RISKUNIT_ID,
			SOURCE_ID = #{SOURCE_ID},
			HIGH_RISK_PROCESS_ID = #{HIGH_RISK_PROCESS_ID}
		where
			RISKUNIT_ID = #{RISKUNIT_ID}
	</update>

	<update id="updateDocking" parameterType="pd" >
		update
			BUS_RISKUNIT
		set
			IS_DOCKING = 1
		where
			RISKUNIT_ID = #{RISKUNIT_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		de.NAME as DEPT_NAME
		from
		<include refid="tableName"></include> f
		left join OA_DEPARTMENT de on de.DEPARTMENT_ID = f.DEPARTMENT_ID
		where
			f.RISKUNIT_ID = #{RISKUNIT_ID}
	</select>

	<select id="findByCorpId" parameterType="pd" resultType="pd">
		select * from bus_corp_info_related where  CORPINFO_ID = #{CORPINFO_ID}
	</select>

	<select id="findByName" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		left join OA_DEPARTMENT de on de.DEPARTMENT_ID = f.DEPARTMENT_ID
		where 1=1 and  f.ISDELETE = '0'
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			and f.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="RISKUNITNAME != null and RISKUNITNAME != ''"><!-- 重名验证专用 -->
			AND
				f.RISKUNITNAME = #{RISKUNITNAME}
		</if>
		<if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''"><!-- 重名验证专用 -->
			AND
				f.DEPARTMENT_ID = #{DEPARTMENT_ID}
		</if>
		<if test="DEPARTMENT_NAME != null and DEPARTMENT_NAME != ''"><!-- 重名验证专用 -->
			AND
				de.NAME = #{DEPARTMENT_NAME}
		</if>
	</select>
	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="pd">
		select
		<include refid="Field"></include>,
		de.NAME as DEPT_NAME,
		getFullName(de.DEPARTMENT_ID) as DEPT_NAME_ALL,
		de.HEADMAN as HEADMAN,
		c.CORP_NAME as CORP_NAME
		from
		<include refid="tableName"></include> f
		left join OA_DEPARTMENT de on de.DEPARTMENT_ID = f.DEPARTMENT_ID
		left join bus_corp_info c on c.CORPINFO_ID = de.CORPINFO_ID
		where f.ISDELETE = '0'
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
			and f.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		<choose>
			<when test='pd.ISMAIN != null and pd.ISMAIN != "" and pd.ISMAIN == "1"'>
				<if test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
	                and
					f.DEPARTMENT_ID in
					<foreach item="item" index="index"
						collection="pd.DEPT_IDS" open="(" separator="," close=")">
						'${item}'
					</foreach>
	            </if>
			</when>
			<otherwise>
				<choose>
					<when test='pd.ISSUPERVISE != null and pd.ISSUPERVISE != "" and pd.ISSUPERVISE == "1"'>
						<choose>
							<when test='pd.ISLEADER != null and pd.ISLEADER != "" and pd.ISLEADER == "1"'><!-- 监管部门的部门领导需要查看下级部门和监管部门的 -->
								<choose>
									<when test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
										and
										f.DEPARTMENT_ID in
										<foreach item="item" index="index"
												 collection="pd.DEPT_IDS" open="(" separator="," close=")">
											'${item}'
										</foreach>
									</when>
									<otherwise>
										and
										((f.DEPARTMENT_ID in (
										select
										SUB_DEPARTMENT_ID
										from
										oa_supervision_department osd
										where
										osd.SUP_DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
										and osd.ISDELETE = '0'
										and osd.CORPINFO_ID  = #{pd.CORPINFO_ID}
										)) or (
										f.DEPARTMENT_ID in
										<foreach item="item" index="index"
												 collection="pd.DEPARTMENT_IDS" open="(" separator="," close=")">
											'${item}'
										</foreach>
										))
									</otherwise>
								</choose>
							</when>
							<when test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
								and
								f.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="pd.DEPT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</when>
							<otherwise>
								and
								(f.DEPARTMENT_ID in (
								select
								SUB_DEPARTMENT_ID
								from
								oa_supervision_department osd
								where
								osd.SUP_DEPARTMENT_ID = #{pd.DEPARTMENT_ID}
								and osd.ISDELETE = '0'
								and osd.CORPINFO_ID  = #{pd.CORPINFO_ID})
								or f.DEPARTMENT_ID = #{pd.DEPARTMENT_ID})<!-- 监管部门的普通用户需要查看自己部门的 -->
							</otherwise>
						</choose>
					</when>

					<when test='pd.ISLEADER != null and pd.ISLEADER != "" and pd.ISLEADER == "1"'>
						<choose>
							<when test="pd.DEPT_IDS != null and pd.DEPT_IDS != ''">
								and
								f.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="pd.DEPT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</when>
							<otherwise>
								and
								f.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="pd.DEPARTMENT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</otherwise>
						</choose>
					</when>
					<when test="pd.DEPARTMENT_ID != null and pd.DEPARTMENT_ID != ''">
						and f.DEPARTMENT_ID=#{pd.DEPARTMENT_ID}
					</when>
					<otherwise>
					</otherwise>
				</choose>
			</otherwise>
		</choose>
		<if test="pd.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 关键词检索 -->
			and (f.RISKUNITNAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%'))
		</if>

		<if test="pd.isBaoBao != '' and pd.isBaoBao != null">
			and exists(select 1 from bus_riskpoint b left join bus_list_checkitem c on c.RISKPOINT_ID = b.RISKPOINT_ID
			left join bus_listmanager d on d.LISTMANAGER_ID = c.LISTMANAGER_ID where f.RISKUNIT_ID = b.RISK_UNIT_ID and d.BAO_BAO_TYPE = #{pd.isBaoBao})
		</if>

		order by f.DEPARTMENT_ID,f.OPERATTIME desc

	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		c.CORP_NAME,
		de.NAME as DEPT_NAME
		from
		<include refid="tableName"></include> f
		left join BUS_CORP_INFO c on c.CORPINFO_ID = f.CORPINFO_ID
		left join OA_DEPARTMENT de on de.DEPARTMENT_ID = f.DEPARTMENT_ID
		where f.ISDELETE = '0'
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			and f.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="RISKUNITNAME != null and RISKUNITNAME != ''"><!-- 重名验证专用 -->
			AND
				f.RISKUNITNAME = #{RISKUNITNAME}
		</if>
		<if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''"><!-- 重名验证专用 -->
			AND
				f.DEPARTMENT_ID = #{DEPARTMENT_ID}
		</if>
		order by f.OPERATTIME desc
	</select>
	<select id="listOut" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		c.CORP_NAME,
		de.NAME as DEPT_NAME
		from
		<include refid="tableName"></include> f
		left join BUS_CORP_INFO c on c.CORPINFO_ID = f.CORPINFO_ID
		left join OA_DEPARTMENT de on de.DEPARTMENT_ID = f.DEPARTMENT_ID
		where f.ISDELETE = '0'
		and f.RISKUNIT_ID in
		<foreach item="item" index="index" open="(" separator="," close=")" collection="ArrayDATA_IDS">
			#{item}
		</foreach>
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			and f.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="RISKUNITNAME != null and RISKUNITNAME != ''"><!-- 重名验证专用 -->
			AND
			f.RISKUNITNAME = #{RISKUNITNAME}
		</if>
		<if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''"><!-- 重名验证专用 -->
			AND
			f.DEPARTMENT_ID = #{DEPARTMENT_ID}
		</if>
		order by f.OPERATTIME desc

	</select>
	<!-- 列表(首页) -->
	<select id="listIndexAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		c.CORP_NAME,
		de.NAME as DEPT_NAME
		from
		<include refid="tableName"></include> f
		left join BUS_CORP_INFO c on c.CORPINFO_ID = f.CORPINFO_ID
		left join OA_DEPARTMENT de on de.DEPARTMENT_ID = f.DEPARTMENT_ID
		where f.ISDELETE = '0'
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			and f.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		<if test="NOREPETITION != null and NOREPETITION != ''"><!-- 重名验证专用 -->
			<choose>
	            <when test="NOREPETITION=='1'">
	                group by f.RISKUNITNAME
	            </when>
	            <when test="NOREPETITION == '2'">
	                group by f.DEPARTMENT_ID,f.RISKUNITNAME
	            </when>
	            <otherwise>
	            </otherwise>
	        </choose>
		</if>

		order by f.OPERATTIME desc
	</select>

	<!-- 批量删除 -->
	<delete id="deleteAll" parameterType="String">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
			RISKUNIT_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>
  <!--  点击风险单元名称 整理详情 -->
	<select id="getRisByUnitId" parameterType="page" resultType="pd">
		select
				 f.IDENTIFICATION_ID,
				i.PARTSNAME,
				f.risk_descr,
				f.LEVELID,
				f.EME_MEASURES,
				f.MEASURES,
				f.ACCIDENTS,
				f.ACCIDENTS_NAME,
				f.EME_MEASURES
		FROM
					BUS_RISKPOINT f
				LEFT JOIN bus_identificationparts i ON f.IDENTIFICATION_ID = i.IDENTIFICATIONPARTS_ID
				WHERE
					1 = 1
				AND f.RISK_UNIT_ID = #{pd.RISK_UNIT_ID}
				and f.ISDELETE =0
				and i.ISDELETE =0
		order by f.OPERATTIME desc

	</select>

	<!-- 风险分布情况统计（BI页） -->
	<select id="listUnitLevel" parameterType="pd" resultType="pd">
		SELECT
			COUNT(a.LEVELID) COUNT,
			a.LEVELID
		FROM (
			SELECT
				f.RISKUNIT_ID,
				f.RISKUNITNAME,
				MIN(r.LEVELID) LEVELID
			FROM
				BUS_RISKUNIT f
				LEFT JOIN BUS_RISKPOINT r ON r.RISK_UNIT_ID = f.RISKUNIT_ID
			WHERE
				f.CORPINFO_ID = #{CORPINFO_ID}
				AND f.ISDELETE = '0'
				AND r.ISDELETE = '0'
			GROUP BY
				f.DEPARTMENT_ID,
				f.RISKUNITNAME
		) a
		GROUP BY a.LEVELID
	</select>

	<!-- 查询未对接数据 -->
	<select id="listForDocking" parameterType="pd" resultType="pd" >
		SELECT
			u.*
		FROM
			BUS_RISKUNIT u
		WHERE
			u.CORPINFO_ID = #{CORPINFO_ID}
		  and u.IS_DOCKING is null
		  and u.ISDELETE = 0
	</select>
</mapper>
