<?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.ruoyi.system.mapper.SysSmsLogMapper">
    
    <resultMap type="SysSmsLog" id="SysSmsLogResult">
        <result property="id"    column="id"    />
        <result property="phoneNo"    column="phone_no"    />
        <result property="message"    column="message"    />
        <result property="code"    column="code"    />
        <result property="status"    column="status"    />
        <result property="remarks"    column="remarks"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="type"    column="type"    />
        <result property="isUsed"    column="is_used"    />
    </resultMap>

    <sql id="selectSysSmsLogVo">
        select id, phone_no, message, code, status, remarks, create_time, update_time, type, is_used from sys_sms_log
    </sql>

    <select id="selectSysSmsLogList" parameterType="SysSmsLog" resultMap="SysSmsLogResult">
        <include refid="selectSysSmsLogVo"/>
        <where>  
            <if test="phoneNo != null  and phoneNo != ''"> and phone_no = #{phoneNo}</if>
            <if test="message != null  and message != ''"> and message = #{message}</if>
            <if test="code != null  and code != ''"> and code = #{code}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            <if test="type != null "> and type = #{type}</if>
            <if test="isUsed != null "> and is_used = #{isUsed}</if>
        </where>
    </select>
    
    <select id="selectSysSmsLogById" parameterType="Long" resultMap="SysSmsLogResult">
        <include refid="selectSysSmsLogVo"/>
        where id = #{id}
    </select>

    <insert id="insertSysSmsLog" parameterType="SysSmsLog" useGeneratedKeys="true" keyProperty="id">
        insert into sys_sms_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="phoneNo != null">phone_no,</if>
            <if test="message != null">message,</if>
            <if test="code != null">code,</if>
            <if test="status != null">status,</if>
            <if test="remarks != null">remarks,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="type != null">type,</if>
            <if test="isUsed != null">is_used,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="phoneNo != null">#{phoneNo},</if>
            <if test="message != null">#{message},</if>
            <if test="code != null">#{code},</if>
            <if test="status != null">#{status},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="type != null">#{type},</if>
            <if test="isUsed != null">#{isUsed},</if>
         </trim>
    </insert>

    <update id="updateSysSmsLog" parameterType="SysSmsLog">
        update sys_sms_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="phoneNo != null">phone_no = #{phoneNo},</if>
            <if test="message != null">message = #{message},</if>
            <if test="code != null">code = #{code},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="type != null">type = #{type},</if>
            <if test="isUsed != null">is_used = #{isUsed},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysSmsLogById" parameterType="Long">
        delete from sys_sms_log where id = #{id}
    </delete>

    <delete id="deleteSysSmsLogByIds" parameterType="String">
        delete from sys_sms_log where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <select id="getCountLastHour" parameterType="java.lang.String" resultType="java.lang.Integer">
        select count(1) from sys_sms_log where phone_no = #{phoneNumber,jdbcType=VARCHAR} and create_time >= date_sub(now(), interval 1 hour)
    </select>

    <select id="checkCode" parameterType="SysSmsLog" resultMap="SysSmsLogResult">
        <include refid="selectSysSmsLogVo"/>
        where type !=4 and status=1 and is_used=0
        <if test="phoneNo != null  and phoneNo != ''"> and phone_no = #{phoneNo} </if>
        and create_time >= #{createTime, jdbcType=TIMESTAMP} order by create_time desc limit 1;
    </select>
</mapper>