<?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.bonus.boot.manager.basic.dao.AppLoginAuditDao">

    <resultMap type="com.bonus.boot.manager.basic.entity.AppLoginAudit" id="AppLoginAuditMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="userId" column="USER_ID" jdbcType="VARCHAR"/>
        <result property="userName" column="USERNAME" jdbcType="VARCHAR"/>
        <result property="deviceId" column="DEVICE_ID" jdbcType="VARCHAR"/>
        <result property="deviceModel" column="DEVICE_MODEL" jdbcType="VARCHAR"/>
        <result property="androidVersion" column="ANDROID_VERSION" jdbcType="VARCHAR"/>
        <result property="loginDate" column="LOGIN_DATE" jdbcType="VARCHAR"/>
        <result property="loginTime" column="LOGIN_TIME" jdbcType="VARCHAR"/>
        <result property="isActive" column="is_active" jdbcType="VARCHAR"/>
        <result property="telepNumber" column="telep_number" jdbcType="VARCHAR"/>

        <result property="userName" column="USER_NAME" jdbcType="VARCHAR"/>
        <result property="loginName" column="LOGIN_NAME" jdbcType="VARCHAR"/>
        <result property="ipAddress" column="IP_ADDRESS" jdbcType="VARCHAR"/>
        <result property="isApp" column="IS_APP" jdbcType="VARCHAR"/>
        <result property="createTime" column="CREATE_TIME" jdbcType="VARCHAR"/>
        <result property="loginResult" column="LOGIN_RESULT" jdbcType="VARCHAR"/>

    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="AppLoginAuditMap">
        select
          id, USER_ID, DEVICE_ID, DEVICE_MODEL, ANDROID_VERSION, LOGIN_DATE, LOGIN_TIME, is_active, telep_number
        from app_login_audit
        where id = #{id}
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="AppLoginAuditMap">
        SELECT
        ala.`id`,
        ala.`USER_NAME`,
        ala.`LOGIN_NAME`,
        ala.`IP_ADDRESS`,
        if(ala.IS_APP = '1','app','后台') as is_app,
        ala.CREATE_TIME,
        ala.LOGIN_RESULT
        FROM
        sys_login_log ala
        <where>
            <if test="user.telepNumber != null and user.telepNumber != ''">
                and (
                    ala.USER_NAME like concat ('%',#{user.telepNumber},'%')
                )
            </if>
        </where>
        limit #{page},#{pageSize}
    </select>

    <select id="count" resultType="java.lang.Integer">
        select
            count(1)
        FROM
        app_login_audit ala
        <where>
            <if test="user.telepNumber != null and user.telepNumber != ''">
                and (
                    ala.telep_number like concat ('%',#{user.telepNumber},'%')
                )
            </if>
            and ala.is_active = '1'
        </where>
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into app_login_audit(USER_ID, DEVICE_ID, DEVICE_MODEL, ANDROID_VERSION, LOGIN_DATE, LOGIN_TIME, is_active, telep_number)
        values (#{userId}, #{deviceId}, #{deviceModel}, #{androidVersion}, #{loginDate}, #{loginTime}, #{isActive}, #{telepNumber})
    </insert>

    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into app_login_audit(USER_ID, DEVICE_ID, DEVICE_MODEL, ANDROID_VERSION, LOGIN_DATE, LOGIN_TIME, is_active, telep_number)
        values
        <foreach collection="entities" item="entity" separator=",">
        (#{entity.userId}, #{entity.deviceId}, #{entity.deviceModel}, #{entity.androidVersion}, #{entity.loginDate}, #{entity.loginTime}, #{entity.isActive}, #{entity.telepNumber})
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into app_login_audit(USER_ID, DEVICE_ID, DEVICE_MODEL, ANDROID_VERSION, LOGIN_DATE, LOGIN_TIME, is_active, telep_number)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.userId}, #{entity.deviceId}, #{entity.deviceModel}, #{entity.androidVersion}, #{entity.loginDate}, #{entity.loginTime}, #{entity.isActive}, #{entity.telepNumber})
        </foreach>
        on duplicate key update
        USER_ID = values(USER_ID),
        DEVICE_ID = values(DEVICE_ID),
        DEVICE_MODEL = values(DEVICE_MODEL),
        ANDROID_VERSION = values(ANDROID_VERSION),
        LOGIN_DATE = values(LOGIN_DATE),
        LOGIN_TIME = values(LOGIN_TIME),
        is_active = values(is_active),
        telep_number = values(telep_number)
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update app_login_audit
        <set>
            <if test="userId != null and userId != ''">
                USER_ID = #{userId},
            </if>
            <if test="deviceId != null and deviceId != ''">
                DEVICE_ID = #{deviceId},
            </if>
            <if test="deviceModel != null and deviceModel != ''">
                DEVICE_MODEL = #{deviceModel},
            </if>
            <if test="androidVersion != null and androidVersion != ''">
                ANDROID_VERSION = #{androidVersion},
            </if>
            <if test="loginDate != null and loginDate != ''">
                LOGIN_DATE = #{loginDate},
            </if>
            <if test="loginTime != null and loginTime != ''">
                LOGIN_TIME = #{loginTime},
            </if>
            <if test="isActive != null and isActive != ''">
                is_active = #{isActive},
            </if>
            <if test="telepNumber != null and telepNumber != ''">
                telep_number = #{telepNumber},
            </if>
        </set>
        where id = #{id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from app_login_audit where id = #{id}
    </delete>

</mapper>

