<?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.zehong.system.mapper.TEntranceGuardPersonInfoMapper">
    
    <resultMap type="TEntranceGuardPersonInfo" id="TEntranceGuardPersonInfoResult">
        <result property="entranceGuardPersonId"    column="entrance_guard_person_id"    />
        <result property="personName"    column="person_name"    />
        <result property="personNum"    column="person_num"    />
        <result property="actionType"    column="action_type"    />
        <result property="actionTime"    column="action_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <resultMap id="statisticsPersonByActionTypeResult"   type="HashMap">
        <result property="actionType" column="action_type" jdbcType="VARCHAR" />
        <result property="counts" column="counts" jdbcType="DATE"/>
    </resultMap>

    <sql id="selectTEntranceGuardPersonInfoVo">
        select entrance_guard_person_id, person_name, person_num, action_type, action_time, create_by, create_time, update_by, update_time, is_del, remark from t_entrance_guard_person_info
    </sql>

    <select id="selectTEntranceGuardPersonInfoList" parameterType="TEntranceGuardPersonInfo" resultMap="TEntranceGuardPersonInfoResult">
        <include refid="selectTEntranceGuardPersonInfoVo"/>
        <where>  
            <if test="personName != null  and personName != ''"> and person_name like concat('%', #{personName}, '%')</if>
            <if test="personNum != null  and personNum != ''"> and person_num = #{personNum}</if>
            <if test="actionType != null  and actionType != ''"> and action_type = #{actionType}</if>
            <if test="actionTime != null "> and action_time = #{actionTime}</if>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
        </where>
    </select>
    
    <select id="selectTEntranceGuardPersonInfoById" parameterType="Long" resultMap="TEntranceGuardPersonInfoResult">
        <include refid="selectTEntranceGuardPersonInfoVo"/>
        where entrance_guard_person_id = #{entranceGuardPersonId}
    </select>
        
    <insert id="insertTEntranceGuardPersonInfo" parameterType="TEntranceGuardPersonInfo" useGeneratedKeys="true" keyProperty="entranceGuardPersonId">
        insert into t_entrance_guard_person_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="personName != null">person_name,</if>
            <if test="personNum != null">person_num,</if>
            <if test="actionType != null">action_type,</if>
            <if test="actionTime != null">action_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="personName != null">#{personName},</if>
            <if test="personNum != null">#{personNum},</if>
            <if test="actionType != null">#{actionType},</if>
            <if test="actionTime != null">#{actionTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateTEntranceGuardPersonInfo" parameterType="TEntranceGuardPersonInfo">
        update t_entrance_guard_person_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="personName != null">person_name = #{personName},</if>
            <if test="personNum != null">person_num = #{personNum},</if>
            <if test="actionType != null">action_type = #{actionType},</if>
            <if test="actionTime != null">action_time = #{actionTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where entrance_guard_person_id = #{entranceGuardPersonId}
    </update>

    <delete id="deleteTEntranceGuardPersonInfoById" parameterType="Long">
        delete from t_entrance_guard_person_info where entrance_guard_person_id = #{entranceGuardPersonId}
    </delete>

    <delete id="deleteTEntranceGuardPersonInfoByIds" parameterType="String">
        delete from t_entrance_guard_person_info where entrance_guard_person_id in 
        <foreach item="entranceGuardPersonId" collection="array" open="(" separator="," close=")">
            #{entranceGuardPersonId}
        </foreach>
    </delete>
    
    <select id="statisticsPersonByActionType" resultMap="statisticsPersonByActionTypeResult">
        SELECT
          action_type,
          COUNT(*)AS counts
        FROM
            t_entrance_guard_person_info
        GROUP BY
            action_type
    </select>
    
    <select id="statisticsStayInPersons" resultType="java.lang.String">
        SELECT
            COUNT(*)
        FROM
            (
                (
                    SELECT
                      *
                    FROM
                     (
                       SELECT
                                action_time AS importTime,
                                person_num
                        FROM
                                t_entrance_guard_person_info
                        WHERE
                                action_type = '0'
                        ORDER BY action_time DESC
                     )a
                     GROUP BY a.person_num
                )import,
                (
                   SELECT
                     *
                    FROM
                    (
                      SELECT
                            action_time AS exportTime,
                            person_num
                      FROM
                            t_entrance_guard_person_info
                      WHERE
                            action_type = '1'
                      ORDER BY action_time DESC
                     )b
                    GROUP BY b.person_num
                )export
          )
        WHERE import.person_num = export.person_num AND (export.exportTime - import.importTime) > 0
    </select>

    <select id="lastImportAndExportData"  resultMap="TEntranceGuardPersonInfoResult">
        SELECT
          present.*
        FROM(
            SELECT
                *
            FROM
                t_entrance_guard_person_info
            ORDER BY action_time DESC
        )present
        GROUP BY
            present.action_type
    </select>
</mapper>