<?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.platform.dao.SysSmsLogDao">

    <resultMap type="com.platform.entity.SysSmsLogEntity" id="smsLogMap">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="mobile" column="mobile"/>
        <result property="templateId" column="template_id"/>
        <result property="code" column="code"/>
        <result property="sign" column="sign"/>
        <result property="sendStatus" column="send_status"/>
        <result property="sendId" column="send_id"/>
        <result property="successNum" column="success_num"/>
        <result property="content" column="content"/>
        <result property="returnMsg" column="return_msg"/>
        <result property="createTime" column="create_time"/>
    </resultMap>


	<select id="querySmsCodeByPhoneNum" resultType="com.platform.entity.SysSmsLogEntity">
        select
        a.id,
        a.user_id,
        a.mobile,
        a.code,
        a.send_status,
        a.create_time,
        a.content
        from sys_sms_log a
        where a.mobile = #{phoneNum} and a.send_status = 1
        order by a.create_time desc limit 0 , 1
    </select>

	<select id="queryObject" resultType="com.platform.entity.SysSmsLogEntity">
		select
			`id`,
			`user_id`,
			`mobile`,
			`template_id`,
			`code`,
			`sign`,
			`send_status`,
			`send_id`,
			`success_num`,
			`content`,
			`return_msg`,
			`create_time`
		from sys_sms_log
		where id = #{id}
	</select>

	<select id="queryList" resultType="com.platform.entity.SysSmsLogEntity">
		select
		sys_sms_log.id,
		sys_sms_log.user_id,
		sys_sms_log.mobile,
		sys_sms_log.template_id,
		sys_sms_log.code,
		sys_sms_log.sign,
		sys_sms_log.send_status,
		sys_sms_log.send_id,
		sys_sms_log.success_num,
		sys_sms_log.content,
		sys_sms_log.return_msg,
		sys_sms_log.create_time,
		sys_user.username user_name
		from sys_sms_log
		LEFT JOIN sys_user ON sys_user.user_id = sys_sms_log.user_id
		WHERE 1=1
		<if test="sendId != null and sendId != ''">
			AND sys_sms_log.send_id LIKE concat('%',#{sendId},'%')
		</if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
			<otherwise>
                order by stime DESC
			</otherwise>
        </choose>
		<if test="offset != null and limit != null">
			limit #{offset}, #{limit}
		</if>
	</select>
	
 	<select id="queryTotal" resultType="int">
		select count(*) from sys_sms_log
		WHERE 1=1
		<if test="sendId != null and sendId != ''">
			AND send_id LIKE concat('%',#{sendId},'%')
		</if>
	</select>
	 
	<insert id="save" parameterType="com.platform.entity.SysSmsLogEntity">
		insert into sys_sms_log(
			`id`,
			`user_id`,
			`mobile`,
			`template_id`,
			`code`,
			`sign`,
			`send_status`,
			`send_id`,
			`success_num`,
			`content`,
			`return_msg`,
			`create_time`)
		values(
			#{id},
			#{userId},
			#{mobile},
			#{templateId},
			#{code},
			#{sign},
			#{sendStatus},
			#{sendId},
			#{successNum},
			#{content},
			#{returnMsg},
			#{createTime})
	</insert>
	 
	<update id="update" parameterType="com.platform.entity.SysSmsLogEntity">
		update sys_sms_log 
		<set>
			<if test="userId != null">`user_id` = #{userId}, </if>
			<if test="mobile != null">`mobile` = #{mobile}, </if>
			<if test="templateId != null">`template_id` = #{templateId}, </if>
			<if test="code != null">`code` = #{code}, </if>
			<if test="sign != null">`sign` = #{sign}, </if>
			<if test="sendStatus != null">`send_status` = #{sendStatus}, </if>
			<if test="sendId != null">`send_id` = #{sendId}, </if>
			<if test="successNum != null">`success_num` = #{successNum}, </if>
			<if test="content != null">`content` = #{content}, </if>
			<if test="returnMsg != null">`return_msg` = #{returnMsg},</if>
			<if test="createTime != null">`create_time` = #{createTime} </if>
		</set>
		where id = #{id}
	</update>
	
	<delete id="delete">
		delete from sys_sms_log where id = #{value}
	</delete>
	
	<delete id="deleteBatch">
		delete from sys_sms_log where id in 
		<foreach item="id" collection="array" open="(" separator="," close=")">
			#{id}
		</foreach>
	</delete>

</mapper>