package com.zcloud.service.task.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.task.StageStudentRelationMapper;
import com.zcloud.service.curriculum.KcmiddlekjService;
import com.zcloud.service.task.StageStudentRelationService;
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.ArrayList;
import java.util.List;

/**
 * 说明：阶段考试学员关系表
 * 作者：luoxiaobao
 * 时间：2021-10-08
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class StageStudentRelationServiceImpl implements StageStudentRelationService {

	@Autowired
	private StageStudentRelationMapper stagestudentrelationMapper;
	@Autowired
	private KcmiddlekjService kcmiddlekjService;

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

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

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

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

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

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

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

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

	/**
	 * 获取所选考试人员
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PageData> findByExamPaper(PageData pd) throws Exception {
		return stagestudentrelationMapper.findByExamPaper(pd);
	}

	/**
	 * 保存所选考试人员
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public void saveFromPager(PageData pd) throws Exception {
		String[] curriculumusers = pd.getString("CURRICULUMUSERS").split(",");
		for (int i = 0; i < curriculumusers.length; i++) {
			PageData c = new PageData();
			c.put("STAGESTUDENTRELATION_ID", UuidUtil.get32UUID());
			c.put("STAGEEXAMPAPER_ID", pd.getString("STAGEEXAMPAPER_ID"));    // 试卷
			c.put("USER_ID", curriculumusers[i]);    // 课件ID
			c.put("STAGEEXAMSTATE", "1");    // 阶段考试状态（1.未开始 2.考试中 3.考试完成 4.未参加）
			c.put("STAGEEXAMSCORE", "0");    // 阶段考试得分
			c.put("CREATOR", pd.getString("CREATOR"));    //添加人
			c.put("CREATTIME", pd.getString("CREATTIME"));    //添加时间
			c.put("OPERATOR", pd.getString("OPERATOR"));    //修改人
			c.put("OPERATTIME", pd.getString("OPERATTIME"));    //修改时间
			c.put("ISDELETE", "0");    //是否删除(0:有效 1：删除)
			c.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));    //企业ID
			this.save(c);
		}
	}

	/**
	 * 根据试卷ID删除考试人员
	 * @param pd
	 */
	@Override
	public void deleteByExamPaper(PageData pd) throws Exception {
		stagestudentrelationMapper.deleteByExamPaper(pd);
	}

	/**列表【app成绩查询】（获取当前用户的所属考试信息列表）
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PageData> pageDataByUser(Page page) throws Exception {
		return stagestudentrelationMapper.pageDataByUserlistPage(page);
	}

	/**修改（更新学员考试状态）
	 * @param
	 * @throws Exception
	 */
	@Override
	public void updateStudentState(PageData pd) throws Exception {
		stagestudentrelationMapper.updateStudentState(pd);
	}

	/**修改（更新学员考试成绩及状态）
	 * @param
	 * @throws Exception
	 */
	@Override
	public void updateStudentScore(PageData pd) throws Exception {
		stagestudentrelationMapper.updateStudentScore(pd);
	}

	/**
	 * 定时任务，处理没有参加考试的学员阶考，考试未考STAGEEXAMSTATE = '4'
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public void overStage(PageData pd) throws Exception {
		stagestudentrelationMapper.overStage(pd);
	}

	public void saveFromPagerV2(PageData pd) throws Exception {
		String[] curriculumusers = pd.getString("userIds").split(",");
		for (int i = 0; i < curriculumusers.length; i++) {
			if(curriculumusers[i].equals("")){
				continue;
			}
			PageData c = new PageData();
			c.put("STAGESTUDENTRELATION_ID", UuidUtil.get32UUID());
			c.put("STAGEEXAMPAPER_ID", pd.getString("STAGEEXAMPAPER_ID"));    // 试卷
			c.put("USER_ID", curriculumusers[i]);    // 课件ID
			c.put("STAGEEXAMSTATE", "1");    // 阶段考试状态（1.未开始 2.考试中 3.考试完成 4.未参加）
			c.put("STAGEEXAMSCORE", "-1");    // 阶段考试得分
			c.put("CREATOR", pd.getString("CREATOR"));    //添加人
			c.put("CREATTIME", pd.getString("CREATTIME"));    //添加时间
			c.put("OPERATOR", pd.getString("OPERATOR"));    //修改人
			c.put("OPERATTIME", pd.getString("OPERATTIME"));    //修改时间
			c.put("ISDELETE", "0");    //是否删除(0:有效 1：删除)
			c.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));    //企业ID)
			c.put("STUDYTASK_ID", pd.getString("STUDYTASK_ID"));    //任务id
			c.put("COMPLETE_COURSEWARE", "0");    //学习完成数量
			c.put("COMPLETE_CLASSHOUR", "0");    //完成学时
			this.save(c);
		}
	}

	/**列表【app我的任务】（获取当前用户的所属任务信息列表）
	 * @param page
	 * @throws Exception
	 */
	@Override
	public List<PageData> pageTaskByUser(Page page) {
		return stagestudentrelationMapper.pageTaskByUserlistPage(page);
	}

	/**
	 *  【app任务详细】（根据STAGESTUDENTRELATION_ID获取当前的任务及课件）
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	@Override
	public PageData getMyTask(PageData pd) throws Exception {
		PageData data = stagestudentrelationMapper.getMyTask(pd);
		pd.put("STUDYTASK_ID",data.getString("STUDYTASK_ID"));
		List<PageData> kckjList = kcmiddlekjService.listAllByTask(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;
	}


	/**
	 /**列表【app成绩查询】（获取当前用户的所属考试信息列表）
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PageData> pageTaskScoreByUser(Page page) throws Exception {
		return stagestudentrelationMapper.pageTaskScoreByUserlistPage(page);
	}

	/**
	 /**列表【app成绩查询】（获取当前用户的所属考试所有信息列表）
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<PageData> pageTaskScoreByUserAll(Page page) throws Exception {
		return stagestudentrelationMapper.pageTaskScoreByUserAll(page);
	}

	/**修改  学习完成的 任务 人员关系表
	 * @param pd
	 * @throws Exception
	 */
	public void updateClassHourAndCount(PageData pd)throws Exception{
		stagestudentrelationMapper.updateClassHourAndCount(pd);
	}
}

