<?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.FaceDeviceDao">


    <select id="getIdBySn" resultType="java.lang.Integer">
        SELECT id
        FROM face_device
        WHERE sn = #{sn} LIMIT 0,1
    </select>

    <select id="getFaceDevices" resultType="com.yx.face.model.vo.FaceDeviceVO">
        SELECT
        fd.id AS deviceId,
        fd.sn AS sn,
        fd.name AS name,
        fd.user_count AS userCount,
        fd.pass_count AS passCount,
        fd.app_version AS appVersion,
        fd.enable AS enable,
        fd.online AS online,
        fd.remark AS remark,
        fd.auth AS auth,
        fd.pass AS pass,
        fd.inter_ip AS interIp,
        fd.create_time AS createTime,
        a.province_name as provinceName,
        a.city_name as cityName,
        a.area_name as areaName,
        a.id as adminId,
        a.username as username
        FROM face_device fd
        left join admin a on a.id = fd.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="fde.username != null and fde.username !=''">
                and a.username = #{fde.username}
            </if>
            <if test="fde.sn != null and fde.sn !=''">
                and fd.sn = #{fde.sn}
            </if>
            <if test="fde.name != null and fde.name !=''">
                and fd.name LIKE CONCAT('%',#{fde.name},'%')
            </if>
            <if test="fde.provinceId != null">
                and a.province_id =#{fde.provinceId}
            </if>
            <if test="fde.cityId != null">
                and a.city_id =#{fde.cityId}
            </if>
            <if test="fde.areaId != null">
                and a.area_id =#{fde.areaId}
            </if>

            <if test="fde.pass != null">
                and fd.type =#{fde.pass}
            </if>
            <if test="fde.online != null">
                and fd.online =#{fde.online}
            </if>
            <if test="fde.enable != null">
                and fd.enable =#{fde.enable}
            </if>
        </where>
        ORDER BY fd.id DESC
    </select>

    <select id="getSnList" resultType="com.yx.face.model.entity.FaceDevice">
        SELECT id,
        sn
        FROM face_device
        WHERE enable = 1
        AND auth = 1
        <if test="adminId != null">
            AND admin_id = #{adminId}
        </if>
    </select>

    <select id="getFaceDeviceBySn" resultType="com.yx.face.model.entity.FaceDevice">
        SELECT *
        FROM face_device
        WHERE sn = #{sn} LIMIT 0,1
    </select>

    <select id="getAdminIdBySn" resultType="java.lang.Integer">
        SELECT admin_id
        FROM face_device
        WHERE sn = #{sn} LIMIT 0,1
    </select>
    <select id="getFaceDeviceList" resultType="com.yx.face.model.vo.FaceDeviceVO">
        select
        fd.sn AS sn,
        fd.name AS name,
        a.id AS adminId
        FROM face_device fd
        left join admin a on a.id = fd.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>
        </where>
    </select>
    <select id="selectList" resultType="java.lang.String">
        SELECT sn
        FROM face_device
        WHERE enable = 1
          AND online = 1
          AND auth = 1
          AND admin_id = #{adminId}
    </select>
    <select id="getFaceDeviceOne" resultType="java.lang.String">
        SELECT sn
        FROM face_device
        WHERE enable = 1
          AND online = 1
          AND auth = 1
          AND sn = #{sn}
    </select>
    <select id="selectListNo" resultType="java.lang.String">
        SELECT sn
        FROM face_device
        WHERE (enable = 0 or online = 0 or auth = 0)
          AND admin_id = #{adminId}
    </select>
    <select id="selectAdminId" resultType="java.lang.Integer">
        SELECT admin_id
        FROM face_device
        WHERE (enable = 1 or online = 1 or auth = 1)
          AND sn = #{sn}
    </select>
    <select id="selectSnList" resultType="com.yx.face.model.entity.FaceDevice">
        SELECT
        fd.id AS id,
        fd.sn AS sn,
        fd.enable AS enable,
        fd.online AS online,
        fd.auth AS auth
        FROM face_device fd
        left join admin a on a.id = fd.admin_id
        <where>
            <if test="type == 1">
                and a.province_id = #{provinceId}
                and a.type in (3 ,4 ,5)
            </if>
            <if test="type == 2">
                and a.id =#{adminId}
            </if>
            <if test="type == 3">
                <if test="sn != null and sn !=''">
                    and fd.sn = #{sn}
                </if>
            </if>
        </where>
    </select>
</mapper>

