package com.zcloud.service.paper.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.paper.StageExamPaperCoursewareMapper;
import com.zcloud.service.paper.StageExamPaperCoursewareService;
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 StageExamPaperCoursewareServiceImpl implements StageExamPaperCoursewareService {

	@Autowired
	private StageExamPaperCoursewareMapper stageexampapercoursewareMapper;

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

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

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

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

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

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

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

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

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

	/**
	 * 保存所选课件范围
	 * @param pd
	 */
	@Override
	public void saveFromPager(PageData pd) throws Exception {
		String[] coursewares = pd.getString("COURSEWARES").split(",");
		for (int i = 0; i < coursewares.length; i++) {
			PageData c = new PageData();
			c.put("STAGEEXAMPAPERCOURSEWARE_ID", UuidUtil.get32UUID());
			c.put("STAGEEXAMPAPER_ID", pd.getString("STAGEEXAMPAPER_ID"));    // 试卷
			c.put("COURSEWARE_ID", coursewares[i].split("_")[0]);    // 课件ID
			c.put("COURSEWARETYPE", coursewares[i].split("_")[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
			this.save(c);
		}
	}

	/**
	 * 根据试卷ID删除试卷课件范围
	 * @param pd
	 */
	@Override
	public void deleteByExamPaper(PageData pd) throws Exception {
		stageexampapercoursewareMapper.deleteByExamPaper(pd);
	}
}

