package com.qmth.qrzk.repository.user.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

import com.qmth.qrzk.dao.IQuestionTypeDao;
import com.qmth.qrzk.repository.common.base.BaseDao;
import com.qmth.qrzk.repository.service.query.QuestionTypeQueryParams;
import com.qmth.qrzk.repository.user.model.Course;
import com.qmth.qrzk.repository.user.model.CourseQuestionType;
import com.qmth.qrzk.repository.user.model.ImportBean;
import com.qmth.qrzk.repository.user.model.QuestionType;
import com.qmth.qrzk.repository.user.model.User;
import com.qmth.qrzk.repository.user.service.IQuestionTypeService;

@Service("questionTypeServiceImpl")
public class QuestionTypeServiceImpl implements IQuestionTypeService {

	@Autowired
	@Qualifier("questionTypeDaoImpl")
	private IQuestionTypeDao questionTypeDao;
	
	@Autowired
	private CourseServiceImpl courseService;
	
	@Autowired
	private BaseDao dao;
	@Override
	public List<QuestionType> findNamesByCourseId(List<Integer> ls) {
		return questionTypeDao.findQuestionName(ls);
	}
	
	/**
	 * 根据courseId查出关联表对应的题型集合id
	 */
	@Override
	public List<Integer> findQuestionTypes(int courseId) {
		List<CourseQuestionType> list=questionTypeDao.findCourseQuestionType(courseId);
		List<Integer> ls=new ArrayList<Integer>();
		for(CourseQuestionType cqt:list){
			ls.add(cqt.getQuestionTypeId());
		}
		return ls;
	}

	@Override
	public List<QuestionType> queryQuestionTypes(QuestionTypeQueryParams params) {
		return questionTypeDao.queryQuestionTypes(params);
	}

	@Override
	public int queryQuestionTypesCount(QuestionTypeQueryParams params) {
		return questionTypeDao.queryQuestionTypesCount(params);
	}
	public void delQuestionType(int QuestionId){
		dao.delete("delQuestionTypeById", QuestionId);
	}
	@Override
	public QuestionType queryById(int id) {
		// TODO Auto-generated method stub
		return questionTypeDao.selectById(id);
	}

	@Override
	public List<QuestionType> queryByCourseId(int courseId) {
		return questionTypeDao.queryByCourseId(courseId);
	}

	@Override
	public int addQuestionType(QuestionType questionType) {
		// TODO Auto-generated method stub
		return questionTypeDao.addQuestionType(questionType);
	}

	@Override
	public int updateQuestionType(QuestionType questionType) {
		// TODO Auto-generated method stub
		return questionTypeDao.updateQuestionType(questionType);
	}

	@Override
	public void updateCourseQuestionTypes(int courseId,
			List<CourseQuestionType> list) {
		questionTypeDao.deleteLink(courseId);
		if(list != null && list.size() > 0){
			questionTypeDao.addCourseQuestionType(list);
		}
	}

	@Override
	public int addCourseQuestionType(List<CourseQuestionType> list) {
		// TODO Auto-generated method stub
		return questionTypeDao.addCourseQuestionType(list);
	}

	@Override
	public QuestionType findByName(String name) {
		// TODO Auto-generated method stub
		List<QuestionType> list=questionTypeDao.findByName(name);
		if(list.size()>0){
			return list.get(0);
		}else{
			return null;
		}
	}

	@Override
	public int getCount(CourseQuestionType cq) {
		// TODO Auto-generated method stub
		return questionTypeDao.getCount(cq);
	}

	@Override
	public int addCourseLinkQuestionType(CourseQuestionType cq) {
		// TODO Auto-generated method stub
		return questionTypeDao.addCourseLinkQuestionType(cq);
	}
	
	public void saveImportData(List<ImportBean> list){
		for(ImportBean bean:list){
			Course course = courseService.findByCode(bean.getCourseCode());
			QuestionType questionType=findByName(bean.getQuestionName());
			//因为前面过滤了课程代码是否存在  所以这里不用继续判断
			
			if(questionType!=null){	//表示数据库中存在该题型   存在的话关联题型与课程
				//查找b_course_question_type表 课程和题型是否有关联
				CourseQuestionType cq=new CourseQuestionType();
				cq.setCourseId(course.getId());
				cq.setQuestionTypeId(questionType.getId());
				int count=getCount(cq);
				if(count==0){	//如果没有关联
					addCourseLinkQuestionType(cq);
				}
			}else{		//存在课程信息但不存在题型信息
				//新增题型信息
				questionType=new QuestionType();
				questionType.setCode(bean.getQuestionCode());
				questionType.setName(bean.getQuestionName());
				String name = questionType.getName();
				if(name.equals("单项选择题") || name.equals("单选题") || name.equals("单选")){
					questionType.setCorrectOptionsNum(1);
				}else if(name.equals("多项选择题") || name.equals("多选题") || name.equals("多选")){
					questionType.setCorrectOptionsNum(2);
				}else{
					questionType.setCorrectOptionsNum(0);
				}
				addQuestionType(questionType);	//题型id
				
				//关联课程与题型
				CourseQuestionType cq=new CourseQuestionType();
				cq.setCourseId(course.getId());
				cq.setQuestionTypeId(questionType.getId());
				addCourseLinkQuestionType(cq);
			}
		}
	}
	
	public QuestionType findByNameWithCreate(String name,double score,int courseId,User curUser){
		QuestionType type = this.findByName(name);
		if(type == null){
			type = new QuestionType();
			type.setName(name);
			if(name.equals("单项选择题") || name.equals("单选题") || name.equals("单选")){
				type.setCorrectOptionsNum(1);
			}else if(name.equals("多项选择题") || name.equals("多选题") || name.equals("多选")){
				type.setCorrectOptionsNum(2);
			}else{
				type.setCorrectOptionsNum(0);
			}
			type.setDefaultScore(score);
			type.setCreateTime(new Date());
			type.setCreateBy(curUser.getId());
			type.setRemark("导入试题时自动创建");
			dao.insert("addQuestionType", type);
		}
		CourseQuestionType cqtTmp = new CourseQuestionType();
		cqtTmp.setCourseId(courseId);
		cqtTmp.setQuestionTypeId(type.getId());
		CourseQuestionType cqt = dao.getObject("getCourseQuestionTypeByTypeIdAndCourseId", cqtTmp);
		if(cqt == null){
			dao.insert("addCourseLinkQuestionType", cqtTmp);
		}
		return type;
	}
}
