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

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.INSPECTION_ID,
		f.INSPECTION_USER_ID,
		f.INSPECTION_USER_OPINION,
		f.INSPECTION_USER_SIGN_IMG,
		f.INSPECTION_USER_SIGN_TIME,
		f.ISDELETE,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.CORPINFO_ID,
		f.INSPECTION_INSPECTOR_ID
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		INSPECTION_ID,
		INSPECTION_USER_ID,
		INSPECTION_USER_OPINION,
		INSPECTION_USER_SIGN_IMG,
		INSPECTION_USER_SIGN_TIME,
		ISDELETE,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		CORPINFO_ID,
		INSPECTION_INSPECTOR_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{INSPECTION_ID},
		#{INSPECTION_USER_ID},
		#{INSPECTION_USER_OPINION},
		#{INSPECTION_USER_SIGN_IMG},
		#{INSPECTION_USER_SIGN_TIME},
		#{ISDELETE},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{CORPINFO_ID},
		#{INSPECTION_INSPECTOR_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
			INSPECTION_INSPECTOR_ID = #{INSPECTION_INSPECTOR_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			INSPECTION_ID = #{INSPECTION_ID},
			INSPECTION_USER_ID = #{INSPECTION_USER_ID},
			INSPECTION_USER_OPINION = #{INSPECTION_USER_OPINION},
			INSPECTION_USER_SIGN_IMG = #{INSPECTION_USER_SIGN_IMG},
			INSPECTION_USER_SIGN_TIME = #{INSPECTION_USER_SIGN_TIME},
			OPERATOR = #{OPERATOR},
			OPERATTIME = #{OPERATTIME},
			INSPECTION_INSPECTOR_ID = INSPECTION_INSPECTOR_ID
		where
			INSPECTION_INSPECTOR_ID = #{INSPECTION_INSPECTOR_ID}
	</update>

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

	<!-- 通过INSPECTOR_ID获取数据 -->
	<select id="findByInspectionId" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
		f.INSPECTION_ID = #{INSPECTION_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_DEPARTMENT_ID,
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		 LEFT JOIN BUS_INSPECTION_SAFETYENVIRONMENTAL se ON se.INSPECTION_ID = f.INSPECTION_ID
		 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 f.ISDELETE = '0' and se.ISDELETE = '0'
		<if test="pd.supDeparIds != null and pd.supDeparIds != ''"><!-- 权限显示 -->
			<choose>
				<when test="pd.roleLevel == '0'">
				</when>
				<when test="pd.roleLevel == '1'">
					and siu.DEPARTMENT_ID in (${pd.supDeparIds})
				</when>
				<when test="pd.roleLevel == '3'">
--  					siu.USER_ID in ( #{pd.loginUserId} )
				</when>
			</choose>
		</if>
		<if test="pd.INSPECTION_USER_ID != null and pd.INSPECTION_USER_ID != ''"><!-- 检查人 -->
			and f.INSPECTION_USER_ID = #{pd.INSPECTION_USER_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.KEYWORDS != null and pd.KEYWORDS != ''"><!-- 被检查单位 -->
			and (
			isd.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			OR iod.NAME LIKE CONCAT(CONCAT('%', #{pd.KEYWORDS}),'%')
			)
		</if>
		GROUP BY f.INSPECTION_ID
		ORDER BY FIELD (se.INSPECTION_STATUS , '0','1','2','3','4','5','6','7','8','-1','-2') asc, se.INSPECTION_TIME_START DESC , se.INSPECTION_TIME_END DESC
	</select>

	<!-- 列表(全部) -->
	<select id="listAll" parameterType="pd" resultType="pd">
		select
		u.NAME INSPECTION_USER_NAME,
		u.DEPARTMENT_ID INSPECTION_DEPARTMENT_ID,
		D.NAME INSPECTION_DEPARTMENT_NAME,
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		LEFT JOIN sys_user u ON u.USER_ID = f.INSPECTION_USER_ID
		LEFT JOIN oa_department d ON d.DEPARTMENT_ID = u.DEPARTMENT_ID
		where f.ISDELETE = '0'
		<if test="INSPECTION_ID != null and INSPECTION_ID != ''"><!-- 检查ID -->
			and f.INSPECTION_ID = #{INSPECTION_ID}
		</if>
		<if test="ISVERIFY != null and ISVERIFY != ''"><!-- 已核实 -->
			and f.INSPECTION_USER_SIGN_TIME IS NOT NULL
		</if>
		ORDER BY f.CREATTIME ASC
	</select>

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

	<!-- 列表(查询当前检查中其他检查人人未核实的集合) -->
	<select id="listOtherNotVerify" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where f.ISDELETE = '0'
		 and f.INSPECTION_ID = #{INSPECTION_ID} and f.INSPECTION_USER_ID != #{INSPECTION_USER_ID} and f.INSPECTION_USER_SIGN_TIME IS NULL
	</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="confirmCount" parameterType="pd" resultType="pd">
		select
		COUNT(*) confirmCount
		from
		<include refid="tableName"></include> f
		LEFT JOIN BUS_INSPECTION_SAFETYENVIRONMENTAL se ON se.INSPECTION_ID = f.INSPECTION_ID
		where f.ISDELETE = '0'
		and f.INSPECTION_USER_ID = #{INSPECTION_USER_ID} and se.INSPECTION_STATUS in ('0', '1')
		and f.INSPECTION_USER_SIGN_TIME is null
	</select>

	<!-- 一公司app首页安全环保检查统计
	 * 		检查数[COUNT_INSPECTION]，被检查数[COUNT_INSPECTED]，检查隐患数[COUNT_HIDDEN]，检查隐患验收数[COUNT_HIDDEN_ACCEPT]
	 * 		 -->
	<select id="countHome" parameterType="pd" resultType="pd">
		SELECT
			(SELECT COUNT(DISTINCT f.INSPECTION_ID) FROM bus_inspection_safetyenvironmental f
		LEFT JOIN bus_inspection_safetyenvironmental_inspector si ON si.INSPECTION_ID = f.INSPECTION_ID AND si.ISDELETE = '0'
		WHERE f.CORPINFO_ID = #{CORPINFO_ID} AND f.ISDELETE = '0' AND (f.INSPECTION_ORIGINATOR_ID = #{USER_ID} OR si.INSPECTION_USER_ID = #{USER_ID})) AS COUNT_INSPECTION,

			(SELECT COUNT(DISTINCT f.INSPECTION_ID) FROM bus_inspection_safetyenvironmental f
		WHERE f.CORPINFO_ID = #{CORPINFO_ID} AND f.ISDELETE = '0' AND f.INSPECTED_SITEUSER_ID = #{USER_ID}) AS COUNT_INSPECTED,

			(SELECT COUNT(DISTINCT h.HIDDEN_ID) FROM bus_hidden h
			LEFT JOIN bus_inspection_safetyenvironmental f ON f.INSPECTION_ID = h.FOREIGN_ID AND f.CORPINFO_ID = #{CORPINFO_ID} AND f.ISDELETE = '0'
		WHERE h.ISDELETE = '0' AND h.STATE != '100' AND f.INSPECTED_SITEUSER_ID = #{USER_ID}) AS COUNT_HIDDEN,

			(SELECT COUNT(DISTINCT h.HIDDEN_ID) FROM bus_hidden h
			LEFT JOIN bus_inspection_safetyenvironmental f ON f.INSPECTION_ID = h.FOREIGN_ID AND f.CORPINFO_ID = #{CORPINFO_ID} AND f.ISDELETE = '0'
		WHERE h.ISDELETE = '0' AND h.STATE IN ('4','8') and h.FINAL_CHECK = '1' AND f.INSPECTED_SITEUSER_ID = #{USER_ID}) AS COUNT_HIDDEN_ACCEPT
	</select>

</mapper>
