package cn.hmsoft.ltc.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.stereotype.Component;

import cn.hmsoft.ltc.common.constants.LtContext;
import cn.hmsoft.ltc.dao.CourseDao;
import cn.hmsoft.ltc.dao.UsersDao;
import cn.hmsoft.ltc.model.Course;
import cn.hmsoft.ltc.model.Users;
import cn.hmsoft.ltc.service.iface.CourseService;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
 * 课程管理接口实现类
 * @author Administrator
 *
 */
@Component
public class CourseServiceImpl implements CourseService { 
	
	private CourseDao courseDao = new CourseDao();
	
	private UsersDao usersDao = new UsersDao();

	@Override
	public Integer insertCourse(Course course) throws SQLException {
		course.setVerificationTextbook(0);
		this.courseDao.insertCourse(course);
		return this.courseDao.getCourseById(course.getCode());
	}

	@Override
	public int selectCourse(String courseCode) throws SQLException {
		int count = courseDao.selectCourse(courseCode);
		return count;
	}

	@Override
	public boolean checkCode(String courseCode) throws SQLException {
		boolean flag = this.courseDao.checkCode(courseCode);
		return flag;
	}

	@Override
	public ObservableList<Course> selectCourse(Integer courseId, String schoolName) throws SQLException {
		final ObservableList<Course> dataList = FXCollections.observableArrayList();
		List<Course> courseList = this.courseDao.selectCourseBySchool(courseId, schoolName);
		dataList.addAll(courseList);
		return dataList;
	}

	@Override
	public boolean deleteCourse(int id) throws SQLException {
		boolean flag = this.courseDao.deleteCourse(id);
		return flag;
	}

	@Override
	public ObservableList<Course> selectCourseComBobox() throws SQLException {
		final ObservableList<Course> dataList = FXCollections.observableArrayList();
		List<Course> courseList = this.courseDao.selectCourseBySchool(null, LtContext.LOGIN_SCHOOL);
		dataList.addAll(courseList);
		return dataList;
	}

	@Override
	public Course selectCourseById(Integer id) throws SQLException {
		return courseDao.selectCourseById(id);
	}
	
	@Override
	public ObservableList<Course> selectThesisCourseSelectBox() throws SQLException {
		final ObservableList<Course> dataList = FXCollections.observableArrayList();
		List<Course> courseList = this.courseDao.selectCourseBySchool();
		dataList.addAll(courseList);
		return dataList;
	}
	
	@Override
	public ObservableList<Course> selectSubjectCourseComBobox() throws SQLException {
		//通过老师选择的命题课程查询当前的课程信息
		Users user = this.usersDao.selectUserById(LtContext.LOGIN_USER_ID);
		final ObservableList<Course> dataList = FXCollections.observableArrayList();
		Course course = this.courseDao.selectCourseById(user.getCourseId());
		String strName = course.getCode() + "_" + course.getName();
		course.setStrName(strName);
		dataList.add(course);
		
		return dataList;
	}

	@Override
	public Course updateCourseById(Integer id, Integer chapterNumber) throws SQLException {
		this.courseDao.updateCourseByCode(id, chapterNumber);
		Course course = courseDao.selectCourseById(id);
		return course;
	}

	@Override
	public void updateCourseById(Integer id) throws SQLException {
		this.courseDao.updateCourseById(id);
	}

}
