package com.zcloud.service.onlinexxks.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.KcmiddlekjMapper;
import com.zcloud.mapper.dsno3.task.StudytaskCurriculumMapper;
import com.zcloud.service.onlinexxks.StudytaskCurriculumService;
import com.zcloud.util.UuidUtil;

/**
 * 说明：任务-课程关系表
 * 作者：luoxiaobao
 * 时间：2021-12-21
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class StudytaskCurriculumServiceImpl implements StudytaskCurriculumService {

	@Autowired
	private StudytaskCurriculumMapper studytaskcurriculumMapper;
	@Autowired
	private KcmiddlekjMapper kcmiddlekjMapper;

	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public PageData save(PageData pd)throws Exception{
		//任务id 和课程id
		// 其他地方没有获取课件数量，所以只有这里重新查询去除
		String [] curIds = pd.getString("curIds").split(",");
		List<String> arrIds = new ArrayList<>();
		for (int i=0 ;i< curIds.length;i++){
			PageData save = new PageData();
			save.put("STUDYTASK_ID",pd.getString("STUDYTASK_ID"));
			save.put("CURRICULUM_ID",curIds[i]);
			save.put("STUDYTASKCURRICULUM_ID", UuidUtil.get32UUID());
			studytaskcurriculumMapper.save(save);
			arrIds.add(curIds[i]);
		}
		PageData pageData = new PageData();
		pageData.put("ArrayDATA_IDS",arrIds);
		Map<String,String> groupIDs = new HashMap<>();
		Double sumCla = 0d;
		List<PageData> kcAllList = kcmiddlekjMapper.findClassHourByCurriculumIds(pageData);
		for (PageData p : kcAllList){
			if(!groupIDs.containsKey(p.getString("COURSEWAREID"))){
				Double CLASSHOUR = 0d;
				if("1".equals(p.getString("COURSEWARETYPE"))){//vc
					CLASSHOUR = Double.parseDouble(p.get("vc").toString());
				}else if("2".equals(p.getString("COURSEWARETYPE"))){//dc
					CLASSHOUR = Double.parseDouble(p.get("dc").toString());
				}
				groupIDs.put(p.getString("COURSEWAREID"),"");
				sumCla += CLASSHOUR;
			}
		}
		pageData.put("sumCla",sumCla);
		pageData.put("sumCount",groupIDs.size());
		return pageData;
	}

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

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

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

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

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

	/**批量删除
	 * @param ArrayDATA_IDS
	 * @throws Exception
	 */
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		studytaskcurriculumMapper.deleteAll(ArrayDATA_IDS);
	}
	public Integer countByCurriculumId (PageData pageData)throws Exception{
		return studytaskcurriculumMapper.countByCurriculumId(pageData);
	}
}

