/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.sg.audit.information.web;

import java.io.*;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


/*import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;*/
import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.modules.sg.audit.information.entity.*;
import com.jeeplus.modules.sg.audit.information.service.InformationService;
import com.jeeplus.modules.sg.audit.information.utils.FreemarkerUtil;
import com.jeeplus.modules.sg.audit.information.utils.ImportInformation;
import com.jeeplus.modules.sg.audit.information.utils.RandomUtil;
import com.jeeplus.modules.sg.audit.information.utils.ResponseUtil;
import com.jeeplus.modules.sg.audit.material.entity.Material;
import com.jeeplus.modules.sg.audit.material.service.MaterialService;
import com.jeeplus.modules.sg.audit.record.entity.Record;
import com.jeeplus.modules.sg.audit.record.service.RecordService;
import freemarker.template.Configuration;
import freemarker.template.Template;
/*import io.swagger.models.auth.In;
import net.sf.ehcache.util.concurrent.ConcurrentHashMap;*/
import org.apache.poi.ss.usermodel.Row;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
/*import org.apache.shiro.session.Session;
import org.h2.util.New;*/
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.access.method.P;
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 com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ImportExcel;
//import sun.tools.jconsole.JConsole;

//import static org.quartz.jobs.FileScanJob.FILE_NAME;

/**
 * 项目管理列表Controller
 *
 * @author
 */
@Controller
@RequestMapping(value = "${adminPath}/sg/information")
public class InformationController extends BaseController {

    @Autowired
    private InformationService informationService;
    @Autowired
    private MaterialService materialService;
    @Autowired
    private RecordService recordService;

    @ModelAttribute
    public Information get(@RequestParam(required = false) String id) {
        Information entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = informationService.get(id);
        }
        if (entity == null) {
            entity = new Information();
        }
        return entity;
    }

    /**
     * 项目管理列表页
     */
    @RequiresPermissions("modules:sg:information:information:list")
    @RequestMapping(value = {"list", ""})
    public String list(Information information, Model model) {
        model.addAttribute("informationList", information);
        return "modules/sg/audit/information/informationList";
    }

    /**
     * 项目管理列表数据
     */
    @ResponseBody
    @RequiresPermissions("modules:sg:information:information:list")
    @RequestMapping(value = "data")
    public Map<String, Object> data(Information information, HttpServletRequest request, HttpServletResponse response, Model model) {
        String hiddenSign = information.getHiddenSign();
        HttpSession session = request.getSession();
        Object path = session.getAttribute("path");
        if (hiddenSign.equals("不是第一次加载")) {
            if (path != null && !path.equals("")) {
                List<Information> tList = new ArrayList<>();
                try {
                    BufferedReader bufferedReader = null;
                    bufferedReader = new BufferedReader(new FileReader(path.toString()));
                    String string;
                    while ((string = bufferedReader.readLine()) != null) {
                        Information information1 = new Information();
                        information1.setProjectId(string);
                        tList.add(information1);
                    }
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                information.setFileList(tList);
            }
        } else {
            if (path != null && path != "") {
                RandomUtil.delete(path.toString());
                session.removeAttribute("path");
            }
        }

        String str = information.getProjectId();
        if (str.equals("")) {

        } else {
            String[] strArr = str.split("\\s+");
            information.setAttrs(strArr);
        }
        Page<Information> page = informationService.findPage(new Page<Information>(request, response), information);
        return getBootstrapData(page);
    }


    @RequiresPermissions(value = {"modules:sg:information:information:view", "modules:sg:information:information:add", "modules:sg:information:information:edit"}, logical = Logical.OR)
    @RequestMapping(value = "form")
    public String form(Information information, Model model) {
        model.addAttribute("Information", information);
        return "modules/sg/audit/information/informationForm";
    }

/*
    //配置超期时间节点页--暂未使用
    @RequiresPermissions(value = {"modules:sg:information:information:add", "modules:sg:information:information:edit"}, logical = Logical.OR)
    @RequestMapping(value = "editOverTime")
    public String constant(constant constant, Model model) {
        String overTime = informationService.findOverTime("overTime");
        constant.setOverTime(overTime);
        model.addAttribute("constant", constant);
        return "modules/sg/audit/information/constantForm";
    }
*/

    //变更超期时间节点
    @RequestMapping(value = "updateConstant")
    @ResponseBody
    public String updateConstant(constant constant) {
        int count = informationService.updateConstant(constant);
        if (count > 0) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }


    /**
     * 导入甲供物资和项目信息
     */
//    @RequiresPermissions("modules:sg:information:information:import")
    @ResponseBody
    @RequestMapping(value = "importTwo")
    public AjaxJson importFile(MultipartFile btnCom, MultipartFile btnInformation, HttpServletResponse response, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        try {
            //导入综合信息Excel数据
            ImportExcel importExcel = new ImportExcel(btnInformation, 1, 0);
            int excelLastDataRowNum = importExcel.getLastDataRowNum();
            ImportInformation.importCom(importExcel, excelLastDataRowNum);
            //导入欠供物资Excel数据
            ImportExcel ei = new ImportExcel(btnCom, 1, 0);
            int lastDataRowNum = ei.getLastDataRowNum();
            ImportInformation.importmation(ei, lastDataRowNum);
            //插入导入记录数据，导入人及导入时间
            Record record = new Record();
            recordService.save(record);
            j.setMsg("成功");
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("失败");
        }
        return j;
    }


    /**
     * 导入综合信息Excel数据
     */
    @RequiresPermissions("modules:sg:information:information:import")
    @ResponseBody
    @RequestMapping(value = "importCom")
    public AjaxJson importFileCom(@RequestParam("file") MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        try {
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            int lastDataRowNum = ei.getLastDataRowNum();
            Map<String, Integer> importCom = ImportInformation.importCom(ei, lastDataRowNum);
            int failureNum = importCom.get("failureNum");
            int successNum = importCom.get("successNum");
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 项目管理记录。");
            }
            j.setMsg("已成功导入 " + successNum + " 项目管理记录" + failureMsg);
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入项审定日期失败！失败信息：" + e.getMessage());
        }
        return j;
    }


    /**
     * 导入甲供物资Excel数据
     */
    @RequiresPermissions("modules:sg:information:information:import")
    @ResponseBody
    @RequestMapping(value = "import")
    public AjaxJson importFile(@RequestParam("file") MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        try {
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
//			List<Information> list = ei.getDataList(Information.class);
            int lastDataRowNum = ei.getLastDataRowNum();
            Map<String, Object> information = ImportInformation.importmation(ei, lastDataRowNum);
            //获取失败条数
            int failureNum = Integer.parseInt(information.get("failureNum").toString());
            //获取成功条数
            int successNum = Integer.parseInt(information.get("successNum").toString());

            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 项目管理记录。");
            }
            j.setMsg("已成功导入 " + successNum + " 项目管理记录;" + failureMsg);
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入项目管理信息失败！失败信息：" + e.getMessage());
        }
        return j;
    }


    /**
     * 导入送审
     * */
    @RequiresPermissions("modules:sg:information:information:importTrial")
    @ResponseBody
    @RequestMapping(value = "importInTrial")
    public AjaxJson importInTrial(@RequestParam("file") MultipartFile file, HttpServletResponse response, HttpServletRequest request) {
        AjaxJson j = new AjaxJson();
        try {
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            int lastDataRowNum = ei.getLastDataRowNum();
            Map<String, Integer> importInTrial = ImportInformation.importInTrial(ei, lastDataRowNum);
            int failureNum = importInTrial.get("failureNum");
            int successNum = importInTrial.get("successNum");
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 项目管理记录。");
            }
            j.setMsg("已成功导入 " + successNum + " 条记录" + failureMsg);
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入项审定日期失败！失败信息：" + e.getMessage());
        }
        return j;
    }

    //批量查询
    @RequiresPermissions("modules:sg:information:information:import")
    @ResponseBody
    @RequestMapping(value = "btnSearch")
    public Map<String, Object> btnSearch(@RequestParam("file") MultipartFile file, Information information, HttpServletRequest request, HttpServletResponse response, Model model) {
        AjaxJson j = new AjaxJson();
        StringBuilder failureMsg = new StringBuilder();
        ImportExcel ei = null;
        String canonicalPath = "";
        try {
            ei = new ImportExcel(file, 1, 0);
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入项审定日期失败！失败信息：" + e.getMessage());
        }
        int lastDataRowNum = ei.getLastDataRowNum();
        List<Information> infor = new ArrayList<>();
        for (int i = 1; i < lastDataRowNum; i++) {
            Information information1 = new Information();
            Row row = ei.getRow(i);
            Object projectId = ei.getCellValue(row, 0);
            Object projectName = ei.getCellValue(row, 1);
            information1.setProjectId(projectId.toString());
            information1.setProjectName(projectName.toString());
            infor.add(information1);
        }
        List<Information> listBy = informationService.findListBy(infor);
        try {
            HttpSession session = request.getSession();
            Object oldPath = session.getAttribute("path");
            if (oldPath != null && oldPath != "") {
                RandomUtil.delete(oldPath.toString());
                session.removeAttribute("path");
            }
            File path = new File(this.getClass().getResource("/").getPath() + "/freemarker");
            String fileName = RandomUtil.getRandomFileName();
            File temp = new File(path + "\\" + fileName);
            /**
             * 在临时文件中写入内容
             */
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(temp));
            for (Information items :
                    listBy) {
                String a = items.getProjectId();
                bufferedWriter.write(a + "\r");
            }
            bufferedWriter.close();

            canonicalPath = temp.getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String listCount = String.valueOf(listBy.size());
        long l = Long.parseLong(listCount);
        Page<Information> page = new Page(1, listBy.size(), l, listBy);
        HttpSession session = request.getSession();
        session.setAttribute("path", canonicalPath);
        return getBootstrapData(page);
    }

    //导入原因类型
    @RequiresPermissions("modules:sg:information:information:importReason")
    @ResponseBody
    @RequestMapping(value = "btnInReason")
    public AjaxJson btnInReason(@RequestParam("file") MultipartFile file, Information information, HttpServletRequest request, HttpServletResponse response, Model model) {
        AjaxJson j = new AjaxJson();
        try {
            StringBuilder failureMsg = new StringBuilder();
            StringBuilder errorMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            int lastDataRowNum = ei.getLastDataRowNum();
            Map<String, Object> importInTrial = ImportInformation.btnInReason(ei, lastDataRowNum);
            if (importInTrial != null) {
                int failureNum = Integer.parseInt(importInTrial.get("failureNum").toString());
                int successNum = Integer.parseInt(importInTrial.get("successNum").toString());
                if (failureNum > 0) {
                    failureMsg.insert(0, "，失败 " + failureNum + " 项目管理记录。");
                }
                ArrayList error = (ArrayList) importInTrial.get("error");
                if (error.size() > 0) {
                    errorMsg.append("以下项目定义号因出库数量或审定数量不一致未导入");
                    for (int i = 0; i < error.size(); i++) {
                        errorMsg.append("\r\n");
                        errorMsg.append(error.get(i));
                    }
                }
                j.setMsg("已成功导入 " + successNum + " 条记录" + errorMsg);
            } else {
                j.setMsg("数据都已更新过");
            }

        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入项目原因！失败信息：" + e.getMessage());
        }
        return j;
    }

    /**
     * 批量导入模板
     */
    @ResponseBody
    @RequiresPermissions("modules:sg:information:information:export")
    @RequestMapping(value = "import/template")
    public AjaxJson importFileTemplate(HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
            String fileName = "批量项目查询导入模板.xlsx";
            List<Information> list = Lists.newArrayList();
            new ExportExcel("", InformationDto.class, 1).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("批量项目查询下载失败！失败信息：" + e.getMessage());
        }
        return j;
    }


    /**
     * 导入送审模板下载
     */
    @ResponseBody
    @RequiresPermissions("modules:sg:information:information:export")
    @RequestMapping(value = "import/templates")
    public AjaxJson importFileTemplateS(HttpServletResponse response) {
        AjaxJson j = new AjaxJson();
        try {
            String fileName = "导入送审.xlsx";
            List<append> list = Lists.newArrayList();
            new ExportExcel("", append.class, 1).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导入送审下载失败！失败信息：" + e.getMessage());
        }
        return j;
    }


    /**
     * 导出物料清单
     * */
    //	public static Map<String, ExportExcel> excelParamCache = new ConcurrentHashMap<>();
    @ResponseBody
    @RequiresPermissions("modules:sg:information:information:export")
    @RequestMapping(value = "exportMaterial")
    public AjaxJson exportMaterial(Information information, HttpServletRequest request, HttpServletResponse response) {
        String projectId = "";
        String projectName = "";
        String projectType = "";
        String projectYear = "";
        String firstUnits = "";
        String secondUnits = "";
        String firstFinish = "";
        String extendedStatus = "";
        String extendedTime = "";
        String hiddenSign = "";
        try {
            projectId = (String)request.getParameter("projectId");
            projectId = URLDecoder.decode(projectId,"UTF-8");
            if (projectId.equals("")) {

            } else {
                String[] strArr = projectId.split("\\s+");
                information.setAttrs(strArr);
            }
            projectName = (String)request.getParameter("projectName");
            projectName = URLDecoder.decode(projectName,"UTF-8");
            projectType = (String)request.getParameter("projectType");
            projectType = URLDecoder.decode(projectType,"UTF-8");
            projectYear = (String)request.getParameter("projectYear");
            projectYear = URLDecoder.decode(projectYear,"UTF-8");
            firstUnits = (String)request.getParameter("firstUnits");
            firstUnits = URLDecoder.decode(firstUnits,"UTF-8");
            secondUnits = (String)request.getParameter("secondUnits");
            secondUnits = URLDecoder.decode(secondUnits,"UTF-8");
            firstFinish = (String)request.getParameter("firstFinish");
            firstFinish = URLDecoder.decode(firstFinish,"UTF-8");
            extendedStatus = (String)request.getParameter("extendedStatus");
            extendedStatus = URLDecoder.decode(extendedStatus,"UTF-8");
            extendedTime = (String)request.getParameter("extendedTime");
            extendedTime = URLDecoder.decode(extendedTime,"UTF-8");
            hiddenSign = (String)request.getParameter("hiddenSign");
            hiddenSign = URLDecoder.decode(hiddenSign,"UTF-8");
            information.setProjectName(projectName);
            information.setProjectType(projectType);
            information.setProjectType(projectYear);
            information.setFirstUnits(firstUnits);
            information.setSecondUnits(secondUnits);
            information.setFirstFinish(firstFinish);
            information.setExtendedStatus(extendedStatus);
            information.setExtendedTime(extendedTime);
            information.setHiddenSign(hiddenSign);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        AjaxJson j = new AjaxJson();
        HttpSession session = request.getSession();
        Object path = session.getAttribute("path");
        if (hiddenSign.equals("不是第一次加载")) {
            if (path != null && !path.equals("")) {
                List<Information> tList = new ArrayList<>();
                try {
                    BufferedReader bufferedReader = null;
                    bufferedReader = new BufferedReader(new FileReader(path.toString()));
                    String string;
                    while ((string = bufferedReader.readLine()) != null) {
                        Information information1 = new Information();
                        information1.setProjectId(string);
                        tList.add(information1);
                    }
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                information.setFileList(tList);
            }
        } else {
            if (path != null && path != "") {
                RandomUtil.delete(path.toString());
                session.removeAttribute("path");
            }
        }
        List<Information> list = informationService.findList(information);
        try {
            String fileName = "导出物料清单" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            List<Material> materialList = informationService.allNo(list);
            new ExportExcel("导出物料清单", Material.class).setDataList(materialList).write(response, fileName).dispose();
            j.setSuccess(true);
            j.setMsg("导出成功！");
            return j;
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导出物料清单！失败信息：" + e.getMessage());
        }
        return j;
    }

    /**
     * 导出项目清单
     * */
    @ResponseBody
    @RequiresPermissions("modules:sg:information:information:export")
    @RequestMapping(value = "export")
    public AjaxJson export(Information information, HttpServletRequest request, HttpServletResponse response) {
        String projectId = "";
        String projectName = "";
        String projectType = "";
        String projectYear = "";
        String firstUnits = "";
        String secondUnits = "";
        String firstFinish = "";
        String extendedStatus = "";
        String extendedTime = "";
        String hiddenSign = "";
        try {
            projectId = (String)request.getParameter("projectId");
            projectId = URLDecoder.decode(projectId,"UTF-8");
            if (projectId.equals("")) {

            } else {
                String[] strArr = projectId.split("\\s+");
                information.setAttrs(strArr);
            }
            projectName = (String)request.getParameter("projectName");
            projectName = URLDecoder.decode(projectName,"UTF-8");
            projectType = (String)request.getParameter("projectType");
            projectType = URLDecoder.decode(projectType,"UTF-8");
            projectYear = (String)request.getParameter("projectYear");
            projectYear = URLDecoder.decode(projectYear,"UTF-8");
            firstUnits = (String)request.getParameter("firstUnits");
            firstUnits = URLDecoder.decode(firstUnits,"UTF-8");
            secondUnits = (String)request.getParameter("secondUnits");
            secondUnits = URLDecoder.decode(secondUnits,"UTF-8");
            firstFinish = (String)request.getParameter("firstFinish");
            firstFinish = URLDecoder.decode(firstFinish,"UTF-8");
            extendedStatus = (String)request.getParameter("extendedStatus");
            extendedStatus = URLDecoder.decode(extendedStatus,"UTF-8");
            extendedTime = (String)request.getParameter("extendedTime");
            extendedTime = URLDecoder.decode(extendedTime,"UTF-8");
            hiddenSign = (String)request.getParameter("hiddenSign");
            hiddenSign = URLDecoder.decode(hiddenSign,"UTF-8");
            information.setProjectName(projectName);
            information.setProjectType(projectType);
            information.setProjectType(projectYear);
            information.setFirstUnits(firstUnits);
            information.setSecondUnits(secondUnits);
            information.setFirstFinish(firstFinish);
            information.setExtendedStatus(extendedStatus);
            information.setExtendedTime(extendedTime);
            information.setHiddenSign(hiddenSign);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        AjaxJson j = new AjaxJson();
        HttpSession session = request.getSession();
        Object path = session.getAttribute("path");
        if (hiddenSign.equals("不是第一次加载")) {
            if (path != null && !path.equals("")) {
                List<Information> tList = new ArrayList<>();
                try {
                    BufferedReader bufferedReader = null;
                    bufferedReader = new BufferedReader(new FileReader(path.toString()));
                    String string;
                    while ((string = bufferedReader.readLine()) != null) {
                        Information information1 = new Information();
                        information1.setProjectId(string);
                        tList.add(information1);
                    }
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                information.setFileList(tList);
            }
        } else {
            if (path != null && path != "") {
                RandomUtil.delete(path.toString());
                session.removeAttribute("path");
            }
        }

        try {
            String fileName = "导出项目清单" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            List<Information> informationList = informationService.findList(information);
            new ExportExcel("导出项目清单", OutInformation.class).setDataList(informationList).write(response, fileName).dispose();
            j.setSuccess(true);
            j.setMsg("导出成功！");
            return j;
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导出项目清单！失败信息：" + e.getMessage());
        }
        return j;
    }


    //导出word
    @ResponseBody
    @RequiresPermissions("modules:sg:information:information:export")
    @RequestMapping(value = "exportInformation")
    public void exportInformation(Information information, HttpServletRequest request, HttpServletResponse response) {
        String projectId = "";
        String projectName = "";
        String projectType = "";
        String projectYear = "";
        String firstUnits = "";
        String secondUnits = "";
        String firstFinish = "";
        String extendedStatus = "";
        String extendedTime = "";
        String hiddenSign = "";
        try {
            projectId = (String)request.getParameter("projectId");
            projectId = URLDecoder.decode(projectId,"UTF-8");
            if (projectId.equals("")) {

            } else {
                String[] strArr = projectId.split("\\s+");
                information.setAttrs(strArr);
            }
            projectName = (String)request.getParameter("projectName");
            projectName = URLDecoder.decode(projectName,"UTF-8");
            projectType = (String)request.getParameter("projectType");
            projectType = URLDecoder.decode(projectType,"UTF-8");
            projectYear = (String)request.getParameter("projectYear");
            projectYear = URLDecoder.decode(projectYear,"UTF-8");
            firstUnits = (String)request.getParameter("firstUnits");
            firstUnits = URLDecoder.decode(firstUnits,"UTF-8");
            secondUnits = (String)request.getParameter("secondUnits");
            secondUnits = URLDecoder.decode(secondUnits,"UTF-8");
            firstFinish = (String)request.getParameter("firstFinish");
            firstFinish = URLDecoder.decode(firstFinish,"UTF-8");
            extendedStatus = (String)request.getParameter("extendedStatus");
            extendedStatus = URLDecoder.decode(extendedStatus,"UTF-8");
            extendedTime = (String)request.getParameter("extendedTime");
            extendedTime = URLDecoder.decode(extendedTime,"UTF-8");
            hiddenSign = (String)request.getParameter("hiddenSign");
            hiddenSign = URLDecoder.decode(hiddenSign,"UTF-8");
            information.setProjectName(projectName);
            information.setProjectType(projectType);
            information.setProjectType(projectYear);
            information.setFirstUnits(firstUnits);
            information.setSecondUnits(secondUnits);
            information.setFirstFinish(firstFinish);
            information.setExtendedStatus(extendedStatus);
            information.setExtendedTime(extendedTime);
            information.setHiddenSign(hiddenSign);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        AjaxJson j = new AjaxJson();
        HttpSession session = request.getSession();
        Object outPath = session.getAttribute("path");
        if (hiddenSign.equals("不是第一次加载")) {
            if (outPath != null && !outPath.equals("")) {
                List<Information> tList = new ArrayList<>();
                try {
                    BufferedReader bufferedReader = null;
                    bufferedReader = new BufferedReader(new FileReader(outPath.toString()));
                    String string;
                    while ((string = bufferedReader.readLine()) != null) {
                        Information information1 = new Information();
                        information1.setProjectId(string);
                        tList.add(information1);
                    }
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                information.setFileList(tList);
            }
        } else {
            if (outPath != null && outPath != "") {
                RandomUtil.delete(outPath.toString());
                session.removeAttribute("path");
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        Template template = null;
        File path = new File(this.getClass().getResource("/").getPath() + "/freemarker");
        Configuration cfg = new Configuration();
        try {
            cfg.setDefaultEncoding("UTF-8");
            //配置cfg对象
            cfg.setDirectoryForTemplateLoading(path);
//            根据模板名称获取Template对象
            template = cfg.getTemplate("甲供物资超欠供说明.ftl");
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String, Object> data = new HashMap<>();
        List<Information> informationList = informationService.findListWord(information);
        for (int i = 0; i < informationList.size(); i++) {
            stringBuilder.append(i + 1 + "." + informationList.get(i).getProjectName() + "(" + informationList.get(i).getProjectId() + ")");
            Information information1 = informationList.get(i);
            List<Material> mateial = materialService.findMateial(information1);
            for (int k = 0; k < mateial.size(); k++) {
                stringBuilder.append("<w:p></w:p>");
                String info = mateial.get(k).getInfo();
				String showInfo = "";
				if (info == null || info.equals("") || info == "") {
					showInfo = "暂无";
				} else {
					showInfo = info;
				}
                stringBuilder.append(k + 1 + "." + mateial.get(k).getMaterialName() + "(" + mateial.get(k).getInforId() + ")，出库数量为" + mateial.get(k).getOutboundNumber() + ",审定数量为" + mateial.get(k).getAuthorizedNumber() + ",由于原因" + mateial.get(k).getStatus() + ":" + showInfo + ",已完成甲供物资领退料。");
            }
            stringBuilder.append("<w:p></w:p>");
            stringBuilder.append("<w:p></w:p>");
        }
        data.put("mb", stringBuilder);
        File docFile = new File(path + "\\甲供物资超欠供说明.doc");
        FreemarkerUtil.generateFile(data, template, docFile);
        ResponseUtil.docResponse("甲供物资超欠供说明.doc", docFile, response);
    }
}


