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

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.core.servlet.ValidateCodeServlet;
import com.jeeplus.modules.sg.audit.information.entity.Information;
import com.jeeplus.modules.sg.audit.information.service.InformationService;
import com.jeeplus.modules.sg.audit.information.utils.RandomUtil;
import com.jeeplus.modules.sg.financial.erpcredit.entity.ErpCredit;
import com.jeeplus.modules.sg.financial.erpcredit.entity.ErpCreditEquipment;
import com.jeeplus.modules.sg.financial.erpcredit.entity.ErpCreditMaterial;
import com.jeeplus.modules.sg.financial.erpcredit.entity.ErpJudge;
import com.jeeplus.modules.sg.financial.erpcredit.mapper.ErpCreditEquipmentMapper;
import com.jeeplus.modules.sg.financial.erpcredit.mapper.ErpCreditMapper;
import com.jeeplus.modules.sg.financial.erpcredit.mapper.ErpCreditMaterialMapper;
import com.jeeplus.modules.sg.financial.erpcredit.util.ErpInfo;
import com.jeeplus.modules.sg.financial.erpcredit.util.ExportTemplate;
import com.jeeplus.modules.sg.financial.erpcredit.util.ExportUtil;
import com.jeeplus.modules.sg.financial.erpcredit.util.ValidationUtils;
import com.jeeplus.modules.sg.financial.settlement.entity.MaintainData;
import com.jeeplus.modules.sg.financial.settlement.mapper.DataMaintenanceMapper;
import com.jeeplus.modules.sg.financial.settlement.service.DataMaintenanceService;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Transactional(readOnly = true)
public class ErpCreditService extends CrudService<ErpCreditMapper,ErpCredit> {
    @Autowired
    private ErpCreditEquipmentMapper erpCreditEquipmentMapper;
    @Autowired
    private ErpCreditMaterialMapper erpCreditMaterialMapper;
    @Autowired
    private ErpCreditMapper erpCreditMapper;
    @Autowired
    private DataMaintenanceMapper dataMaintenanceMapper;
    @Autowired
    private InformationService informationService;
    @Autowired
    private DataMaintenanceService dataMaintenanceService;

//    public List<ErpCredit> getListErpCredit(List<String> itemId,String status){
//        List<ErpCredit> erpCreditList = new ArrayList<ErpCredit>();
//        List<ErpCredit> erpCredits = erpCreditMapper.findErpCreditList(itemId);
//        for (ErpCredit erpCredit:erpCredits){
//            if (erpCredit.getStatus().equals(status)){
//                erpCreditList.add(erpCredit);
//            }
//        }
//        List<String> strings = new ArrayList<String>();
//        for (ErpCredit erpCredit:erpCreditList){
//            strings.add(erpCredit.getId());
//        }
//        List<ErpCreditEquipment> erpCreditChild1List = erpCreditEquipmentMapper.findErpCreditChild1List(strings);
//        List<ErpCreditMaterial> erpCreditChild2List = erpCreditMaterialMapper.findErpCreditChild2List(strings);
//        if (null!=erpCreditList&&erpCreditList.size()>0){
//            for (ErpCredit erpCredit:erpCreditList){
//                if (null!=erpCreditChild1List&&erpCreditChild1List.size()>0){
//                    for (ErpCreditEquipment erpCreditEquipment:erpCreditChild1List){
//                        if (erpCredit.getId().equals(erpCreditEquipment.getErpCredit().getId())){
//                            erpCredit.getErpCreditChild1s().add(erpCreditEquipment);
//                        }
//                    }
//                }
//                if (null!=erpCreditChild2List&&erpCreditChild2List.size()>0){
//                    for (ErpCreditMaterial erpCreditChild2:erpCreditChild2List){
//                        if (erpCredit.getId().equals(erpCreditChild2.getErpCredit().getId())){
//                            erpCredit.getErpCreditChild2s().add(erpCreditChild2);
//                        }
//                    }
//                }
//            }
//        }
//        return erpCreditList;
//    }

    /**
     * 根据id获取erpCredit对象，同时获取属于它的设备和材料列表
     * @param id
     * @return
     */
    public ErpCredit get(String id) {
        ErpCredit erpCredit = super.get(id);
        List<ErpCreditEquipment> eqmList = erpCreditEquipmentMapper.findList(new ErpCreditEquipment(erpCredit));
//        List<ErpCreditEquipment> equSures = ErpInfo.getEquNumbers(eqmList);//特殊情况处理   物料信息
        erpCredit.setErpCreditChild1s(eqmList);
        erpCredit.setErpCreditChild2s(erpCreditMaterialMapper.findList(new ErpCreditMaterial(erpCredit)));
        return erpCredit;
    }

//    public List<ErpCredit> findList(ErpCredit erpCredit) {
//        return super.findList(erpCredit);
//    }
//
//    public Page<ErpCredit> findPage(Page<ErpCredit> page, ErpCredit erpCredit) {
//        return super.findPage(page, erpCredit);
//    }

    /**
     * 替换数据库中所有erpCredit及其设备、物料数据
     * @param erpCredits
     */
    @Transactional(readOnly = false)
    public void replaceAll(List<ErpCredit> erpCredits){
        String random  = ErpInfo.getStatus();
        deleteAll();//清空原来表数据
        for (int i = 0; i < erpCredits.size()/1000+1; i++) {
            int end=(i+1)*1000;
            if (end>erpCredits.size()){
                end=erpCredits.size();
            }
            List<ErpCredit> subList = erpCredits.subList(i * 1000, end);
            saveList(subList,random);
        }
    }

    @Transactional(readOnly = false)
    public void saveList(List<ErpCredit> erpCredits,String string) {
        List<ErpCreditEquipment> erpCreditEquipments = new ArrayList<ErpCreditEquipment>();
        List<ErpCreditMaterial> erpCreditMaterials = new ArrayList<ErpCreditMaterial>();
        for(ErpCredit erpCredit1 : erpCredits){
            erpCredit1.preInsert();
            erpCredit1.setStatus(string);
            for (ErpCreditEquipment erpCreditEquipment:erpCredit1.getErpCreditChild1s()){
                erpCreditEquipment.setErpCredit(erpCredit1);
                erpCreditEquipment.preInsert();
                erpCreditEquipments.add(erpCreditEquipment);
            }
            for (ErpCreditMaterial erpCreditMaterial:erpCredit1.getErpCreditChild2s()){
                erpCreditMaterial.setErpCredit(erpCredit1);
                erpCreditMaterial.preInsert();
                erpCreditMaterials.add(erpCreditMaterial);
            }
        }
        if (null!=erpCredits&&erpCredits.size()>0){
            erpCreditMapper.insertList(erpCredits);
        }
        if (null!=erpCreditEquipments&&erpCreditEquipments.size()>0){
            erpCreditEquipmentMapper.insertList(erpCreditEquipments);
        }
        if (null!=erpCreditMaterials&&erpCreditMaterials.size()>0){
            erpCreditMaterialMapper.insertList(erpCreditMaterials);
        }
    }

    @Transactional(readOnly = false)
    public void save(ErpCredit erpCredit) {
        super.save(erpCredit);
        for (ErpCreditEquipment erpCreditChild1 : erpCredit.getErpCreditChild1s()){
            if (ErpCreditEquipment.DEL_FLAG_NORMAL.equals(erpCredit.getDelFlag())){
                if (StringUtils.isBlank(erpCreditChild1.getId())) {
                    erpCreditChild1.setErpCredit(erpCredit);
                    erpCreditChild1.preInsert();
                    erpCreditEquipmentMapper.insert(erpCreditChild1);
                }else {
                    erpCreditChild1.preUpdate();
                    erpCreditEquipmentMapper.update(erpCreditChild1);
                }
            }else {
                erpCreditEquipmentMapper.delete(erpCreditChild1);
            }
        }
        for (ErpCreditMaterial erpCreditChild2 : erpCredit.getErpCreditChild2s()){
            if (ErpCreditMaterial.DEL_FLAG_NORMAL.equals(erpCreditChild2.getDelFlag())){
                if (StringUtils.isBlank(erpCreditChild2.getId())){
                    erpCreditChild2.setErpCredit(erpCredit);
                    erpCreditChild2.preInsert();
                    erpCreditMaterialMapper.insert(erpCreditChild2);
                }else {
                    erpCreditChild2.preUpdate();
                    erpCreditMaterialMapper.update(erpCreditChild2);
                }
            }else {
                erpCreditMaterialMapper.delete(erpCreditChild2);
            }
        }
    }

    @Transactional(readOnly = false)
    public void delete(ErpCredit erpCredit) {
        super.delete(erpCredit);
        erpCreditEquipmentMapper.delete(new ErpCreditEquipment(erpCredit));
        erpCreditMaterialMapper.delete(new ErpCreditMaterial(erpCredit));
    }

    /**
     * 删除所有erpCredit及其设备物料数据
     */
    @Transactional(readOnly = false)
    public void deleteAll(){
        erpCreditMapper.deleteAll();
        erpCreditEquipmentMapper.deleteAll();
        erpCreditMaterialMapper.deleteAll();
    }

    /**
     * 根据id查找相应概预算信息
     * @param strings
     * @return
     */
    public List<MaintainData> getFindListMain(List<String> strings){
        return dataMaintenanceMapper.findListMain(strings);
    }

    /**
     * 导出自动决算书
     * @param request
     * @param response
     */
    public void getExport(HttpServletRequest request, HttpServletResponse response){
        List<ErpCredit> erpCreditList = ValidationUtils.getSessionRedisList(request,ValidationUtils.CREDIT_KEY_PREFIX,ErpCredit.class);
        List<String> strs = new ArrayList<String>();
        for (ErpCredit erpCredit1:erpCreditList){
            strs.add(erpCredit1.getItemId());
        }
        List<MaintainData> maintainDatas = getFindListMain(strs);
        ExportTemplate exportTemplate = new ExportTemplate();
        File newFile = exportTemplate.createNewFile();
        // 新文件写入数据，并下载
        InputStream inputStream = null;
        XSSFWorkbook workbook = null;
        XSSFFont font=null;
        XSSFSheet sheetOverView = null;
        XSSFSheet sheetView = null;
        XSSFSheet sheetOtherFee = null;//定义其他费用明细表费用
        XSSFSheet sheetAssetsTotal= null;//资产总表
        XSSFSheet sheetLine1 = null;//移交生产总值一览表
        XSSFSheet sheetEquip=null;//移交总值设备表
        XSSFSheet sheetEquipMaterial=null;//设备材料费用
        XSSFSheet sheetSettlement=null;//结算表
        XSSFSheet sheetLine2=null;//线路2
        XSSFSheet sheetEquip2=null;//设备2
        XSSFSheet newForm=null;//新表格
        try {
            inputStream = new FileInputStream(newFile);// 将excel文件转为输入流
            workbook = new XSSFWorkbook(inputStream);// 创建个workbook，
            font = workbook.createFont();
            sheetOverView =workbook.getSheetAt(0);//概述表
            sheetView = workbook.getSheetAt(1);//定义一览表
            sheetOtherFee = workbook.getSheetAt(2);//其他费用表
            sheetAssetsTotal = workbook.getSheetAt(4);//移交生产总值
            sheetLine1=workbook.getSheetAt(7);//线路1
            sheetEquip=workbook.getSheetAt(8);//设备1
            sheetEquipMaterial=workbook.getSheetAt(9);//设备材料清单表
            sheetSettlement=workbook.getSheetAt(10);//结算表
            sheetLine2=workbook.getSheetAt(5);//线路2
            sheetEquip2=workbook.getSheetAt(6);//设备2
            newForm=workbook.getSheetAt(3);//新表
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            // 写数据
            FileOutputStream fos = new FileOutputStream(newFile);
            ExportUtil.getSheetOverView(sheetOverView,erpCreditList,maintainDatas);
            ExportUtil.getSheetView(sheetView,erpCreditList,maintainDatas,workbook);//调用一览表写入方法
            ExportUtil.getSheetOtherFee(sheetOtherFee,erpCreditList,maintainDatas);
            ExportUtil.getSheetAssetsTotal(sheetAssetsTotal,erpCreditList,maintainDatas);
            ExportUtil.getSheetLine1(sheetLine1,erpCreditList,workbook);
            ExportUtil.getSheetEquip(sheetEquip,erpCreditList,maintainDatas,workbook);
            ExportUtil.getSheetEquipMaterial(sheetEquipMaterial,erpCreditList,workbook);
            ExportUtil.getSheetSettlement(sheetSettlement,erpCreditList,maintainDatas);
            ExportUtil.getSheetLine2(sheetLine2,erpCreditList,workbook);
            ExportUtil.getSheetEquip2(sheetEquip2,erpCreditList,maintainDatas,workbook);
            ExportUtil.getOtherExpenses(newForm,erpCreditList,maintainDatas,workbook);
            workbook.write(fos);
            fos.flush();
            fos.close();
            InputStream fis = new BufferedInputStream(new FileInputStream(
                    newFile));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            response.reset();
            response.setContentType("text/html;charset=UTF-8");
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/x-msdownload");
            String newName = URLEncoder.encode("财务竣工决算表" + System.currentTimeMillis() + ".xlsx","UTF-8");
            response.addHeader("Content-Disposition","attachment;filename=\"" + newName + "\"");
            response.addHeader("Content-Length", "" + newFile.length());
            toClient.write(buffer);
            toClient.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != inputStream) {
                    inputStream.close();
                }
                if(newFile.exists()){
                    newFile.delete();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

//    //导出
//    public void getExport(List<ErpCredit> erpCreditList, HttpServletResponse response){
//        List<String> strs = new ArrayList<String>();
//        for (ErpCredit erpCredit1:erpCreditList){
//            strs.add(erpCredit1.getItemId());
//        }
//        List<MaintainData> maintainDatas = getFindListMain(strs);
//        ExportTemplate exportTemplate = new ExportTemplate();
//        File newFile = exportTemplate.createNewFile();
//        // 新文件写入数据，并下载
//        InputStream inputStream = null;
//        XSSFWorkbook workbook = null;
//        XSSFFont font=null;
//        XSSFSheet sheetOverView = null;
//        XSSFSheet sheetView = null;
//        XSSFSheet sheetOtherFee = null;//定义其他费用明细表费用
//        XSSFSheet sheetAssetsTotal= null;//资产总表
//        XSSFSheet sheetLine1 = null;//移交生产总值一览表
//        XSSFSheet sheetEquip=null;//移交总值设备表
//        XSSFSheet sheetEquipMaterial=null;//设备材料费用
//        XSSFSheet sheetSettlement=null;//结算表
//        XSSFSheet sheetLine2=null;//线路2
//        XSSFSheet sheetEquip2=null;//设备2
//        try {
//            inputStream = new FileInputStream(newFile);// 将excel文件转为输入流
//            workbook = new XSSFWorkbook(inputStream);// 创建个workbook，
//            font = workbook.createFont();
//            sheetOverView =workbook.getSheetAt(0);//概述表
//            sheetView = workbook.getSheetAt(1);//定义一览表
//            sheetOtherFee = workbook.getSheetAt(2);//其他费用表
//            sheetAssetsTotal = workbook.getSheetAt(3);//移交生产总值
//            sheetLine1=workbook.getSheetAt(4);//线路1
//            sheetEquip=workbook.getSheetAt(5);//设备1
//            sheetEquipMaterial=workbook.getSheetAt(6);//设备材料清单表
//            sheetSettlement=workbook.getSheetAt(7);//结算表
//            sheetLine2=workbook.getSheetAt(9);//线路2
//            sheetEquip2=workbook.getSheetAt(10);//设备2
//        } catch (Exception e1) {
//            e1.printStackTrace();
//        }
//        try {
//            // 写数据
//            FileOutputStream fos = new FileOutputStream(newFile);
//            ExportUtil.getSheetOverView(sheetOverView,erpCreditList,maintainDatas);
//            ExportUtil.getSheetView(sheetView,erpCreditList,maintainDatas,workbook);//调用一览表写入方法
//            ExportUtil.getSheetOtherFee(sheetOtherFee,erpCreditList,maintainDatas);
//            ExportUtil.getSheetAssetsTotal(sheetAssetsTotal,erpCreditList,maintainDatas);
//            ExportUtil.getSheetLine1(sheetLine1,erpCreditList,workbook);
//            ExportUtil.getSheetEquip(sheetEquip,erpCreditList,maintainDatas,workbook);
//            ExportUtil.getSheetEquipMaterial(sheetEquipMaterial,erpCreditList,workbook);
//            ExportUtil.getSheetSettlement(sheetSettlement,erpCreditList,maintainDatas);
//            ExportUtil.getSheetLine2(sheetLine2,erpCreditList,workbook);
//            ExportUtil.getSheetEquip2(sheetEquip2,erpCreditList,maintainDatas,workbook);
//            workbook.write(fos);
//            fos.flush();
//            fos.close();
//            InputStream fis = new BufferedInputStream(new FileInputStream(
//                    newFile));
//            byte[] buffer = new byte[fis.available()];
//            fis.read(buffer);
//            fis.close();
//            response.reset();
//            response.setContentType("text/html;charset=UTF-8");
//            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
//            response.setContentType("application/x-msdownload");
//            String newName = URLEncoder.encode("财务竣工决算表" + System.currentTimeMillis() + ".xlsx","UTF-8");
//            response.addHeader("Content-Disposition","attachment;filename=\"" + newName + "\"");
//            response.addHeader("Content-Length", "" + newFile.length());
//            toClient.write(buffer);
//            toClient.flush();
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                if (null != inputStream) {
//                    inputStream.close();
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//    }

    //验证：该预算数据
    //验证汇总
    public List<ErpJudge> getAllCheck(List<ErpCredit> erpCredits){
        Boolean flag = true;
        ErpJudge erpJudge = null;
        String str = "无";
        //得到所有概预算数据集合
        List<MaintainData> maintainDatas = dataMaintenanceService.getJudge();
        List<Information> infoFlag = getInfoFlag(erpCredits);
        List<ErpJudge> mainAll = getMainAll(erpCredits, maintainDatas);
        for (Information information:infoFlag){
            flag = true;
            for (ErpJudge erpJudge1:mainAll){
                if (information.getProjectId().equals(erpJudge1.getItemId())){
                    erpJudge1.setInfoFlag("否");
                    flag = false;
                }
            }
            if (flag){
                erpJudge = new ErpJudge();
                erpJudge.setItemId(information.getProjectId());
                erpJudge.setMainFlag(str);
                erpJudge.setMainStartDate(str);//是否有开工时间
                erpJudge.setMainEndDate(str);//是否有结束时间
                erpJudge.setMainApprovalNumber(str);//批准文号
                erpJudge.setMainDesign(str);//设计费
                erpJudge.setMainSupervision(str);//监理费
                erpJudge.setMainConstructionUnits(str);
                erpJudge.setMainDesignUnits(str);//设计单位
                erpJudge.setMainInvestmen(str);//发文总投资
                erpJudge.setInfoFlag("否");
                mainAll.add(erpJudge);
            }
        }
        return mainAll;
    }


    //得到物料为空的
    public List<Information> getInfoFlag(List<ErpCredit> erpCredits){
        List<String> strings = new ArrayList<String>();
        for (ErpCredit erpCredit:erpCredits){
            strings.add(erpCredit.getItemId());
        }
       //得到物料是为完成数据
        List<Information> informationList = informationService.findNoFinishList(strings);
        return informationList;
    }
    //维护数据汇总
    public List<ErpJudge> getMainAll(List<ErpCredit> erpCredits,List<MaintainData> maintainDatas){
        String str = "无";
        List<ErpJudge> mainFlagSure = getMainFlagSure(erpCredits, maintainDatas);//维护数据有无的集合
        List<ErpJudge> mainJuger = getMainJuger(erpCredits, maintainDatas);//得到所有维护数据 遍历得到 ErpJudge
        for (ErpJudge erpJudge:mainJuger){
            if (erpJudge.getMainStartDate().equals(str)||//是否有开工时间
                erpJudge.getMainEndDate().equals(str)||//是否有结束时间
                erpJudge.getMainApprovalNumber().equals(str)||//批准文号
                erpJudge.getMainDesign().equals(str)||//设计费
                erpJudge.getMainSupervision().equals(str)||//监理费
                erpJudge.getMainConstructionUnits().equals(str)||
                erpJudge.getMainDesignUnits().equals(str)||//设计单位
                erpJudge.getMainInvestmen().equals(str)){//发文总投资
                mainFlagSure.add(erpJudge);
            }
        }
        return mainFlagSure;
    }
    //得到维护数据有无的集合
    public List<ErpJudge> getMainFlagSure(List<ErpCredit> erpCredits,List<MaintainData> maintainDatas){
        String str = "无";
        Boolean flag  = true;
        List<ErpJudge> erpJudges = new ArrayList<ErpJudge>();//得到所有维护数据的集合
        ErpJudge erpJudge = null;
        for (ErpCredit erpCredit1:erpCredits){
            flag = true;
            for (MaintainData maintainData:maintainDatas){
                if (erpCredit1.getItemId().equals(maintainData.getProjectId())){
                    flag = false;
                    break;
                }
            }
            if (flag){
                erpJudge = new ErpJudge();
                erpJudge.setItemId(erpCredit1.getItemId());
                erpJudge.setMainFlag(str);
                erpJudge.setMainStartDate(str);//是否有开工时间
                erpJudge.setMainEndDate(str);//是否有结束时间
                erpJudge.setMainApprovalNumber(str);//批准文号
                erpJudge.setMainDesign(str);//设计费
                erpJudge.setMainSupervision(str);//监理费
                erpJudge.setMainConstructionUnits(str);
                erpJudge.setMainDesignUnits(str);//设计单位
                erpJudge.setMainInvestmen(str);//发文总投资
                erpJudge.setInfoFlag("是");//物料是否完成
                erpJudges.add(erpJudge);
            }
        }
        return erpJudges;
    }
    //得到所有维护数据 遍历得到 ErpJudge
    public List<ErpJudge> getMainJuger(List<ErpCredit> erpCredits,List<MaintainData> maintainDatas){
        List<ErpJudge> erpJudges = new ArrayList<ErpJudge>();
        ErpJudge erpJudge = null;
        for (MaintainData maintainData:maintainDatas){
            for (ErpCredit erpCredit1:erpCredits){
                if (erpCredit1.getItemId().equals(maintainData.getProjectId())){
                    erpJudge = new ErpJudge();
                    erpJudge.setItemId(maintainData.getProjectId());//项目id
                    erpJudge.setMainFlag("有");
                    erpJudge.setMainStartDate("无");//是否有开工时间
                    erpJudge.setMainEndDate("无");//是否有结束时间
                    erpJudge.setMainApprovalNumber("无");//批准文号
                    erpJudge.setMainDesign("无");//设计费
                    erpJudge.setMainSupervision("无");//监理费
                    erpJudge.setMainConstructionUnits("无");
                    erpJudge.setMainDesignUnits("无");//设计单位
                    erpJudge.setMainInvestmen("无");//发文总投资
                    if (null!=maintainData.getStartDate()){
                        erpJudge.setMainStartDate("有");//是否有开工时间
                    }
                    if ( null!=maintainData.getEndDate()){
                        erpJudge.setMainEndDate("有");//竣工时间
                    }
                    if (null!=maintainData.getApprovalNumber()){
                        erpJudge.setMainApprovalNumber("有");//批准文号
                    }
                    if (null!=maintainData.getDesignFee()){
                        erpJudge.setMainDesign("有");//设计费
                    }
                    if (null!=maintainData.getSupervisionFee()){
                        erpJudge.setMainSupervision("有");//监理费
                    }
                    if (null!=maintainData.getConstructionUnits()){
                        erpJudge.setMainConstructionUnits("有");//施工单位
                    }
                    if (null!=maintainData.getDesignUnits()){
                        erpJudge.setMainDesignUnits("有");//设计单位
                    }
                    if (null!=maintainData.getInvestment()){
                        erpJudge.setMainInvestmen("有");//发文投资
                    }
                    erpJudges.add(erpJudge);
                }
            }
        }
        return erpJudges;
    }

    public void getFtpList(HttpServletRequest request,String string){
        String canonicalPath = "";
        try {
            HttpSession session = request.getSession();
            Object oldPath = session.getAttribute("ErpPath");
            if (oldPath != null && oldPath != "") {
                RandomUtil.delete(oldPath.toString());
                session.removeAttribute("ErpPath");
            }
            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));
            bufferedWriter.write(string);
            bufferedWriter.close();
            canonicalPath = temp.getCanonicalPath();
            request.getSession().setAttribute("ErpPath",canonicalPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public String getFtpErpList(HttpServletRequest request){
        HttpSession session = request.getSession();
        Object path = session.getAttribute("ErpPath");
        String str= "";
        if (path != null && !path.equals("")) {
            try {
                BufferedReader bufferedReader = null;
                bufferedReader = new BufferedReader(new FileReader(path.toString()));
                String string;
                while ((string = bufferedReader.readLine()) != null) {
                    str = string;
                }
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        RandomUtil.delete(path.toString());
        session.removeAttribute("ErpPath");
        return str;
    }

//    public String getAllFtpErpList(HttpServletRequest request){
//        HttpSession session = request.getSession();
//        Object path = session.getAttribute("AllErpPath");
//        String str= "";
//        if (path != null && !path.equals("")) {
//            try {
//                BufferedReader bufferedReader = null;
//                bufferedReader = new BufferedReader(new FileReader(path.toString()));
//                String string;
//                while ((string = bufferedReader.readLine()) != null) {
//                    str = string;
//                }
//                bufferedReader.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//        RandomUtil.delete(path.toString());
//        session.removeAttribute("AllErpPath");
//        return str;
//    }
    //    public void getAllFtpList(HttpServletRequest request,String string){
//        String canonicalPath = "";
//        try {
//            HttpSession session = request.getSession();
//            Object oldPath = session.getAttribute("AllErpPath");
//            if (oldPath != null && oldPath != "") {
//                RandomUtil.delete(oldPath.toString());
//                session.removeAttribute("AllErpPath");
//            }
//            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));
//            bufferedWriter.write(string);
//            bufferedWriter.close();
//            canonicalPath = temp.getCanonicalPath();
//            request.getSession().setAttribute("AllErpPath",canonicalPath);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
}