package com.zcloud.service.task.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.task.StageExamMapper;
import com.zcloud.service.paper.StageexampaperService;
import com.zcloud.service.task.StageExamRecordService;
import com.zcloud.service.task.StageExamService;
import com.zcloud.service.task.StageStudentRelationService;
import com.zcloud.service.task.StudyTaskService;
import com.zcloud.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 说明：阶段考试
 * 作者：luoxiaobao
 * 时间：2021-09-15
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class StageExamServiceImpl implements StageExamService {

	@Autowired
	private StageExamMapper stageexamMapper;
	@Autowired
	private StageExamRecordService stageExamRecordService;
	@Autowired
	private StageStudentRelationService stageStudentRelationService;
	@Autowired
	private StudyTaskService studyTaskService;
	@Autowired
	private StageexampaperService stageexampaperService;
	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		stageexamMapper.save(pd);
	}

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

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

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

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

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

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

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

	/**提交试卷
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public PageData submit(PageData pd) throws Exception {
		String[] QUESTION_ID = pd.getString("QUESTION_ID").split(",");
		String[] QUESTIONTYPE = pd.getString("QUESTIONTYPE").split(",");
		String[] QUESTIONSCORE = pd.getString("QUESTIONSCORE").split(",");
		String[] COURSEWAREID = pd.getString("COURSEWAREID").split(",");
		String[] COURSEWARETYPE = pd.getString("COURSEWARETYPE").split(",");
		String[] ANSWERRIGHT = pd.getString("ANSWERRIGHT").split(",");
		String[] ANSWER = pd.getString("ANSWER").split(",");
		int EXAMQUESTIONRIGHT = 0;
		int EXAMQUESTIONWRONG = 0;
//		int EXAMSCORE = EXAMQUESTIONRIGHT * Integer.parseInt(pd.getString("QUESTIONSCORE"));
		int EXAMSCORE = 0;
		//记录考试答题记录
		for (int i = 0; i < QUESTION_ID.length; i++) {
			PageData r = new PageData();
			r.put("STAGEEXAMRECORD_ID", UuidUtil.get32UUID());
			r.put("CURRICULUM_ID", pd.getString("CURRICULUMID"));
			r.put("STAGEEXAMPAPER_ID", pd.getString("STAGEEXAMPAPER_ID"));
			r.put("STAGEEXAM_ID", pd.getString("STAGEEXAM_ID"));
			r.put("STUDYTASK_ID", pd.getString("STUDYTASK_ID"));
			r.put("COURSEWARE_ID", COURSEWAREID[i]);
			r.put("COURSEWARETYPE", COURSEWARETYPE[i]);
			r.put("QUESTION_ID", QUESTION_ID[i]);
			r.put("USER_ID", pd.getString("USER_ID"));
			r.put("ANSWER", ANSWER[i]);
			r.put("ANSWERRIGHT", ANSWERRIGHT[i]);
			if (ANSWER[i].equals(ANSWERRIGHT[i])) {
				EXAMSCORE += Integer.parseInt(QUESTIONSCORE[i]);
				EXAMQUESTIONRIGHT++;
			} else {
				EXAMQUESTIONWRONG++;
			}
			r.put("CREATOR", pd.getString("CREATOR"));	//添加人
			r.put("CREATTIME", pd.getString("CREATTIME"));	//添加时间
			r.put("OPERATOR", pd.getString("OPERATOR"));	//修改人
			r.put("OPERATTIME", pd.getString("OPERATTIME"));	//修改时间
			r.put("ISDELETE", "0");	//是否删除(0:有效 1：删除)
			r.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));	//企业ID
			stageExamRecordService.save(r);
		}

		//保存考试信息
//		PageData update = this.findById(pd);
		pd.put("CURRICULUM_ID", pd.getString("CURRICULUMID"));
		pd.put("EXAMQUESTIONNUM", EXAMQUESTIONRIGHT+EXAMQUESTIONWRONG+"");
		pd.put("EXAMQUESTIONRIGHT", EXAMQUESTIONRIGHT+"");
		pd.put("EXAMQUESTIONWRONG", EXAMQUESTIONWRONG+"");
//		pd.put("STUDYTASK_ID", pd.get("STUDYTASK_ID"));
		pd.put("EXAMSCORE", EXAMSCORE+"");
		this.save(pd);

		//更新学员考试成绩
		PageData student = new PageData();
		student.put("EXAMTYPE", "3");	//阶段考试得分
		student.put("STAGEEXAMSCORE", EXAMSCORE+"");	//考试得分
		PageData paper =  stageexampaperService.findById(pd);
		List<PageData> stageexam =  stageexampaperService.findByTaskId(pd);  //查询考了几次

		if(Integer.parseInt(paper.getString("PASSSCORE"))>EXAMSCORE){ //及格分大于考试分还可以考试
			if(Integer.parseInt(paper.get("NUMBER_OF_EXAMINATIONS").toString()) > stageexam.size()){  //如果考试次数大于考的次数
				student.put("STAGEEXAMSTATE", '2');	//考试状态
			} else if(paper.get("NUMBER_OF_EXAMINATIONS").toString().equals("-1")){  //如果是无限次考试
				student.put("STAGEEXAMSTATE", '2');	//考试状态
			} else {
				student.put("STAGEEXAMSTATE", '3');	//考试状态
			}
		} else {
			student.put("STAGEEXAMSTATE", '3');	//考试状态
		}
		student.put("OPERATOR", pd.getString("OPERATOR"));	//修改人
		student.put("OPERATTIME", pd.getString("OPERATTIME"));	//修改时间
		student.put("STAGESTUDENTRELATION_ID", pd.getString("STAGESTUDENTRELATION_ID"));
		stageStudentRelationService.updateStudentScore(student);
//		PageData task = new PageData();
//		task.put("STUDYTASK_ID", pd.getString("STUDYTASK_ID"));
//		task.put("OPERATOR", pd.getString("OPERATOR"));	//修改人
//		task.put("OPERATTIME", pd.getString("OPERATTIME"));	//修改时间
//		task.put("STATE",2);
//		studyTaskService.updateTaskState(task);
		return pd;
	}

	/**试卷答题展示
	 * @param
	 * @throws Exception
	 */
	@Override
	public PageData findResult(PageData pd) throws Exception {
		PageData data = stageexamMapper.findByPaperUser(pd); // 考试成绩 STAGEEXAM_ID
		pd.put("STAGEEXAM_ID", data.getString("STAGEEXAM_ID"));
		List<PageData> questionList = stageExamRecordService.findByExam(pd); // 考试详情 STAGEEXAM_ID
		PageData studyTaskData = studyTaskService.findById(data); //任务
		PageData stageexampaperData = stageexampaperService.getDataById(data); //试卷
		data.put("studyTaskData", studyTaskData);
		data.put("stageexampaperData", stageexampaperData);
		data.put("QUESTIONLIST", questionList);
		return data;
	}

	/**根据任务id获取全部用户的全部考试成绩
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> getUserExamScoreByStudystaskId(PageData pd)throws Exception{
		return stageexamMapper.getUserExamScoreByStudystaskId(pd);
	}
	/**通过id获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findByPaperUser(PageData pd)throws Exception{
		return stageexamMapper.findByPaperUser(pd);
	}
}

