<?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.api.app.dao.login.AppLoginDao">
    <select id="findUserCodeByTel" parameterType="java.lang.String" resultType="com.api.model.app.UserCode">
        select *  from user_code where tel = #{tel}
    </select>

    <update id="updateUserCode" parameterType="com.api.model.app.UserCode">
        update user_code set code = #{code},code_send_date = #{codeSendDate} where id = #{id}
    </update>

    <insert id="insertUserCode" parameterType="com.api.model.app.UserCode">
        insert into user_code(
            tel,
            code,
            code_send_date
        )values (
            #{tel},
            #{code},
            #{codeSendDate}
        )
    </insert>

    <delete id="deleteUserCodeByTel" parameterType="java.lang.String">
        delete from user_code where tel = #{tel}
    </delete>

    <select id="findUserResidentByTel" parameterType="java.lang.String" resultType="com.api.model.basicArchives.UserResident">
        select * from user_resident where tel = #{tel}
    </select>

    <insert id="insertLoginToken" parameterType="com.api.model.app.UserLoginToken">
        insert into user_login_token(
            user_login_session,
            resident_id,
            user_login_date
        )values (
            #{userLoginSession},
            #{residentId},
            #{userLoginDate}
        )
    </insert>

    <insert id="insertResidentEstateExamine" parameterType="com.api.model.my.MyHouse">
        insert into
        <trim prefix="cpm_resident_estate_examine(" suffix=")" suffixOverrides=",">
            building_unit_estate_id,
            resident_id,
            type,
            status,
            <if test="effectiveTimeStart != null">
                effective_time_start,
            </if>
            <if test="effectiveTimeEnd != null">
                effective_time_end,
            </if>
            <if test="name != null and name != ''">
                name,
            </if>
            <if test="idType != null">
                id_type,
            </if>
            <if test="idNumber != null and idNumber != ''">
                id_number,
            </if>
            <if test="reviewer != null">
                reviewer,
            </if>
            <if test="reviewerDate != null">
                reviewer_date,
            </if>
            is_delete,
            create_date,
        </trim>
        values
        <trim prefix="(" suffix=")" suffixOverrides=",">
            #{estateId},
            #{residentId},
            #{type},
            #{status},
            <if test="effectiveTimeStart != null">
                #{effectiveTimeStart},
            </if>
            <if test="effectiveTimeEnd != null">
                #{effectiveTimeEnd},
            </if>
            <if test="name != null and name != ''">
                #{name},
            </if>
            <if test="idType != null">
                #{idType},
            </if>
            <if test="idNumber != null and idNumber != ''">
                #{idNumber},
            </if>
            <if test="reviewer != null">
                #{reviewer},
            </if>
            <if test="reviewerDate != null">
                #{reviewerDate},
            </if>
            #{isDelete},
            #{createDate},
        </trim>
    </insert>

    <select id="findULTByTokenId" parameterType="java.lang.Long" resultType="com.api.vo.app.UserLoginTokenVo">
        select * from user_login_token where user_login_session = #{tokenId}
    </select>

    <select id="findUserResidentById" parameterType="java.lang.Integer" resultType="com.api.model.basicArchives.UserResident">
        select * from user_resident where id = #{id}
    </select>

    <update id="updateULTById" parameterType="com.api.vo.app.UserLoginTokenVo">
        update user_login_token
        set user_login_date = #{userLoginDate}
        where id = #{id}
    </update>

    <delete id="deleteULTByResidentId" parameterType="java.lang.Integer">
        delete from user_login_token where resident_id = #{residentId}
    </delete>

    <select id="findRequestLog" parameterType="com.api.model.app.AppRequestLog" resultType="com.api.model.app.AppRequestLog">
        select * from sys_resident_request_log where resident_id = #{residentId} and DATE(last_date) = DATE(#{lastDate}) and login_ip = #{loginIp}
    </select>

    <update id="updateRequestLog" parameterType="com.api.model.app.AppRequestLog">
        update sys_resident_request_log
        set request_num = request_num + 1,
        last_date = #{lastDate}
        where id = #{id} and login_ip = #{loginIp}
    </update>
    <insert id="insertRequestLog" parameterType="com.api.model.app.AppRequestLog">
        insert into sys_resident_request_log(
            resident_id,
            request_num,
            last_date,
            login_ip
        )values (
            #{residentId},
            #{requestNum},
            #{lastDate},
            #{loginIp}
        )
    </insert>
</mapper>