package com.example.system.mapper;

import com.example.system.model.UserTable;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {

    @Insert("INSERT INTO user_table (user_name, s_t_id, user_password, email, phone, user_privileges, college_id) " +
            "VALUES (#{userName}, #{sTId}, #{userPassword}, #{email}, #{phone}, #{userPrivileges}, #{collegeId})")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    void createUser(UserTable user);

    @Select("SELECT * FROM user_table WHERE s_t_id=#{sTId}")
    UserTable findBySTId(String sTId);

    @Update("UPDATE user_table SET phone=#{phone}, email=#{email}, user_password=#{userPassword} WHERE s_t_id=#{sTId} and user_status=0")
    Integer updateUser(UserTable user);

    @Update("UPDATE user_table SET user_status=0 WHERE s_t_id=#{sTId}")
    Integer updateUserStatus(String sTId);

    @Select("SELECT * FROM user_table WHERE user_name=#{userName} and user_status=0")
    UserTable findUserByUserName(String userName);

    @Select("SELECT * FROM user_table WHERE user_id=#{userId} and user_status=0")
    UserTable findUserById(Long userId);

    @Select("SELECT * FROM user_table WHERE user_status=0")
    List<UserTable> findAllUsers();

    //批量删除用户
    @Update("<script>"+"update user_table set user_status=1  where user_id in" +
            "<foreach collection='list' item='userId' open='(' close=');' separator=','>" +
            "#{userId}" +
            "</foreach>"+
            "</script>")
    Integer deleteList(List<Integer> list);
    //根据用户id查找用户姓名
    @Select("select user_name from user_table where user_id=#{studentId}")
    String getNameById(Integer studentId);

    @Update("update user_table SET " +
            "user_name=#{userName}, s_t_id=#{sTId}, user_password=#{userPassword}, " +
            "user_privileges=#{userPrivileges}, college_id=#{collegeId}, major_table_id=#{majorTableId}," +
            "teacher_title=#{teacherTitle},birthdate=#{birthdate},gender=#{gender}," +
            "phone=#{phone}, email=#{email}, user_password=#{userPassword}" +
            "WHERE user_id=#{userId}")
    int updateOther(UserTable user);

    //获取用户总数量
    @Select("SELECT COUNT(user_id) FROM user_table;")
    Object getCount();

    //获取老师总数量
    @Select("SELECT COUNT(user_id) FROM user_table WHERE user_privileges=1;")
    Object getTeacherCount();

    //获取学生总数量
    @Select("SELECT COUNT(user_id) FROM user_table WHERE user_privileges=2;")
    Object getStudentCount();
}