package backend.dao;

import backend.model.TestRecord;
import backend.utils.DBConnectionPool;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TestRecordDao {
    List<TestRecord> commonResultSet(ResultSet rs) throws SQLException{
        List<TestRecord> testRecordPoList = new ArrayList<>();
        while(rs.next()){
            TestRecord testRecord = new TestRecord();
            testRecord.setId(rs.getInt("id"));
            testRecord.setUserId(rs.getInt("userId"));
            testRecord.setQuestionId(rs.getInt("questionId"));
            testRecord.setScore(rs.getInt("score"));
            testRecord.setAnswer(rs.getString("answer"));
            testRecord.setTime(rs.getDate("time"));
            testRecordPoList.add(testRecord);
        }
        return testRecordPoList;
    }

    public List<TestRecord> getAll() {
        List<TestRecord> testRecordPoList = new ArrayList<>();
        //1. 从连接池中拿到连接
        Connection conn = DBConnectionPool.getConnection();
        if(conn==null)
            return null;
        //2. 创建Statement对象
        String sql = "select id,userId,questionId,score,answer,time from testRecord";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            testRecordPoList=commonResultSet(rs);
            rs.close();
            stmt.close();
        } catch(SQLException e){
            System.out.println("数据库异常："+ e.getMessage());
        }
        finally {
            DBConnectionPool.releaseConnection(conn);
        }
        return testRecordPoList;
    }
    public List<TestRecord> getByUserId(Integer userId) {
        List<TestRecord> testRecordPoList = new ArrayList<>();
        //1. 从连接池中拿到连接
        Connection conn = DBConnectionPool.getConnection();
        if(conn==null)
            return null;
        //2. 创建Statement对象
        String sql = "select id,userId,questionId,score,answer,time from testRecord where userId = ?";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, userId);
            ResultSet rs = stmt.executeQuery();
            testRecordPoList=commonResultSet(rs);
            rs.close();
            stmt.close();
        } catch(SQLException e){
            System.out.println("数据库异常："+ e.getMessage());
        } finally {
            DBConnectionPool.releaseConnection(conn);
        }
        return testRecordPoList;
    }
    public List<TestRecord> getByQuestionId(Integer questionId) {
        List<TestRecord> testRecordPoList = new ArrayList<>();
        //1. 从连接池中拿到连接
        Connection conn = DBConnectionPool.getConnection();
        if(conn==null)
            return null;
        //2. 创建Statement对象
        String sql = "select id,userId,questionId,score,answer,time from testRecord where questionId = ?";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, questionId);
            ResultSet rs = stmt.executeQuery();
            testRecordPoList=commonResultSet(rs);
            rs.close();
            stmt.close();
        }
        catch(SQLException e){
            System.out.println("数据库异常："+ e.getMessage());
        } finally {
            DBConnectionPool.releaseConnection(conn);
        }
        return testRecordPoList;
    }
    public List<TestRecord> getByUserIdAndQuestionId(Integer userId, Integer questionId) {
        List<TestRecord> testRecordPoList = new ArrayList<>();
        //1. 从连接池中拿到连接
        Connection conn = DBConnectionPool.getConnection();
        if(conn==null)
            return null;
        //2. 创建Statement对象
        String sql = "select id,userId,questionId,score,answer,time from testRecord where userId = ? and questionId = ?";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, userId);
            stmt.setInt(2, questionId);
            ResultSet rs = stmt.executeQuery();
            testRecordPoList = commonResultSet(rs);
            rs.close();
            stmt.close();
        } catch(SQLException e){
            System.out.println("数据库异常："+ e.getMessage());
        } finally {
            DBConnectionPool.releaseConnection(conn);
        }
        return testRecordPoList;
    }


    public boolean insert(TestRecord testRecord){
        int row = 0;
        //1. 从连接池中拿到连接
        Connection conn = DBConnectionPool.getConnection();
        if(conn==null)
            return false;
        //2. 创建Statement对象
        String sql = "insert into testRecord(userId,questionId,score,answer,time) values(?,?,?,?,?)";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, testRecord.getUserId());
            stmt.setInt(2, testRecord.getQuestionId());
            stmt.setInt(3, testRecord.getScore());
            stmt.setString(4, testRecord.getAnswer());
            stmt.setDate(5, testRecord.getTime());
            row = stmt.executeUpdate();
            stmt.close();
        } catch(SQLException e){
            System.out.println("数据库异常："+ e.getMessage());
        } finally {
            DBConnectionPool.releaseConnection(conn);
        }
        return row > 0;
    }
    public boolean delete(Integer id){
        int row = 0;
        //1. 从连接池中拿到连接
        Connection conn = DBConnectionPool.getConnection();
        if(conn==null)
            return false;
        //2. 创建Statement对象
        String sql = "delete from testRecord where id = ?";
        try{
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            row = stmt.executeUpdate();
            stmt.close();
        } catch(SQLException e){
            System.out.println("数据库异常："+ e.getMessage());
        } finally {
            DBConnectionPool.releaseConnection(conn);
        }
        return row > 0;
    }

}
