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.curriculum.CourseStudentRelationMapper;
import com.zcloud.service.onlinexxks.CourseStudentRelationService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.UuidUtil;

/**
 * 说明：课程学员关系表
 * 作者：luoxiaobao
 * 时间：2021-09-10
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class CourseStudentRelationServiceImpl implements CourseStudentRelationService{

	@Autowired
	private CourseStudentRelationMapper coursestudentrelationMapper;
	@Autowired
	private UsersService usersService;

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

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

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

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

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

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

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

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

	/**新增(保存课程主表后保存符合人员类别的人员)
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public void saveByCurriculum(PageData pd) throws Exception {
		pd.put("LEARNERCATEGORY",pd.getString("CURRICULUMUSERS"));
		List<PageData> userList = usersService.listCascader(pd);
		for (PageData user : userList) {
			PageData obj = new PageData();
			obj.put("COURSESTUDENTRELATION_ID", UuidUtil.get32UUID());
			obj.put("CURRICULUM_ID", pd.getString("CURRICULUM_ID"));
			obj.put("USER_ID", user.getString("USER_ID"));
			obj.put("STUDYCOUNT", "0");
			obj.put("COURSEEXAMCOUNT", "0");
			obj.put("STAGEEXAMCOUNT", "0");
			obj.put("STUDYSTATE", "1");
			obj.put("COURSEEXAMSTATE", "1");
			obj.put("STAGEEXAMSTATE", "1");
			obj.put("CREATOR", pd.getString("CREATOR"));	//添加人
			obj.put("CREATTIME", pd.getString("CREATTIME"));	//添加时间
			obj.put("OPERATOR", pd.getString("OPERATOR"));	//修改人
			obj.put("OPERATTIME", pd.getString("OPERATTIME"));	//修改时间
			obj.put("ISDELETE", "0");	//是否删除(0:有效 1：删除)
			obj.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));	//企业ID
			this.save(obj);
		}
	}

	/**批量删除（根据所选的课程进行删除课程学员关系表）
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public void deleteByCurriculumIds(PageData pd) throws Exception {
		coursestudentrelationMapper.deleteByCurriculumIds(pd);
	}

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

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

	/**
	 * 课程结束（定时任务，按处理没有参加考试的学员课程，课程结束STUDYSTATE = '4'，考试未考COURSEEXAMSTATE = '4'）
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public void overCourseNotInvolved(PageData pd) throws Exception {
		coursestudentrelationMapper.overCourseNotInvolved(pd);
	}

	/**
	 * 课程结束（定时任务，处理参加考试的学员课程，课程结束STUDYSTATE = '4'）
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public void overCourse(PageData pd) throws Exception {
		coursestudentrelationMapper.overCourse(pd);
	}
}

