package cn.hmsoft.ltc.dao;

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

import cn.hmsoft.ltc.common.util.DbUtil;
import cn.hmsoft.ltc.model.CourseQuestion;
import cn.hmsoft.ltc.model.QuestionType;

/**
 * 课程题型映射.
 * @author zq
 *
 */
public class CourseQuestionDao {

	public void insertCourseQuestion(CourseQuestion cq) {
		DbUtil util = new DbUtil();
		util.insert("insert into course_question(courseId,quesTypeId,score,quesNums,ord) "
				+ "values(" + cq.getCourseId() + ", "
					   + cq.getQuesTypeId() + ", "
				       + cq.getScore() + ", "
				       + cq.getQuesNums() + ", "
				       + cq.getOrd() + ")");
	}
	
	public List<CourseQuestion> findCourseQuestion(Integer courseId) throws SQLException {
		DbUtil util = new DbUtil();
		List<CourseQuestion> list = new ArrayList<CourseQuestion>();
		ResultSet rs = util.query("select cq.id,cq.courseId,cq.quesTypeId,cq.score,cq.quesNums,qt.code quesTypeCode, qt.name quesTypeName,cq.ord,qt.guide_text from course_question cq,question_type qt where cq.quesTypeId=qt.id and cq.courseId=" + courseId + " order by cq.ord asc");
		while (rs.next()) {
			CourseQuestion cq = new CourseQuestion();
			cq.setId(rs.getInt(1));
			cq.setCourseId(rs.getInt(2));
			cq.setQuesTypeId(rs.getInt(3));
			cq.setScore(rs.getInt(4));
			cq.setQuesNums(rs.getInt(5));
			cq.setQuesTypeCode(rs.getString(6));
			cq.setQuesTypeName(rs.getString(7));
			cq.setOrd(rs.getInt(8));
			cq.setQuideText(rs.getString(9));
			list.add(cq);
		}
		return list;
	}

	public void deleteCourseQuestion(Integer courseId) {
		DbUtil util = new DbUtil();
		util.delete("delete from course_question where courseId=" + courseId);
	}

	/**
	 * 构造题目类型.
	 * @param courseId
	 * @return
	 * @throws SQLException
	 */
	public List<QuestionType> findQuestionTypeList(Integer courseId) throws SQLException {
		DbUtil util = new DbUtil();
		List<QuestionType> list = new ArrayList<QuestionType>();
		StringBuilder sql = new StringBuilder();
		sql.append("select qt.id,qt.code,qt.name,cq.score,cq.quesNums,cq.ord from course_question cq,question_type qt");
		sql.append(" where cq.quesTypeId=qt.id and cq.courseId=").append(courseId);
		sql.append(" order by cq.ord asc");
		ResultSet rs = util.query(sql.toString());
		while (rs.next()) {
			QuestionType qt = new QuestionType();
			qt.setId(rs.getInt(1));
			qt.setCode(rs.getString(2));
			qt.setName(rs.getString(3));
			qt.setDefaultScore(rs.getInt(4));
			qt.getQuesNumsTx().setText(rs.getString(5));//小题量
			qt.setSubTotal(rs.getInt(5) * rs.getInt(4));
			qt.getTypeScoreTx().setText(String.valueOf(rs.getInt(4)));
			list.add(qt);
		}
		return list;
	}

	public List<CourseQuestion> findCourseQuestionByType(Integer courseId, Integer typeId) throws SQLException {
		DbUtil util = new DbUtil();
		List<CourseQuestion> list = new ArrayList<CourseQuestion>();
		ResultSet rs = util.query("select cq.id,cq.courseId,cq.quesTypeId,cq.score,cq.quesNums,qt.code quesTypeCode, qt.name quesTypeName,cq.ord from course_question cq,question_type qt where cq.quesTypeId=qt.id and cq.courseId=" + courseId + " and quesTypeId=" + typeId + " order by cq.ord asc");
		while (rs.next()) {
			CourseQuestion cq = new CourseQuestion();
			cq.setId(rs.getInt(1));
			cq.setCourseId(rs.getInt(2));
			cq.setQuesTypeId(rs.getInt(3));
			cq.setScore(rs.getInt(4));
			cq.setQuesNums(rs.getInt(5));
			cq.setQuesTypeCode(rs.getString(6));
			cq.setQuesTypeName(rs.getString(7));
			cq.setOrd(rs.getInt(8));
			list.add(cq);
		}
		return list;
	}
}
