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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.yx.face.model.entity.UserControlEarlyWarningList">
        <id column="id" property="id" />
        <result column="early_warning_name" property="earlyWarningName" />
        <result column="update_time" property="updateTime" />
        <result column="create_time" property="createTime" />
        <result column="admin_id" property="adminId" />
        <result column="early_warning_id_number" property="earlyWarningIdNumber" />
        <result column="control_name" property="controlName" />
        <result column="control_phone" property="controlPhone" />
        <result column="control_unit" property="controlUnit" />
    </resultMap>
    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, early_warning_name, update_time, create_time, admin_id, early_warning_id_number, control_name, control_phone, control_unit
    </sql>
    <select id="getPageList" resultType="com.yx.face.model.vo.UserControlEarlyWarningVO">
        select
            u.id,
            u.early_warning_name,
            u.early_warning_id_number,
            u.control_name,
            u.control_phone,
            u.control_unit,
            u.create_time,
            a.username,
            a.id as adminId
        from user_control_early_warning_list u
        Left join admin a on a.id = u.admin_id
        <where>
            <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 u.admin_id =#{adminVO.adminId}
            </if>


            <if test="query.earlyWarningName != null and query.earlyWarningName !=''">
                AND u.early_warning_name LIKE CONCAT('%',#{query.earlyWarningName},'%')
            </if>
            <if test="query.earlyWarningIdNumber != null and query.earlyWarningIdNumber !=''">
                AND u.early_warning_id_number = #{query.earlyWarningIdNumber}
            </if>
            <if test="query.controlName != null and query.controlName !=''">
                AND u.control_name LIKE CONCAT('%',#{query.controlName},'%')
            </if>
            <if test="query.controlPhone != null and query.controlPhone !=''">
                AND u.control_phone = #{query.controlPhone}
            </if>
            <if test="query.controlUnit != null and query.controlUnit !=''">
                AND u.control_unit LIKE CONCAT('%',#{query.controlUnit},'%')
            </if>

            <if test="query.username != null and query.username !=''">
                AND a.username = #{query.username}
            </if>
        </where>


    </select>
    <select id="getAdminWarningInformation" resultType="com.yx.face.model.vo.AdminAndUserEarlyWarningVO">
        SELECT
               c.control_name as controlName ,
               c.control_phone  as controlPhone ,
               c.control_unit  as controlUnit,
               c.admin_id as adminId
        FROM face_device as a
        LEFT JOIN admin as b on a.admin_id = b.id
        LEFT JOIN user_control_early_warning_list as c on b.id  = c.admin_id
        WHERE a.sn = #{deviceSn}
          and c.early_warning_id_number = #{idNumber}
    </select>
    <select id="getControlUnitList" resultType="com.yx.face.model.vo.UserControlEarlyWarningVO">
        select
        distinct
        u.control_unit
        from user_control_early_warning_list u
        Left join admin a on a.id = u.admin_id
        <where>
            <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 u.admin_id =#{adminVO.adminId}
            </if>
        </where>
    </select>
    <delete id="batchDelete">
        delete from user_control_early_warning_list
        <where>
            <if test="startTime != null and startTime != ''">
                AND  <![CDATA[ create_time >= #{startTime}]]>
            </if>
            <if test="endTime != null and endTime != '' ">
                AND  <![CDATA[ create_time <= #{endTime}]]>
            </if>
            <if test="controlUnitList !=null and controlUnitList.size()> 0">
                and  control_unit in
                <foreach collection="controlUnitList" index="index" item="controlUnit" open="(" close=")" separator=",">
                    #{controlUnit}
                </foreach>
            </if>
        </where>
    </delete>
</mapper>
