<?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.yx.face.dao.FacePassDao">

    <delete id="removeFacePassBySn">
        DELETE
        FROM face_pass
        WHERE device_sn = #{sn}
        <if test="userId != null">
            AND user_id = #{userId}
        </if>
    </delete>

    <select id="getFacePasses" resultType="com.yx.face.model.vo.FacePassVO">
        SELECT fp.id AS facePassId,
        fp.user_id AS userId,
        ui.username AS username,
        ui.phone AS phone,
        ui.avatar AS avatar,
        fp.device_sn AS deviceSn,
        fp.device_id AS deviceId,
        fd.name AS deviceName,
        fp.start_time AS startTime,
        fp.end_time AS endTime,
        fp.type AS type,
        fp.status AS status,
        fp.create_time AS createTime
        FROM face_pass AS fp
        INNER JOIN user_info ui ON fp.user_id = ui.id
        LEFT JOIN face_device fd ON fp.device_id = fd.id
        left join admin a on a.id = fp.admin_id
        <where>
            <if test="adminVo.type == 1">

            </if>
            <if test="adminVo.type == 2">
                and a.province_id = #{adminVo.provinceId}
                and a.type in (3 ,4 ,5)
            </if>
            <if test="adminVo.type == 3">
                and a.province_id = #{adminVo.provinceId}
                and a.type in (4 , 5)
                and a.tag_id = #{adminVo.tagId}
            </if>
            <if test="adminVo.type == 4">
                and a.city_id = #{adminVo.cityId}
                and a.type = 5
                and a.tag_id = #{adminVo.tagId}
            </if>
            <if test="adminVo.type == 5">
                and a.id =#{adminVo.adminId}
            </if>


            <if test="query.username != null and query.username != ''">
                AND ui.username = #{query.username}
            </if>
            <if test="query.phone != null and query.phone != ''">
                AND ui.phone = #{query.phone}
            </if>
            <if test="query.deviceSn != null and query.deviceSn != ''">
                AND fp.device_sn = #{query.deviceSn}
            </if>
        </where>
        ORDER BY fp.id DESC
    </select>

    <select id="getFacePassIdBySn" resultType="java.lang.Long">
        SELECT id AS facePassId
        FROM face_pass
        WHERE device_sn = #{sn}
          AND user_id = #{userId}
          AND admin_id = #{adminId}
        ORDER BY id DESC LIMIT 0,1
    </select>

    <select id="getFacePassByTaskId" resultType="java.util.Map">
        SELECT user_id as userId FROM user_face_records WHERE task_id =#{taskId}
    </select>

    <select id="getRemoveFacePass" resultType="com.yx.face.model.vo.FacePassVO">
        SELECT device_sn AS deviceSn,
               user_id   AS userId
        FROM face_pass
        WHERE <![CDATA[ end_time <= #{now}]]>
        GROUP BY device_sn, user_id
    </select>

    <select id="getFacePassBySn" resultType="java.lang.Long">
        SELECT id
        FROM face_pass
        WHERE device_sn = #{sn}
          AND user_id = #{userId}
          AND <![CDATA[ end_time >= #{now}]]>
        ORDER BY id DESC LIMIT 0,1
    </select>
    <select id="getUserName" resultType="java.util.HashMap">
        SELECT  avatar , username , phone , id_number as idNumber, create_time as createTime FROM user_info WHERE id= #{userId} ORDER BY id DESC LIMIT 0,1
    </select>

</mapper>

