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.ProjectOverview;
import com.jeeplus.modules.filing.approvalStage.entity.ProjectOverview;
import com.jeeplus.modules.filing.approvalStage.entity.StudyStage;
import com.jeeplus.modules.filing.approvalStage.service.ProjectOverviewService;
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-29
 */
@Controller
@RequestMapping(value = "${adminPath}/filing/projectOverview")
public class ProjectOverviewController extends BaseController {

	@Autowired
	private ProjectOverviewService projectOverviewService;
	
	@ModelAttribute
	public ProjectOverview get(@RequestParam(required=false) String id) {
		ProjectOverview entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = projectOverviewService.get(id);
		}
		if (entity == null){
			entity = new ProjectOverview();
		}
		return entity;
	}
	
	/**
	 * 项目概述列表页面
	 */
	@RequiresPermissions("test:filing:projectOverview:list")
	@RequestMapping(value = {"list", ""})
	public String list(ProjectOverview projectOverview, Model model) {
		model.addAttribute("projectOverview", projectOverview);
		return "modules/filing/projectOverview/projectOverviewList";
	}
	
		/**
	 * 项目概述列表数据
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:projectOverview:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(ProjectOverview projectOverview, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProjectOverview> page = projectOverviewService.findPage(new Page<ProjectOverview>(request, response,-1), projectOverview);
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加,项目概述表单页面-父项目
	 */
	@RequiresPermissions(value={"test:filing:projectOverview:view","test:filing:projectOverview:add","test:filing:projectOverview:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(ProjectOverview projectOverview, Model model) {
		model.addAttribute("projectOverview", projectOverview);
		return "modules/filing/projectOverview/projectOverviewForm";
	}

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

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


	/**
	 * 删除项目概述
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:projectOverview:del")
	@RequestMapping(value = "delete")
	public AjaxJson delete(ProjectOverview projectOverview) {
		AjaxJson j = new AjaxJson();
		projectOverviewService.delete(projectOverview);
		j.setMsg("删除项目概述成功");
		return j;
	}
	
	/**
	 * 批量删除项目概述
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:projectOverview:del")
	@RequestMapping(value = "deleteAll")
	public AjaxJson deleteAll(String ids) {
		AjaxJson j = new AjaxJson();
		String idArray[] =ids.split(",");
		for(String id : idArray){
			projectOverviewService.delete(projectOverviewService.get(id));
		}
		j.setMsg("删除项目概述成功");
		return j;
	}
	
	/**
	 * 导出excel文件
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:projectOverview:export")
    @RequestMapping(value = "export")
    public AjaxJson exportFile(ProjectOverview projectOverview, HttpServletRequest request, HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		try {
            String fileName = "项目概述"+DateUtils.getDate("yyyyMMddHHmmss")+".xlsx";
			//查询所有父级的信息
			List<ProjectOverview> list = projectOverviewService.findList(projectOverview);
			List<ProjectOverview> returnList = new ArrayList<>();
			//根据父级，查询所有子级的信息
			for (ProjectOverview a :
					list) {
				List<ProjectOverview> childList = projectOverviewService.findChildList(a);
				returnList.add(a);
				for (ProjectOverview b :
						childList) {
					returnList.add(b);
				}
			}
    		new ExportExcel("项目概述", ProjectOverview.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(ProjectOverview projectOverview, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProjectOverview> page = projectOverviewService.findChildPage(new Page<ProjectOverview>(request, response), projectOverview);
		return getBootstrapData(page);
	}
	

	/**
	 * 导入Excel数据
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:projectOverview: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<ProjectOverview> list = ei.getDataList(ProjectOverview.class);
			for (ProjectOverview projectOverview :
					list) {
				projectOverviewService.save(projectOverview);
			}
			j.setMsg( "已成功导入条项目概述记录");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导入项目概述失败！失败信息："+e.getMessage());
		}
		return j;
    }
	
	/**
	 * 下载导入项目概述模板
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:projectOverview: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("项目概述", ProjectOverview.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg( "导入模板下载失败！失败信息："+e.getMessage());
		}
		return j;
    }
	

}