package com.cn.dao.impl; // 包名，存放实现类

import java.sql.*; // 导入Java SQL相关的类
import java.util.*; // 导入Java.util相关的类，如List和ArrayList

import com.cn.dao.PrepDao; // 导入PrepDao接口
import com.cn.domain.Prep; // 导入Prep实体类
import com.cn.util.JDBCUtil; // 导入JDBC工具类，用于数据库连接和关闭

/**
 * PrepDao的实现类，提供预订信息的数据访问操作。
 */
public class PrepDaoImpl implements PrepDao {
    private Connection conn = null; // 声明数据库连接对象
    private PreparedStatement pstmt = null; // 声明预编译语句对象
    private ResultSet rs = null; // 声明结果集对象

    @Override
    public int add(Prep prep) throws SQLException { // 添加预订信息的方法
        String sql = "insert into prep (trainid,pmemberid,startstation,endstation,trainnumber,starttime,endtime,price,way,booktime) values (?,?,?,?,?,?,?,?,?,?)";
        conn = JDBCUtil.getConnection(); // 获取数据库连接
        pstmt = conn.prepareStatement(sql); // 准备SQL语句
        pstmt.setObject(1, prep.getTrainId()); // 设置预编译语句参数
        pstmt.setObject(2, prep.getPmemberId());
        pstmt.setObject(3, prep.getStartStation());
        pstmt.setObject(4, prep.getEndStation());
        pstmt.setObject(5, prep.getTrainNumber());
        pstmt.setObject(6, prep.getStartTime());
        pstmt.setObject(7, prep.getEndTime());
        pstmt.setObject(8, prep.getPrice());
        pstmt.setObject(9, prep.isWay()); // 设置是否往返
        pstmt.setObject(10, prep.getBooktime());
        int recordNumber = pstmt.executeUpdate(); // 执行插入操作，并返回影响的行数
        closeResources(); // 关闭资源
        return recordNumber;
    }

    @Override
    public int delete(Integer prepId) throws SQLException { // 根据ID删除预订信息的方法
        String sql = "delete from prep where prepid=?";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, prepId);
        int recordNumber = pstmt.executeUpdate();
        closeResources();
        return recordNumber;
    }

    @Override
    public int update(Prep prep) throws SQLException { // 更新预订信息的方法
        String sql = "update prep set trainid=?,pmemberid=?,startstation=?,endstation=?,trainnumber=?,starttime=?,endtime=?,price=?,way=? where prepid=?";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, prep.getTrainId());
        pstmt.setObject(2, prep.getPmemberId());
        pstmt.setObject(3, prep.getStartStation());
        pstmt.setObject(4, prep.getEndStation());
        pstmt.setObject(5, prep.getTrainNumber());
        pstmt.setObject(6, prep.getStartTime());
        pstmt.setObject(7, prep.getEndTime());
        pstmt.setObject(8, prep.getPrice());
        pstmt.setObject(9, prep.isWay());
        pstmt.setObject(10, prep.getPrepId());
        int recordNumber = pstmt.executeUpdate();
        closeResources();
        return recordNumber;
    }

    @Override
    public List<Prep> getAll() throws SQLException { // 获取所有预订信息的方法
        String sql = "select * from prep";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        rs = pstmt.executeQuery();
        List<Prep> list = new ArrayList<>(); // 创建预订信息列表
        while (rs.next()) {
            // 从结果集中提取数据并创建Prep对象，然后添加到列表中
            int prepId = rs.getInt("prepId");
            // ... 省略其他字段的提取和对象创建代码 ...
            Prep prep = new Prep(prepId, /* 其他参数 */);
            list.add(prep);
        }
        closeResources();
        return list;
    }

    @Override
    public List<Prep> getPrepByPmemberId(Integer pmemberId) throws SQLException { // 根据个人会员ID获取预订信息的方法
        String sql = "select * from prep where pmemberid=?";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, pmemberId);
        rs = pstmt.executeQuery();
        List<Prep> list = new ArrayList<>();
        while (rs.next()) {
            // 从结果集中提取数据并创建Prep对象，然后添加到列表中
            int prepId = rs.getInt("prepId");
            // ... 省略其他字段的提取和对象创建代码 ...
            Prep prep = new Prep(prepId, /* 其他参数 */);
            list.add(prep);
        }
        closeResources();
        return list;
    }

    @Override
    public Prep getById(Integer prepId) throws SQLException { // 根据ID获取预订信息的方法
        String sql = "select * from prep where prepid=?";
        conn = JDBCUtil.getConnection();
        pstmt = conn.prepareStatement(sql);
        pstmt.setObject(1, prepId);
        rs = pstmt.executeQuery();
        Prep prep = null;
        if (rs.next()) {
            // 从结果集中提取数据并创建Prep对象
            int trainId = rs.getInt("trainId");
            // ... 省略其他字段的提取和对象创建代码 ...
            prep = new Prep(prepId, /* 其他参数 */);
        }
        closeResources();
        return prep;
    }

    // 关闭数据库资源的方法
    private void closeResources() {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
