package com.zcloud.service.onlinexxks.impl;

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

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

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.dsno3.paper.StageexampaperInputMapper;
import com.zcloud.service.onlinexxks.QuestionService;
import com.zcloud.service.onlinexxks.StageExamPaperInputQuestionService;
import com.zcloud.service.onlinexxks.StageexampaperInputService;
import com.zcloud.util.Jurisdiction;

/**
 * 说明：阶段试卷表
 * 作者：luoxiaobao
 * 时间：2021-12-17
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class StageexampaperInputServiceImpl implements StageexampaperInputService {

	@Autowired
	private StageexampaperInputMapper stageexampaperinputMapper;
	@Autowired
	private QuestionService questionService;
	@Autowired
	private StageExamPaperInputQuestionService stageExamPaperInputQuestionService;
	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		stageexampaperinputMapper.save(pd); // 保存试卷表

	}

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

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

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

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

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

	/**批量删除
	 * @param ArrayDATA_IDS
	 * @throws Exception
	 */
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		stageexampaperinputMapper.deleteAll(ArrayDATA_IDS);
	}
	public  List<PageData>  getSaveStage (PageData pd) throws Exception {
		List<PageData> valList = new ArrayList<>();
		for (int i=1; i<=3;i++){
			String xitiType = "";
			Integer QUESTIONSCORE = 0;
			if(1==i){
				xitiType = "DANYUANTICOUNT";
				QUESTIONSCORE =Integer.parseInt(pd.getString("DANXUANTINUMBER"));
			}else if(2==i){
				xitiType = "DUOXUANTICOUNT";
				QUESTIONSCORE =Integer.parseInt(pd.getString("DUOXUANTINUMBER"));
			}else if(3==i){
				xitiType = "PANDUITICOUNT";
				QUESTIONSCORE =Integer.parseInt(pd.getString("PANDUITINUMBER"));
			}
			PageData questionPd = new PageData();
			questionPd.put("TRAINTYPE",pd.getString("TRAINTYPE")); // 培训行业类型
			questionPd.put("POSTTYPE",pd.getString("POSTTYPE")); // 岗位培训类型
			questionPd.put("QUESTIONTYPE",i); //试题类型（单选题、多选题、判断题）
			questionPd.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID()); //企业信息
			List<PageData> questionAllList = questionService.listAllByType(questionPd); // 获取该类型的习题数据
			Integer danxuanNmber = Integer.parseInt(pd.getString(xitiType)); //选择该类型数据的个数

			if(danxuanNmber>questionAllList.size()){
				System.out.println("课程习题总数少于考试试题数,无法生成试卷试题");
				return null;
			}
			List<Integer> tempList = new ArrayList<Integer>(); //索引
			for (int j = 0; j < danxuanNmber; j++) {
				int random = new Random().nextInt(questionAllList.size());
				if (!tempList.contains(random)) {
					tempList.add(random);
					PageData quMap = questionAllList.get(random);
					quMap.put("QUESTIONSCORE",QUESTIONSCORE); //添加分数
					valList.add(quMap);
				} else {
					j--;
				}
			}
		}
		return valList;
	}

	/**修改 状态
	 * @param pd
	 * @throws Exception
	 */
	public void editState(PageData pd)throws Exception{
		stageexampaperinputMapper.editState(pd);
	}
}

