package com.zcloud.service.paper.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.paper.StageExamPaperInputQuestionMapper;
import com.zcloud.mapper.datasource.paper.StageExamPaperQuestionMapper;
import com.zcloud.service.paper.StageExamPaperQuestionService;
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-10-08
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class StageExamPaperQuestionServiceImpl implements StageExamPaperQuestionService {

	@Autowired
	private StageExamPaperQuestionMapper stageexampaperquestionMapper;
	@Autowired
	private StageExamPaperInputQuestionMapper stageexampaperinputquestionMapper;

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

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

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

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

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

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

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

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

	/**新增(保存抽取后的试题数据)
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public void saveAll(PageData pd, List<PageData> questions) throws Exception {
		for (PageData q : questions) {
			PageData question = new PageData();
			question.put("STAGEEXAMPAPERQUESTION_ID", UuidUtil.get32UUID());    //主键
			question.put("STAGEEXAMPAPER_ID", pd.getString("STAGEEXAMPAPER_ID"));    // 试卷
//			question.put("CURRICULUM_ID", pd.getString("CURRICULUMID"));    // 课程
			question.put("COURSEWARE_ID", q.getString("COURSEWAREID"));    // 课件
			question.put("COURSEWARETYPE", q.getString("COURSEWARETYPE"));    // 课件类型
			question.put("QUESTION_ID", q.getString("QUESTION_ID"));
			question.put("QUESTIONSCORE", Integer.parseInt(q.get("QUESTIONSCORE").toString()));
			question.put("CREATOR", pd.getString("CREATOR"));    //添加人
			question.put("CREATTIME", pd.getString("CREATTIME"));    //添加时间
			question.put("OPERATOR", pd.getString("OPERATOR"));    //修改人
			question.put("OPERATTIME", pd.getString("OPERATTIME"));    //修改时间
			question.put("ISDELETE", "0");    //是否删除(0:有效 1：删除)
			question.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));    //企业ID
			this.save(question);
		}
	}

	/**列表(根据试卷ID获取试题数据)
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public List<PageData> findByExamPaper(PageData pd) {
		return stageexampaperquestionMapper.findByExamPaper(pd);
	}

	/**删除(根据试卷ID删除试题数据)
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public void deleteByExamPaper(PageData pd) {
		stageexampaperquestionMapper.deleteByExamPaper(pd);
	}

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

	}
	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void saveAllV2(PageData pd) throws Exception {
		List<PageData> inputList = stageexampaperinputquestionMapper.listAll(pd);
		for (PageData q :inputList) {
			PageData question = new PageData();
			question.put("STAGEEXAMPAPERQUESTION_ID", UuidUtil.get32UUID());    //主键
			question.put("STAGEEXAMPAPER_ID", pd.getString("STAGEEXAMPAPER_ID"));    // 试卷
			question.put("CURRICULUM_ID", q.getString("CURRICULUM_ID"));    // 课程ID
			question.put("COURSEWARE_ID", q.getString("COURSEWARE_ID"));    // 课件
			question.put("COURSEWARETYPE", q.getString("COURSEWARETYPE"));    // 课件类型
			question.put("QUESTION_ID", q.getString("QUESTION_ID")); //习题ID
			question.put("CREATOR", pd.getString("CREATOR"));    //添加人
			question.put("CREATTIME", pd.getString("CREATTIME"));    //添加时间
			question.put("OPERATOR", pd.getString("OPERATOR"));    //修改人
			question.put("OPERATTIME", pd.getString("OPERATTIME"));    //修改时间
			question.put("ISDELETE", "0");    //是否删除(0:有效 1：删除)
			question.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));    //企业ID
			question.put("QUESTIONSCORE", q.getString("QUESTIONSCORE"));    // 试卷 试题分值
			this.save(question);
		}
	}
}

