package com.qmth.qrzk.repository.pager.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qmth.qrzk.repository.common.base.BaseDao;
import com.qmth.qrzk.repository.pager.model.PaperDifficultyEstimateDetail;

@Service
public class PaperDifficultyEstimateService {
	
	@Autowired
    private BaseDao dao;
	
	public List<PaperDifficultyEstimateDetail> getByPaperId(int paperId){
		/**
		List<List<PaperDifficultyEstimateDetail>> result = new ArrayList<List<PaperDifficultyEstimateDetail>>();
		
		List<PaperDifficultyEstimateDetail> details = dao.getList("getPaperDifficultyEstimateByPaperId", paperId);
		
		String questionType = "";
		for(PaperDifficultyEstimateDetail detail : details){
			
			if(detail.getQuestionType().equalsIgnoreCase(questionType)){
				List<PaperDifficultyEstimateDetail> list = result.get(result.size()-1);
				list.add(detail);
			}else{
				questionType = detail.getQuestionType();
				List<PaperDifficultyEstimateDetail> list = new ArrayList<PaperDifficultyEstimateDetail>();
				list.add(detail);
				result.add(list);
			}
			
		}
			
		return result;
		**/
		
		return dao.getList("getPaperDifficultyEstimateByPaperId", paperId);
	}
	
	/**
	 * 查询试卷的预估难度表，用于对外访问：自考成绩分析，接口人：舒东辉。
	 * @param paperCode
	 * @return
	 */
	public List<Map<String,Object>> getByPaperNo(String paperCode){
		List<PaperDifficultyEstimateDetail> pdeList = dao.getList("getPaperDifficultyEstimateByPaperCode", paperCode);
		List<Map<String,Object>> details = new ArrayList<Map<String,Object>>(pdeList.size());
		for(PaperDifficultyEstimateDetail pde:pdeList){
			Map<String,Object> detail = new HashMap<String, Object>();
			detail.put("questionType", pde.getQuestionType());
			detail.put("sortNo", pde.getSortNo());
			detail.put("questionScore", pde.getQuestionScore());
			detail.put("estimateDifficultyValue", pde.getEstimateDifficultyValue());
			detail.put("estimateDifficultyName", pde.getEstimateDifficultyName());
			details.add(detail);
		}
		return details;
	}
	
	public void createDifficultyEstimate(int userId, int paperId){
		//先清除试卷下的预估表
		deleteEstimateDifficultyByPaperId(paperId);
		
		List<PaperDifficultyEstimateDetail> estimates = dao.getList("queryPaperDetailInfo", paperId);
		for(int i=0; i < estimates.size(); i++){
			PaperDifficultyEstimateDetail item = estimates.get(i);
			item.setSortNo(i+1);
			item.setCreatedBy(userId);
			item.setUpdatedBy(userId);
		}
		if(estimates.size() > 0){
			dao.insert("insertDifficultyEstimates", estimates);
		}
	}

	public void updateEstimateDifficultyValue(List<PaperDifficultyEstimateDetail> list){
		if(list != null){
			for(PaperDifficultyEstimateDetail d:list){
				dao.update("updateEstimateDifficultyValue", d);
			}
		}
	}
	
	public void updatePaperDifficultyEstimateByQuestion(PaperDifficultyEstimateDetail paperDifficultyEstimateDetail){
		if(paperDifficultyEstimateDetail != null){
			dao.update("updatePaperDifficultyEstimateByQuestion", paperDifficultyEstimateDetail);
		}
	}
	
	public void deleteEstimateDifficultyByPaperId(int paperId){
		dao.delete("deleteEstimateDifficultyByPaperId", paperId);
	}
	
	/**
	 * <p>校验及格分数预估表是否需要重置: </p>
	 * @author songyue
	 * @date 2016-05-31
	 */
	public boolean checkPaperDifficultyEstimate(int paperId){
		Integer count = dao.get("checkPaperDifficultyEstimate", paperId);
		if(count > 0){
			return true;
		}else{
			return false;
		}
	}
}
