<?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.unilife.mapper.UserMapper">
    <resultMap id="userResultMap" type="com.unilife.model.entity.User">
        <id column="id" property="id"/>
        <result column="email" property="email"/>
        <result column="password" property="password"/>
        <result column="username" property="username"/>
        <result column="nickname" property="nickname"/>
        <result column="avatar" property="avatar"/>
        <result column="role" property="role"/>
        <result column="is_verified" property="isVerified"/>
        <result column="status" property="status"/>
        <result column="login_ip" property="loginIp"/>
    </resultMap>
    <insert id="insert" parameterType="com.unilife.model.entity.User" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO users (
            username, email, password,nickname,avatar,bio, gender,student_id,department, major,grade,points,role,status,is_verified,login_ip,login_time
        ) VALUES (
                     #{username},
                     #{email},
                     #{password},
                     #{nickname},
                     #{avatar},
                     #{bio},
                     #{gender},
                     #{studentId},
                     #{department},
                     #{major},
                     #{grade},
                     #{points},
                     #{role},
                     #{status},
                     #{isVerified},
                     #{loginIp},
                     #{loginTime}
                 )
    </insert>

    <select id="findByEmail" resultMap="userResultMap">
        SELECT id, email, password, username, nickname, avatar, role, is_verified, status,login_ip
        FROM users
        WHERE email = #{email}
    </select>

    <update id="updateLoginInfo">
        UPDATE users
        SET login_ip = #{ipLocation},
            login_time = #{loginTime},
            updated_at = NOW()
        WHERE id = #{userId}
    </update>



    <update id="UpdateIPLocation" parameterType="com.unilife.model.entity.User">
        UPDATE users
        SET login_ip = #{loginIp}
        WHERE email = #{email}
    </update>

    <select id="getUserByEmail" resultType="com.unilife.model.entity.User">
        select username,
               email,
               password,
               nickname,
               avatar,
               bio,
               gender,
               student_id,
               department,
               major,
               grade,
               points,
               role,
               status,
               is_verified,
               login_ip,
               login_time
        from users where email=#{email};
    </select>

    <!-- 用户信息管理相关SQL -->
    <select id="getUserById" resultType="com.unilife.model.entity.User">
        SELECT id,
               username,
               email,
               nickname,
               avatar,
               bio,
               gender,
               student_id as studentId,
               department,
               major,
               grade,
               points,
               role,
               status,
               is_verified as isVerified,
               login_ip as loginIp,
               login_time as loginTime,
               created_at as createdAt,
               updated_at as updatedAt
        FROM users
        WHERE id = #{id}
    </select>

    <update id="updateUserProfile" parameterType="com.unilife.model.entity.User">
        UPDATE users
        SET nickname = #{nickname},
            bio = #{bio},
            gender = #{gender},
            department = #{department},
            major = #{major},
            grade = #{grade},
            updated_at = NOW()
        WHERE id = #{id}
    </update>

    <update id="updatePassword">
        UPDATE users
        SET password = #{newPassword},
            updated_at = NOW()
        WHERE id = #{id}
    </update>

    <update id="updateAvatar">
        UPDATE users
        SET avatar = #{avatar},
            updated_at = NOW()
        WHERE id = #{id}
    </update>

    <update id="updateEmail">
        UPDATE users
        SET email = #{email},
            updated_at = NOW()
        WHERE id = #{id}
    </update>
</mapper>
