package com.zcloud.service.onlinexxks.impl;

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.relation.CoursewareRelationMapper;
import com.zcloud.service.onlinexxks.CoursewareRelationService;
import com.zcloud.util.Tools;
import com.zcloud.util.UuidUtil;

/**
 * 说明：课件和字典的关系表
 * 作者：luoxiaobao
 * 时间：2022-03-10
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class CoursewareRelationServiceImpl implements CoursewareRelationService {

	@Autowired
	private CoursewareRelationMapper coursewarerelationMapper;

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

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

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

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

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

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

	/**批量删除
	 * @param ArrayDATA_IDS
	 * @throws Exception
	 */
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		coursewarerelationMapper.deleteAll(ArrayDATA_IDS);
	}
//DATACOURSEWARE_ID
	@Override
	public void saveByCourseware(PageData pd,String type) throws Exception {
		String coursewareID = "";
		if("1".equals(type)){
			coursewareID =  pd.getString("VIDEOCOURSEWARE_ID");
		}else if ("2".equals(type)){
			coursewareID =  pd.getString("DATACOURSEWARE_ID");
		}
		pd.put("RECORD_ID",coursewareID);
		coursewarerelationMapper.deleteByCourseware(pd); //先删除，在添加。
		if(!Tools.isEmpty(pd.get("trainingSection"))){ //培训板块
			String[] trainingSectionIDS = pd.getString("trainingSection").split(";");
			for (int i =0 ;i<trainingSectionIDS.length;i++){
				PageData relation = new PageData();
				relation.put("COURSEWARERELATION_ID", UuidUtil.get32UUID());
				relation.put("DICTIONARIES_IDS", trainingSectionIDS[i]);
				relation.put("RECORD_ID", coursewareID);
				relation.put("RECORD_TYPE_ID", "trainingSection");
				this.save(relation);
			}
		}
		if(!Tools.isEmpty(pd.get("relatedTerm"))){ //相关词条
			String[] relatedTermIDS = pd.getString("relatedTerm").split(";");
			for (int i =0 ;i<relatedTermIDS.length;i++){
				PageData relation = new PageData();
				relation.put("COURSEWARERELATION_ID", UuidUtil.get32UUID());
				relation.put("DICTIONARIES_IDS", relatedTermIDS[i]);
				relation.put("RECORD_ID", coursewareID);
				relation.put("RECORD_TYPE_ID", "relatedTerm");
				this.save(relation);
			}
		}
	}
	
	/**新增字典关联表。通用
	 * @param pd 课件信息
	 * @throws Exception
	 */
	@Override
	public void saveAllRelation(PageData pd,String idName) throws Exception {
		pd.put("RECORD_ID",pd.getString(idName));
		coursewarerelationMapper.deleteByCourseware(pd); //先删除，在添加。
		if(!Tools.isEmpty(pd.get("trainingSection"))){ //培训板块
			String[] trainingSectionIDS = pd.getString("trainingSection").split(";");
			for (int i =0 ;i<trainingSectionIDS.length;i++){
				PageData relation = new PageData();
				relation.put("COURSEWARERELATION_ID", UuidUtil.get32UUID());
				relation.put("DICTIONARIES_IDS", trainingSectionIDS[i]);
				relation.put("RECORD_ID", pd.getString(idName));
				relation.put("RECORD_TYPE_ID", "trainingSection");
				this.save(relation);
			}
		}
		if(!Tools.isEmpty(pd.get("relatedTerm"))){ //相关词条
			String[] relatedTermIDS = pd.getString("relatedTerm").split(";");
			for (int i =0 ;i<relatedTermIDS.length;i++){
				PageData relation = new PageData();
				relation.put("COURSEWARERELATION_ID", UuidUtil.get32UUID());
				relation.put("DICTIONARIES_IDS", relatedTermIDS[i]);
				relation.put("RECORD_ID", pd.getString(idName));
				relation.put("RECORD_TYPE_ID", "relatedTerm");
				this.save(relation);
			}
		}
	}

	@Override
	public void deleteByCourseware(PageData pd) throws Exception {
		coursewarerelationMapper.deleteByCourseware(pd);
	}

}

