package com.zcloud.controller.task;

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.paper.StageexampaperService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.ObjectExcelView;
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 org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 说明：阶段考试试卷表
 * 作者：luoxiaobao
 * 时间：2021-07-04
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/app/stageexampaper")
public class AppStageexampaperController extends BaseController {

	@Autowired
	private StageexampaperService stageexampaperService;


	/**修改（考试开考或者考试人员已考试，试卷不能再变动）
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/updateExamState")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "阶段考试试卷表",instructionsOperate = "阶段考试试卷表",instructionsType = "修改")
	public Object updateExamState() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("OPERATOR", pd.getString("USERNAME"));	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		stageexampaperService.updateExamState(pd);
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	/**通过试卷ID获取阶段试卷相关数据（试卷、试卷试题）
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/getExam")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "阶段考试试卷表",instructionsOperate = "阶段考试试卷表",instructionsType = "通过试卷ID获取阶段试卷相关数据")
	public Object getExam() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = stageexampaperService.getExam(pd);	//通过试卷ID获取数据
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}















	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/add")
	// @RequiresPermissions( value = {"stageexampaper:add", "curriculum:add"}, logical = Logical.OR)
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "阶段考试试卷表",instructionsOperate = "阶段考试试卷表",instructionsType = "新增")
	public Object add() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String date = DateUtil.date2Str(new Date());
		pd.put("STAGEEXAMPAPER_ID", this.get32UUID());	//主键
		pd.put("CREATOR", Jurisdiction.getUsername());	//添加人
		pd.put("CREATTIME", date);	//添加时间
		pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		pd.put("OPERATTIME", date);	//修改时间
		pd.put("EXAMSTATE", "0");	//试卷状态(0:初始 1：已考试)
		pd.put("ISDELETE", "0");	//是否删除(0:有效 1：删除)
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
		stageexampaperService.save(pd);
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	/**删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
	// @RequiresPermissions( value = {"stageexampaper:del", "curriculum:del"}, logical = Logical.OR)
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "阶段考试试卷表",instructionsOperate = "阶段考试试卷表",instructionsType = "删除")
	public Object delete() throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		stageexampaperService.delete(pd);
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/edit")
	// @RequiresPermissions( value = {"stageexampaper:edit", "curriculum:edit"}, logical = Logical.OR)
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "阶段考试试卷表",instructionsOperate = "阶段考试试卷表",instructionsType = "修改")
	public Object edit() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		stageexampaperService.edit(pd);
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	/**列表
	 * @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());	//企业ID
		page.setPd(pd);
		List<PageData>	varList = stageexampaperService.list(page);	//列出Stageexampaper列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/listCurriculum")
	// @RequiresPermissions( value = {"stageexampaper:list", "curriculum:list"}, logical = Logical.OR)
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "阶段考试试卷表",instructionsOperate = "阶段考试试卷表",instructionsType = "列表")
	public Object listCurriculum(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
		page.setPd(pd);
		List<PageData>	varList = stageexampaperService.listCurriculum(page);	//列出Stageexampaper列表
		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 = stageexampaperService.findById(pd);	//根据ID读取
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	/**批量删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAll")
	// @RequiresPermissions( value = {"stageexampaper:del", "curriculum:del"}, logical = Logical.OR)
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "阶段考试试卷表",instructionsOperate = "阶段考试试卷表",instructionsType = "批量删除")
	public Object deleteAll() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		String DATA_IDS = pd.getString("DATA_IDS");
		if(Tools.notEmpty(DATA_IDS)){
			String ArrayDATA_IDS[] = DATA_IDS.split(",");
			pd.put("ArrayDATA_IDS", ArrayDATA_IDS);	//待删除ids
			stageexampaperService.deleteAll(pd);
			errInfo = "success";
		}else{
			errInfo = "error";
		}
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**导出到excel
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/excel")
	// @RequiresPermissions("toExcel")
	@LogAnno(menuType= "手机",menuServer= "阶段考试试卷表",instructionsOperate = "阶段考试试卷表",instructionsType = "导出到excel")
	public ModelAndView exportExcel() throws Exception{
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		Map<String,Object> dataMap = new HashMap<String,Object>();
		List<String> titles = new ArrayList<String>();
		titles.add("试卷名称");	//1
		titles.add("考试时间（开始时间）");	//2
		titles.add("答卷时间（持续时间）");	//3
		titles.add("试卷分数");	//4
		titles.add("适用人员");	//5
		titles.add("试题数量（10、15、20、25）");	//6
		titles.add("试卷状态");	//7
		titles.add("添加人");	//8
		titles.add("添加时间");	//9
		titles.add("修改人");	//10
		titles.add("修改时间");	//11
		titles.add("是否删除(0:有效 1：删除)");	//12
		titles.add("企业ID");	//13
		dataMap.put("titles", titles);
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
		List<PageData> varOList = stageexampaperService.listAll(pd);
		List<PageData> varList = new ArrayList<PageData>();
		for(int i=0;i<varOList.size();i++){
			PageData vpd = new PageData();
			vpd.put("var1", varOList.get(i).getString("EXAMNAME"));	    //1
			vpd.put("var2", varOList.get(i).getString("EXAMTIME"));	    //2
			vpd.put("var3", varOList.get(i).getString("ANSWERSHEETTIME"));	    //3
			vpd.put("var4", varOList.get(i).getString("EXAMSCORE"));	    //4
			vpd.put("var5", varOList.get(i).getString("CURRICULUMUSERS"));	    //5
			vpd.put("var6", varOList.get(i).getString("QUESTIONNUM"));	    //6
			vpd.put("var7", varOList.get(i).getString("EXAMSTATE"));	    //7
			vpd.put("var8", varOList.get(i).getString("CREATOR"));	    //8
			vpd.put("var9", varOList.get(i).getString("CREATTIME"));	    //9
			vpd.put("var10", varOList.get(i).getString("OPERATOR"));	    //10
			vpd.put("var11", varOList.get(i).getString("OPERATTIME"));	    //11
			vpd.put("var12", varOList.get(i).get("ISDELETE").toString());	//12
			vpd.put("var13", varOList.get(i).getString("CORPINFO_ID"));	    //13
			varList.add(vpd);
		}
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv,dataMap);
		return mv;
	}

	/**列表（考试结果列表）
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/listSummary")
	// @RequiresPermissions( value = {"stageexampaper:list", "curriculum:list"}, logical = Logical.OR)
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "阶段考试试卷表",instructionsOperate = "阶段考试试卷表",instructionsType = "考试结果列表")
	public Object listSummary(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
		page.setPd(pd);
		List<PageData>	varList = stageexampaperService.listSummary(page);	//列出Stageexampaper列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

}
