<?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.bus.CardUserMapper">

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

	<!-- 字段用于新增 -->
	<sql id="Field">
		CARDID,
		CARDNUMBER,
		USERID,
		USERNAME,
		USERNUMBER,
		USERDEPARTMENTNAME,
		CARDTYPE,
		EQUIPMENTNAME,
		EQUIPMENTID,
		TIME,
		STATE
	</sql>

	<!-- 字段值 -->
	<sql id="FieldValue">
		#{CARDID},
		#{CARDNUMBER},
		#{USERID},
		#{USERNAME},
		#{USERNUMBER},
		#{USERDEPARTMENTNAME},
		#{CARDTYPE},
		#{EQUIPMENTNAME},
		#{EQUIPMENTID},
		#{TIME},
		#{STATE}
	</sql>

	<!-- 新增-->
	<insert id="save" parameterType="pd">
		insert into
	<include refid="tableName"></include>
		(
	<include refid="Field"></include>
		) values (
	<include refid="FieldValue"></include>
		)
	</insert>

	<!-- 删除-->
	<delete id="delete" parameterType="pd">
		update
		<include refid="tableName"></include>
		set
			ISDELETE = '1'
		where
		HIDDENREGION_ID = #{HIDDENREGION_ID}
	</delete>

	<select id="datalistPage" parameterType="page" resultType="pd">
		select
			<include refid="Field"></include>
		from
			<include refid="tableName"></include> f
		where 1 = 1
		<if test="pd.USERNAME != null and pd.USERNAME != ''"><!-- 关键词检索 -->
			and f.USERNAME LIKE CONCAT(CONCAT('%', #{pd.USERNAME}),'%')
		</if>
		<if test="pd.STARTTIME != null and pd.STARTTIME != ''"><!-- 关键词检索 -->
			and f.TIME <![CDATA[ >= ]]>  CONCAT_WS(' ',#{pd.STARTTIME}, '00:00')
		</if>
		<if test="pd.ENDTIME != null and pd.ENDTIME != ''"><!-- 关键词检索 -->
			and f.TIME <![CDATA[ <= ]]>  CONCAT_WS(' ',#{pd.ENDTIME}, '23:59')
		</if>
		<if test="pd.STATE != null and pd.STATE != ''"><!-- 关键词检索 -->
			and f.STATE = #{pd.STATE}
		</if>
		<!--where
			PARENT_ID = '0'
		and
			ISDELETE = '0'
		&lt;!&ndash;<if test="pd.HIDDENREGION != null and pd.HIDDENREGION != ''">&lt;!&ndash; 关键词检索 &ndash;&gt;
			AND
			(
			HIDDENREGION LIKE CONCAT(CONCAT('%', #{pd.HIDDENREGION}),'%')
			)
		</if>
		<if test="pd.COMMENTS != null and pd.COMMENTS != ''">&lt;!&ndash; 关键词检索 &ndash;&gt;
			AND
			(
			COMMENTS LIKE CONCAT(CONCAT('%', #{pd.COMMENTS}),'%')
			)
		</if>&ndash;&gt;-->
		ORDER BY f.`TIME` DESC
	</select>

	<select id="statisticsGroupDay" parameterType="pd" resultType="pd">
		SELECT f.CREATDAY, COUNT(f.USERID) COUNT FROM
			(SELECT f.USERID,SUBSTR(f.`TIME`, 1, 10) CREATDAY
				FROM bus_carduser f
					WHERE f.`TIME` >= CONCAT_WS("", date_format(now(),'%Y-%m'), '-01 00:00:00')
						<if test="STATENAME != null and STATENAME != ''"><!-- 确认人 -->
							<choose>
								<when test="STATENAME == '进入人员'">
									AND f.STATE = 0
								</when>
								<when test="STATENAME == '外出人员'">
									AND f.STATE = 1
								</when>
							</choose>
						</if>
			) f GROUP BY f.CREATDAY
	</select>

	<!-- 统计今日人员刷卡数据（今日进入[COUNTUSERIN]，今日离场[COUNTUSEROUT]，现场人数[COUNTUSER]，入场刷卡数[COUNTCARDIN]，出场刷卡数[COUNTCARDOUT]） -->
	<select id="statisticsCardUserCount" parameterType="pd" resultType="pd">
		SELECT f.*,(COUNTUSERIN-COUNTUSEROUT) COUNTUSER
			FROM
			(SELECT
				 COUNT(DISTINCT (case when f.STATE = 0 then f.USERID END)) COUNTUSERIN,
				 COUNT(DISTINCT (case when f.STATE = 1 then f.USERID END)) COUNTUSEROUT,
				 COUNT(case when f.STATE = 0 then f.CARDID END) COUNTCARDIN,
				 COUNT(case when f.STATE = 1 then f.CARDID END) COUNTCARDOUT
				FROM bus_carduser f
				 WHERE f.`TIME` >= CONCAT_WS(" ", DATE_FORMAT(CURDATE(),'%Y-%m-%d'), '00:00:00')
			) f
	</select>

	<select id="findById" parameterType="pd" resultType="pd">
		select
			<include refid="Field"></include>
		from
			<include refid="tableName"></include> f
		where f.CARDID = #{CARDID}
	</select>
</mapper>
