package com.zcloud.service.curriculum.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.curriculum.CoursepapersMapper;
import com.zcloud.service.courseware.QuestionService;
import com.zcloud.service.curriculum.CoursemiddleService;
import com.zcloud.service.curriculum.CoursepapersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 说明：课程试卷管理
 * 作者：luoxiaobao
 * 时间：2021-07-03
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class CoursepapersServiceImpl implements CoursepapersService {

	@Autowired
	private CoursepapersMapper coursepapersMapper;
	@Autowired
	private CoursemiddleService coursemiddleService;
	@Autowired
	private QuestionService questionService;

	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		coursepapersMapper.save(pd);
		//创建试卷试题（按照试卷试题数随机抽取课程习题）
		int QUESTIONSCORE = 100 / Integer.parseInt(pd.getString("QUESTIONNUM"));
		List<PageData> questions = questionService.listByQuestionNum(pd);
		pd.put("QUESTIONSCORE", QUESTIONSCORE);
		//保存抽取后的试题数据
		coursemiddleService.saveAll(pd, questions);
	}

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

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

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

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

	/**通过id获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findById(PageData pd)throws Exception{
		PageData data = coursepapersMapper.findById(pd);
		List<PageData> questionList = coursemiddleService.findByExamPaper(pd);
		data.put("QUESTIONLIST", questionList);
		return data;
	}

	/**通过课程ID获取数据
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public PageData findByCurriculumExam(PageData pd) throws Exception {
		PageData data = coursepapersMapper.findByCurriculum(pd);
		pd.put("COURSEPAPERS_ID", data.getString("COURSEPAPERS_ID"));
		List<PageData> questionList = coursemiddleService.findByExamPaper(pd);
		Object[] ANSWER = new Object[questionList.size()];
		String[] QUESTION_ID = new String[questionList.size()];
		String[] QUESTIONTYPE = new String[questionList.size()];
		String[] COURSEWAREID = new String[questionList.size()];
		String[] COURSEWARETYPE = new String[questionList.size()];
		String[] ANSWERRIGHT = 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");
			COURSEWAREID[i] = questionList.get(i).getString("COURSEWAREID");
			COURSEWARETYPE[i] = questionList.get(i).getString("COURSEWARETYPE");
			ANSWERRIGHT[i] = questionList.get(i).getString("ANSWER");
		}
		data.put("QUESTIONSCORE", questionList.get(0).get("QUESTIONSCORE").toString());
		data.put("ANSWER", ANSWER);
		data.put("QUESTION_ID", QUESTION_ID);
		data.put("QUESTIONTYPE", QUESTIONTYPE);
		data.put("COURSEWAREID", COURSEWAREID);
		data.put("COURSEWARETYPE", COURSEWARETYPE);
		data.put("ANSWERRIGHT", ANSWERRIGHT);
		data.put("QUESTIONLIST", questionList);
		return data;
	}

	/**检查考试信息是否齐全（通过课程ID获取数据）
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public PageData checkCurriculumExam(PageData pd) throws Exception {
		PageData data = coursepapersMapper.findByCurriculum(pd);
		if (data!=null) {
			pd.put("COURSEPAPERS_ID", data.getString("COURSEPAPERS_ID"));
			List<PageData> questionList = coursemiddleService.findByExamPaper(pd);
			data.put("QUESTIONLIST", questionList);
		}
		return data;
	}

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

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

	/**删除(根据课程ID删除试题数据)
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public void deleteByCurriculumIds(PageData pd)throws Exception {
		coursepapersMapper.deleteByCurriculumIds(pd);
	}
}

