package cn.edu.whu.boot.user.mapper;

import cn.edu.whu.boot.user.entities.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserMapper {
    @Select("SELECT * FROM user_staff")
    @Results({
            @Result(property = "realName",  column = "real_name")
    })
    List<User> findAll();
    
    @Select("SELECT * FROM user_staff " +
            "ORDER BY id " +
            "LIMIT #{limit} " +
            "OFFSET #{offset}")
    @Results({
            @Result(property = "realName",  column = "real_name")
    })
    List<User> findWithinPage(@Param("limit") int limit, @Param("offset") int offset);

    @Select("SELECT COUNT(*) FROM user_staff")
    int countAll();

    @Select("SELECT * FROM user_staff WHERE id = #{id}")
    @Results({
            @Result(property = "realName",  column = "real_name")
    })
    User findById(int id);

    @Select("SELECT * FROM user_staff WHERE username = #{username}")
    @Results({
            @Result(property = "realName",  column = "real_name")
    })
    User findByUsername(String username);

    @Select("SELECT COUNT(*) FROM user_staff WHERE username LIKE #{pattern}")
    int countLikeUsername(String pattern);

    @Select("SELECT * FROM user_staff WHERE username LIKE #{pattern} " +
            "ORDER BY id " +
            "LIMIT #{limit} " +
            "OFFSET #{offset}")
    @Results({
            @Result(property = "realName",  column = "real_name")
    })
    List<User> findLikeUsernameWithinPage(@Param("pattern") String pattern, @Param("limit") int limit, @Param("offset") int offset);

    @Select("SELECT * FROM user_staff WHERE phone = #{phone}")
    @Results({
            @Result(property = "realName",  column = "real_name")
    })
    User findByPhone(String phone);

    @Select("SELECT * FROM user_staff WHERE email = #{email}")
    @Results({
            @Result(property = "realName",  column = "real_name")
    })
    User findByEmail(String email);


    @Select("SELECT * FROM user_staff WHERE role = #{role}")
    @Results({
            @Result(property = "realName",  column = "real_name")
    })
    List<User> findByRole(int role);

    @Select("INSERT INTO user_staff(username, real_name, password, salt, phone, email, role) VALUES(#{username}, #{realName}, #{password}, #{salt}, #{phone}, #{email}, #{role}) RETURNING *")
    @Results({
            @Result(property = "realName", column = "real_name")
    })
    @Options(flushCache = Options.FlushCachePolicy.TRUE)
    User insert(User user);

    @Select("UPDATE user_staff SET username = #{username}, password = #{password}, salt = #{salt}, phone = #{phone}, email = #{email}, role = #{role} WHERE id = #{id} RETURNING *")
    @Results({
            @Result(property = "realName", column = "real_name")
    })
    @Options(flushCache = Options.FlushCachePolicy.TRUE)
    User update(User user);

    @Delete("DELETE FROM user_staff WHERE id = #{id}")
    int deleteById(@Param("id") int id);
}