<?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.hiddenDangerCheckStandard.CustomCheckUserMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.CHECKRECORD_ID,
		f.DEPARTMENT_ID,
		f.USER_ID,
		f.CHECKUSER_ID,
		f.DEPARTMENT_NAME,
		f.USER_NAME
	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		CHECKRECORD_ID,
		DEPARTMENT_ID,
		USER_ID,
		CHECKUSER_ID,
		DEPARTMENT_NAME,
		USER_NAME
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CHECKRECORD_ID},
		#{DEPARTMENT_ID},
		#{USER_ID},
		#{CHECKUSER_ID},
		#{DEPARTMENT_NAME},
		#{USER_NAME}
	</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">
		delete from
		<include refid="tableName"></include>
		where
			CHECKRECORD_ID = #{CHECKRECORD_ID}
	</delete>

	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			CHECKRECORD_ID = #{CHECKRECORD_ID},
			DEPARTMENT_ID = #{DEPARTMENT_ID},
			USER_ID = #{USER_ID},
			CHECKUSER_ID = CHECKUSER_ID
		where
			CHECKUSER_ID = #{CHECKUSER_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			f.CHECKUSER_ID = #{CHECKUSER_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>
		from
		<include refid="tableName"></include> f
	</select>

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


	<!-- 查询人员检查数 -->
	<select id="countCheByUser" parameterType="pd" resultType="pd">
		SELECT
			c.USER_ID,
			count( c.CHECKUSER_ID ) NUM
		FROM
			bus_checkuser c
			LEFT JOIN bus_checkrecord cr ON c.CHECKRECORD_ID = cr.CHECKRECORD_ID
		WHERE
			cr.isdelete = '0'
			AND cr.FINISHED = '1'
			AND cr.ISSTATISTICS = '1'
			AND c.user_id IS NOT NULL
			AND c.user_id != ''
			AND cr.CORPINFO_ID = #{CORPINFO_ID}
			and
			cr.CHECK_TIME BETWEEN CONCAT(#{STARTTIME},' 00:00:00') and CONCAT(#{ENDTIME},' 23:59:59')
		GROUP BY
			c.user_id
	</select>

	<!-- 查看检查清单检查人 -->
	<select id="getCheckUser" parameterType="pd" resultType="pd">
		SELECT
			GROUP_CONCAT(DISTINCT IFNULL(f.USER_NAME, u.NAME)) USERS
		FROM
			<include refid="tableName"></include> f
			LEFT JOIN SYS_USER u ON u.USER_ID = f.USER_ID
			WHERE u.NAME IS NOT NULL
			AND u.NAME != ''
			AND f.CHECKRECORD_ID = #{CHECKRECORD_ID}
		GROUP BY
			f.CHECKRECORD_ID
	</select>

	<!-- 绩效得分 -->
	<select id="getDeptExamine" parameterType="pd" resultType="pd" >
		SELECT
			d.DEPARTMENT_ID,
			ifnull(sta.NUM,0) as sta_count,
			ifnull(che.NUM,0)  as check_count
		FROM
			OA_DEPARTMENT d
		left JOIN (
			SELECT
				sum(f.NOTEXAMINEDNUM) NUM,
				u.DEPARTMENT_ID
			FROM
				bus_list_statistics f
			LEFT JOIN SYS_USER u ON u.USER_ID = f.USER_ID
			WHERE
				(
					f.STARTTIME BETWEEN #{STARTTIME} and #{ENDTIME}
					or
					f.ENDTIME  BETWEEN #{STARTTIME} and #{ENDTIME}
					or
					(f.STARTTIME &lt;= #{STARTTIME} and f.ENDTIME &gt;= #{ENDTIME})
				)
			group by u.DEPARTMENT_ID
		) sta on sta.DEPARTMENT_ID=d.DEPARTMENT_ID

		LEFT JOIN (
			SELECT
				count( distinct cr.LISTMANAGER_ID ) NUM,
				c.DEPARTMENT_ID
			FROM
				bus_checkuser c
				LEFT JOIN bus_checkrecord cr ON c.CHECKRECORD_ID = cr.CHECKRECORD_ID
			WHERE
				cr.isdelete = '0'
				AND cr.FINISHED = '1'
				AND cr.ISSTATISTICS = '1'
				AND cr.CHECK_TIME BETWEEN #{STARTTIME} and #{ENDTIME}
				group by c.DEPARTMENT_ID
		) che on che.DEPARTMENT_ID = d.DEPARTMENT_ID
		where
			d.ISDELETE = 0
	</select>

	<select id="getUserExamine" parameterType="pd" resultType="pd" >
		SELECT
			u.USER_ID,
			ifnull(sta.NUM,0) as sta_count,
			ifnull(che.NUM,0)  as check_count
		FROM
			SYS_USER u
		left JOIN (
			SELECT
				sum(f.NOTEXAMINEDNUM) NUM,
				f.USER_ID
			FROM
				bus_list_statistics f
			WHERE
				(
					f.STARTTIME BETWEEN #{STARTTIME} and #{ENDTIME}
					or
					f.ENDTIME  BETWEEN #{STARTTIME} and #{ENDTIME}
					or
					(f.STARTTIME &lt;= #{STARTTIME} and f.ENDTIME &gt;= #{ENDTIME})
				)
			group by f.USER_ID
		) sta on sta.USER_ID=u.USER_ID

		LEFT JOIN (
			SELECT
				count( distinct cr.LISTMANAGER_ID ) NUM,
				c.USER_ID
			FROM
				bus_checkuser c
				LEFT JOIN bus_checkrecord cr ON c.CHECKRECORD_ID = cr.CHECKRECORD_ID
			WHERE
				cr.isdelete = '0'
				AND cr.FINISHED = '1'
				AND cr.ISSTATISTICS = '1'
				AND cr.CHECK_TIME BETWEEN #{STARTTIME} and #{ENDTIME}
				group by c.USER_ID
		) che on che.USER_ID = u.USER_ID
		where
			u.ISDELETE = 0
	</select>
</mapper>
