package JDBC;

import pojo.Vehicle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class VehicleJDBC {
    public VehicleJDBC() {
        super();
    }

    public Connection getConnection() throws Exception {
        Class.forName("com.mysql.cj.jdbc.Driver");
        return DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/parkinglot", "root", "root");
    }

    public List<Vehicle> selectAllVehicles() throws Exception {
        Connection conn = this.getConnection();
        PreparedStatement ps = conn.prepareStatement("SELECT *\n" +
                "FROM vehicleuserview");
        ResultSet rs = ps.executeQuery();
        List<Vehicle> vehicles = new ArrayList<>();
        while(rs.next()) {
            Vehicle vehicle = new Vehicle(
                    rs.getString("car_license"),
                    rs.getString("name"),
                    rs.getString("brand"),
                    rs.getString("model"),
                    rs.getString("telephone"),
                    rs.getString("address")

            );
            vehicles.add(vehicle);
        }
        conn.close();
        return vehicles;
    }

    public List<Vehicle> selectVehiclesByCase(String car_license, String brand, String model, String building) throws Exception {
        Connection conn = this.getConnection();
        String sql = "SELECT * FROM vehicleuserview";
        StringBuilder whereClause = new StringBuilder(" WHERE ");

        if (car_license != null && !car_license.isEmpty()) {
            whereClause.append("car_license LIKE '%").append(car_license).append("%' AND ");
        }
        if (brand != null && !brand.isEmpty()) {
            whereClause.append("brand = '").append(brand).append("' AND ");
        }
        if (model != null && !model.isEmpty()) {
            whereClause.append("model = '").append(model).append("' AND ");
        }
        // 添加楼号模糊匹配
        if (building != null && !building.isEmpty()) {
            whereClause.append("address LIKE '").append(building).append("号楼%' AND ");
        }

        if (whereClause.toString().endsWith(" AND ")) {
            whereClause.delete(whereClause.lastIndexOf(" AND "), whereClause.length());
        }
        if (!" WHERE ".equals(whereClause.toString())) {
            sql += whereClause.toString();
        }
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        List<Vehicle> vehicles = new ArrayList<>();
        while (rs.next()) {
            Vehicle vehicle = new Vehicle(
                    rs.getString("car_license"),
                    rs.getString("name"),
                    rs.getString("brand"),
                    rs.getString("model"),
                    rs.getString("telephone"),
                    rs.getString("address")
            );
            vehicles.add(vehicle);
        }
        conn.close();
        return vehicles;
    }


    public Vehicle get_car_ById(int id) throws Exception {
        Connection conn = this.getConnection();
        PreparedStatement ps = conn.prepareStatement("SELECT *\n" +
                "FROM vehicleinformation\n" +
                "WHERE user_id = ?\n"
        );
        ps.setInt(1, id);
        ResultSet rs = ps.executeQuery();
        Vehicle pr = null;
        while(rs.next()) {
            pr = new Vehicle(
                    rs.getString("car_license"),
                    rs.getInt("user_id"),
                    rs.getString("brand"),
                    rs.getString("model")

            );
        }
        conn.close();
        return pr;
    }
}



