package top.gtb520.java.backend.apibackserver.api.u;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import top.gtb520.java.backend.apibackserver.db.mysql;

import static top.gtb520.java.backend.apibackserver.utils.tools.BLUE;
import static top.gtb520.java.backend.apibackserver.utils.tools.echo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class user {
    @GetMapping("/user")
    public List<Map<String, Object>> GetUsers() throws SQLException {
        echo(BLUE, "url: /user");

        mysql db = new mysql();
        List<Map<String, Object>> users = new ArrayList<>();
        try {
            ResultSet rs = db.query("SELECT * FROM user");
            while (rs.next()) {
                Map<String, Object> user = new HashMap<>();
                // 假设用户表有id, name, email等字段，根据实际表结构修改
                user.put("id", rs.getInt("id"));
                user.put("user_name", rs.getString("user_name"));
                user.put("email", rs.getString("email"));
                user.put("user_uuid", rs.getString("user_uuid"));
                users.add(user);
            }
        } finally {
            db.close();
        }

        System.out.println(users);
        return users;
    }

    @GetMapping("/user/id/{id}")
    public List<Map<String, Object>> GetUserById(@PathVariable int id) throws SQLException {
        echo(BLUE, "url: /user/id");
        mysql db = new mysql();
        List<Map<String, Object>> usersID = new ArrayList<>();
        try {
            ResultSet rs = db.query("SELECT * FROM user WHERE id = " + id);
            while (rs.next()) {
                Map<String, Object> userID = new HashMap<>();
                // 假设用户表有id, name, email等字段，根据实际表结构修改
                userID.put("id", rs.getInt("id"));
                userID.put("user_name", rs.getString("user_name"));
                userID.put("email", rs.getString("email"));
                userID.put("user_uuid", rs.getString("user_uuid"));
                usersID.add(userID);
            }
        } finally {
            db.close();
        }
        System.out.println(usersID);
        return usersID;
    }

}
