package cn.hmsoft.ltc.service.impl;

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

import org.springframework.stereotype.Component;

import cn.hmsoft.ltc.dao.QuestionTypeDao;
import cn.hmsoft.ltc.dao.SubjectDao;
import cn.hmsoft.ltc.model.QuestionType;
import cn.hmsoft.ltc.model.Subject;
import cn.hmsoft.ltc.service.iface.SubjectService;
import javafx.collections.ObservableList;

/**
 * 题目管理service实现类
 * 
 * @author xuwenjin
 * @date 2019-03-27
 *
 */
@Component
public class SubjectServiceImpl implements SubjectService {

	private QuestionTypeDao questionTypeDao = new QuestionTypeDao();
	private SubjectDao subjectDao = new SubjectDao();

	@Override
	public Integer insertSubject(Subject subject) throws SQLException {
		return subjectDao.insertSubject(subject);
	}

	@Override
	public void deleteSubject(Subject subject) throws SQLException {
		subjectDao.deleteSubject(subject.getSubjectId());
	}

	@Override
	public void updateSubject(Subject subject) throws SQLException {
		subjectDao.updateSubject(subject);

	}

	public void updateSubjectTinyScoreFlag(String paperNo, Integer typeId) {
		subjectDao.updateSubjectByParam(paperNo, typeId);
	}
	
	public int findSubjectScoreCount(String paperNo, Integer typeId) throws SQLException {
		return this.subjectDao.findSubjectScoreCount(paperNo, typeId);
	}
	
	@Override
	public ObservableList<Subject> selectSubject(Subject subject) throws SQLException {
		Map<Integer, QuestionType> map = getQuestionTypeMap();
		ObservableList<Subject> list = subjectDao.selectSubject(subject);
		int i=1;
		for (Subject sub : list) {
			sub.setIndex(i);
			sub.setQuestionTypeName(map.get(sub.getQuestionTypeId()) != null ? map.get(sub.getQuestionTypeId()).getName() : "");
			i++;
		}
		return list;
	}

	public Map<Integer, QuestionType> getQuestionTypeMap() {
		Map<Integer, QuestionType> map = new HashMap<Integer, QuestionType>();
		try {
			List<QuestionType> questionTypeList = this.questionTypeDao.selectQuestionType();
			for (QuestionType qt : questionTypeList) {
				map.put(qt.getId(), qt);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return map;
	}
	
	public List<Subject> findSubjectList(String testPaperId, Integer typeId) throws SQLException {
		return this.subjectDao.findSubjectList(testPaperId, typeId);
	}

	@Override
	public void deleteSubjectByPageNo(String testPaperId, Integer typeId) throws SQLException {
		this.subjectDao.deleteSubjectByPageNo(testPaperId, typeId);
	}

	@Override
	public Map<String, Integer> findSubjectChapterDist(String paperNo) throws SQLException {
		return this.subjectDao.findSubjectChapterDist(paperNo);
	}
	
	@Override
	public ObservableList<Subject> selectEstimatedDifficulty(Subject subject) throws SQLException {
		Map<Integer, QuestionType> map = getQuestionTypeMap();
		ObservableList<Subject> list = subjectDao.selectEstimatedDifficulty(subject);
		int i=1;
		for (Subject sub : list) {
			sub.setIndex(i);
			sub.setQuestionTypeName(map.get(sub.getQuestionTypeId()) != null ? map.get(sub.getQuestionTypeId()).getName() : "");
			i++;
		}
		return list;
	}
}
