package cn.hmsoft.ltc.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import cn.hmsoft.ltc.common.util.DbUtil;
import cn.hmsoft.ltc.model.QuestionType;

/**
 * 题型管理
 * @author Administrator
 *
 */
public class QuestionTypeDao {

	/**
	 * 新增题型
	 * @param user
	 */
	public void insertQuestionType(QuestionType questionType) {
		DbUtil util=new DbUtil();
		util.insert("insert into question_type(code,name,correct_options_num,status_code,default_score,edite_status,guide_text) "
				+ "values('" + questionType.getCode() + "', "
					   + "'" + questionType.getName() + "', "
				       + "'" + questionType.getCorrectOptionsNum() + "', "
				       + "'" + questionType.getStatusCode() + "', "
				       + "'" + questionType.getDefaultScore() + "',"
				       + questionType.getEditeStatus() + ", "
					   + "'" + questionType.getGuideText() + "')");
	}
	
	/**
	 *  查询题型
	 * @return
	 * @throws SQLException
	 */
	public List<QuestionType> selectQuestionType() throws SQLException {
		List<QuestionType> questionTypeList = new ArrayList<QuestionType>();
		DbUtil util=new DbUtil();
		StringBuffer strSql = new StringBuffer();
		strSql.append("select id,name,code,correct_options_num,status_code,guide_text,default_score,edite_status from question_type order by ord").toString();
		ResultSet rs = util.query(strSql.toString());
		NumberFormat format = NumberFormat.getInstance();
		while (rs.next()) {
			QuestionType question = new QuestionType();
			question.setId(rs.getInt(1));
			question.setName(rs.getString(2));
			question.setCode(rs.getString(3));
			question.setCorrectOptionsNum(rs.getInt(4));
			question.setStatusCode(rs.getInt(5));
			question.setGuideText(rs.getString(6));
			question.setDefaultScore(rs.getInt(7));
			question.setScoreStr(format.format(question.getDefaultScore()));
			question.setEditeStatus(rs.getInt(8));
			if(question.getCorrectOptionsNum() == 0) {
				question.setCorrectOptionsName("主观题");
			}else if(question.getCorrectOptionsNum() == 1){
				question.setCorrectOptionsName("单选题");
			}else if(question.getCorrectOptionsNum() == 2) {
				question.setCorrectOptionsName("多选题");
			}
			question.setStatusName(question.getStatusCode()==0 ? "停用" : "启用");
			questionTypeList.add(question);
		}
		return questionTypeList;
	}
	
	/**
	 * 根据ID删除题型信息
	 * @param id
	 * @return true 成功，false 失败
	 * @throws SQLException
	 */
	public boolean deleteQuestionType(int id)throws SQLException{
		boolean flag = false;
		DbUtil util=new DbUtil();
		StringBuffer strSql = new StringBuffer();
		strSql.append("delete from question_type where id = " + id);
		int count = util.delete(strSql.toString());
		if(count>0) {
			flag = true;; //成功
		}
		return flag;
	}
	
	/**
	 *  查询题型
	 * @return
	 * @throws SQLException
	 */
	public QuestionType selectQuestionTypeById(Integer id) throws SQLException {
		DbUtil util = new DbUtil();
		StringBuffer strSql = new StringBuffer();
		strSql.append("select id,name,code,correct_options_num,status_code,guide_text,default_score,edite_status from question_type where id = " + id).toString();
		ResultSet rs = util.query(strSql.toString());
		while (rs.next()) {
			QuestionType question = new QuestionType();
			question.setId(rs.getInt(1));
			question.setName(rs.getString(2));
			question.setCode(rs.getString(3));
			question.setCorrectOptionsNum(rs.getInt(4));
			question.setStatusCode(rs.getInt(5));
			question.setGuideText(rs.getString(6));
			question.setDefaultScore(rs.getInt(7));
			question.setEditeStatus(rs.getInt(8));
			return question;	
		}
		return null;
	}
}
