<?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.inspection.SafetyEnvironmentalExplainMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.INSPECTION_ID,
		f.INSPECTED_EXPLAIN,
		f.INSPECTED_EXPLAIN_FILENAME,
		f.INSPECTED_EXPLAIN_FILEPATH,
		f.INSPECTED_SITEUSER_SIGN_IMG,
		f.INSPECTED_SITEUSER_SIGN_TIME,
		f.ISDELETE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.CORPINFO_ID,
		f.INSPECTION_EXPLAIN_ID,
		f.VALID,
		f.ISPASS
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		INSPECTION_ID,
		INSPECTED_EXPLAIN,
		INSPECTED_EXPLAIN_FILENAME,
		INSPECTED_EXPLAIN_FILEPATH,
		INSPECTED_SITEUSER_SIGN_IMG,
		INSPECTED_SITEUSER_SIGN_TIME,
		ISDELETE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		CORPINFO_ID,
		INSPECTION_EXPLAIN_ID,
		VALID,
		ISPASS
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{INSPECTION_ID},
		#{INSPECTED_EXPLAIN},
		#{INSPECTED_EXPLAIN_FILENAME},
		#{INSPECTED_EXPLAIN_FILEPATH},
		#{INSPECTED_SITEUSER_SIGN_IMG},
		#{INSPECTED_SITEUSER_SIGN_TIME},
		#{ISDELETE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{CORPINFO_ID},
		#{INSPECTION_EXPLAIN_ID},
		#{VALID},
		#{ISPASS}
	</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
			INSPECTION_EXPLAIN_ID = #{INSPECTION_EXPLAIN_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			INSPECTION_ID = #{INSPECTION_ID},
			INSPECTED_EXPLAIN = #{INSPECTED_EXPLAIN},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			INSPECTION_EXPLAIN_ID = INSPECTION_EXPLAIN_ID,
			VALID = #{VALID},
			ISPASS = #{ISPASS}
		where
			INSPECTION_EXPLAIN_ID = #{INSPECTION_EXPLAIN_ID}
	</update>

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

	<!-- 列表 -->
	<select id="datalistPage" parameterType="page" resultType="com.zcloud.entity.PageData">
		select
			iou.NAME INSPECTION_ORIGINATOR_NAME,
			iod.NAME INSPECTION_DEPARTMENT_NAME,
			isd.NAME INSPECTED_DEPARTMENT_NAME,
			isu.NAME INSPECTED_SITEUSER_NAME,
			IFNULL(GROUP_CONCAT(REPLACE(siu.NAME,'/',',')),'') AS INSPECTION_USER_NAME,
			CASE WHEN IFNULL(se.INSPECTION_TYPE_OTHER, '') = '' THEN d.NAME ELSE se.INSPECTION_TYPE_OTHER END INSPECTION_TYPE_NAME,
			se.INSPECTION_TIME_START,
			se.INSPECTION_TIME_END,
			se.INSPECTION_STATUS,
			se.INSPECTION_SUBJECT,
			se.INSPECTED_EXPLAIN,
			se.INSPECTED_EXPLAIN_FILENAME,
			se.INSPECTED_EXPLAIN_FILEPATH,
			se.INSPECTED_SITEUSER_SIGN_IMG,
			se.INSPECTED_SITEUSER_SIGN_TIME,
			se.INSPECTION_ID,
			se.INSPECTED_SITEUSER_ID,
			se.INSPECTED_DEPARTMENT_ID
		from BUS_INSPECTION_SAFETYENVIRONMENTAL se
			LEFT JOIN sys_user iou ON iou.USER_ID = se.INSPECTION_ORIGINATOR_ID
			LEFT JOIN oa_department iod ON iod.DEPARTMENT_ID = iou.DEPARTMENT_ID
			LEFT JOIN oa_department isd ON isd.DEPARTMENT_ID = se.INSPECTED_DEPARTMENT_ID
			LEFT JOIN sys_user isu ON isu.USER_ID = se.INSPECTED_SITEUSER_ID
			LEFT JOIN bus_inspection_safetyenvironmental_inspector si ON si.INSPECTION_ID = se.INSPECTION_ID and si.ISDELETE = '0'
			LEFT JOIN sys_user siu ON siu.USER_ID = si.INSPECTION_USER_ID
			LEFT JOIN sys_dictionaries d ON d.BIANMA = se.INSPECTION_TYPE
			where se.ISDELETE = '0' and se.INSPECTION_STATUS = '2'
		<if test="pd.roleLevel != null and pd.roleLevel != ''"><!-- 权限显示 -->
			<choose>
				<when test='pd.roleLevel == "0"'>
				</when>
				<when test='pd.roleLevel =="1"'>
					and siu.DEPARTMENT_ID in (${pd.supDeparIds}) or se.INSPECTED_DEPARTMENT_ID in (${pd.supDeparIds})
				</when>
				<when test='pd.roleLevel =="2"'>
					and siu.USER_ID in (#{pd.loginUserId}) or se.INSPECTED_SITEUSER_ID in (#{pd.loginUserId})
				</when>
			</choose>
		</if>
		<if test='pd.INSPECTED_SITEUSER_ID != null and pd.INSPECTED_SITEUSER_ID != ""'><!-- 被检查单位现场负责人 -->
			and se.INSPECTED_SITEUSER_ID = #{pd.INSPECTED_SITEUSER_ID}
		</if>
		<if test="pd.INSPECTION_SUBJECT != null and pd.INSPECTION_SUBJECT != ''"><!-- 检查题目 -->
			and se.INSPECTION_SUBJECT = #{pd.INSPECTION_SUBJECT,jdbcType=VARCHAR}
		</if>
		<if test="pd.INSPECTED_DEPARTMENT_NAME != null and pd.INSPECTED_DEPARTMENT_NAME != ''"><!-- 被检查单位 -->
			and isd.NAME LIKE CONCAT(CONCAT('%', #{pd.INSPECTED_DEPARTMENT_NAME}),'%')
		</if>
		<if test="pd.INSPECTION_DEPARTMENT_NAME != null and pd.INSPECTION_DEPARTMENT_NAME != ''"><!-- 检查部门 -->
			and iod.NAME LIKE CONCAT(CONCAT('%', #{pd.INSPECTION_DEPARTMENT_NAME}),'%')
		</if>
		<if test="pd.INSPECTION_ORIGINATOR_NAME != null and pd.INSPECTION_ORIGINATOR_NAME != ''"><!-- 检查发起人 -->
			and iou.NAME LIKE CONCAT(CONCAT('%', #{pd.INSPECTION_ORIGINATOR_NAME}),'%')
		</if>
		<if test="pd.INSPECTION_TYPE != null and pd.INSPECTION_TYPE != ''"><!-- 检查类型 -->
			and se.INSPECTION_TYPE = #{pd.INSPECTION_TYPE}
		</if>
		<if test="pd.INSPECTION_TIME_START != null and pd.INSPECTION_TIME_START != ''"><!-- 检查时间 -->
			and se.INSPECTION_TIME_START &gt;= CONCAT(#{pd.INSPECTION_TIME_START}, ' 00:00')
		</if>
		<if test="pd.INSPECTION_TIME_END != null and pd.INSPECTION_TIME_END != ''"><!-- 检查时间 -->
			and se.INSPECTION_TIME_END &lt;= CONCAT(#{pd.INSPECTION_TIME_END}, ' 23:59')
		</if>
		<if test="pd.INSPECTION_STATUS != null and pd.INSPECTION_STATUS != ''"><!-- 检查状态 -->
			and se.INSPECTION_STATUS = #{pd.INSPECTION_STATUS}
		</if>
		<if test="pd.CORPINFO_ID != null and pd.CORPINFO_ID != ''"><!-- 关键词检索 -->
			and se.CORPINFO_ID = #{pd.CORPINFO_ID}
		</if>
		GROUP BY se.INSPECTION_ID
		ORDER BY se.CREATTIME DESC
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0' and f.CORPINFO_ID = #{pd.CORPINFO_ID}
	</select>

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

	<!-- 列表(根据所选ID获取数据) -->
	<select id="findByIds" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			INSPECTION_EXPLAIN_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
				#{item}
		</foreach>
	</select>

	<!-- 删除（根据检查ID） -->
	<delete id="deleteByInspection" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		ISDELETE = '1',
		OPERATOR = #{OPERATOR},
		OPERATTIME = #{OPERATTIME}
		where
		INSPECTION_ID = #{INSPECTION_ID}
	</delete>

	<!-- 列表(全部) -->
	<select id="findExplainById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>,
		a.INSPECTION_ORIGINATOR_SIGN_IMG,
		a.INSPECTED_EXPLAIN_REFUSE,
		a.INSPECTION_ORIGINATOR_SIGN_TIME
		from
		<include refid="tableName"></include> f
		left join bus_inspection_safetyenvironmental a on a.INSPECTION_ID = f.INSPECTION_ID
		where f.ISDELETE = '0'
		<if test="INSPECTION_ID != '' and INSPECTION_ID != null">
			and f.INSPECTION_ID =#{INSPECTION_ID} and f.INSPECTED_EXPLAIN is not null and  f.INSPECTED_EXPLAIN != ''
		</if>
	</select>

	<!-- 修改 -->
	<update id="killAllMessage" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
		VALID = '0'
		where
		INSPECTION_EXPLAIN_ID != #{INSPECTION_EXPLAIN_ID} and INSPECTION_ID = #{INSPECTION_ID}
	</update>

	<select id="findMessage" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		<if test="INSPECTION_ID != '' and INSPECTION_ID != null">
			and f.INSPECTION_ID =#{INSPECTION_ID} and f.VALID = '1'
		</if>
	</select>

	<select id="findMessageByTime" parameterType="pd" resultType="pd">
		select *
		from bus_inspection_safetyenvironmental_explain a
		where a.INSPECTION_ID = #{INSPECTION_ID}
		  and not exists(select *
						 from bus_inspection_safetyenvironmental_explain b
						 where a.INSPECTION_ID = b.INSPECTION_ID
						   and str_to_date(b.CREATTIME, '%Y-%m-%d %h:%i:%s') > str_to_date(a.CREATTIME, '%Y-%m-%d %h:%i:%s'));
	</select>

</mapper>
