package com.zcloud.service.onlinexxks.impl;

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

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.curriculum.CurriculumMapper;
import com.zcloud.service.onlinexxks.CourseStudentRelationService;
import com.zcloud.service.onlinexxks.CoursemiddleService;
import com.zcloud.service.onlinexxks.CoursepapersService;
import com.zcloud.service.onlinexxks.CurriculumService;
import com.zcloud.service.onlinexxks.KcmiddlekjService;
import com.zcloud.service.onlinexxks.StageExamPaperQuestionService;
import com.zcloud.service.onlinexxks.StageexampaperService;

/**
 * 说明：课程管理
 * 作者：luoxiaobao
 * 时间：2021-07-03
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class CurriculumServiceImpl implements CurriculumService{

	@Autowired
	private CurriculumMapper curriculumMapper;

	@Autowired
	private KcmiddlekjService kcmiddlekjService;
	@Autowired
	private CoursemiddleService coursemiddleService;
	@Autowired
	private CoursepapersService coursepapersService;
//	@Autowired
//	private StageexammiddleService stageexammiddleService;
	@Autowired
	private StageExamPaperQuestionService stageExamPaperQuestionService;
	@Autowired
	private StageexampaperService stageexampaperService;
	@Autowired
	private CourseStudentRelationService courseStudentRelationService;

	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		curriculumMapper.save(pd);
		//保存课程主表后保存符合人员类别的人员
//		courseStudentRelationService.saveByCurriculum(pd);
	}

//	/**删除
//	 * @param pd
//	 * @throws Exception
//	 */
//	public void delete(PageData pd)throws Exception{
//		String[] ArrayDATA_IDS = {pd.getString("CURRICULUM_ID")};
//		pd.put("ArrayDATA_IDS",ArrayDATA_IDS);
////		stageExamPaperQuestionService.deleteByCurriculumIds(pd);	//删除课程阶段考试相关的试卷试题
////		stageexampaperService.deleteByCurriculumIds(pd);	//删除课程阶段考试相关的试卷
//		coursemiddleService.deleteByCurriculumIds(pd);	//删除课程相关的试卷试题
//		coursepapersService.deleteByCurriculumIds(pd);	//删除课程相关的试卷
//		courseStudentRelationService.deleteByCurriculumIds(pd);	//删除课程学员关系
//		kcmiddlekjService.deleteByCurriculumIds(pd);	//删除课程课件关系
//		curriculumMapper.delete(pd);
//	}
//
	/**删除
	 * @param pd
	 * @throws Exception
	 */
	public void delete(PageData pd)throws Exception{
		curriculumMapper.delete(pd);
	}

	/**修改
	 * @param pd
	 * @throws Exception
	 */
	public void edit(PageData pd)throws Exception{
		curriculumMapper.edit(pd);
//		if (!old.getString("CURRICULUMUSERS").equals(pd.getString("CURRICULUMUSERS"))) {
//			courseStudentRelationService.deleteByCurriculumIds(pd);	//删除课程学员关系
//			courseStudentRelationService.saveByCurriculum(pd);
//		}
	}

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

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

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

	@Override
	public PageData getMyCurriculum(PageData pd) throws Exception {
		PageData data = curriculumMapper.findById(pd);
		List<PageData> kckjList = kcmiddlekjService.listAll(pd);
		List<PageData> videoList = new ArrayList<PageData>();
		List<PageData> dataList = new ArrayList<PageData>();
		for (PageData kj : kckjList) {
			if ("1".equals(kj.getString("COURSEWARETYPE"))) {
				videoList.add(kj);
			} else if ("2".equals(kj.getString("COURSEWARETYPE"))) {
				dataList.add(kj);
			}
		}
		data.put("VIDEOLIST", videoList);
		data.put("DATALIST", dataList);
		return data;
	}

	/**批量删除
	 * @param pd
	 * @throws Exception
	 */
	public void deleteAll(PageData pd)throws Exception{
//		stageExamPaperQuestionService.deleteByCurriculumIds(pd);	//删除课程阶段考试相关的试卷试题
//		stageexampaperService.deleteByCurriculumIds(pd);	//删除课程阶段考试相关的试卷
		coursemiddleService.deleteByCurriculumIds(pd);	//删除课程相关的试卷试题
		coursepapersService.deleteByCurriculumIds(pd);	//删除课程相关的试卷
		kcmiddlekjService.deleteByCurriculumIds(pd);	//删除课程相关的课件关系
		courseStudentRelationService.deleteByCurriculumIds(pd);	//删除课程学员关系
		curriculumMapper.deleteAll(pd);
	}

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

	/** 列表(根据课程ID获取课程及习题数据)
	 * @param pd
	 * @return
	 */
	@Override
	public PageData findForStudyByCurriculumId(PageData pd) throws Exception {
		PageData data = curriculumMapper.findById(pd);

		List<PageData> questionList = kcmiddlekjService.findForStudyByCurriculumId(pd);
		Object[] ANSWER = new Object[questionList.size()];
		String[] QUESTION_ID = new String[questionList.size()];
		String[] QUESTIONTYPE = new String[questionList.size()];
		String[] COURSEWAREID = new String[questionList.size()];
		String[] COURSEWARETYPE = new String[questionList.size()];
		String[] ANSWERRIGHT = new String[questionList.size()];
		for (int i = 0; i < questionList.size(); i++) {
			if ("2".equals(questionList.get(i).getString("QUESTIONTYPE"))) {	//多选
				String[] multipleANSWER = new String[questionList.get(i).getString("ANSWER").length()];
				for (int j = 0; j < multipleANSWER.length; j++) {
					multipleANSWER[j] = "";
				}
				ANSWER[i] = multipleANSWER;
			} else {
				ANSWER[i] = "";
			}
			QUESTION_ID[i] = questionList.get(i).getString("QUESTION_ID");
			QUESTIONTYPE[i] = questionList.get(i).getString("QUESTIONTYPE");
			COURSEWAREID[i] = questionList.get(i).getString("COURSEWAREID");
			COURSEWARETYPE[i] = questionList.get(i).getString("COURSEWARETYPE");
			ANSWERRIGHT[i] = questionList.get(i).getString("ANSWER");
		}
		data.put("ANSWER", ANSWER);
		data.put("QUESTION_ID", QUESTION_ID);
		data.put("QUESTIONTYPE", QUESTIONTYPE);
		data.put("COURSEWAREID", COURSEWAREID);
		data.put("COURSEWARETYPE", COURSEWARETYPE);
		data.put("ANSWERRIGHT", ANSWERRIGHT);
		data.put("QUESTIONLIST", questionList);
		return data;
	}

	@Override
	public PageData countForStudyByCoursewareId(PageData pd) throws Exception {
		PageData data = new PageData();
		List<PageData> questionList = kcmiddlekjService.findForStudyByCoursewareId(pd);
		data.put("COUNT", questionList.size());
		return data;
	}

	/** 列表(根据ID获取课程及习题数据)
	 * @param pd
	 * @return
	 */
	@Override
	public PageData findForStudyByCoursewareId(PageData pd) throws Exception {
		PageData data = new PageData();

		List<PageData> questionList = kcmiddlekjService.findForStudyByCoursewareId(pd);
		Object[] ANSWER = new Object[questionList.size()];
		String[] QUESTION_ID = new String[questionList.size()];
		String[] QUESTIONTYPE = new String[questionList.size()];
		String[] COURSEWAREID = new String[questionList.size()];
		String[] COURSEWARETYPE = new String[questionList.size()];
		String[] ANSWERRIGHT = new String[questionList.size()];
		for (int i = 0; i < questionList.size(); i++) {
			if ("2".equals(questionList.get(i).getString("QUESTIONTYPE"))) {	//多选
				String[] multipleANSWER = new String[questionList.get(i).getString("ANSWER").length()];
				for (int j = 0; j < multipleANSWER.length; j++) {
					multipleANSWER[j] = "";
				}
				ANSWER[i] = multipleANSWER;
			} else {
				ANSWER[i] = "";
			}
			QUESTION_ID[i] = questionList.get(i).getString("QUESTION_ID");
			QUESTIONTYPE[i] = questionList.get(i).getString("QUESTIONTYPE");
			COURSEWAREID[i] = questionList.get(i).getString("COURSEWAREID");
			COURSEWARETYPE[i] = questionList.get(i).getString("COURSEWARETYPE");
			ANSWERRIGHT[i] = questionList.get(i).getString("ANSWER");
		}
		data.put("ANSWER", ANSWER);
		data.put("QUESTION_ID", QUESTION_ID);
		data.put("QUESTIONTYPE", QUESTIONTYPE);
		data.put("COURSEWAREID", COURSEWAREID);
		data.put("COURSEWARETYPE", COURSEWARETYPE);
		data.put("ANSWERRIGHT", ANSWERRIGHT);
		data.put("QUESTIONLIST", questionList);
		return data;
	}

	/**我的课程（获取当前用户的所属课程）
	 * @param page
	 * @throws Exception
	 */
	@Override
	public List<PageData> pageDataByUser(Page page)throws Exception {
		return curriculumMapper.pageDataByUserlistPage(page);
	}

	/**学员课程列表（查询课程信息及可想关联的学员统计）
	 * @param page
	 * @throws Exception
	 */
	@Override
	public List<PageData> listSummary(Page page)throws Exception{
		return curriculumMapper.listSummarylistPage(page);
	}

	/**启用禁用
	 * @param pd
	 * @throws Exception
	 */
	public void editState(PageData pd)throws Exception{
		curriculumMapper.editState(pd);
	}

	/**查询视频库课程
	 * @param pd
	 * @throws Exception
	 */
	public PageData findByRemoteId(PageData pd)throws Exception{
		PageData data = curriculumMapper.findByRemoteId(pd);
		return data;
	}
}

