package com.jeeplus.modules.filing.earlyEngineering.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.constructionStage.entity.ConstructionStage;
import com.jeeplus.modules.filing.earlyEngineering.entity.EarlyEngineering;
import com.jeeplus.modules.filing.earlyEngineering.service.EarlyEngineeringService;
import com.jeeplus.modules.filing.filingImages.service.FilingImagesService;
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;

/**
 * 工程前期阶段MAPPER接口
 * @author qy
 * @version 2021-04-06
 */
@Controller
@RequestMapping(value = "${adminPath}/filing/earlyEngineering")
public class EarlyEngineeringController extends BaseController {

	@Autowired
	private EarlyEngineeringService earlyEngineeringService;

	@Autowired
	private FilingImagesService filingImagesService;

	@ModelAttribute
	public EarlyEngineering get(@RequestParam(required=false) String id) {
		EarlyEngineering entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = earlyEngineeringService.get(id);
		}
		if (entity == null){
			entity = new EarlyEngineering();
		}
		return entity;
	}
	
	/**
	 * 工程前期阶段列表页面
	 */
	@RequiresPermissions("test:filing:earlyEngineering:list")
	@RequestMapping(value = {"list", ""})
	public String list(EarlyEngineering earlyEngineering, Model model) {
		model.addAttribute("earlyEngineering", earlyEngineering);
		return "modules/filing/earlyEngineering/earlyEngineeringList";
	}
	
		/**
	 * 工程前期阶段列表数据
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:earlyEngineering:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(EarlyEngineering earlyEngineering, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EarlyEngineering> page = earlyEngineeringService.findPage(new Page<EarlyEngineering>(request, response,-1), earlyEngineering);
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加,工程前期阶段表单页面
	 */
	@RequiresPermissions(value={"test:filing:earlyEngineering:view","test:filing:earlyEngineering:add","test:filing:earlyEngineering:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(EarlyEngineering earlyEngineering, Model model) {
		model.addAttribute("earlyEngineering", earlyEngineering);
		return "modules/filing/earlyEngineering/earlyEngineeringForm";
	}

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

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


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

	/**
	 * 导入Excel数据
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:earlyEngineering: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<EarlyEngineering> list = ei.getDataList(EarlyEngineering.class);
			for (EarlyEngineering earlyEngineering :
					list) {
				earlyEngineeringService.save(earlyEngineering);
			}
			j.setMsg( "已成功导入条工程前期阶段记录");
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("导入工程前期阶段失败！失败信息："+e.getMessage());
		}
		return j;
    }
	
	/**
	 * 下载导入工程前期阶段数据模板
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:earlyEngineering:import")
    @RequestMapping(value = "import/template")
     public AjaxJson importFileTemplate(HttpServletResponse response) {
		AjaxJson j = new AjaxJson();
		try {
            String fileName = "工程前期阶段数据导入模板.xlsx";
    		List<EarlyEngineering> list = Lists.newArrayList(); 
    		new ExportExcel("工程前期阶段数据", EarlyEngineering.class, 1).setDataList(list).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			j.setSuccess(false);
			j.setMsg( "导入模板下载失败！失败信息："+e.getMessage());
		}
		return j;
    }
	

}