package course;

import common.DBTool;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 这是课程表（t_course）的DAO类，提供了增删改查和列出方法
 * @author Awin-x
 */
public class CourseDB {
    public static ArrayList<CourseInfo> getAllCourses(){
        ArrayList<CourseInfo> courseList = new ArrayList<>();
        try{
            PreparedStatement ps = DBTool.getPreparedStatement("select * from t_course");
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                int id = rs.getInt("N_COURSE_ID");
                String name = rs.getString("VC_COURSE_NAME");
                int type = rs.getInt("N_TYPE");
                float credits = rs.getFloat("F_CREDIT");
                int grade = rs.getInt("N_GRADE");
                int major = rs.getInt("N_MAJOR");
                String detail = rs.getString("VC_DETAIL");
                courseList.add(new CourseInfo(id, name, type, credits, grade, major, detail));
            }
            DBTool.close(ps, rs);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return courseList;
    }

    public static CourseInfo getCourseInfo(int id){
        CourseInfo courseInfo = null;
        try{
            PreparedStatement ps = DBTool.getPreparedStatement("select * from t_course WHERE N_COURSE_ID = ?");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            while(rs.next()){
                String name = rs.getString("VC_COURSE_NAME");
                int type = rs.getInt("N_TYPE");
                float credits = rs.getFloat("F_CREDIT");
                int grade = rs.getInt("N_GRADE");
                int major = rs.getInt("N_MAJOR");
                String detail = rs.getString("VC_DETAIL");
                courseInfo = new CourseInfo(id, name, type, credits, grade, major, detail);
            }
            DBTool.close(ps, rs);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return courseInfo;
    }

    public static int editCourse(CourseInfo course){
        int flag;
        try{
            PreparedStatement ps = DBTool.getPreparedStatement("UPDATE t_course SET VC_COURSE_NAME = ?,"+
                    " N_TYPE = ?, F_CREDIT = ?, N_GRADE = ?, N_MAJOR = ?, VC_DETAIL = ? WHERE N_COURSE_ID = ?");
            ps.setString(1, course.getName());
            ps.setInt(2, course.getType());
            ps.setFloat(3, course.getCredit());
            ps.setInt(4, course.getGrade());
            ps.setInt(5, course.getMajor());
            ps.setString(6, course.getDetail());
            ps.setInt(7, course.getId());
            flag = ps.executeUpdate();
            DBTool.close(ps);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return flag;
    }

    public static void insertCourse(CourseInfo course){
        try{
            String sql = "INSERT INTO t_course (VC_COURSE_NAME, N_TYPE, F_CREDIT, N_GRADE, N_MAJOR, VC_DETAIL) VALUES(?, ?, ?, ?, ?, ?)";
            PreparedStatement ps = DBTool.getPreparedStatement(sql);
            ps.setString(1, course.getName());
            ps.setInt(2, course.getType());
            ps.setFloat(3, course.getCredit());
            ps.setInt(4, course.getGrade());
            ps.setInt(5, course.getMajor());
            ps.setString(6, course.getDetail());
            ps.executeUpdate();
            DBTool.close(ps);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void deleteCourse(int id){
        try{
            String sql = "DELETE FROM t_course WHERE N_COURSE_ID = ?";
            PreparedStatement ps = DBTool.getPreparedStatement(sql);
            ps.setInt(1, id);
            ps.executeUpdate();
            DBTool.close(ps);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
