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.constants.SysConst.PaperStageEnum;
import cn.hmsoft.ltc.common.util.DbUtil;
import cn.hmsoft.ltc.model.TestPaper;

/**
 * 试卷管理管理
 * @author zq
 *
 */
public class TestPaperDao {

	/**
	 * 新增试卷信息
	 * @param user
	 */
	public void insertTestPaper(TestPaper testPaper) {
		DbUtil util = new DbUtil();
		util.insert("insert into paper(paperNo,courseId,courseCode,CourseName,remark,status,userId,chapterNumber,keyChapters) "
				+ "values('" + testPaper.getPaperNo() + "', "
				       + testPaper.getCourseId() + ", "
					   + "'" + testPaper.getCourseCode() + "', "
					   + "'" + testPaper.getCourseName() + "', "
					   + "'" + testPaper.getRemark() + "', "
					   + testPaper.getStatus() + ", "
					   + testPaper.getUserId() + ", "
				       + testPaper.getChapterNumber() + ", "
				       + "'" + testPaper.getKeyChapters() + "')");
	}
	
	/**
	 *  根据用户查询试卷信息
	 *  @param userId 用户id
	 * @return
	 * @throws SQLException
	 */
	public List<TestPaper> selectTestPaperByUserId(Integer userId) throws SQLException {
		DbUtil util = new DbUtil();
		List<TestPaper> lstTestPaper = new ArrayList<TestPaper>();
		StringBuffer strSql = new StringBuffer();
		strSql.append("select id,paperNo,courseId,courseCode,CourseName,remark,status,userId,chapterNumber,keyChapters from paper where userId = '" + userId + "'").toString();
		ResultSet rs = util.query(strSql.toString());
		while (rs.next()) {
			TestPaper testPaper = new TestPaper();
			testPaper.setId(rs.getInt(1));
			testPaper.setPaperNo(rs.getString(2));
			testPaper.setCourseId(rs.getInt(3));
			testPaper.setCourseCode(rs.getString(4));
			testPaper.setCourseName(rs.getString(5));
			testPaper.setRemark(rs.getString(6));
			testPaper.setStatus(rs.getInt(7));
			testPaper.setUserId(rs.getInt(8));
			testPaper.setChapterNumber(rs.getInt(9));
			testPaper.setKeyChapters(rs.getString(10));
			testPaper.setStatusName(PaperStageEnum.getCaptionByVal(testPaper.getStatus()));
			testPaper.setPaperName(testPaper.getPaperNo() + "_" + testPaper.getCourseName());
			lstTestPaper.add(testPaper);
		}
		return lstTestPaper;
	}

	/**
	 * 查询试卷信息 
	 * @param paperNo 试卷编号
	 * @return
	 * @throws SQLException
	 */
	public TestPaper selectTestPaperByPageNo(String paperNo) throws SQLException {
		DbUtil util = new DbUtil();
		StringBuffer strSql = new StringBuffer();
		strSql.append("select id,paperNo,courseId,courseCode,CourseName,remark,status,userId,chapterNumber,keyChapters from paper where paperNo = '" + paperNo + "'").toString();
		ResultSet rs = util.query(strSql.toString());
		while (rs.next()) {
			TestPaper testPaper = new TestPaper();
			testPaper.setId(rs.getInt(1));
			testPaper.setPaperNo(rs.getString(2));
			testPaper.setCourseId(rs.getInt(3));
			testPaper.setCourseCode(rs.getString(4));
			testPaper.setCourseName(rs.getString(5));
			testPaper.setRemark(rs.getString(6));
			testPaper.setStatus(rs.getInt(7));
			testPaper.setUserId(rs.getInt(8));
			testPaper.setChapterNumber(rs.getInt(9));
			testPaper.setKeyChapters(rs.getString(10));
			testPaper.setStatusName(PaperStageEnum.getCaptionByVal(testPaper.getStatus()));
			testPaper.setPaperName(testPaper.getPaperNo() + "_" + testPaper.getCourseName());
			return testPaper;
		}
		return null;
	}

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

	public void updateTestPaper(String paperNo, Integer status) {
		DbUtil util = new DbUtil();
		util.update("update paper set status=" + status + " where paperNo='" + paperNo + "'");
	}

	public void updateTestPaperKeyChapter(String paperNo, String keyChapter) {
		DbUtil util = new DbUtil();
		util.update("update paper set keyChapters='" + keyChapter + "' where paperNo='" + paperNo + "'");
	}
	
	/**
	 * 通过课程id修改课程章节数量
	 * @param chapterNumber  课程章节数量
	 * @param courseId      课程id
	 */
	public void updateTestPaperChapterNumber(Integer chapterNumber, Integer courseId) {
		DbUtil util = new DbUtil();
		util.update("update paper set chapterNumber=" + chapterNumber + " where courseId=" + courseId );
	}
}
