<?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="org.springblade.face.mapper.LoginRecordMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="org.springblade.face.entity.LoginRecord">
        <id column="id" property="id" />
        <result column="login_ip" property="loginIp" />
        <result column="login_address" property="loginAddress" />
        <result column="login_time" property="loginTime" />
        <result column="login_user" property="loginUser" />
        <result column="user_id" property="userId" />
        <result column="create_time" property="createTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, login_ip, login_address, login_time, login_user, user_id, create_time
    </sql>


    <select id="getLoginRecordNum" resultType="org.springblade.face.vo.UserLoginRecordVO">
        SELECT
            id as userId,
            account as account,
            `name` as loginUser,
            login_status as loginStatus
        FROM
            blade_user
        where is_deleted = 0
        <if test="query.userNmae != null and query.userNmae != ''">
            and find_in_set (name,#{query.userNmae,jdbcType=VARCHAR})
        </if>
        <if test="query.loginStatus != null ">
            and login_status = #{query.loginStatus}
        </if>
    </select>

    <select id="getLoginRecordPage" resultType="org.springblade.face.vo.UserLoginRecordVO">
        SELECT
        id as userId,
        account as account,
        `name` as loginUser,
        login_status as loginStatus
        FROM
        blade_user
        where is_deleted = 0
        <if test="query.userNmae != null and query.userNmae != ''">
            and find_in_set (name,#{query.userNmae,jdbcType=VARCHAR})
        </if>
        <if test="query.loginStatus != null ">
            and login_status = #{query.loginStatus}
        </if>
    </select>

    <select id="getLoginRecordDetailByMonth" resultType="org.springblade.face.vo.LoginRecordExportVO">
        SELECT
            bu.real_name,
            lr.login_time,
            lr.login_ip,
            lr.login_address,
        CASE
                WHEN lr.login_type = 1 THEN
                '账号密码登录'
                WHEN lr.login_type = 2 THEN
                '微信登录'
                WHEN lr.login_type = 3 THEN
                '人脸登录' ELSE '其他方式登录'
            END AS login_type
        FROM
            login_record lr
            LEFT JOIN blade_user bu ON lr.user_id = bu.id
        WHERE
            DATE_FORMAT( lr.login_time, '%Y-%m' ) = #{month}
    </select>
    <select id="getLoginRecordTotalByMonth" resultType="org.springblade.face.vo.LoginRecordTotalExportVO">
        SELECT
            bu.real_name,
            CASE
                WHEN bu.login_status = 0 THEN
                '正常'
                WHEN bu.login_status = 1 THEN
                '预警'
                WHEN bu.login_status = 2 THEN
                '警告' ELSE '其他'
            END AS login_status,
            lr.*
        FROM
            (
            SELECT
                user_id,
                sum(
                IF
                ( login_type = 1, 1, 0 )) AS accountLoginCount,
                sum(
                IF
                ( login_type = 2, 1, 0 )) AS wxLoginCount,
                sum(
                IF
                ( login_type = 3, 1, 0 )) AS faceLoginCount
            FROM
                login_record
            WHERE
                DATE_FORMAT( login_time, '%Y-%m' ) = #{month}
            GROUP BY
                user_id
            ) lr
            LEFT JOIN blade_user bu ON lr.user_id = bu.id
    </select>
</mapper>
