package com.zcloud.controller.platformCurriculum;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.logs.LogAnno;
import com.zcloud.service.courseware.DataCoursewareService;
import com.zcloud.service.courseware.QuestionService;
import com.zcloud.service.courseware.VideoCoursewareService;
import com.zcloud.service.curriculum.ChapterService;
import com.zcloud.service.curriculum.CurriculumService;
import com.zcloud.service.curriculum.KcmiddlekjService;
import com.zcloud.service.platformCourseware.PlatformCoursewareRecordService;
import com.zcloud.service.platformCourseware.PlatformDataCoursewareService;
import com.zcloud.service.platformCourseware.PlatformQuestionService;
import com.zcloud.service.platformCourseware.PlatformVideoCoursewareService;
import com.zcloud.service.platformCurriculum.PlatformChapterService;
import com.zcloud.service.platformCurriculum.PlatformCurriculumRecordService;
import com.zcloud.service.platformCurriculum.PlatformCurriculumService;
import com.zcloud.service.platformRelation.PlatformCoursewareRelationService;
import com.zcloud.service.relation.CoursewareRelationService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
import com.zcloud.util.UuidUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * 说明：课程管理
 * 作者：luoxiaobao
 * 时间：2021-07-03
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/platform/curriculum")
public class PlatformCurriculumController extends BaseController {

	@Autowired
	private PlatformCurriculumService platformCurriculumService;
	@Autowired
	private PlatformVideoCoursewareService platformVideocoursewareService;
	@Autowired
	private PlatformDataCoursewareService platformDatacoursewareService;
	@Autowired
	private PlatformCurriculumRecordService platformCurriculumRecordService;
	@Autowired
	private PlatformChapterService platformChapterService;
	@Autowired
	private CurriculumService curriculumService;
	@Autowired
	private ChapterService chapterService;
	@Autowired
	private VideoCoursewareService videocoursewareService;
	@Autowired
	private DataCoursewareService datacoursewareService;
	@Autowired
	private PlatformCoursewareRecordService platformCoursewareRecordService;
	@Autowired
	private PlatformQuestionService platformQuestionService;
	@Autowired
	private QuestionService questionService;
	@Autowired
	private KcmiddlekjService kcmiddlekjService;
	@Autowired
	private PlatformCoursewareRelationService platformCoursewareRelationService;
	@Autowired
	private CoursewareRelationService coursewareRelationService;

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	// @RequiresPermissions("curriculum:list")
	@ResponseBody
	@LogAnno(menuType= "教育培训",menuServer= "平台资源库",instructionsOperate = "课程管理",instructionsType = "列表")
	public Object list(Page page) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String KEYWORDS = pd.getString("KEYWORDS");						//关键词检索条件
		if(Tools.notEmpty(KEYWORDS))pd.put("KEYWORDS", KEYWORDS.trim());
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
		pd.put("TRAINTYPE", Jurisdiction.getCORPTRAINTYPE());
		page.setPd(pd);
		List<PageData>	varList = platformCurriculumService.list(page);	//列出Curriculum列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	@RequestMapping(value="/goEdit")
	@ResponseBody
	@LogAnno(menuType= "教育培训",menuServer= "平台资源库",instructionsOperate = "课程管理",instructionsType = "去修改页面获取数据")
	public Object goEdit() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
		pd = platformCurriculumService.findById(pd);	//根据ID读取
		pd.put("CURRICULUMID",pd.get("CURRICULUM_ID"));
		List<PageData> chapterList = platformChapterService.listAll(pd);
		if(chapterList != null && chapterList.size() > 0) {
			for(PageData chapter : chapterList) {

				chapter.put("CURRICULUMID",chapter.get("CURRICULUM_ID"));
				List<PageData> videoList = platformVideocoursewareService.listAllByCurriculumId(chapter); // 选择的视频课件
				List<PageData> dataList = platformDatacoursewareService.listAllByCurriculumId(chapter);
				chapter.put("xzVideoList", videoList);
				chapter.put("xzDataList", dataList);
			}
		}
		map.put("pd", pd);
		map.put("chapterList", chapterList);
		map.put("result", errInfo);
		return map;
	}
	/** 把平台资源加入到我的课程中
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/joinMyCurriculum")
	@ResponseBody
	@LogAnno(menuType= "教育培训",menuServer= "平台资源库",instructionsOperate = "课程管理",instructionsType = "把平台资源加入到我的课程中")
	public Object joinMyCurriculum() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		if(Tools.isEmpty(pd.getString("CURRICULUM_ID"))){
			errInfo = "failure";
			map.put("result", errInfo);
			map.put("msg", "参数错误！请联系管理员");
			return map;
		}

		PageData cur = platformCurriculumService.findById(pd);	//根据ID读取
		// 新增课程购买记录
		addRecord(cur);
		// 新增课程
		addCur(cur);
		// 购买课程下所有课件及习题
		List<PageData> chapterList = platformChapterService.listAll(pd);
		if(chapterList != null && chapterList.size() > 0) {
			for(PageData chapter : chapterList) {
				chapter.put("CURRICULUMID",chapter.get("CURRICULUM_ID"));
				// 章节下课件
				List<PageData> videoList = platformVideocoursewareService.listAllByCurriculumId(chapter); // 选择的视频课件
				List<PageData> dataList = platformDatacoursewareService.listAllByCurriculumId(chapter);
				chapter.put("CURRICULUM_ID",cur.get("CURRICULUM_ID"));

				// 新增章节
				addChapter(chapter);
				if(videoList != null && videoList.size() > 0) {
					PageData videoRec = new PageData();
					videoRec.put("TYPE", "1");
					videoRec.put("PATTERN", "2");
					for(PageData video : videoList) {
						videoRec.put("COURSEWARE_ID", video.get("VIDEOCOURSEWARE_ID"));
						// 查看课件是否通过课程购买过
						PageData rec = platformCoursewareRecordService.findByCouId(videoRec);
						String coursewareId = "";
						if(rec != null && rec.get("RECORD_ID") != null && Tools.notEmpty(rec.get("RECORD_ID").toString())) {	// 购买过-获取购买后的课件id
							coursewareId = rec.getString("CORP_COURSEWARE_ID");
						} else {	// 没购买过，进行课件购买
							// 新增课件信息
							if(Tools.notEmpty(cur.getString("CURRICULUM_ID_REMOTE"))){
								video.put("CURRICULUM_ID_REMOTE",cur.getString("CURRICULUM_ID_REMOTE"));
							}
							PageData corpCou = addVideoCourse(video);
							// 购买记录
							purVideoCourseware(video,corpCou.getString("VIDEOCOURSEWARE_ID"));
							//习题
							video.put("COURSEWAREID", video.getString("VIDEOCOURSEWARE_ID"));
							coursewareId = corpCou.getString("VIDEOCOURSEWARE_ID");
							addQuestion(video,coursewareId);
						}
						// 增加 课件-课程中间表
						PageData middle = new PageData();
						middle.put("CHAPTER_ID", chapter.getString("CHAPTER_ID"));
						middle.put("CURRICULUMID", cur.getString("CURRICULUM_ID"));
						middle.put("COURSEWAREID", coursewareId);// 课件
						middle.put("COURSEWARETYPE", "1");
						addCurAndCou(middle);
					}
				}

				if(dataList != null && dataList.size() > 0) {
					PageData dataRec = new PageData();
					dataRec.put("TYPE", "2");
					dataRec.put("PATTERN", "2");
					for(PageData data : dataList) {
						dataRec.put("COURSEWARE_ID", data.get("DATACOURSEWARE_ID"));
						// 查看课件是否通过课程购买过
						PageData rec = platformCoursewareRecordService.findByCouId(dataRec);
						String coursewareId = "";
						if(rec != null && rec.get("RECORD_ID") != null && Tools.notEmpty(rec.get("RECORD_ID").toString())) {
							// 购买过-获取购买后的课件id
							coursewareId = rec.getString("CORP_COURSEWARE_ID");
						} else {
							// 没购买过，进行课件购买
							// 新增课件信息
							PageData corpCou = addDataCourse(data);
							// 购买记录
							purDataCourseware(data,corpCou.getString("DATACOURSEWARE_ID"));
							//习题
							data.put("COURSEWAREID", data.getString("DATACOURSEWARE_ID"));
							coursewareId = corpCou.getString("DATACOURSEWARE_ID");
							addQuestion(data,coursewareId);
						}
						// 增加 课件-课程中间表
						PageData middle = new PageData();
						middle.put("CHAPTER_ID", chapter.getString("CHAPTER_ID"));
						middle.put("CURRICULUMID", cur.getString("CURRICULUM_ID"));
						middle.put("COURSEWAREID", coursewareId);// 课件ID
						middle.put("COURSEWARETYPE", "2");
						addCurAndCou(middle);
					}
				}
			}
		}
		map.put("result", errInfo);
		return map;
	}


	/**
	 * 增加章节
	 * @param pd
	 * @param coursewareId
	 * @throws Exception
	 */
	public void addChapter(PageData chapter) throws Exception{
		chapter.put("CHAPTER_ID", this.get32UUID());
		chapter.put("CREATOR",Jurisdiction.getUSER_ID());
		chapter.put("CREATTIME",DateUtil.date2Str(new Date()));
		chapter.put("OPERATOR",Jurisdiction.getUSER_ID());
		chapter.put("OPERATTIME",DateUtil.date2Str(new Date()));
		chapter.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
		chapter.put("ISDELETE",'0');
		chapterService.save(chapter);
	}
	/**
	 * 增加课程
	 * @param pd
	 * @param coursewareId
	 * @throws Exception
	 */
	public void addCur(PageData cur) throws Exception{
		PageData pd = new PageData();
		pd.put("RECORD_ID",cur.getString("CURRICULUM_ID"));
		cur.put("CURRICULUM_ID", this.get32UUID());
		cur.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
		cur.put("CREATOR", Jurisdiction.getUSER_ID());
		cur.put("OPERATOR", Jurisdiction.getUSER_ID());
		cur.put("CREATTIME", DateUtil.date2Str(new Date()));
		cur.put("OPERATTIME", DateUtil.date2Str(new Date()));
		cur.put("ISDELETE", "0");
		cur.put("ISPLATFORM", "1");
		curriculumService.save(cur);
		List<PageData> coursewareAllList = platformCoursewareRelationService.listAll(pd);//课件字典关系信息，全部的信息
		if(coursewareAllList != null && coursewareAllList.size() > 0) {
			for(PageData relation : coursewareAllList) {
				relation.put("COURSEWARERELATION_ID", UuidUtil.get32UUID());
				relation.put("RECORD_ID", cur.get("CURRICULUM_ID"));
				coursewareRelationService.save(relation);
			}
		}

	}
	/**
	 * 增加课程购买记录
	 * @param pd
	 * @param coursewareId
	 * @throws Exception
	 */
	public void addRecord(PageData cur) throws Exception{
		PageData record = new PageData();
		record.put("RECORD_ID",this.get32UUID());
		record.put("CURRICULUM_ID",cur.get("CURRICULUM_ID"));
		record.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
		record.put("CREATOR",Jurisdiction.getUSER_ID());
		record.put("CREATTIME",DateUtil.date2Str(new Date()));
		record.put("OPERATOR",Jurisdiction.getUSER_ID());
		record.put("OPERATTIME",DateUtil.date2Str(new Date()));
		record.put("CREATORNAME",Jurisdiction.getUsername());
		record.put("OPERATORNAME",Jurisdiction.getUsername());
		platformCurriculumRecordService.save(record);
	}

	/**
	 * 增加习题
	 * @param pd
	 * @param coursewareId
	 * @throws Exception
	 */
	public void addQuestion (PageData pd, String coursewareId) throws Exception{
		List<PageData> questionList =  platformQuestionService.listByCouId(pd);

		// 新增课件下习题信息
		if(questionList != null && questionList.size() > 0) {
			Integer max = 0;
			for(PageData question : questionList) {
				question.put("QUESTION_ID", this.get32UUID());	//主键
				question.put("COURSEWAREID", coursewareId);	//主键
				question.put("CREATOR", Jurisdiction.getUsername());	//添加人
				question.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
				question.put("OPERATOR", Jurisdiction.getUsername());	//修改人
				question.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
				question.put("ISDELETE", "0");	//是否删除(0:有效 1：删除)
				question.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
				question.put("ISPLATFORMPAPER", "0");
				max ++;
				question.put("QUESTIONNUMBER",max); //题号
				questionService.save(question);
			}
		}
	}

	/**
	 * 增加课件课程中间表
	 * @param pd
	 * @param coursewareId
	 * @throws Exception
	 */
	public void addCurAndCou (PageData middle) throws Exception{
		middle.put("KCMIDDLEKJ_ID", UuidUtil.get32UUID());	//主键
		middle.put("CREATOR", Jurisdiction.getUsername());	//添加人
		middle.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
		middle.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		middle.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		middle.put("ISDELETE", "0");	//是否删除(0:有效 1：删除)
		middle.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID\
		kcmiddlekjService.save(middle);
	}

	/**
	 * 购买课件
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public void purDataCourseware(PageData pd, String corpCouId) throws Exception{


//		Double PRICE = 1d;
//		if(!Tools.isEmpty(plaCou.get("PRICE"))){
//			PRICE = Double.parseDouble(plaCou.get("PRICE").toString());
//		}

//		if(0d != PRICE){
//			errInfo = "failure";
//			map.put("pd", "");
//			map.put("msg", "添加失败！该产品暂时不能添加！");
//			map.put("result", errInfo);
//		}

		/** 添加企业购买记录 开始 **/
		PageData record = new PageData();
		record.put("RECORD_ID",this.get32UUID());
		record.put("COURSEWARE_ID",pd.getString("DATACOURSEWARE_ID"));
		record.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
		record.put("CREATOR",pd.get("CREATOR"));
		record.put("CREATTIME",DateUtil.date2Str(new Date()));
		record.put("OPERATOR",pd.get("OPERATOR"));
		record.put("OPERATTIME",DateUtil.date2Str(new Date()));
		record.put("CREATORNAME",pd.get("CREATORNAME"));
		record.put("OPERATORNAME",pd.get("OPERATORNAME"));
		record.put("CORP_COURSEWARE_ID",corpCouId);
		record.put("TYPE","2");
		record.put("PATTERN","2");
		platformCoursewareRecordService.save(record);
		/** 添加企业购买记录 结束 **/
	}

	/**
	 * 新增课件信息
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public PageData addDataCourse(PageData course) throws Exception{

		PageData addPd = new PageData();
		addPd.put("PLATFORMID",  course.get("DATACOURSEWARE_ID"));
		addPd.put("DATACOURSEWARE_ID", this.get32UUID());	//主键 平台主键
		addPd.put("COURSEWARENAME", course.get("COURSEWARENAME"));	//课件名称
		addPd.put("SPEAKER", course.get("teacherName"));	//主讲人
		addPd.put("COURSEWAREINTRODUCE", course.get("COURSEWAREINTRODUCE"));	//课件介绍
		addPd.put("COURSEWAREFILES", course.get("COURSEWAREFILES"));	//课件文件路径
		addPd.put("COURSEWARECAPTURE", course.get("COURSEWARECAPTURE"));	//课件截图

		addPd.put("COURSEWAREUSERS", course.get("COURSEWAREUSERS"));	//视频课件时长(秒)
		addPd.put("CREATOR", Jurisdiction.getUsername());	//添加人
		addPd.put("CREATTIME",  course.get("CREATTIME"));	//添加时间 平台创建时间
		addPd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		addPd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		addPd.put("ISDELETE", "0");	//是否删除(0:有效 1：删除)
		addPd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
		addPd.put("STATE", "0");	//课件状态 0启用、1禁用

		addPd.put("TRAINTYPE", Jurisdiction.getCORPTRAINTYPE());//企业的培训行业类型
		addPd.put("POSTTYPE",  course.get("POSTTYPE"));//岗位培训类型
		addPd.put("ONTLINETYPE",  course.get("ONTLINETYPE"));//岗位培训类型
		addPd.put("CLASSHOUR",  course.get("CLASSHOUR"));//课时，单位分鐘
		addPd.put("ONTLINETYPE",  course.get("ONTLINETYPE"));//岗位培训类型
		addPd.put("ISPLATFORM", "2");//是否未平台
		datacoursewareService.save(addPd);
		PageData pd = new PageData();
		pd.put("RECORD_ID",course.getString("DATACOURSEWARE_ID"));
		List<PageData> coursewareAllList = platformCoursewareRelationService.listAll(pd);//课件字典关系信息，全部的信息
		if(coursewareAllList != null && coursewareAllList.size() > 0) {
			for(PageData relation : coursewareAllList) {
				relation.put("COURSEWARERELATION_ID", UuidUtil.get32UUID());
				relation.put("RECORD_ID", addPd.get("DATACOURSEWARE_ID"));
				coursewareRelationService.save(relation);
			}
		}

		return addPd;
	}

	/**
	 * 购买课件
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public void purVideoCourseware(PageData pd, String corpCouId) throws Exception{
//		Double PRICE = 1d;
//		if(!Tools.isEmpty(plaCou.get("PRICE"))){
//			PRICE = Double.parseDouble(plaCou.get("PRICE").toString());
//		}

//		if(0d != PRICE){
//			errInfo = "failure";
//			map.put("pd", "");
//			map.put("msg", "添加失败！该产品暂时不能添加！");
//			map.put("result", errInfo);
//		}

		/** 添加企业购买记录 开始 **/
		PageData record = new PageData();
		record.put("RECORD_ID",this.get32UUID());
		record.put("COURSEWARE_ID",pd.get("VIDEOCOURSEWARE_ID"));
		record.put("CORPINFO_ID",pd.get("CORPINFO_ID"));
		record.put("CREATOR",pd.get("CREATOR"));
		record.put("CREATTIME",DateUtil.date2Str(new Date()));
		record.put("OPERATOR",pd.get("OPERATOR"));
		record.put("OPERATTIME",DateUtil.date2Str(new Date()));
		record.put("CREATORNAME",pd.get("CREATORNAME"));
		record.put("OPERATORNAME",pd.get("OPERATORNAME"));
		record.put("CORP_COURSEWARE_ID",corpCouId);
		record.put("TYPE","1");
		record.put("PATTERN","2");
		platformCoursewareRecordService.save(record);
		/** 添加企业购买记录 结束 **/
	}

	/**
	 * 新增课件信息
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public PageData addVideoCourse(PageData course) throws Exception{
		PageData addPd = new PageData();
		addPd.put("PLATFORMID",  course.get("VIDEOCOURSEWARE_ID"));
		addPd.put("VIDEOCOURSEWARE_ID_REMOTE",  course.get("VIDEOCOURSEWARE_ID_REMOTE"));
		addPd.put("VIDEOCOURSEWARE_ID", this.get32UUID());	//主键 平台主键
		addPd.put("COURSEWARENAME", course.get("COURSEWARENAME"));	//课件名称
		System.out.println(course.get("teacherName"));
		addPd.put("SPEAKER", course.get("teacherName"));	//主讲人
		addPd.put("COURSEWAREINTRODUCE", course.get("COURSEWAREINTRODUCE"));	//课件介绍
		addPd.put("VIDEOFILES", course.get("VIDEOFILES"));	//课件文件路径
		addPd.put("VIDEOCAPTURE", course.get("VIDEOCAPTURE"));	//课件截图

		addPd.put("VIDEOTIME", course.get("VIDEOTIME"));	//视频课件时长(秒)
		addPd.put("CREATOR", Jurisdiction.getUsername());	//添加人
		addPd.put("CREATTIME",  course.get("CREATTIME"));	//添加时间 平台创建时间
		addPd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		addPd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		addPd.put("ISDELETE", "0");	//是否删除(0:有效 1：删除)
		addPd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
		addPd.put("STATE", "0");	//课件状态 0启用、1禁用

		addPd.put("TRAINTYPE", Jurisdiction.getCORPTRAINTYPE());//企业的培训行业类型
		addPd.put("POSTTYPE",  course.get("POSTTYPE"));//岗位培训类型
		addPd.put("ONTLINETYPE",  course.get("ONTLINETYPE"));//岗位培训类型
		addPd.put("CLASSHOUR",  course.get("CLASSHOUR"));//课时，单位分鐘
		addPd.put("ONTLINETYPE",  course.get("ONTLINETYPE"));//岗位培训类型
		if(course.get("VIDEOCOURSEWARE_ID_REMOTE")!=null){
			addPd.put("ISPLATFORM","3");//是否商城
		}else{
			addPd.put("ISPLATFORM",  "2");//是否平台
		}
		addPd.put("CURRICULUM_ID_REMOTE",  course.get("CURRICULUM_ID_REMOTE"));//原课程ID
		videocoursewareService.save(addPd);
		PageData pd = new PageData();
		pd.put("RECORD_ID",course.getString("VIDEOCOURSEWARE_ID"));
		List<PageData> coursewareAllList = platformCoursewareRelationService.listAll(pd);//课件字典关系信息，全部的信息
		if(coursewareAllList != null && coursewareAllList.size() > 0) {
			for(PageData relation : coursewareAllList) {
				relation.put("COURSEWARERELATION_ID", UuidUtil.get32UUID());
				relation.put("RECORD_ID", addPd.get("VIDEOCOURSEWARE_ID"));
				coursewareRelationService.save(relation);
			}
		}

		return addPd;
	}
}
