package com.jeeplus.modules.filing.approvalStage.web;

import com.google.common.collect.Lists;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.filing.approvalStage.entity.StudyStage;
import com.jeeplus.modules.filing.approvalStage.entity.StudyStage;
import com.jeeplus.modules.filing.approvalStage.service.StudyStageService;
import com.jeeplus.modules.filing.approvalStage.service.StudyStageService;
import com.jeeplus.modules.filing.approvalStage.service.StudyStageService;
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.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 可研阶段Controller
 * @author qy
 * @version 2021-03-24
 */
@Controller
@RequestMapping(value = "${adminPath}/filing/studyStage")
public class StudyStageController extends BaseController {

	@Autowired
	private StudyStageService studyStageService;
	
	@ModelAttribute
	public StudyStage get(@RequestParam(required=false) String id) {
		StudyStage entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = studyStageService.get(id);
		}
		if (entity == null){
			entity = new StudyStage();
		}
		return entity;
	}
	
	/**
	 * 可研阶段列表页面
	 */
	@RequiresPermissions("test:filing:studyStage:list")
	@RequestMapping(value = {"list", ""})
	public String list(StudyStage studyStage, Model model) {
		model.addAttribute("studyStage", studyStage);
		return "modules/filing/studyStage/studyStageList";
	}
	
		/**
	 * 可研阶段列表数据
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:studyStage:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(StudyStage studyStage, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<StudyStage> page = studyStageService.findPage(new Page<StudyStage>(request, response,-1), studyStage);
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加,可研阶段表单页面-父项目
	 */
	@RequiresPermissions(value={"test:filing:studyStage:view","test:filing:studyStage:add","test:filing:studyStage:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(StudyStage studyStage, Model model) {
		model.addAttribute("studyStage", studyStage);
		return "modules/filing/studyStage/studyStageForm";
	}

	/**
	 * 查看，增加,可研阶段表单页面-子项目
	 */
	@RequiresPermissions(value={"test:filing:studyStage:view","test:filing:studyStage:add","test:filing:studyStage:edit"},logical=Logical.OR)
	@RequestMapping(value = "childForm")
	public String childForm(StudyStage studyStage, Model model) {
		model.addAttribute("studyStage", studyStage);
		return "modules/filing/studyStage/studyStageChildForm";
	}
	
	/**
	 * 保存可研阶段
	 */
	@ResponseBody
	@RequiresPermissions(value={"test:filing:studyStage:add","test:filing:studyStage:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	public AjaxJson save(StudyStage studyStage, Model model) throws Exception{
		AjaxJson j = new AjaxJson();
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(studyStage);
		if (StringUtils.isNotBlank(errMsg)){
			j.setSuccess(false);
			j.setMsg(errMsg);
			return j;
		}
		//新增或编辑表单保存
		studyStageService.save(studyStage);//保存
		j.setSuccess(true);
		j.setMsg("保存可研阶段成功");
		return j;
	}

	/**
	 * 保存可研阶段
	 */
	@ResponseBody
	@RequiresPermissions(value={"test:filing:studyStage:add","test:filing:studyStage:edit"},logical=Logical.OR)
	@RequestMapping(value = "updateSave")
	public AjaxJson updateSave(StudyStage studyStage, Model model) throws Exception{
		AjaxJson j = new AjaxJson();
		/**
		 * 后台hibernate-validation插件校验
		 */
		String errMsg = beanValidator(studyStage);
		if (StringUtils.isNotBlank(errMsg)){
			j.setSuccess(false);
			j.setMsg(errMsg);
			return j;
		}
		//新增或编辑表单保存
		studyStageService.save(studyStage);//保存
		j.setSuccess(true);
		j.setMsg("保存可研阶段成功");
		return j;
	}


	/**
	 * 删除可研阶段
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:studyStage:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(StudyStage studyStage) {
		AjaxJson j = new AjaxJson();
		studyStageService.delete(studyStage);
		j.setMsg("删除可研阶段成功");
		return j;
	}
	
	/**
	 * 批量删除可研阶段
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:studyStage:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			studyStageService.delete(studyStageService.get(id));
		}
		j.setMsg("删除可研阶段成功");
		return j;
	}
	
	/**
	 * 导出excel文件
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:studyStage:export")
    @RequestMapping(value = "export")
    public AjaxJson exportFile(StudyStage studyStage, HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		try {
            String fileName = "可研阶段"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
			//查询所有父级的信息
			List<StudyStage> list = studyStageService.findList(studyStage);
			List<StudyStage> returnList = new ArrayList<>();
			//根据父级，查询所有子级的信息
			for (StudyStage a :
					list) {
				List<StudyStage> childList = studyStageService.findChildList(a);
				returnList.add(a);
				for (StudyStage b :
						childList) {
					returnList.add(b);
				}
			}
    		new ExportExcel("可研阶段", StudyStage.class).setDataList(returnList).write(response, fileName).dispose();
    		j.setSuccess(true);
    		j.setMsg("导出成功！");
    		return j;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导出可研阶段记录失败！失败信息："+e.getMessage());
		}
			return j;
    }
    
    @ResponseBody
    @RequestMapping(value = "detail")
	public Map<String, Object> detail(StudyStage studyStage, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<StudyStage> page = studyStageService.findChildPage(new Page<StudyStage>(request, response), studyStage);
		return getBootstrapData(page);
	}
	

	/**
	 * 导入Excel数据
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:studyStage:import")
    @RequestMapping(value = "import")
   	public AjaxJson importFile(@RequestParam("file")MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		try {
			ImportExcel ei = new ImportExcel(file, 1, 0);
			List<StudyStage> list = ei.getDataList(StudyStage.class);
			for (StudyStage studyStage :
					list) {
				studyStageService.save(studyStage);
			}
			j.setMsg( "已成功导入条可研阶段记录");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导入可研阶段失败！失败信息："+e.getMessage());
		}
		return j;
    }
	
	/**
	 * 下载导入可研阶段数据模板
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:studyStage:import")
    @RequestMapping(value = "import/template")
     public AjaxJson importFileTemplate(HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		try {
            String fileName = "可研阶段数据导入模板.xlsx";
    		List<StudyStage> list = Lists.newArrayList();
    		new ExportExcel("可研阶段数据", StudyStage.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg( "导入模板下载失败！失败信息："+e.getMessage());
		}
		return j;
    }
	

}