package com.zcloud.controller.platformCourseware;

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.QuestionService;
import com.zcloud.service.courseware.VideoCoursewareService;
import com.zcloud.service.platformCourseware.PlatformCoursewareRecordService;
import com.zcloud.service.platformCourseware.PlatformQuestionService;
import com.zcloud.service.platformCourseware.PlatformVideoCoursewareService;
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-06-26
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/platform/videocourseware")
public class PlatformVideoCoursewareController extends BaseController {

	@Autowired
	private PlatformVideoCoursewareService platformVideoCoursewareService;
	@Autowired
	private PlatformCoursewareRecordService platformCoursewareRecordService;
	@Autowired
	private PlatformQuestionService platformQuestionService;
	@Autowired
	private VideoCoursewareService videocoursewareService;
	@Autowired
	private QuestionService questionService;
	@Autowired
	private PlatformCoursewareRelationService platformCoursewareRelationService;
	@Autowired
	private CoursewareRelationService coursewareRelationService;

//	/**列表 去平台请求数据
//	 * @param page
//	 * @throws Exception
//	 */
//	@RequestMapping(value="/list")
//	// @RequiresPermissions("courseware:list")
//	@ResponseBody
//	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());
//			String corpInfoID = Jurisdiction.getCORPINFO_ID();
//			String url = "/purchase/getVideoCoursewareList";
//		Object [] params =  new Object[]{"showCount","currentPage","KEYWORDS","corpInfoID"}; // 参数名
//
//		Object [] values =new Object[]{page.getShowCount(),page.getCurrentPage(),pd.getString("KEYWORDS"),corpInfoID}; // 参数值
//		List<NameValuePair> paramsList = HttpClientService.getParams(params, values); // 获取参数对象
//		/**
//		 * 发送post
//		 */
//		Object result2 = HttpClientService.sendPost(url, paramsList);
//		JSONObject jSONObject = JSONObject.parseObject(result2.toString());
//
//		map.put("varList", jSONObject.get("varList"));
//		map.put("page", jSONObject.get("page"));
//		map.put("result", errInfo);
//		return map;
//	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	// @RequiresPermissions("courseware: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());
		pd.put("TRAINTYPE", Jurisdiction.getCORPTRAINTYPE());
		page.setPd(pd);
		List<PageData>	varList = platformVideoCoursewareService.listByCorpinfo(page);	//列出VideocoursewareCorpInfoRecord列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	/** 把平台资源加入到我的课件中
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/joinMyCourseware")
	// @RequiresPermissions("courseware:list")
	@ResponseBody
	@LogAnno(menuType= "教育培训",menuServer= "平台资源库",instructionsOperate = "课件资源管理",instructionsType = "把平台资源加入到我的课件中")
	public Object joinMyCourseware() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String corpInfoID = Jurisdiction.getCORPINFO_ID();
		String userID = Jurisdiction.getUSER_ID();
		if(Tools.isEmpty(pd.getString("COURSEWARE_ID"))){
			errInfo = "failure";
			map.put("result", errInfo);
			map.put("msg", "参数错误！请联系管理员");
			return map;
		}
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
		pd.put("CREATOR", Jurisdiction.getUSER_ID());
		pd.put("OPERATOR", Jurisdiction.getUSER_ID());
		pd.put("CREATORNAME", Jurisdiction.getUsername());
		pd.put("OPERATORNAME", Jurisdiction.getUsername());
		pd.put("TYPE", "1");
//		String url = "/purchase/joinMyCourseware";
//		Object [] params =  new Object[]{"coursewareID","corpInfoID","userID"}; // 参数名
//		Object [] values =new Object[]{coursewareId,corpInfoID,userID}; // 参数值
//		List<NameValuePair> paramsList = HttpClientService.getParams(params, values); // 获取参数对象
//		/**
//		 * 发送post
//		 */
//		Object result2 = HttpClientService.sendPost(url, paramsList);
//		JSONObject jSONObject = JSONObject.parseObject(result2.toString());
//		if(Tools.isEmpty(jSONObject.get("result"))){ // 返回信息失败
//			errInfo = "failure";
//			map.put("result", errInfo);
//			map.put("msg", "添加失败！请联系管理员");
//			return map;
//		}
//		if(!"success".equals(jSONObject.getString("result"))){ //返回信息提示错误
//			errInfo = "failure";
//			map.put("result", errInfo);
//			map.put("msg", jSONObject.get("msg"));
//			return map;
//		}
//		JSONObject valMap = JSONObject.parseObject(jSONObject.get("pd").toString()); // 返回的对象
		// 新增课件信息
		PageData corpCou = addCourse(pd);
		// 购买记录
		purchaseCourseware(pd,corpCou.getString("VIDEOCOURSEWARE_ID"));
		//习题
		pd.put("COURSEWAREID", pd.getString("COURSEWARE_ID"));
		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", corpCou.getString("VIDEOCOURSEWARE_ID"));	//主键
				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
				max ++;
				question.put("QUESTIONNUMBER",max); //题号
				question.put("ISPLATFORMPAPER", "0");
				questionService.save(question);
			}
		}



		map.put("result", errInfo);
		return map;
	}

	/**
	 * 购买课件
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public void purchaseCourseware(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","1");
		platformCoursewareRecordService.save(record);
		/** 添加企业购买记录 结束 **/
	}

	/**
	 * 新增课件信息
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public PageData addCourse(PageData pd) throws Exception{
		pd.put("VIDEOCOURSEWARE_ID", pd.getString("COURSEWARE_ID"));
		PageData course = new PageData();
		course = platformVideoCoursewareService.findById(pd);

		pd.put("RECORD_ID",pd.getString("VIDEOCOURSEWARE_ID"));
		List<PageData> coursewareAllList = platformCoursewareRelationService.listAll(pd);//课件字典关系信息，全部的信息


		PageData addPd = new PageData();
		addPd.put("PLATFORMID",  course.get("VIDEOCOURSEWARE_ID"));
		addPd.put("VIDEOCOURSEWARE_ID", this.get32UUID());	//主键 平台主键
		addPd.put("COURSEWARENAME", course.get("COURSEWARENAME"));	//课件名称
		addPd.put("SPEAKER", course.get("SPEAKERNAME"));	//主讲人
		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("CURRENCY",course.get("CURRENCY"));  //是否是通用课件
		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",  "1");//是否未平台
		if(course.get("CURRICULUM_ID_REMOTE")!=null){
			if(Tools.notEmpty(course.get("CURRICULUM_ID_REMOTE").toString())){
				addPd.put("CURRICULUM_ID_REMOTE",course.get("CURRICULUM_ID_REMOTE"));
			}
		}
		if(course.get("VIDEOCOURSEWARE_ID_REMOTE")!=null){
			if(Tools.notEmpty(course.get("VIDEOCOURSEWARE_ID_REMOTE").toString())){
				addPd.put("VIDEOCOURSEWARE_ID_REMOTE",course.get("VIDEOCOURSEWARE_ID_REMOTE"));
			}
		}
		videocoursewareService.save(addPd);
		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;
	}
}
