package com.zcloud.service.curriculum.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.curriculum.KcmiddlekjMapper;
import com.zcloud.service.curriculum.KcmiddlekjService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
import com.zcloud.util.UuidUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 说明：课件课程中间表
 * 作者：luoxiaobao
 * 时间：2021-07-03
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class KcmiddlekjServiceImpl implements KcmiddlekjService {

	@Autowired
	private KcmiddlekjMapper kcmiddlekjMapper;

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

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

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

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

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

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

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

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

	/**批量删除（根据所选课程id删除所选关系课件）
	 * @param pd
	 * @throws Exception
	 */
	public void deleteByCurriculumIds(PageData pd) throws Exception{
		kcmiddlekjMapper.deleteByCurriculumIds(pd);
	}

	/**新增(保存课程主表后保存所选课件中间表)
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public void saveByCurriculum(PageData pd) {
		String curriculums = StringUtils.isNotBlank(pd.getString("VIDEOCOURSEWARES")) ? pd.getString("VIDEOCOURSEWARES") : "";
		curriculums += StringUtils.isNotBlank(curriculums) ? "," : "";
		curriculums += StringUtils.isNotBlank(pd.getString("DATACOURSEWARES")) ? pd.getString("DATACOURSEWARES") : "";
		String[] curriculumArr = curriculums.split(",");
		for (int i = 0; i < curriculumArr.length; i++) {
			PageData kckj = new PageData();
			kckj.put("KCMIDDLEKJ_ID", UuidUtil.get32UUID());	//主键
			kckj.put("CURRICULUMID", pd.getString("CURRICULUM_ID"));
//			kckj.put("COURSEWAREID", curriculumArr[i].split("_")[0]);
//			kckj.put("COURSEWARETYPE", curriculumArr[i].split("_")[1]);
			kckj.put("CREATOR", Jurisdiction.getUsername());	//添加人
			kckj.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
			kckj.put("OPERATOR", Jurisdiction.getUsername());	//修改人
			kckj.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
			kckj.put("ISDELETE", "0");	//是否删除(0:有效 1：删除)
			kckj.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID\
			kcmiddlekjMapper.save(kckj);
		}
	}

	/**列表(根据课程ID获取数据)
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> findByCurriculumId(PageData pd) throws Exception {
		return kcmiddlekjMapper.findByCurriculumId(pd);
	}

	/** 列表(根据课程ID获取习题数据)
	 * @param pd
	 * @return
	 */
	@Override
	public List<PageData> findForStudyByCurriculumId(PageData pd) throws Exception {
		return kcmiddlekjMapper.findForStudyByCurriculumId(pd);
	}

	/** 列表(根据课程ID获取习题数据)
	 * @param pd
	 * @return
	 */
	@Override
	public List<PageData> findForStudyByCoursewareId(PageData pd) throws Exception {
		return kcmiddlekjMapper.findForStudyByCoursewareId(pd);
	}
	/**新增(保存课程主表后保存所选课件中间表)
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public void saveByCurriculumV2(PageData pd) {
		String voidIds = StringUtils.isNotBlank(pd.getString("VIDEOCOURSEWARES")) ? pd.getString("VIDEOCOURSEWARES") : "";
		String dataIds = StringUtils.isNotBlank(pd.getString("DATACOURSEWARES")) ? pd.getString("DATACOURSEWARES") : "";
		if(!Tools.isEmpty(voidIds)){
			String[] voidArr = voidIds.split(",");
			for (int i = 0; i < voidArr.length; i++) {
				PageData kckj = new PageData();
				kckj.put("KCMIDDLEKJ_ID", UuidUtil.get32UUID());	//主键
				kckj.put("CURRICULUMID", pd.getString("CURRICULUM_ID"));
				kckj.put("COURSEWAREID", voidArr[i]);// 课件ID
				kckj.put("COURSEWARETYPE", "1");//课件类型  1  2
				kckj.put("CREATOR", Jurisdiction.getUsername());	//添加人
				kckj.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
				kckj.put("OPERATOR", Jurisdiction.getUsername());	//修改人
				kckj.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
				kckj.put("ISDELETE", "0");	//是否删除(0:有效 1：删除)
				kckj.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID\
				kckj.put("CHAPTER_ID", pd.getString("CHAPTER_ID"));
				kcmiddlekjMapper.save(kckj);
			}
		}
		if(!Tools.isEmpty(dataIds)){
			String[] dataArr = dataIds.split(",");
			for (int i = 0; i < dataArr.length; i++) {
				PageData kckj = new PageData();
				kckj.put("KCMIDDLEKJ_ID", UuidUtil.get32UUID());	//主键
				kckj.put("CURRICULUMID", pd.getString("CURRICULUM_ID"));
				kckj.put("COURSEWAREID", dataArr[i]);// 课件ID
				kckj.put("COURSEWARETYPE", "2");//课件类型  1  2
				kckj.put("CREATOR", Jurisdiction.getUsername());	//添加人
				kckj.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
				kckj.put("OPERATOR", Jurisdiction.getUsername());	//修改人
				kckj.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
				kckj.put("ISDELETE", "0");	//是否删除(0:有效 1：删除)
				kckj.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID\
				kckj.put("CHAPTER_ID", pd.getString("CHAPTER_ID"));
				kcmiddlekjMapper.save(kckj);
			}
		}
	}

	public List<PageData> findByCurriculumIds (PageData pd)throws Exception{
		return kcmiddlekjMapper.findByCurriculumIds(pd);
	}

	/**【app学习详细】（根据STUDYTASK_ID获取当前的任务下的相关课件）
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public List<PageData> listAllByTask(PageData pd) throws Exception {
		return kcmiddlekjMapper.listAllByTask(pd);
	}
	public void deleteByCURRICULUMID(PageData pd)throws Exception{
		kcmiddlekjMapper.deleteByCURRICULUMID(pd);
	}

	public String countByCourseWareId (PageData pageData)throws Exception{
		return  kcmiddlekjMapper.countByCourseWareId(pageData);
	}
}

