<?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.dsno1.check.CheckUserMapper">

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

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

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

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

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CHECKRECORD_ID},
		#{DEPARTMENT_ID},
		#{USER_ID},
		#{CHECKUSER_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">
		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 cr.TYPE = '1'
			AND c.user_id IS NOT NULL
			AND c.user_id != ''
			AND cr.CORPINFO_ID = #{CORPINFO_ID}
			and
				cr.CHECK_TIME BETWEEN #{STARTTIME} and #{ENDTIME}
		GROUP BY
			c.user_id
	</select>

	<!-- 查看检查清单检查人 -->
	<select id="getCheckUser" parameterType="pd" resultType="pd">
		SELECT
			GROUP_CONCAT(DISTINCT U.NAME) USERS
		FROM
			BUS_CHECKUSER 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>

	<!-- 清单检查人(公司试用结束时清除检查人) -->
	<delete id="deleteByCorp" parameterType="pd">
		DELETE FROM
			BUS_CHECKUSER
		WHERE
			CHECKRECORD_ID IN (
				SELECT
					CHECKRECORD_ID
				FROM
					BUS_CHECKRECORD
				WHERE
					CORPINFO_ID = #{CORPINFO_ID}
					AND ISDELETE = '0'
			)
	</delete>
</mapper>
