package com.zcloud.controller.platformPaper;

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.paper.StageExamPaperInputQuestionService;
import com.zcloud.service.paper.StageexampaperInputService;
import com.zcloud.service.platformPaper.PlatformStaExaPapInpRecService;
import com.zcloud.service.platformPaper.PlatformStageExamPaperInputQuestionService;
import com.zcloud.service.platformPaper.PlatformStageexampaperInputService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
import org.apache.shiro.authz.annotation.Logical;
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-12-17
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/platform/stageexampaperinput")
public class PlatformStageexampaperInputController extends BaseController {

	@Autowired
	private PlatformStageexampaperInputService platformStageExamPaperInputService;
	@Autowired
	private PlatformStageExamPaperInputQuestionService platformStageExamPaperInputQuestionService;
	@Autowired
	private PlatformStaExaPapInpRecService platformStaExaPapInpRecService;
	@Autowired
	private StageexampaperInputService StageExamPaperInputService;
	@Autowired
	private StageExamPaperInputQuestionService stageExamPaperInputQuestionService;
	@Autowired
	private QuestionService questionService;


	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	// @RequiresPermissions( value = {"stageexampaper:list", "curriculum:list"}, logical = Logical.OR)
	@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 = platformStageExamPaperInputService.list(page);	//列出StageexampaperInput列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	 /**去修改页面获取数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goEdit")
//	// @RequiresPermissions( value = {"stageexampaper:edit", "curriculum:edit"}, logical = Logical.OR)
	@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 = platformStageExamPaperInputService.findById(pd);	//根据ID读取
		List<PageData> inputQue = platformStageExamPaperInputQuestionService.listAllAndQuestion(pd); //根据试卷id获取生成的习题
		map.put("inputQue", inputQue);
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}
	/** 把平台资源加入到我的试卷
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/joinMyPaper")
	@ResponseBody
	@LogAnno(menuType= "教育培训",menuServer= "平台资源库",instructionsOperate = "试卷管理",instructionsType = "购买试卷")
	public Object joinMyPaper() 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("STAGEEXAMPAPERINPUT_ID"))){
			errInfo = "failure";
			map.put("result", errInfo);
			map.put("msg", "参数错误！请联系管理员");
			return map;
		}
		pd.put("RECORD_ID", this.get32UUID());
		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("CREATTIME",DateUtil.date2Str(new Date()));
		pd.put("OPERATTIME",DateUtil.date2Str(new Date()));
		platformStaExaPapInpRecService.save(pd);

//		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 course = addPurRecord(pd);
		// 新增试卷信息
		PageData paper = platformStageExamPaperInputService.findById(pd);
		List<PageData> platformPaperQuestions = platformStageExamPaperInputQuestionService.listAllAndQuestion(pd);
		PageData newPaper = addPaper(paper);
		if(platformPaperQuestions != null && platformPaperQuestions.size() > 0) {
			for(PageData ppq : platformPaperQuestions) {
				PageData question = addQuestion(ppq);
				addInputQuestion(newPaper, question, ppq);
			}
		}
		map.put("result", errInfo);
		return map;
	}

	public PageData addPaper(PageData paper)throws Exception{
		PageData newPaper = new PageData();
		newPaper.put("STAGEEXAMPAPERINPUT_ID", this.get32UUID());
		newPaper.put("EXAMNAME", paper.get("EXAMNAME"));
		newPaper.put("TRAINTYPE", paper.get("TRAINTYPE"));
		newPaper.put("POSTTYPE", paper.get("POSTTYPE"));
		newPaper.put("EXAMSCORE", paper.get("EXAMSCORE"));
		newPaper.put("PASSSCORE", paper.get("PASSSCORE"));
		newPaper.put("DUOXUANTICOUNT", paper.get("DUOXUANTICOUNT"));
		newPaper.put("DUOXUANTINUMBER", paper.get("DUOXUANTINUMBER"));
		newPaper.put("DANYUANTICOUNT", paper.get("DANYUANTICOUNT"));
		newPaper.put("DANXUANTINUMBER", paper.get("DANXUANTINUMBER"));
		newPaper.put("PANDUITICOUNT", paper.get("PANDUITICOUNT"));
		newPaper.put("PANDUITINUMBER", paper.get("PANDUITINUMBER"));
		newPaper.put("EXAMSTATE", paper.get("EXAMSTATE"));
		newPaper.put("CREATOR", Jurisdiction.getUsername());
		newPaper.put("CREATTIME", DateUtil.date2Str(new Date()));
		newPaper.put("OPERATOR", Jurisdiction.getUsername());
		newPaper.put("OPERATTIME", DateUtil.date2Str(new Date()));
		newPaper.put("ISDELETE", "0");
		newPaper.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
		newPaper.put("STATE", "0");
		newPaper.put("ISPLATFORM", "1");
		StageExamPaperInputService.save(newPaper);
		return newPaper;
	}

	public PageData addQuestion(PageData ppq)throws Exception{
		PageData question = new PageData();
		question.put("QUESTION_ID", this.get32UUID());	//主键
		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", "1");
		question.put("QUESTIONNUMBER",ppq.get("QUESTIONNUMBER"));
		question.put("QUESTIONTYPE",ppq.get("QUESTIONTYPE"));
		question.put("QUESTIONDRY",ppq.get("QUESTIONDRY"));
		question.put("OPTIONA",ppq.get("OPTIONA"));
		question.put("OPTIONB",ppq.get("OPTIONB"));
		question.put("OPTIONC",ppq.get("OPTIONC"));
		question.put("OPTIOND",ppq.get("OPTIOND"));
		question.put("ANSWER",ppq.get("ANSWER"));
		question.put("COURSEWAREID",ppq.get("COURSEWARE_ID"));
		question.put("COURSEWARETYPE",ppq.get("COURSEWARETYPE"));
		questionService.save(question);
		return question;
	}

	public void addInputQuestion(PageData newPaper, PageData question, PageData ppq)throws Exception{
		PageData sepiq = new PageData();
		sepiq.put("STAGEEXAMPAPER_INPUTQUESTION_ID", this.get32UUID());
		sepiq.put("STAGEEXAMPAPERINPUT_ID", newPaper.get("STAGEEXAMPAPERINPUT_ID"));
		sepiq.put("COURSEWARETYPE", ppq.get("COURSEWARETYPE"));
		sepiq.put("COURSEWARE_ID", ppq.get("COURSEWARE_ID"));
		sepiq.put("QUESTION_ID", question.get("QUESTION_ID"));
		sepiq.put("CREATOR", Jurisdiction.getUsername());	//添加人
		sepiq.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
		sepiq.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		sepiq.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		sepiq.put("ISDELETE", "0");	//是否删除(0:有效 1：删除)
		sepiq.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
		sepiq.put("QUESTIONSCORE", ppq.get("QUESTIONSCORE"));	//企业ID
		stageExamPaperInputQuestionService.save(sepiq);
	}

}
