<?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.kmall.api.dao.ApiUserMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap id="BaseResultMap" type="com.kmall.api.entity.UserVo" >
        <result property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="password" column="password"/>
        <result property="gender" column="gender"/>
        <result property="birthday" column="birthday"/>
        <result property="register_time" column="register_time"/>
        <result property="last_login_time" column="last_login_time"/>
        <result property="last_login_ip" column="last_login_ip"/>
        <result property="user_level_id" column="user_level_id"/>
        <result property="nickname" column="nickname"/>
        <result property="mobile" column="mobile"/>
        <result property="register_ip" column="register_ip"/>
        <result property="avatar" column="avatar"/>
        <result property="weixin_openid" column="weixin_openid"/>
        <result column="id_no" property="idNo" jdbcType="VARCHAR" />
        <result column="creater_sn" property="createrSn" jdbcType="VARCHAR" />
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
        <result column="moder_sn" property="moderSn" jdbcType="VARCHAR" />
        <result column="mod_time" property="modTime" jdbcType="TIMESTAMP" />
        <result column="tstm" property="tstm" jdbcType="TIMESTAMP" />
    </resultMap>

    <sql id="Base_Column_List" >
        id, username, password, gender, birthday, register_time, last_login_time, last_login_ip,
        user_level_id, nickname, mobile, register_ip, avatar, weixin_openid, id_no, creater_sn,
        create_time, moder_sn, mod_time, tstm
    </sql>
    <select id="queryObject" resultMap="BaseResultMap" >
        select
        <include refid="Base_Column_List" />
        from mall_user
        where id = #{id}
    </select>

    <select id="queryObjectByIdNoAndName" resultMap="BaseResultMap" >
        select
        <include refid="Base_Column_List" />
        from mall_user
        where id_no = #{idNo} and username = #{userName}
    </select>

    <select id="queryByOpenId" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
         from mall_user
        where 1 = 1
        <if test="openId != null">
            and `weixin_openid` = #{openId}
        </if>
    </select>

    <select id="queryList" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List" />
         from mall_user
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
		select count(*) from mall_user
	</select>

    <insert id="save" parameterType="com.kmall.api.entity.UserVo" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
		insert into mall_user
        <trim prefix="(" suffix=")" suffixOverrides="," >

            `username`,
            `password`,
            `gender`,
            `birthday`,
            `register_time`,
            `last_login_time`,
            `last_login_ip`,
            `user_level_id`,
            `nickname`,
            `mobile`,
            `register_ip`,
            `avatar`,
            `weixin_openid`,
            <if test="idNo != null" >
                id_no,
            </if>
            <if test="createrSn != null" >
                creater_sn,
            </if>
            <if test="createTime != null" >
                create_time,
            </if>
            <if test="moderSn != null" >
                moder_sn,
            </if>
            <if test="modTime != null" >
                mod_time,
            </if>
            <if test="tstm != null" >
                tstm,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides="," >
            #{username},
            #{password},
            #{gender},
            #{birthday},
            #{register_time},
            #{last_login_time},
            #{last_login_ip},
            #{user_level_id},
            #{nickname},
            #{mobile},
            #{register_ip},
            #{avatar},
            #{weixin_openid},
            <if test="idNo != null" >
                #{idNo,jdbcType=VARCHAR},
            </if>
            <if test="createrSn != null" >
                #{createrSn,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null" >
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="moderSn != null" >
                #{moderSn,jdbcType=VARCHAR},
            </if>
            <if test="modTime != null" >
                #{modTime,jdbcType=TIMESTAMP},
            </if>
            <if test="tstm != null" >
                #{tstm,jdbcType=TIMESTAMP},
            </if>
        </trim>
	</insert>

    <update id="update" parameterType="com.kmall.api.entity.UserVo">
        update mall_user
        <set >
            <if test="username != null">`username` = #{username},</if>
            <if test="password != null">`password` = #{password},</if>
            <if test="gender != null">`gender` = #{gender},</if>
            <if test="birthday != null">`birthday` = #{birthday},</if>
            <if test="register_time != null">`register_time` = #{register_time},</if>
            <if test="last_login_time != null">`last_login_time` = #{last_login_time},</if>
            <if test="last_login_ip != null">`last_login_ip` = #{last_login_ip},</if>
            <if test="user_level_id != null">`user_level_id` = #{user_level_id},</if>
            <if test="nickname != null">`nickname` = #{nickname},</if>
            <if test="mobile != null">`mobile` = #{mobile},</if>
            <if test="register_ip != null">`register_ip` = #{register_ip},</if>
            <if test="avatar != null">`avatar` = #{avatar},</if>
            <if test="weixin_openid != null">`weixin_openid` = #{weixin_openid},</if>
            <if test="idNo != null" >
                id_no = #{idNo,jdbcType=VARCHAR},
            </if>
            <if test="createrSn != null" >
                creater_sn = #{createrSn,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null" >
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="moderSn != null" >
                moder_sn = #{moderSn,jdbcType=VARCHAR},
            </if>
            <if test="modTime != null" >
                mod_time = #{modTime,jdbcType=TIMESTAMP},
            </if>
            <if test="tstm != null" >
                tstm = #{tstm,jdbcType=TIMESTAMP},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kmall.api.entity.SmsLogVo" id="smslogMap">
        <result property="id" column="id"/>
        <result property="user_id" column="user_id"/>
        <result property="mobile" column="mobile"/>
        <result property="stime" column="stime"/>
        <result property="sms_code" column="sms_code"/>
        <result property="send_status" column="send_status"/>
        <result property="content" column="content"/>
        <result property="smsode" column="sms_code"/>
    </resultMap>

    <select id="querySmsCodeByUserId" resultMap="smslogMap">
         a.id,
        a.user_id,
        a.mobile,
        a.stime,
        a.sms_code,
        a.send_status,
        a.content
        from sys_sms_log a
        left join sys_user b on a.user_id = b.user_id
        where a.user_id = #{id}  ORDER BY stime desc limit 1
    </select>

    <insert id="saveSmsCodeLog" parameterType="com.kmall.api.entity.SmsLogVo">
        insert into mall_sms_log(
        `id`,
        `user_id`,
        `phone`,
        `log_date`,
        `sms_code`,
        `send_status`,
        `sms_text`)
        values(
        #{id},
        #{user_id},
        #{phone},
        #{log_date},
        #{sms_code},
        #{send_status},
        #{sms_text}
        )
    </insert>

</mapper>