package com.zcloud.service.questionnaire.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.dsno3.questionnaire.QuestionitemMapper;
import com.zcloud.service.questionnaire.QuestionitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 说明：习题管理
 * 作者：luoxiaobao
 * 时间：2021-07-03
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class QuestionitemServiceImpl implements QuestionitemService{

	@Autowired
	private QuestionitemMapper questionitemMapper;

	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		questionitemMapper.save(pd);
	}

	/**
	 * 获取选项及选择次数
	 * @param pd
	 * @return
	 */
	public List<PageData> listItemAndFrequency(PageData pd) {
		return questionitemMapper.listItemAndFrequency(pd);
	}

//	/**新增
//	 * @param pd
//	 * @throws Exception
//	 */
//	public void surveysave(PageData pd)throws Exception{
//		SurveyquestionMapper.surveysave(pd);
//	}

	/**删除
	 * @param pd
	 * @throws Exception
	 */
	public void delete(PageData pd)throws Exception{
		questionitemMapper.delete(pd);
	}

	/**修改
	 * @param pd
	 * @throws Exception
	 */
	public void edit(PageData pd)throws Exception{
		questionitemMapper.edit(pd);
	}
	public void deleteitem(PageData pd)throws Exception{
		questionitemMapper.deleteitem(pd);
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	public List<PageData> list(Page page)throws Exception{
		return questionitemMapper.datalistPage(page);
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	public List<PageData> surveylist(Page page)throws Exception{
		return questionitemMapper.surveydatalistPage(page);
	}

	/**列表(全部)
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> listAll(PageData pd)throws Exception{
		return questionitemMapper.listAll(pd);
	}

	/**通过id获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findById(PageData pd)throws Exception{
		return questionitemMapper.findById(pd);
	}

	/**批量删除
	 * @param pd
	 * @throws Exception
	 */
	public void deleteAll(PageData pd)throws Exception{
		questionitemMapper.deleteAll(pd);
	}

	/**列表(根据所选ID获取数据)
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> findByIds(PageData pd) throws Exception {
		return questionitemMapper.findByIds(pd);
	}

	/**批量删除(根据所选课件ids删除习题)
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public void deleteByCoursewareIds(PageData pd) throws Exception {
		questionitemMapper.deleteByCoursewareIds(pd);
	}

	/**列表(根据课程所选课件获取习题数据)
	 * @param pd CURRICULUMID	课件ID
	 * @throws Exception
	 */
	@Override
	public List<PageData> listByCourseware(PageData pd) throws Exception {
		return questionitemMapper.listByCourseware(pd);
	}

	/**列表(按照试卷试题数随机抽取课程习题)
	 * @param pd CURRICULUMID	课件ID
	 * @throws Exception
	 */
	@Override
	public List<PageData> listByQuestionNum(PageData pd) throws Exception {
		int QUESTIONNUM = Integer.parseInt(pd.getString("QUESTIONNUM"));
		List<PageData> qlist = this.listByCourseware(pd);	//根据课程所选课件获取习题数据
		List<Integer> tempList = new ArrayList<Integer>();
		List<PageData> questions = new ArrayList<PageData>();
		if (QUESTIONNUM > qlist.size()) {
			System.out.println("课程习题总数少于考试试题数,无法生成试卷试题");
			return questions;
		}
		for (int i = 0; i < QUESTIONNUM; i++) {
			int random = new Random().nextInt(qlist.size());
			if (!tempList.contains(random)) {
				tempList.add(random);
				questions.add(qlist.get(random));
			} else {
				i--;
			}
		}
		System.out.println("抽取的习题索引值：" + tempList.toString());
		return questions;
	}

	/**
	 * 按照试卷试题数及试卷所选课件范围随机抽取试题
	 * @param pd CURRICULUMID	课件ID
	 * @return
	 */
	@Override
	public List<PageData> findForStageByQuestionNum(PageData pd) {
		int QUESTIONNUM = Integer.parseInt(pd.getString("QUESTIONNUM"));
		pd.put("COURSEWARE_IDS",pd.getString("COURSEWARES").split(","));
		List<PageData> qlist = questionitemMapper.findForStageByQuestionNum(pd);	//根据阶段考试所选课件获取习题数据
		List<Integer> tempList = new ArrayList<Integer>();
		List<PageData> questions = new ArrayList<PageData>();
		if (QUESTIONNUM > qlist.size()) {
			System.out.println("课程习题总数少于考试试题数,无法生成试卷试题");
			return questions;
		}
		for (int i = 0; i < QUESTIONNUM; i++) {
			int random = new Random().nextInt(qlist.size());
			if (!tempList.contains(random)) {
				tempList.add(random);
				questions.add(qlist.get(random));
			} else {
				i--;
			}
		}
		System.out.println("抽取的阶段考试试题索引值：" + tempList.toString());
		return questions;
	}

	/**列表(根据课程所选课件获取习题数据)
	 * @param pd CURRICULUMID	课件ID
	 * @throws Exception
	 */
	@Override
	public PageData countByCourseware(PageData pd) throws Exception {
		Integer size = questionitemMapper.countByCourseware(pd);
		pd.put("DATACOUNT", size);
		return pd;
	}

	/**列表(根据阶段考试所选课件范围获取习题数据)
	 * @param pd COURSEWARES	课件ID+课件分类
	 * @throws Exception
	 */
	@Override
	public PageData countByStageExamCourseware(PageData pd) throws Exception {
		Integer size = questionitemMapper.countByStageExamCourseware(pd);
		pd.put("DATACOUNT", size);
		return pd;
	}
	/**列表(全部) 根据类型获取习题数据  -- 准备随机选题
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> listAllByType (PageData pd)throws Exception{
		return questionitemMapper.listAllByType(pd);
	}

	public Integer hasQuestionNumber (PageData pd) throws Exception{
		return  questionitemMapper.hasQuestionNumber(pd);
	}
	/**
	 * 获取最大题号，导入时候使用
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public Integer maxQuestionNumber (PageData pd) throws Exception{
		return  questionitemMapper.maxQuestionNumber(pd);
	}
	/**
	 *获取
	 * 该企业下的
	 * 该培训行业类型
	 * 该岗位培训类型
	 * 下的习题个数
	 * @param pd
	 * @return
	 */
	public List<PageData> getCountByQuestionType(PageData pd) throws Exception{
		return questionitemMapper.getCountByQuestionType(pd);
	}

	public List<PageData> findByQuestionId(PageData pd)throws Exception{
		return questionitemMapper.findByQuestionId(pd);
	}

//	public List<PageData> findBySurveyId(PageData pd)throws Exception{
//		return questionitemMapper.findBySurveyId(pd);
//	}
}

