<?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.admin.dao.UserDao">

    <resultMap type="com.kmall.admin.entity.UserEntity" id="userMap">
        <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="registerTime" column="register_time"/>
        <result property="lastLoginTime" column="last_login_time"/>
        <result property="lastLoginIp" column="last_login_ip"/>
        <result property="userLevelId" column="user_level_id"/>
        <result property="nickname" column="nickname"/>
        <result property="mobile" column="mobile"/>
        <result property="registerIp" column="register_ip"/>
        <result property="avatar" column="avatar"/>
        <result property="weixinOpenid" column="weixin_openid"/>
    </resultMap>

    <select id="queryObject" resultType="com.kmall.admin.entity.UserEntity">
		select
			id,
			username,
			password,
			gender,
			birthday,
			register_time,
			last_login_time,
			last_login_ip,
			user_level_id,
			nickname,
			mobile,
			register_ip,
			avatar,
			weixin_openid
		from mall_user
		where id = #{id}
	</select>

    <select id="queryList" resultType="com.kmall.admin.entity.UserEntity">
        select
        mall_user.id,
        mall_user.username,
        mall_user.password,
        mall_user.gender,
        mall_user.birthday,
        mall_user.register_time,
        mall_user.last_login_time,
        mall_user.last_login_ip,
        mall_user.user_level_id,
        mall_user.nickname,
        mall_user.mobile,
        mall_user.register_ip,
        mall_user.avatar,
        mall_user.weixin_openid,
        mall_user_level.name levelName
        from mall_user LEFT JOIN mall_user_level ON mall_user.user_level_id=mall_user_level.id
        WHERE 1=1
        <if test="username != null and username.trim() != ''">
            and username like concat('%',#{username},'%')
        </if>
        <if test="mobile != null and mobile.trim() != ''">
            and mobile like concat('%',#{mobile},'%')
        </if>
        <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
        WHERE 1=1
        <if test="username != null and username.trim() != ''">
            and username like concat('%',#{username},'%')
        </if>
        <if test="mobile != null and mobile.trim() != ''">
            and mobile like concat('%',#{mobile},'%')
        </if>
    </select>

    <insert id="save" parameterType="com.kmall.admin.entity.UserEntity" useGeneratedKeys="true" keyProperty="id">
		insert into mall_user(
			`username`,
			`password`,
			`gender`,
			`birthday`,
			`register_time`,
			`last_login_time`,
			`last_login_ip`,
			`user_level_id`,
			`nickname`,
			`mobile`,
			`register_ip`,
			`avatar`,
			`weixin_openid`)
		values(
			#{username},
			#{password},
			#{gender},
			#{birthday},
			#{registerTime},
			#{lastLoginTime},
			#{lastLoginIp},
			#{userLevelId},
			#{nickname},
			#{mobile},
			#{registerIp},
			#{avatar},
			#{weixinOpenid})
	</insert>

    <update id="update" parameterType="com.kmall.admin.entity.UserEntity">
        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="registerTime != null">`register_time` = #{registerTime},</if>
            <if test="lastLoginTime != null">`last_login_time` = #{lastLoginTime},</if>
            <if test="lastLoginIp != null">`last_login_ip` = #{lastLoginIp},</if>
            <if test="userLevelId != null">`user_level_id` = #{userLevelId},</if>
            <if test="nickname != null">`nickname` = #{nickname},</if>
            <if test="mobile != null">`mobile` = #{mobile},</if>
            <if test="registerIp != null">`register_ip` = #{registerIp},</if>
            <if test="avatar != null">`avatar` = #{avatar},</if>
            <if test="weixinOpenid != null">`weixin_openid` = #{weixinOpenid}</if>
        </set>
        where id = #{id}
    </update>

    <delete id="delete">
		delete from mall_user where id = #{value}
	</delete>

    <delete id="deleteBatch">
        delete from mall_user where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>