<?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="cn.hellohao.dao.UserMapper">

    <!-- 用户注册 -->
    <insert id="register" parameterType="cn.hellohao.pojo.User">
        INSERT INTO user (id, username, PASSWORD, email, birthder, LEVEL,uid,isok,memory,groupid)
        VALUES (NULL, #{username}, #{password}, #{email}, #{birthder}, #{level},#{uid},#{isok},#{memory},#{groupid}   )
    </insert>
    <!--  查询是否有username  -->
    <select id="countusername" parameterType="string" resultType="integer">
        select COUNT(*)
        from user
        where username = #{username}
    </select>
    <!--  查询是否有邮箱  -->
    <select id="countmail" parameterType="string" resultType="integer">
        select COUNT(*)
        from user
        where email = #{email}

    </select>
    <!--  uid查询用户并修改  -->
    <update id="uiduser" parameterType="cn.hellohao.pojo.User">
        <!-- UPDATE `user` set username=#{username},`password`=#{password} where email=#{email} -->
        UPDATE `user` set `isok`=1 where uid=#{uid}
    </update>

    <!-- 登录 -->
    <select id="login" parameterType="string" resultType="integer">
        select COUNT(*)
        from user
<!--        where email = #{email}-->
<!--        and password = #{password}-->
        <where>
             1=1
            <if test="email != null">
                and email = #{email}
            </if>
            <if test="password != null">
                and password = #{password}
            </if>
            <if test="uid != null">
                and uid = #{uid}
            </if>

        </where>
    </select>

    <!-- 查询当前用户:根据邮箱,根据id -->
    <select id="getUsers" parameterType="cn.hellohao.pojo.User" resultType="cn.hellohao.pojo.User">
        SELECT id,
        username,
        email,
        password,
        birthder,
        LEVEL,
        uid,
        isok,
        memory,
        groupid
        FROM user
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="email != null">
                and email = #{email}
            </if>
            <if test="uid != null">
                and uid = #{uid}
            </if>
        </where>
    </select>
    <!-- 查询当前用户:根据id -->
    <select id="getUsersid" parameterType="integer" resultType="cn.hellohao.pojo.User">
        SELECT id,
               username,
               email,
               password,
               birthder,
               LEVEL,
               uid,
               isok,
               memory,
               groupid
        FROM user
        WHERE id = #{id}
    </select>
    <!-- 查询当前用户:根据uid -->
    <select id="getUsersMail" parameterType="string" resultType="cn.hellohao.pojo.User">
        SELECT id,
        username,
        email,
        password,
        birthder,
        LEVEL,
        uid,
        isok,
        memory,
        groupid
        FROM user
        WHERE uid = #{uid}
    </select>

    <!-- 插入图片信息 -->
    <insert id="insertimg" parameterType="cn.hellohao.pojo.Images">
        INSERT INTO imgdata (id, imgname, imgurl, userid, updatetime,sizes,abnormal,source,imgtype,md5key,imguid,format,about,great,violation)
        VALUES (NULL, #{imgname}, #{imgurl}, #{userid}, #{updatetime},#{sizes},#{abnormal},#{source},#{imgtype},#{md5key},#{imguid},#{format},#{about},"0",null)
    </insert>

    <!-- 修改资料 -->
    <update id="change" parameterType="cn.hellohao.pojo.User">
        <!-- UPDATE `user` set username=#{username},`password`=#{password} where email=#{email} -->
        UPDATE `user`
        <set>
            <if test="email != null">
                `email` = #{email},
            </if>
            <if test="username != null">
                `username` = #{username},
            </if>
            <if test="password != null">
            `password` = #{password},
            </if>
            <if test="memory != null">
                `memory` = #{memory},
            </if>
            <if test="groupid != null">
                `groupid` = #{groupid}
            </if>
        </set>
        where uid=#{uid}
    </update>

    <update id="changeUser" parameterType="cn.hellohao.pojo.User">
        UPDATE `user`
        <set>
            <if test="email != null">
                `email` = #{email},
            </if>
            <if test="username != null">
                `username` = #{username},
            </if>
            <if test="password != null">
                `password` = #{password},
            </if>
            <if test="memory != null">
                `memory` = #{memory},
            </if>
            <if test="groupid != null">
                `groupid` = #{groupid},
            </if>
            <if test="isok != null">
                `isok` = #{isok},
            </if>
            <if test="uid != null">
                `uid` = #{uid},
            </if>
        </set>
        where id=#{id}
    </update>

    <select id="checkUsername" parameterType="string" resultType="integer">
        SELECT count(username) FROM `user` where username=#{username}
    </select>

    <!-- 查询用户总数 -->
    <select id="getUserTotal" resultType="integer">
        SELECT count(*) FROM `user`
    </select>

    <!-- 获取用户的详细信息 -->
    <select id="getuserlist" resultType="cn.hellohao.pojo.User" parameterType="string">
        SELECT
        id,
        username,
        email,
        (select from_base64(password)) as password,
        birthder,
        LEVEL,
        uid,
        isok,
        ceil((memory/1024/1024)) as memory,
        groupid,
        (select groupname from `group` where id = groupid) as groupname
        FROM
        user
        where
        1=1
        <if test="username != null">
            and CONCAT(
            username,
            email
            ) LIKE '%${username}%'
        </if>
        order by birthder desc
    </select>

    <!--刪除用戶-->
    <delete id="deleuser" parameterType="integer">
        DELETE FROM user WHERE id=#{id}
    </delete>

    <update id="setisok" parameterType="cn.hellohao.pojo.User">
        <!-- UPDATE `user` set username=#{username},`password`=#{password} where email=#{email} -->
        UPDATE `user` set `isok`=#{isok} where id=#{id}
    </update>
    <update id="setmemory" parameterType="cn.hellohao.pojo.User">
        <!-- UPDATE `user` set username=#{username},`password`=#{password} where email=#{email} -->
        UPDATE `user` set `memory`=#{memory} where id=#{id}
    </update>

    <select id="getuserlistforgroupid" parameterType="integer" resultType="cn.hellohao.pojo.User">
        select * from user where groupid=#{groupid}
    </select>

</mapper>