package com.nursinghome.mapper;

import com.nursinghome.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("SELECT * FROM sys_user WHERE username = #{username} LIMIT 1")
    User findByUsername(String username);

    @Select("SELECT * FROM sys_user WHERE id = #{id}")
    User findById(Long id);

    @Select("<script>SELECT * FROM sys_user <where><if test='role != null'>role = #{role}</if></where> ORDER BY id DESC</script>")
    List<User> listByRole(@Param("role") String role);

    @Insert("INSERT INTO sys_user(username, password, name, phone, role, status, created_at, updated_at) VALUES(#{username}, #{password}, #{name}, #{phone}, #{role}, #{status}, NOW(), NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(User user);

    @Update("UPDATE sys_user SET name=#{name}, phone=#{phone}, status=#{status}, updated_at=NOW() WHERE id=#{id}")
    int update(User user);

    @Update("UPDATE sys_user SET password=#{password}, updated_at=NOW() WHERE id=#{id}")
    int updatePassword(@Param("id") Long id, @Param("password") String password);

    @Update("UPDATE sys_user SET status=#{status}, updated_at=NOW() WHERE id=#{id}")
    int updateStatus(@Param("id") Long id, @Param("status") Integer status);
}
