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

import com.google.common.collect.Lists;
import com.jeeplus.common.config.Global;
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.basicInformation.service.BasicInformationService;
import com.jeeplus.modules.filing.filingImages.entity.FilingImages;
import com.jeeplus.modules.filing.filingImages.service.FilingImagesService;
import com.jeeplus.modules.filing.filingImages.utils.ZipCompressUtil;
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 org.springframework.web.servlet.mvc.support.RedirectAttributes;

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

/**
 * 图片管理Controller
 * @author qy
 * @version 2021-03-25
 */
@Controller
@RequestMapping(value = "${adminPath}/filing/filingImages")
public class FilingImagesController extends BaseController {

	@Autowired
	private FilingImagesService filingImagesService;

	@Autowired
	private BasicInformationService basicInformationService;

	@ModelAttribute
	public FilingImages get(@RequestParam(required=false) String id) {
		FilingImages entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = filingImagesService.get(id);
		}
		if (entity == null){
			entity = new FilingImages();
		}
		return entity;
	}
	
	/**
	 * 核准阶段列表页面
	 */
	@RequestMapping(value = {"list", ""})
	public String list(FilingImages filingImages, Model model) {
		model.addAttribute("filingImages", filingImages);
		return "modules/filing/filingImages/filingImages";
	}
	
		/**
	 * 核准阶段列表数据
	 */
	@ResponseBody
	@RequestMapping(value = "data")
	public Map<String, Object> data(FilingImages filingImages, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<FilingImages> page = filingImagesService.findPage(new Page<FilingImages>(request, response,-1), filingImages);
		if (Global.SYS_TYPE.equals("2")) {
			List<FilingImages> list = page.getList();
			for (FilingImages upload:
					list) {
				upload.setImgPath("http://" + request.getServerName()  +upload.getImgPath());
			}
		}
		return getBootstrapData(page);
	}

	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportStudy")
	public String exportStudy(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportStudy(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/studyStage/";
	}

	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportApproval")
	public String exportApproval(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportApproved(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/approvalStage/";
	}


	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportEarlyStage")
	public String exportEarlyStage(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportEarlyStage(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/earlyStage/";
	}


	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportDesignStage")
	public String exportDesignStage(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportDesignStage(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/designStage/";
	}


	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportBiddingContract")
	public String exportBiddingContract(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportBiddingContract(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/biddingContract/";
	}

	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportApplicationFor")
	public String exportApplicationFor(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportApplicationFor(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/applicationFor/";
	}


	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportEarlyEngineering")
	public String exportEarlyEngineering(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportEarlyEngineering(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/applicationFor/";
	}

	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportConstructionStage")
	public String exportConstructionStage(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportConstructionStage(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/constructionStage/";
	}

	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportStartUp")
	public String exportStartUp(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportStartUp(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/constructionStage/startUp";
	}

	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportCostManagment")
	public String exportCostManagment(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportCostManagment(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/constructionStage/costManagment";
	}

	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportProjectOther")
	public String exportProjectOther(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportProjectOther(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/constructionStage/projectOther";
	}

	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportProjectSchedule")
	public String exportProjectSchedule(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportProjectSchedule(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/constructionStage/projectSchedule";
	}

	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportQulityAssurance")
	public String exportQulityAssurance(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportQulityAssurance(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/constructionStage/qulityAssurance";
	}

	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportSafetyQuality")
	public String exportSafetyQuality(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportSafetyQuality(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/constructionStage/safetyQuality";
	}

	/**
	 * 项目相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportSecurityManagment")
	public String exportSecurityManagment(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportSecurityManagment(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/constructionStage/securityManagment";
	}


	/**
	 * 汇总相应文件批量下载并压缩
	 */
	@RequestMapping(value = "exportSummary")
	public String exportSummary(HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
		String listIds = request.getParameter("ids");
		List<String> idList = new ArrayList<>();
		if (null != listIds && !"".equals(listIds)) {
			idList = Arrays.asList(listIds.split(","));
		} else {
			idList = basicInformationService.findAllId();
		}
		try {
			String filePath = filingImagesService.exportSummary(idList);
			filingImagesService.downloadZipFile(filePath,response);
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "批量下载项目文件失败！");
		}
		return "redirect:"+Global.getAdminPath()+"/filing/summary/";
	}
}