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.LegalCompliance;
import com.jeeplus.modules.filing.earlyEngineering.service.LegalComplianceService;
import com.jeeplus.modules.filing.earlyEngineering.service.LegalComplianceService;
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/legalCompliance")
public class LegalComplianceController extends BaseController {

	@Autowired
	private LegalComplianceService legalComplianceService;

	@Autowired
	private FilingImagesService filingImagesService;

	@ModelAttribute
	public LegalCompliance get(@RequestParam(required=false) String id) {
		LegalCompliance entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = legalComplianceService.get(id);
		}
		if (entity == null){
			entity = new LegalCompliance();
		}
		return entity;
	}
	
	/**
	 * 依法合规列表页面
	 */
	@RequiresPermissions("test:filing:legalCompliance:list")
	@RequestMapping(value = {"list", ""})
	public String list(LegalCompliance legalCompliance, Model model) {
		model.addAttribute("legalCompliance", legalCompliance);
		return "modules/filing/legalCompliance/legalComplianceList";
	}
	
		/**
	 * 依法合规列表数据
	 */
	@ResponseBody
	@RequiresPermissions("test:filing:legalCompliance:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(LegalCompliance legalCompliance, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<LegalCompliance> page = legalComplianceService.findPage(new Page<LegalCompliance>(request, response,-1), legalCompliance);
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加,依法合规表单页面
	 */
	@RequiresPermissions(value={"test:filing:legalCompliance:view","test:filing:legalCompliance:add","test:filing:legalCompliance:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(LegalCompliance legalCompliance, Model model) {
		model.addAttribute("legalCompliance", legalCompliance);
		return "modules/filing/legalCompliance/legalComplianceForm";
	}

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

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


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

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

}