package com.zcloud.service.questionnaire.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.dsno2.questionnaire.SurveyquestionMapper;
import com.zcloud.service.questionnaire.QuestionitemService;
import com.zcloud.service.questionnaire.SurveyquestionService;
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 SurveyquestionServiceImpl implements SurveyquestionService {

	@Autowired
	private SurveyquestionMapper surveyquestionMapper;
	@Autowired
	private QuestionitemService questionitemService;


	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		surveyquestionMapper.save(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{
		surveyquestionMapper.delete(pd);
	}

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


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

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

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

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

//	/**
//	 * 通过试卷ID获取数据
//	 * @param pd
//	 * @throws Exception
//	 */
//	@Override
//	public List<PageData> findById(PageData pd) throws Exception {
//		PageData data = surveyquestionMapper.findById(pd);
//		List<PageData> questionList = questionitemService.findById(pd);
//		Object[] ANSWER = new Object[questionList.size()];
//		String[] QUESTION_ID = new String[questionList.size()];
//		String[] QUESTIONTYPE = new String[questionList.size()];
//		String[] QUESTIONDRY = new String[questionList.size()];
//
//		for (int i = 0; i < questionList.size(); i++) {
//			if ("2".equals(questionList.get(i).getString("QUESTIONTYPE"))) {	//多选
//				String[] multipleANSWER = new String[questionList.get(i).getString("ANSWER").length()];
//				for (int j = 0; j < multipleANSWER.length; j++) {
//					multipleANSWER[j] = "";
//				}
//				ANSWER[i] = multipleANSWER;
//			} else {
//				ANSWER[i] = "";
//			}
//			QUESTION_ID[i] = questionList.get(i).getString("QUESTION_ID");
//			QUESTIONTYPE[i] = questionList.get(i).getString("QUESTIONTYPE");
//			QUESTIONDRY[i] = questionList.get(i).getString("QUESTIONDRY");
//		}
//		data.put("ANSWER", ANSWER);
//		data.put("QUESTION_ID", QUESTION_ID);
//		data.put("QUESTIONTYPE", QUESTIONTYPE);
//		data.put("QUESTIONDRY", QUESTIONDRY);
//		data.put("QUESTIONLIST", questionList);
//		return data;
//	}

	public List<PageData> findBySurveyId(PageData pd) {

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

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

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

	/**列表(根据课程所选课件获取习题数据)
	 * @param pd CURRICULUMID	课件ID
	 * @throws Exception
	 */
	@Override
	public List<PageData> listByCourseware(PageData pd) throws Exception {
		return surveyquestionMapper.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 = surveyquestionMapper.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 = surveyquestionMapper.countByCourseware(pd);
		pd.put("DATACOUNT", size);
		return pd;
	}

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

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

