package com.zcloud.service.task.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.task.CourseExamMapper;
import com.zcloud.service.curriculum.CourseStudentRelationService;
import com.zcloud.service.curriculum.CoursemiddleService;
import com.zcloud.service.task.CourseExamRecordService;
import com.zcloud.service.task.CourseExamService;
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-13
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class CourseExamServiceImpl implements CourseExamService {

	@Autowired
	private CourseExamMapper courseexamMapper;
	@Autowired
	private CourseExamRecordService courseExamRecordService;
	@Autowired
	private CourseStudentRelationService courseStudentRelationService;
	@Autowired
	private CoursemiddleService coursemiddleService;

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

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

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

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

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

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

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

	/**列表(根据所选ID获取数据)
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> findByIds(PageData pd) throws Exception {
		return courseexamMapper.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[] 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;
		//记录考试答题记录
		for (int i = 0; i < QUESTION_ID.length; i++) {
			PageData r = new PageData();
			r.put("COURSEEXAMRECORD_ID", UuidUtil.get32UUID());
			r.put("CURRICULUM_ID", pd.getString("CURRICULUMID"));
			r.put("COURSEPAPERS_ID", pd.getString("COURSEPAPERS_ID"));
			r.put("COURSEEXAM_ID", pd.getString("COURSEEXAM_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])) {
				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
			courseExamRecordService.save(r);
		}
		//更新学员考试成绩
		int EXAMSCORE = EXAMQUESTIONRIGHT * Integer.parseInt(pd.getString("QUESTIONSCORE"));
		PageData student = new PageData();
		student.put("EXAMTYPE", "2");	//课程考试得分
		student.put("COURSEEXAMSCORE", EXAMSCORE);	//课程考试得分
		student.put("COURSEEXAMSTATE", EXAMSCORE >= 60 ? '3' : '2');	//课程考试状态
		student.put("OPERATOR", pd.getString("OPERATOR"));	//修改人
		student.put("OPERATTIME", pd.getString("OPERATTIME"));	//修改时间
		student.put("COURSESTUDENTRELATION_ID", pd.getString("COURSESTUDENTRELATION_ID"));
		courseStudentRelationService.updateStudentScore(student);
		//更新考试信息
//		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("EXAMSCORE", EXAMSCORE+"");
		this.save(pd);
		return pd;
	}

	/**试卷答题展示
	 * @param
	 * @throws Exception
	 */
	@Override
	public PageData findResult(PageData pd) throws Exception {
		PageData data = this.findById(pd);
		List<PageData> questionList = courseExamRecordService.findByExam(pd);
		data.put("QUESTIONLIST", questionList);
		return data;
	}
}

