package com.zcloud.service.task.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.task.CourseStudyMapper;
import com.zcloud.service.curriculum.CourseStudentRelationService;
import com.zcloud.service.task.CourseStudyRecordService;
import com.zcloud.service.task.CourseStudyService;
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-16
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class CourseStudyServiceImpl implements CourseStudyService {

	@Autowired
	private CourseStudyMapper coursestudyMapper;
	@Autowired
	private CourseStudyRecordService courseStudyRecordService;
	@Autowired
	private CourseStudentRelationService courseStudentRelationService;

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

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

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

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

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

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

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

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

	/**结束本次学习，保存学习习题
	 * @param
	 * @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++) {
			if (!"-".equals(ANSWER[i])) {
				PageData r = new PageData();
				r.put("COURSESTUDYRECORD_ID", UuidUtil.get32UUID());
				r.put("COURSESTUDY_ID", pd.getString("COURSESTUDY_ID"));
				r.put("CURRICULUM_ID", pd.getString("CURRICULUMID"));
				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
				courseStudyRecordService.save(r);
			}
		}
		PageData student = new PageData();
		student.put("EXAMTYPE", "1");	//课程学习
		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("STUDYQUESTIONNUM", (EXAMQUESTIONRIGHT + EXAMQUESTIONWRONG) + "");
		pd.put("STUDYQUESTIONRIGHT", EXAMQUESTIONRIGHT + "");
		pd.put("STUDYQUESTIONWRONG", EXAMQUESTIONWRONG + "");
		pd.put("OPERATOR", pd.getString("OPERATOR"));
		pd.put("OPERATTIME", pd.getString("OPERATTIME"));
		this.save(pd);
		return pd;
	}
}

