<?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.statistics.ListStatisticsMapper">

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

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

	<!-- 字段 -->
	<sql id="Field">
		f.STARTTIME,
		f.ENDTIME,
		f.NOTEXAMINEDNUM,
		f.TYPE,
		f.PERIOD,
		f.CORPINFO_ID,
		f.LISTSTATISTICS_ID,
		f.CREATOR,
		f.CREATTIME,
		f.OPERATOR,
		f.OPERATTIME,
		f.USER_ID

	</sql>

	<!-- 字段用于新增 -->
	<sql id="Field2">
		STARTTIME,
		ENDTIME,
		NOTEXAMINEDNUM,
		TYPE,
		PERIOD,
		CORPINFO_ID,
		LISTSTATISTICS_ID,
		CREATOR,
		CREATTIME,
		OPERATOR,
		OPERATTIME,
		USER_ID
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{STARTTIME},
		#{ENDTIME},
		#{NOTEXAMINEDNUM},
		#{TYPE},
		#{PERIOD},
		#{CORPINFO_ID},
		#{LISTSTATISTICS_ID},
		#{CREATOR},
		#{CREATTIME},
		#{OPERATOR},
		#{OPERATTIME},
		#{USER_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'
		where
			LISTSTATISTICS_ID = #{LISTSTATISTICS_ID}
	</delete>

	<!-- 物理删除-->
	<delete id="deletewl" parameterType="pd">
		delete from
		<include refid="tableName"></include>
		where
			LISTSTATISTICS_ID = #{LISTSTATISTICS_ID}
	</delete>
	<!-- 修改 -->
	<update id="edit" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			NOTEXAMINEDNUM = #{NOTEXAMINEDNUM},
			OPERATTIME = #{OPERATTIME},
			LISTSTATISTICS_ID = LISTSTATISTICS_ID
		where
			LISTSTATISTICS_ID = #{LISTSTATISTICS_ID}
	</update>

	<!-- 通过ID获取数据 -->
	<select id="findById" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			f.LISTSTATISTICS_ID = #{LISTSTATISTICS_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
			LISTSTATISTICS_ID in
		<foreach item="item" index="index" collection="ArrayDATA_IDS" open="(" separator="," close=")">
                 #{item}
		</foreach>
	</delete>

	<!-- 通过ID获取数据 -->
	<select id="findByCondition" parameterType="pd" resultType="pd">
		select
		<include refid="Field"></include>
		from
		<include refid="tableName"></include> f
		where
			f.STARTTIME = #{STARTTIME}
			and
			f.ENDTIME = #{ENDTIME}
			and
			f.TYPE = #{TYPE}
			and
			f.USER_ID = #{USER_ID}
			<if test="PERIOD != null and PERIOD != ''">
				and
				f.PERIOD = #{PERIOD}
			</if>
			and
			f.CORPINFO_ID = #{CORPINFO_ID}
	</select>

	<!-- 获取统计分母 -->
	<select id="getCountList" parameterType="pd" resultType="pd">
		select
		    f.LIST_TYPE,
		<include refid="Field"></include>
		from
			vi_list_statistics_all f
		where
		f.CORPINFO_ID = #{CORPINFO_ID} and f.TYPE != 'listType0006'
		and
		(
			f.STARTTIME BETWEEN #{STARTTIME} and #{ENDTIME}
			or
			f.ENDTIME  BETWEEN #{STARTTIME} and #{ENDTIME}
			or
			(f.STARTTIME &lt;= #{STARTTIME} and f.ENDTIME &gt;= #{ENDTIME})
		)

	</select>

	<!-- 获取统计分母 -->
	<select id="getTimeAstrict" parameterType="pd" resultType="pd">
	select
	max(DATE_FORMAT(ENDTIME, '%Y-%m-%d' )) MAXTIME,min(DATE_FORMAT(STARTTIME, '%Y-%m-%d' )) MINTIME
	from
	bus_list_statistics
	where
	CORPINFO_ID = #{CORPINFO_ID}

	</select>

	<!--人员应处理清单数 -->
	<select id="countStaByUser" parameterType="pd" resultType="pd">
		SELECT
			sum(f.NOTEXAMINEDNUM) NUM,
			f.USER_ID
		FROM
			vi_list_statistics_all f
		WHERE
			f.CORPINFO_ID = #{CORPINFO_ID}
			and f.USER_ID is not null
			AND f.USER_ID != ''
			and
			(
				f.STARTTIME BETWEEN CONCAT(#{STARTTIME},' 00:00:00') and CONCAT(#{ENDTIME},' 23:59:59')
			   	or
				f.ENDTIME  BETWEEN CONCAT(#{STARTTIME},' 00:00:00') and CONCAT(#{ENDTIME},' 23:59:59')
			   	or
				(f.STARTTIME &lt;= CONCAT(#{STARTTIME},' 00:00:00') and f.ENDTIME &gt;= CONCAT(#{ENDTIME},' 23:59:59'))
			)
		GROUP BY
			f.USER_ID
	</select>
	<!--人员应处理清单数 -->
	<select id="countSta" parameterType="pd" resultType="pd">
		SELECT
			sum(f.NOTEXAMINEDNUM) NUM,
			f.USER_ID
		FROM
			bus_list_statistics f

		LEFT JOIN SYS_USER u ON u.USER_ID = f.USER_ID
		WHERE
			f.CORPINFO_ID = #{CORPINFO_ID}
			and f.USER_ID is not null
			AND f.USER_ID != ''
			<if test="STARTTIME != null and STARTTIME != '' and ENDTIME != null and ENDTIME != ''"><!-- 关键词检索 -->
				and CONCAT(#{STARTTIME}," 00:00:00") &lt;= f.STARTTIME
				and CONCAT(#{ENDTIME}," 23:59:59") &gt;= f.ENDTIME
			</if>
		GROUP BY
			f.USER_ID
	</select>

	<select id="checkSituation" parameterType="pd" resultType="pd">
		SELECT a.* FROM (
			SELECT
				u.USER_ID,
				d.NAME DEPARTMENTNAME,
				getFullName(d.DEPARTMENT_ID) DEPARTMENTNAME_ALL,
				d.DEPARTMENT_ID,
				p.NAME POSTNAME,
				u.NAME USERNAME,
				(
					SELECT
						COUNT( distinct bcr.LISTMANAGER_ID )
					FROM
						vi_list_checkrecord_all bcr
						LEFT JOIN vi_list_checkuser_all bcu ON bcu.CHECKRECORD_ID = bcr.CHECKRECORD_ID and bcu.LIST_TYPE = bcr.LIST_TYPE
					WHERE
						bcu.USER_ID = u.USER_ID
						AND bcr.FINISHED = '1'
						AND bcr.ISSTATISTICS = '1'
						AND bcr.TYPE = '1'
						AND bcr.ISDELETE = '0'

					<if test="STARTTIME != null and STARTTIME != '' and ENDTIME != null and ENDTIME != ''"><!-- 关键词检索 -->
						and
							DATE_FORMAT(bcr.CHECK_TIME,"%Y-%m-%d %H:%i:%S")
							BETWEEN CONCAT(#{STARTTIME}," 00:00:00") and CONCAT(#{ENDTIME}," 23:59:59")
					</if>
				) INSPECTEDNUM,
			<!-- (
				SELECT
					COUNT( 1 )
				FROM
					SYS_FHLOG l
				WHERE
					l.USER_ID = u.USER_ID
					AND TO_DAYS( l.CZTIME ) = TO_DAYS(#{STARTTIME})
			) ISLOGIN -->
			IFNULL(fl.count,0) ISLOGIN
			FROM
				SYS_USER u
				LEFT JOIN OA_DEPARTMENT d ON d.DEPARTMENT_ID = u.DEPARTMENT_ID
				LEFT JOIN SYS_POST p ON p.POST_ID = u.POST_ID
				LEFT JOIN (SELECT COUNT( 1 ) count,l.USER_ID FROM SYS_FHLOG l WHERE TO_DAYS( l.CZTIME ) = TO_DAYS(now()) GROUP BY l.USER_ID) fl ON fl.USER_ID = u.USER_ID
			WHERE
				u.CORPINFO_ID = #{CORPINFO_ID}
				AND u.USER_ID IS NOT NULL
				AND u.ISDELETE = '0'
			GROUP BY
				u.USER_ID
				) a
		WHERE 1 = 1
		<if test="KEYWORDS != null and KEYWORDS != ''"><!-- 关键词检索 -->
			and a.USERNAME LIKE CONCAT(CONCAT('%', #{KEYWORDS}),'%')
		</if>
		<if test="DEPT_IDS != null and DEPT_IDS != ''">
			and
			a.DEPARTMENT_ID in
			<foreach item="item" index="index"
					 collection="DEPT_IDS" open="(" separator="," close=")">
				'${item}'
			</foreach>
		</if>
		<choose>
			<when test='ISMAIN != null and ISMAIN != "" and ISMAIN == "1"'>
				<if test="DEPT_IDS != null and DEPT_IDS != ''">
	                and
					a.DEPARTMENT_ID in
					<foreach item="item" index="index"
						collection="DEPT_IDS" open="(" separator="," close=")">
						'${item}'
					</foreach>
	            </if>
			</when>
			<otherwise>
				<choose>
		            <when test='ISSUPERVISE != null and ISSUPERVISE != "" and ISSUPERVISE == "1"'>
						<choose>
							<when test='ISLEADER != null and ISLEADER != "" and ISLEADER == "1"'>
								<choose>
									<when test="DEPT_IDS != null and DEPT_IDS != ''">
										and
										a.DEPARTMENT_ID in
										<foreach item="item" index="index"
												 collection="DEPT_IDS" open="(" separator="," close=")">
											'${item}'
										</foreach>
									</when>
									<otherwise>
										and
										((a.DEPARTMENT_ID in (
										select
										SUB_DEPARTMENT_ID
										from
										oa_supervision_department osd
										where
										osd.SUP_DEPARTMENT_ID = #{DEPARTMENT_ID}
										and osd.ISDELETE = '0'
										and osd.CORPINFO_ID  = #{CORPINFO_ID}
										)) or (
										a.DEPARTMENT_ID in
										<foreach item="item" index="index"
												 collection="DEPARTMENT_IDS" open="(" separator="," close=")">
											'${item}'
										</foreach>
										))
									</otherwise>
								</choose>
							</when>
							<when test="DEPT_IDS != null and DEPT_IDS != ''">
								or a.USER_ID = #{USER_ID}
								and
								a.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="DEPT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</when>
							<otherwise>

								and
								a.DEPARTMENT_ID in (
								select
								SUB_DEPARTMENT_ID
								from
								oa_supervision_department osd
								where
								osd.SUP_DEPARTMENT_ID = #{DEPARTMENT_ID}
								and osd.ISDELETE = '0'
								and osd.CORPINFO_ID  = #{CORPINFO_ID})

							</otherwise>
						</choose>
					</when>

					<when test='ISLEADER != null and ISLEADER != "" and ISLEADER == "1"'>
						<choose>
							<when test="DEPT_IDS != null and DEPT_IDS != ''">
								and
								a.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="DEPT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</when>
							<otherwise>
								and
								a.DEPARTMENT_ID in
								<foreach item="item" index="index"
										 collection="DEPARTMENT_IDS" open="(" separator="," close=")">
									'${item}'
								</foreach>
							</otherwise>
						</choose>
					</when>
		            <when test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">
						and a.USER_ID = #{USER_ID}
		                and a.DEPARTMENT_ID=#{DEPARTMENT_ID}
		            </when>
		            <otherwise>
		            </otherwise>
		        </choose>
			</otherwise>
		</choose>
	<!-- 	<if test="DEPARTMENT_ID != null and DEPARTMENT_ID != ''">关键词检索
			and a.DEPARTMENT_ID = #{DEPARTMENT_ID}
		</if> -->
		<if test="STATE != null and STATE != ''"><!-- 关键词检索 -->
			<choose>
				<when test='STATE=="1"'>
					and a.ISLOGIN &gt; 0
				</when>
				<when test='STATE=="2"'>
					and a.ISLOGIN = 0
				</when>
			</choose>
		</if>
		order by convert(a.DEPARTMENTNAME using gbk) asc;
	</select>
	<!--人员应处理清单个数（节假日清单数算1） -->
	<select id="countListStaByUser" parameterType="pd" resultType="pd">
		SELECT
			CAST( sum(  f.NOTEXAMINEDNUM ) AS SIGNED ) NUM,
			f.USER_ID
		FROM
			vi_list_statistics_all f

		LEFT JOIN SYS_USER u ON u.USER_ID = f.USER_ID
		WHERE
			f.CORPINFO_ID = #{CORPINFO_ID}
			and f.USER_ID is not null
			AND f.USER_ID != ''
			and
			(
				f.STARTTIME BETWEEN CONCAT(#{STARTTIME},' 00:00:00') and CONCAT(#{ENDTIME},' 23:59:59')
				or
				f.ENDTIME  BETWEEN CONCAT(#{STARTTIME},' 00:00:00') and CONCAT(#{ENDTIME},' 23:59:59')
				or
				(f.STARTTIME &lt;= CONCAT(#{STARTTIME},' 00:00:00') and f.ENDTIME &gt;= CONCAT(#{ENDTIME},' 23:59:59'))
			)
		GROUP BY
			f.USER_ID
	</select>
	<!--人员应处理清单数（各周期） -->
	<select id="countByPeriodUser" parameterType="pd" resultType="pd">
		SELECT
			l.USER_ID,
			IFNULL( SUM( CASE l.PERIOD WHEN 'checkPeriod0001' THEN l.NOTEXAMINEDNUM END ), 0 ) ALLPERIOD1,
			IFNULL( SUM( CASE l.PERIOD WHEN 'checkPeriod0002' THEN l.NOTEXAMINEDNUM END ), 0 ) ALLPERIOD2,
			IFNULL( SUM( CASE l.PERIOD WHEN 'checkPeriod0003' THEN l.NOTEXAMINEDNUM END ), 0 ) ALLPERIOD3,
			IFNULL( SUM( CASE l.PERIOD WHEN 'checkPeriod0004' THEN l.NOTEXAMINEDNUM END ), 0 ) ALLPERIOD4,
			IFNULL( SUM( CASE l.PERIOD WHEN 'checkPeriod0005' THEN l.NOTEXAMINEDNUM END ), 0 ) ALLPERIOD5,
			IFNULL( SUM( CASE l.PERIOD WHEN 'checkPeriod0006' THEN l.NOTEXAMINEDNUM END ), 0 ) ALLPERIOD6,
			IFNULL( SUM( CASE l.PERIOD WHEN 'checkPeriod0007' THEN l.NOTEXAMINEDNUM END ), 0 ) ALLPERIOD7
		FROM
			BUS_LIST_STATISTICS l
		WHERE
			l.CORPINFO_ID = #{CORPINFO_ID}
			AND l.USER_ID IS NOT NULL
			AND l.TYPE != 'listType0005'
			AND DATE_FORMAT(#{TIME},"%Y-%m-%d %H:%i:%S") BETWEEN l.STARTTIME AND l.ENDTIME
		GROUP BY
			l.USER_ID
	</select>

	<select id="listAllForPlan" parameterType="pd" resultType="pd">
		select f.*,d.LEVEL level_bianma
		from BUS_LIST_STATISTICS f
		left join SYS_USER u on u.USER_ID = f.USER_ID
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID = u.DEPARTMENT_ID
		where 1=1
		<if test="YEAR != null and YEAR != ''"><!-- 关键词检索 -->
			and f.STARTTIME  LIKE CONCAT(CONCAT('%', #{YEAR}),'%')
		</if>
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			and f.CORPINFO_ID = #{CORPINFO_ID}
		</if>
	</select>

	<select id="mergeListAllForPlan" parameterType="pd" resultType="pd">
		select * from (select f.*,d.LEVEL level_bianma
		from BUS_LIST_STATISTICS f
		left join SYS_USER u on u.USER_ID = f.USER_ID
		left join OA_DEPARTMENT d on d.DEPARTMENT_ID = u.DEPARTMENT_ID
		where 1=1
		<if test="YEAR != null and YEAR != ''"><!-- 关键词检索 -->
			and f.STARTTIME LIKE CONCAT(CONCAT('%', #{YEAR}),'%')
		</if>
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			and f.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		union
		SELECT
		f.*,
		d.LEVEL level_bianma
		FROM
		bus_hiddendangercheckstandard_statistics f
		LEFT JOIN SYS_USER u ON u.USER_ID = f.USER_ID
		LEFT JOIN OA_DEPARTMENT d ON d.DEPARTMENT_ID = u.DEPARTMENT_ID
		WHERE
		1 = 1
		<if test="YEAR != null and YEAR != ''"><!-- 关键词检索 -->
			and f.STARTTIME LIKE CONCAT(CONCAT('%', #{YEAR}),'%')
		</if>
		<if test="CORPINFO_ID != null and CORPINFO_ID != ''"><!-- 关键词检索 -->
			and f.CORPINFO_ID = #{CORPINFO_ID}
		</if>
		) z
	</select>
</mapper>
