package com.jeeplus.modules.sg.financial.erpcredit.util;

import com.jeeplus.modules.sg.financial.erpcredit.entity.*;
import com.jeeplus.modules.sg.financial.settlement.entity.MaintainData;

import java.io.*;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 导出数据计算类
 */
public class ExportUtilCalculate {
    public static String POWER_DEPARTMENT="配电检修工区";
    public static String POWER_BDZ="变压器";
    public static String POWER_BOXBDZ="箱式变电站";
    public static String POWER_HGW="环网";
    public static String POWER_KGG="高压开关柜";
    public static String POWER_ZYXL="中压线路";
    public static String POWER_BDZCOUNT="160200001";
    public static String POWER_BOXCOUNT="160200002";
    public static String POWER_HGWCOUNT="160200003";
    public static String POWER_KGGCOUNT="160200010";
    public static String POWER_HJ="合计";

    public static String POWER_WU = "无";

    public static String POWER_YU = "有";

    /**
     * 一览表汇总
     * @param erpCreditList
     * @param maintainDataList
     * @return
     */
    public static List<Schedule> getScheduleList(List<ErpCredit> erpCreditList, List<MaintainData> maintainDataList){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        String itemNameAll="合计";//工程名称
        Double budgetBuildProjectAll=0.0;//预算建筑工程
        Double budgetInstallProjectAll=0.0;//预算安装工程
        Double budgetEquipmentValueAll=0.0;//预算设备价值
        Double budgetOtherFeeAll=0.0;//预算其他费用
        Double budgetTotalFeeAll=0.0;//预算合计金额
        Double actualBuildProjectAll=0.0;//实际金额建筑工程
        Double actualIntallProjectAll=0.0;//实际安装工程
        Double actualEquipmentValueAll=0.0;//实际设备价值
        Double actualOtherFeeAll=0.0;//实际其他费用
        Double actualTotalFeeAll=0.0;//实际合计
        Double addSubtractionLinesAll=0.0;//增加额
        Double addSubtractionRateAll=0.0;//增加率
        Double listDeduction = 0.0;//一览表抵扣增值税
        Schedule scheduletotal = new Schedule();
        List<Schedule> schedules = new ArrayList<Schedule>();
        Schedule schedule = null;
        if (null!=erpCreditList&&erpCreditList.size()>0){
            for (ErpCredit erpCredit:erpCreditList){
                schedule = new Schedule();
                schedule = getActualRelative(schedule,erpCredit,maintainDataList);//得到一览表实际金额
                schedules.add(schedule);
            }
        }
        for (Schedule s:schedules){
            if (null!=s.getBudgetBuildProject()){
                budgetBuildProjectAll+=Double.parseDouble(s.getBudgetBuildProject());
                scheduletotal.setBudgetBuildProject(decimalFormat.format(budgetBuildProjectAll));
            }
            if (null!=s.getBudgetTotalFee()){
                budgetInstallProjectAll+=Double.parseDouble(s.getBudgetTotalFee());
                scheduletotal.setBudgetInstallProject(decimalFormat.format(budgetInstallProjectAll));
            }
            if (null!=s.getBudgetEquipmentValue()){
                budgetEquipmentValueAll+=Double.parseDouble(s.getBudgetEquipmentValue());
                scheduletotal.setBudgetEquipmentValue(decimalFormat.format(budgetEquipmentValueAll));
            }
            if (null!=s.getBudgetOtherFee()){
                budgetOtherFeeAll+=Double.parseDouble(s.getBudgetOtherFee());
                scheduletotal.setBudgetOtherFee(decimalFormat.format(budgetOtherFeeAll));
            }
            if (null!=s.getBudgetTotalFee()){
                budgetTotalFeeAll+=Double.parseDouble(s.getBudgetTotalFee());
                scheduletotal.setBudgetTotalFee(decimalFormat.format(budgetTotalFeeAll));
            }
            if (null!=s.getActualBuildProject()){
                actualBuildProjectAll+=Double.parseDouble(s.getActualBuildProject());
                scheduletotal.setActualBuildProject(decimalFormat.format(actualBuildProjectAll));
            }
            if (null!=s.getActualIntallProject()){
                actualIntallProjectAll+=Double.parseDouble(s.getActualIntallProject());
                scheduletotal.setActualIntallProject(decimalFormat.format(actualIntallProjectAll));
            }
            if (null!=s.getActualEquipmentValue()){
                actualEquipmentValueAll+=Double.parseDouble(s.getActualEquipmentValue());
                scheduletotal.setActualEquipmentValue(decimalFormat.format(actualEquipmentValueAll));
            }
            if (null!=s.getActualOtherFee()){
                actualOtherFeeAll+=Double.parseDouble(s.getActualOtherFee());
                scheduletotal.setActualOtherFee(decimalFormat.format(actualOtherFeeAll));
            }
            if (null!=s.getActualTotalFee()&&!"∞".equals(s.getActualTotalFee())){
                actualTotalFeeAll+=Double.parseDouble(s.getActualTotalFee());
                scheduletotal.setActualTotalFee(decimalFormat.format(actualTotalFeeAll));
            }
            if (null!=s.getAddSubtractionLines()){
                addSubtractionLinesAll+=Double.parseDouble(s.getAddSubtractionLines());
                scheduletotal.setAddSubtractionLines(decimalFormat.format(addSubtractionLinesAll));
            }
            if (null!=s.getAddSubtractionRate()&&!"∞".equals(s.getAddSubtractionRate())){
                addSubtractionRateAll+=Double.parseDouble(s.getAddSubtractionRate());
                scheduletotal.setAddSubtractionRate(decimalFormat.format(addSubtractionRateAll));
            }
            if (null!=s.getListDeduction()) {
                listDeduction += Double.parseDouble(s.getListDeduction());
                scheduletotal.setListDeduction(decimalFormat.format(listDeduction));
            }
        }
        scheduletotal.setItemName(itemNameAll);
        schedules.add(scheduletotal);
        return schedules;
    }
    //一览表 实际较预算增减
    public static Schedule getActualRelative(Schedule schedule, ErpCredit erpCredit, List<MaintainData> maintainDataList){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        Schedule scheduleBudget = getScheduleBudget(schedule,erpCredit,maintainDataList);//得到预算合计费用
        Schedule scheduleActual = getScheduleActual(scheduleBudget,erpCredit);//得到实际费用
        double addSubtractionLines = 0.0;//增加额
        double addSubtractionRate1=0.0;//增减去率
        //判断为空的条件
        //如果实际合计金额为空 赋予初始值为0
        if (null==schedule.getActualTotalFee()) {
            schedule.setActualTotalFee("0");
        }
        //如果预算合计金额为空，赋予初始值0
        if (null==schedule.getBudgetTotalFee()){
            schedule.setBudgetTotalFee("0");
        }
        //增加率
        addSubtractionLines=Double.parseDouble(schedule.getActualTotalFee())-Double.parseDouble(schedule.getBudgetTotalFee());
        schedule.setAddSubtractionLines(decimalFormat.format(addSubtractionLines));//添加入郑家率
        if (null==schedule.getBudgetTotalFee()) {//如果预算价值为空，赋初始值为1；
            schedule.setAddSubtractionLines("1");
        }
        //计算增减率
        addSubtractionRate1 = addSubtractionLines/Double.parseDouble(schedule.getBudgetTotalFee())*100;
        schedule.setAddSubtractionRate(decimalFormat.format(addSubtractionRate1));
        if (null != erpCredit.getListDeduction()) {
            schedule.setListDeduction(decimalFormat.format(erpCredit.getListDeduction()));
        }
        return scheduleActual;
    }
    //获取：一览表 预算价值
    public static Schedule getScheduleBudget(Schedule schedule, ErpCredit erpCredit, List<MaintainData> maintainDataList){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        if (null!=maintainDataList&&maintainDataList.size()>0){
            for (MaintainData maintainData:maintainDataList){
                //判断id是否一样
                if (maintainData.getProjectId().equals(erpCredit.getItemId())){
                    schedule.setBudgetBuildProject(decimalFormat.format(maintainData.getBuildingFee()));//建筑费
                    //安装供材 = 安装费+主材费
                    schedule.setBudgetInstallProject(decimalFormat.format(maintainData.getInstallFee()+maintainData.getMaterialFee()));
                    schedule.setBudgetEquipmentValue(decimalFormat.format(maintainData.getEquipmentFee()));//设备购置费
                    //其他费用合计
                    schedule.setBudgetOtherFee(decimalFormat.format(maintainData.getDesignFee()+maintainData.getSupervisionFee()+maintainData.getPreliminaryWorkFee()
                            +maintainData.getDamages()+maintainData.getManagementFee()));
                    //预算合计费用
                    schedule.setBudgetTotalFee(decimalFormat.format(maintainData.getBuildingFee()+maintainData.getInstallFee()
                            +maintainData.getMaterialFee()+maintainData.getEquipmentFee()+maintainData.getDesignFee()+maintainData.getSupervisionFee()+maintainData.getPreliminaryWorkFee()
                            +maintainData.getDamages()+maintainData.getManagementFee()));
                }
            }
        }
        return schedule;
    }
    //计算一览表合计

    //获取：一览表 实际价值
    public static Schedule getScheduleActual(Schedule schedule, ErpCredit erpCredit){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        boolean flag = false;
        List<ErpCreditEquipment> erpCreditEquipmentList = null;//得到设备费金额
        double netAmount=0.0;//入账金额:设备费，材料费
        double actualTotalFee=0.0;//合计金额
        //循环erpCredit集合
        schedule.setItemId(erpCredit.getItemId());//得到项目定义编号
        schedule.setItemName(erpCredit.getItemName());//得到项目名称
        netAmount = getErpCreditMaterial(erpCredit);//得到材料费
        Double eqmOneFlagValue = getEqmOneFlagValue(erpCredit.getErpCreditChild1s());//如果设备仅有一条且配变数量为0
        //设备费&&erpCredit.getTenKvModel()!=null
        if (null!=erpCredit.getConsInstallFee()){
            schedule.setActualIntallProject(decimalFormat.format(netAmount+erpCredit.getConsInstallFee()+eqmOneFlagValue));
            actualTotalFee=actualTotalFee+netAmount+erpCredit.getConsInstallFee()+eqmOneFlagValue;//合计金额累加
        }
        if (null!=erpCredit.getConsBuildingFee()){//如果建筑费不为空
            //得到实际建筑工程
            schedule.setActualBuildProject(decimalFormat.format(erpCredit.getConsBuildingFee()+netAmount+eqmOneFlagValue));
            actualTotalFee+=erpCredit.getConsBuildingFee()+netAmount+eqmOneFlagValue;//合计金额加上
        }
        netAmount = 0.0;//此时变为设备费
        erpCreditEquipmentList = getEquNumbers(erpCredit.getErpCreditChild1s());//当设备台数为0时处理
        //判断设备费
        if (null!=erpCreditEquipmentList&&erpCreditEquipmentList.size()>0){
            for (ErpCreditEquipment erpCreditEquipment:erpCreditEquipmentList){
                if (null!=erpCreditEquipment.getEqmBookedFee1()){
                    netAmount+=erpCreditEquipment.getEqmBookedFee1();//得到设备费累加（设备价值）
                    flag=true;
                }
            }
            if (flag){
                schedule.setActualEquipmentValue(decimalFormat.format(netAmount));//实际设备价值
                actualTotalFee+=netAmount;//实际费用累加
            }
            flag=false;//数据清0
            netAmount=0.0;
        }
        //得到其他费用合计
        if (null!=erpCredit.getOtherTotalFee()){//得到其他费用
            schedule.setActualOtherFee(decimalFormat.format(erpCredit.getOtherTotalFee()));
            actualTotalFee+=erpCredit.getOtherTotalFee();//合计金额累加
        }
        //添加合计金额
        schedule.setActualTotalFee(decimalFormat.format(actualTotalFee));//写入合计金额
        return schedule;
    }


    public static OtherExpenses getExpenses(OtherExpenses otherExpenses, ErpCredit erpCredit, List<MaintainData> maintainDataList){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        Double otherSupervisorFee = getExDouble(erpCredit.getOtherSupervisorFee());
        Double otherDesignFee = getExDouble(erpCredit.getOtherDesignFee());
        Double otherBuildLoan = getExDouble(erpCredit.getOtherBuildLoan());
        Double otherLineDamages = getExDouble(erpCredit.getOtherLineDamages());
        Double otherTotalFee = getExDouble(erpCredit.getOtherTotalFee());
        Double deduction = getExDouble(erpCredit.getDeduction());
        OtherExpenses otherActual = new OtherExpenses();
        otherActual.setProjectName(erpCredit.getItemName());
        otherActual.setProjectNumber(erpCredit.getItemId());
        String format = decimalFormat.format(otherTotalFee - otherLineDamages - otherBuildLoan - otherDesignFee - otherSupervisorFee);
        if (format.equals("-0.00")) {
            format = format.replace("-",  "");
        }
        otherActual.setManagementFunds(format);
        otherActual.setSupervisionFee(decimalFormat.format(otherSupervisorFee));
        otherActual.setDesignFee(decimalFormat.format(otherDesignFee));
        otherActual.setLoanInterest(decimalFormat.format(otherBuildLoan));
        otherActual.setDamages(decimalFormat.format(otherLineDamages));
        otherActual.setTotal(decimalFormat.format(otherTotalFee));
        otherActual.setDeduction(decimalFormat.format(deduction));
        otherActual.setIncludingTax(decimalFormat.format(otherTotalFee + deduction));
        /*OtherExpenses otherBudget = getScheduleBudget(schedule,erpCredit,maintainDataList);//得到预算合计费用
        OtherExpenses otherActual = getScheduleActual(scheduleBudget,erpCredit);//得到实际费用
        double addSubtractionLines = 0.0;//增加额
        double addSubtractionRate1=0.0;//增减去率
        //判断为空的条件
        //如果实际合计金额为空 赋予初始值为0
        if (null==schedule.getActualTotalFee()) {
            schedule.setActualTotalFee("0");
        }
        //如果预算合计金额为空，赋予初始值0
        if (null==schedule.getBudgetTotalFee()){
            schedule.setBudgetTotalFee("0");
        }
        //增加率
        addSubtractionLines=Double.parseDouble(schedule.getActualTotalFee())-Double.parseDouble(schedule.getBudgetTotalFee());
        schedule.setAddSubtractionLines(decimalFormat.format(addSubtractionLines));//添加入郑家率
        if (null==schedule.getBudgetTotalFee()) {//如果预算价值为空，赋初始值为1；
            schedule.setAddSubtractionLines("1");
        }
        //计算增减率
        addSubtractionRate1 = addSubtractionLines/Double.parseDouble(schedule.getBudgetTotalFee())*100;
        schedule.setAddSubtractionRate(decimalFormat.format(addSubtractionRate1));*/
        return otherActual;
    }

    private static Double getExDouble(Double otherSupervisorFee) {
        if (null == otherSupervisorFee) {
            otherSupervisorFee = 0.0;
        }
        return otherSupervisorFee;
    }


    /**
     * 其他费用明细表 实际费用
     * @param erpCreditList
     * @return
     */
    public static ScheduleOther getScheduleOfOtherExpenses(List<ErpCredit> erpCreditList){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        ScheduleOther scheduleOther = new ScheduleOther();
        Double requisitionSite=0.0;//建设场地征用及清理费
        Double  compensationAcquisition=0.0;//土地征用补偿费
        Double compensateFee=0.0;//土地征用补偿费（赔青费）
        Double  managementFee=0.0;//项目建设管理费
        Double  preWorkingFee=0.0;//项目前期工作费
        Double  itemManageFee=0.0;//项目管理经费
        Double  supervisionFee=0.0;//工程监理费
        Double   technicalFee=0.0;//项目建设技术服务费
        Double  surveyFee=0.0;//工程勘察费
        Double  designFee=0.0;//工程设计费
        Double  economicFee=0.0;//技术经济标准变值管理费
        Double  inspectionFee=0.0;//工程建设监督检测费
        Double  preparationFee=0.0;//生产准备费
        Double  basicFee=0.0;//基本预备费
        Double  reserveFee=0.0;//资本化利息
        Double  totalFee=0.0;//合计
        if (null!=erpCreditList&&erpCreditList.size()>0){
            for (ErpCredit erpCredit:erpCreditList){
                //如果线路施工赔偿费不等于空，土地征用补偿费累加
                if (null!=erpCredit.getOtherLineDamages()){
                    compensateFee+=erpCredit.getOtherLineDamages();//赔青费
                }
                //前期工作费合计
                if (null!=erpCredit.getOtherBeWorkFee()){
                    preWorkingFee+=erpCredit.getOtherBeWorkFee();
                }
                //如果法人管理费不为空，则赋值项目管理经费
                if (null!=erpCredit.getOtherLegalManage()){
                    itemManageFee+=erpCredit.getOtherLegalManage();//项目管理经费
                }
                //如果监理费不为空，则赋值为工程监理费
                if (null!=erpCredit.getOtherSupervisorFee()){
                    supervisionFee+=erpCredit.getOtherSupervisorFee();
                }
                //如果设计费不为空，则赋值为工程设计费
                if (null!=erpCredit.getOtherDesignFee()){
                    designFee+=erpCredit.getOtherDesignFee();
                }
                //如果=ERP账生成数据中建设期贷款利息不为空，则累计赋值到资本化利息
                if (null!= erpCredit.getOtherBuildLoan()){
                    reserveFee+=erpCredit.getOtherBuildLoan();
                }
            }
        }
        //项目建设技术服务费（暂无）
        technicalFee=designFee;
        //项目建设管理费
        managementFee=itemManageFee+supervisionFee+preWorkingFee;
        //建设场地征用费
        requisitionSite=compensationAcquisition+compensateFee;
        //合计
        totalFee=requisitionSite+managementFee+technicalFee+inspectionFee+preparationFee+basicFee+reserveFee+preWorkingFee;
        scheduleOther.setCompensateFee(decimalFormat.format(compensateFee));//土地征用补偿费
        scheduleOther.setItemManageFee(decimalFormat.format(itemManageFee));//项目管理经费
        scheduleOther.setSupervisionFee(decimalFormat.format(supervisionFee));//工程监理费
        scheduleOther.setDesignFee(decimalFormat.format(designFee));//工程设计费
        scheduleOther.setReserveFee(decimalFormat.format(reserveFee));//资本化利息
        scheduleOther.setTechnicalFee(decimalFormat.format(technicalFee));//项目设计技术服务费
        scheduleOther.setManagementFee(decimalFormat.format(managementFee));//项目建设管理费
        scheduleOther.setRequisitionSite(decimalFormat.format(requisitionSite));//建设场地征用费
        scheduleOther.setTotalFee(decimalFormat.format(totalFee));//合计
        return scheduleOther;
    }
    /**
     * 其他费用明细表 预算费用
     * @param
     * @return
     */
    public static ScheduleOther getScheduleOfOtherExpensesReg(List<MaintainData> maintainDataList){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        Double requisitionSite=0.0;//建设场地征用及清理费
        ScheduleOther scheduleOther = new ScheduleOther();
        Double  cleaningFees=0.0;//余物清理费
        Double  managementFee=0.0;//项目建设管理费
        Double  preWorkingFee=0.0;//项目前期工作费
        Double  itemManageFee=0.0;//项目管理经费
        Double  supervisionFee=0.0;//工程监理费
        Double  designFee=0.0;//工程设计费
        Double  preparationFee=0.0;//生产准备费
        Double compensateFee=0.0;//土地征用补偿费（赔青费）
        Double  totalFee=0.0;//合计
        if (null!=maintainDataList&&maintainDataList.size()>0){
            for (MaintainData maintainData:maintainDataList){
                compensateFee+=maintainData.getDamages();
                cleaningFees+=maintainData.getDamages();//清理费
                preWorkingFee+=maintainData.getPreliminaryWorkFee();//项目前期工作费
                itemManageFee+=maintainData.getManagementFee();//法人管理经费
                supervisionFee+=maintainData.getSupervisionFee();//监理费
                designFee+=maintainData.getDesignFee();//工程设计费
            }
            //项目建设管理费
            managementFee = preWorkingFee+itemManageFee+supervisionFee;
            //合计金额
            totalFee=preWorkingFee+itemManageFee+supervisionFee+designFee+compensateFee;//合计金额
            scheduleOther.setPreWorkingFee(decimalFormat.format(preparationFee));//项目前期工作费合计
            scheduleOther.setItemManageFee(decimalFormat.format(itemManageFee));//法人管理经费
//            scheduleOther.setCleaningFees(decimalFormat.format(cleaningFees));//余物清理费
            scheduleOther.setSupervisionFee(decimalFormat.format(supervisionFee));//工程监理费
            scheduleOther.setDesignFee(decimalFormat.format(designFee));//工程设计费合计
            scheduleOther.setPreWorkingFee(decimalFormat.format(preparationFee));
            //合计金额
            scheduleOther.setTotalFee(decimalFormat.format(totalFee));//合计金额
            scheduleOther.setCompensateFee(decimalFormat.format(compensateFee));//赔青
            //项目建设管理费
            scheduleOther.setManagementFee(decimalFormat.format(managementFee));
            //工程建设服务费
            scheduleOther.setTechnicalFee(decimalFormat.format(designFee));
            scheduleOther.setRequisitionSite(decimalFormat.format(compensateFee));//建设场地征用及清理费
        }
        return scheduleOther;
    }
    /**
     * 设备材料去重显示2 单价
     */
    public static List<MaterialEquipment> getMaterialUnitPrice(List<MaterialEquipment> materialEquipmentList){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        Double meUnitPrice=0.0;//单价
        for (MaterialEquipment materialEquipment:materialEquipmentList){
            meUnitPrice=0.0;
            if (null!=materialEquipment.getMeAllPrice()&&null!=materialEquipment.getMeNumber()){
                if (!materialEquipment.getMeNumber().equals("0.0")&&!materialEquipment.getMeNumber().equals("0")){
                    meUnitPrice = Double.valueOf(materialEquipment.getMeAllPrice())/Double.valueOf(materialEquipment.getMeNumber());
                    materialEquipment.setMeUnitPrice(decimalFormat.format(meUnitPrice));
                }else {
                    materialEquipment.setMeUnitPrice("");
                }
            }
        }
        return materialEquipmentList;
    }
    /**
     * 设备材料清册去重显示
     */
    public static List<MaterialEquipment> getSureMateralEquipment(List<ErpCredit> erpCredits){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        DecimalFormat decimalFormat1 = new DecimalFormat("#0");
        Boolean flag = true;
        Double meNumber=0.0;//数量
        Double meUnitPrice=0.0;//单价
        Double meAllPrice=0.0;//合计
        MaterialEquipment materialEquipment2 = null;
        List<MaterialEquipment> materialEquipmentList = new ArrayList<MaterialEquipment>();
        List<MaterialEquipment> materialEquipments = getMaterialEquipment(erpCredits);
        for (MaterialEquipment materialEquipment:materialEquipments){
            meNumber = Double.valueOf(materialEquipment.getMeNumber());
            meAllPrice = Double.valueOf(materialEquipment.getMeAllPrice());
            flag = true;
            if (null!=materialEquipmentList&&materialEquipmentList.size()>0){
                for (MaterialEquipment materialEquipment1:materialEquipmentList){
                    if (materialEquipment.getMeSupplierName().equals(materialEquipment1.getMeSupplierName())||
                           materialEquipment.getMeName().equals(materialEquipment1.getMeName())){
                        meNumber = meNumber+Double.valueOf(materialEquipment1.getMeNumber());
                        meAllPrice = meAllPrice+Double.valueOf(materialEquipment1.getMeAllPrice());
                        materialEquipment1.setMeNumber(decimalFormat.format(meNumber));
                        materialEquipment1.setMeAllPrice(decimalFormat.format(meAllPrice));
                        flag = false;
                        break;
                    }
                }
            }
            if (flag){
                materialEquipment2 = new MaterialEquipment();
                materialEquipment2.setMeName(materialEquipment.getMeName());
                materialEquipment2.setMeSupplierName(materialEquipment.getMeSupplierName());
                materialEquipment2.setMeAllPrice(materialEquipment.getMeAllPrice());
                materialEquipment2.setMeNumber(materialEquipment.getMeNumber());
                materialEquipment2.setMeUnit(materialEquipment.getMeUnit());
                materialEquipment2.setId(materialEquipment.getId());
                materialEquipment2.setMeAllPrice(materialEquipment.getMeAllPrice());
                materialEquipment2.setMeSpecifications(materialEquipment.getMeSpecifications());
                materialEquipmentList.add(materialEquipment2);
            }
        }
        List<MaterialEquipment> materialUnitPrice = getMaterialUnitPrice(materialEquipmentList);
        return materialEquipmentList;
    }
    /**
     * 设备材料清册
     * @param erpCredits
     * @return
     */
    public static List<MaterialEquipment> getMaterialEquipment(List<ErpCredit> erpCredits){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        DecimalFormat decimalFormat1 = new DecimalFormat("#0");
        Double count = 0.0;
        List<MaterialEquipment> materialEquipments = new ArrayList<MaterialEquipment>();
        MaterialEquipment materialEquipment = null;
        List<ErpCreditEquipment> erpCreditChild1s = null;
        List<ErpCreditMaterial> erpCreditChild2s = null;
        for (ErpCredit erpCredit:erpCredits){
            erpCreditChild1s=erpCredit.getErpCreditChild1s();//获取设备费描述
            erpCreditChild2s=erpCredit.getErpCreditChild2s();//获取材料表描述
            //设备详情
            if (null!=erpCreditChild1s&&erpCreditChild1s.size()>0){
                for (ErpCreditEquipment erpCreditChild1:erpCreditChild1s){
                    count=0.0;
                    materialEquipment=new MaterialEquipment();
                    materialEquipment.setId(erpCredit.getItemId());//得到项目id
                    //物料描述赋值给名称
                    materialEquipment.setMeName(erpCreditChild1.getEqmMaterialDetails1());
                    //如果供应商不为空，赋值供应商
                    if (null!=erpCreditChild1.getEqmSupplierName()){
                        materialEquipment.setMeSupplierName(erpCreditChild1.getEqmSupplierName());
                    }
                    //得到单位
                    if (null!=erpCreditChild1.getEqmUnit()){
                        materialEquipment.setMeUnit(erpCreditChild1.getEqmUnit());
                    }
                    //得到数量
                    if (null!=erpCreditChild1.getEqmNumbers()){
                        materialEquipment.setMeNumber(String.valueOf(erpCreditChild1.getEqmNumbers()));
                    }
                    //得到合计金额
                    if (null!=erpCreditChild1.getEqmBookedFee1()){
                        materialEquipment.setMeAllPrice(String.valueOf(erpCreditChild1.getEqmBookedFee1()));
                    }else {
                        materialEquipment.setMeAllPrice("0");
                    }
                    materialEquipments.add(materialEquipment);
                }
            }
            //材料详情
            if (null!=erpCreditChild2s&&erpCreditChild2s.size()>0){
                for (ErpCreditMaterial erpCreditChild2:erpCreditChild2s){
                    count=0.0;
                    materialEquipment=new MaterialEquipment();
                    //得到材料费项目定义号
                    materialEquipment.setId(erpCredit.getItemId());
                    //得到材料费名称
                    materialEquipment.setMeName(erpCreditChild2.getEqmMaterialDetails2());
                    //得到供应商
                    if (null!=erpCreditChild2.getMtlSupplierName()){
                        materialEquipment.setMeSupplierName(erpCreditChild2.getMtlSupplierName());
                    }
                    //得到单位
                    if (null!=erpCreditChild2.getMtlUnit()){
                        materialEquipment.setMeUnit(erpCreditChild2.getMtlUnit());
                    }
                    //得到数量
                    if (null!=erpCreditChild2.getMtlNumbers()){
                        materialEquipment.setMeNumber(String.valueOf(erpCreditChild2.getMtlNumbers()));
                    }
                    //得到合计金额
                    if (null!=erpCreditChild2.getMtlBookedFee2()){
                        materialEquipment.setMeAllPrice(String.valueOf(erpCreditChild2.getMtlBookedFee2()));
                    }
                    materialEquipments.add(materialEquipment);
                }
            }
        }
        return materialEquipments;
    }

    /**
     * 移交资产—配电网资产一览表(线路) 所有
     * @return
     */
    public static List<TransferOfPowerLine> getTransFerPower1(List<ErpCredit> erpCredits){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        List<TransferOfPowerLine> transferOfPower1s = new ArrayList<TransferOfPowerLine>();//创建一个线路集合
        TransferOfPowerLine transferOfPower1 = null;//定义线路对象
        List<ErpCreditEquipment> erpCreditEquipmentList = null;//定义设备对象
        Double materialTotal = 0.0;//材料费总计
        Double equipmentCost = 0.0;//设备费总计
        Double eqmValue = 0.0;//相同项目定义号合计:所有设备费
        Double standInFee=0.0;//摊入费用

        Double installEqm = 0.0;//特殊情况处理
        for (ErpCredit erpCredit:erpCredits){//循环erp账户项目
            materialTotal = getErpCreditMaterial(erpCredit);//得到材料费合计
            equipmentCost = getEqmValueTotal(erpCredit.getErpCreditChild1s());//得到设备费合计
            erpCreditEquipmentList = erpCredit.getErpCreditChild1s();//获取设备集合
            installEqm = getEqmOneFlagValue(erpCreditEquipmentList);//得到设备台数都为0的值

            erpCreditEquipmentList = getEquNumbers(erpCreditEquipmentList);//特殊情况处理后的集合
            eqmValue = getEqmValueTotal(erpCreditEquipmentList);//所有设备费合计

            //添加固定数据:线路
            transferOfPower1  = new TransferOfPowerLine();//得到线路集合
            transferOfPower1.setPowerId(erpCredit.getItemId());//添加项目定义号
            transferOfPower1.setPowerName(erpCredit.getItemName());//添加项目名称
            transferOfPower1.setPowerArea("-");//添加其实地点和所在第
            transferOfPower1.setPowerNumberBars("1");
            transferOfPower1.setPowerAssetClass(POWER_ZYXL);//中亚线路
            transferOfPower1.setPowerDepartment(ExportUtilCalculate.POWER_DEPARTMENT);//添加保管
            if (null!=erpCredit.getTenKvItsLine()) {//其中 10kv线路 10kv线路长度
                transferOfPower1.setPowerTenKvLineLength(decimalFormat.format(erpCredit.getTenKvItsLine()));//添加10kv线路
            }
            if (null!=erpCredit.getTenKvModel()){//10kv电缆架空导线型号
                transferOfPower1.setPowerTenKvCableType(erpCredit.getTenKvModel());
            }
            //添加保护管信息
            if (null!=erpCredit.getCableTube()&&!"".equals(erpCredit.getCableTube())){
                transferOfPower1.setPowerTenKvCableType(erpCredit.getCableTube());
            }
            if (null!=erpCredit.getConsBuildingFee()){//建筑费
                transferOfPower1.setPowerBuildFee(decimalFormat.format(erpCredit.getConsBuildingFee()+materialTotal+installEqm));
            }
            if (null!=erpCredit.getConsInstallFee()&&erpCredit.getTenKvModel()!=null){//安装费
                transferOfPower1.setPowerInstallFee(decimalFormat.format(erpCredit.getConsInstallFee() + materialTotal + installEqm - equipmentCost));//添加安装费
            }else {
                transferOfPower1.setPowerInstallFee("-");
            }
            if (transferOfPower1.getPowerInstallFee().equals("-")){//摊入费用
                if (transferOfPower1.getPowerBuildFee()!=null){
                    standInFee = Double.valueOf(transferOfPower1.getPowerBuildFee())+eqmValue;
                    standInFee = Double.valueOf(transferOfPower1.getPowerBuildFee())/standInFee;
                    if (erpCredit.getOtherTotalFee()!=null){
                        standInFee = standInFee*erpCredit.getOtherTotalFee();//乘其他费用合计
                    }
                    transferOfPower1.setPowerStandInFee(decimalFormat.format(standInFee));//添加摊入费用
                }
            }else {
                //摊入费用
                standInFee = Double.valueOf(transferOfPower1.getPowerInstallFee())+eqmValue;
                standInFee = Double.valueOf(transferOfPower1.getPowerInstallFee())/standInFee;
                if (erpCredit.getOtherTotalFee()!=null){
                    standInFee = standInFee*erpCredit.getOtherTotalFee();//乘其他费用合计
                }
                transferOfPower1.setPowerStandInFee(decimalFormat.format(standInFee));//添加摊入费用
            }
            transferOfPower1s.add(transferOfPower1);//添加到集合中
        }
        List<TransferOfPowerLine> powerDeliveredAssets1 = getPowerDeliveredAssets(transferOfPower1s);
        List<TransferOfPowerLine> transFerPowerLineTotal = getTransFerPowerLineTotal(powerDeliveredAssets1);
        return transFerPowerLineTotal;
    }
    //移交资产一览表   交付使用知产价值
    public static List<TransferOfPowerLine> getPowerDeliveredAssets(List<TransferOfPowerLine> lineList){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        Double powerDeliveredAssets=0.0;//交付知产使用价值
        for (TransferOfPowerLine transferOfPowerLine:lineList){
            powerDeliveredAssets=0.0;//交付知产使用价值
            if (null!=transferOfPowerLine.getPowerBuildFee()){//建筑费不为空
                powerDeliveredAssets+=Double.valueOf(transferOfPowerLine.getPowerBuildFee());
            }
            if (null!=transferOfPowerLine.getPowerInstallFee()){//安装费不为空
                if (!transferOfPowerLine.getPowerInstallFee().equals("-")){
                    powerDeliveredAssets+=Double.valueOf(transferOfPowerLine.getPowerInstallFee());
                }
            }
            //摊入费用不为空
            if (null!=transferOfPowerLine.getPowerStandInFee()){
                powerDeliveredAssets+=Double.valueOf(transferOfPowerLine.getPowerStandInFee());
                transferOfPowerLine.setPowerDeliveredAssets(decimalFormat.format(powerDeliveredAssets));
            }
            //添加交付使用知产
        }
        return lineList;
    }
    //移交资产一览表合计
    public static  List<TransferOfPowerLine> getTransFerPowerLineTotal(List<TransferOfPowerLine> lineList){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        Double powerTenKvLineLengthAll=0.0;//10kv线路长度
        Double buildFeeAll=0.0;//建筑费用合计
        Double installFeeAll=0.0;//安装费用合计
        Double standInFeeAll=0.0;//摊入费用合计
        Double powerDeliveredAssetsAll=0.0;//交付知产使用价值合计
        TransferOfPowerLine transferOf = new TransferOfPowerLine();
        for (TransferOfPowerLine transferOfPowerLine:lineList){
            //10kv线路长度合计
            if (null!=transferOfPowerLine.getPowerTenKvLineLength()){
                powerTenKvLineLengthAll+=Double.valueOf(transferOfPowerLine.getPowerTenKvLineLength());//一览表中10kv长度的合计
            }
            //一览表中建筑费用合计
            if (null!=transferOfPowerLine.getPowerBuildFee()){
                buildFeeAll+=Double.valueOf(transferOfPowerLine.getPowerBuildFee());
            }
            //一览表中安装费用合计
            if (null!=transferOfPowerLine.getPowerInstallFee()){
                if (!transferOfPowerLine.getPowerInstallFee().equals("-")){
                    installFeeAll+=Double.valueOf(transferOfPowerLine.getPowerInstallFee());
                }
            }
            //一览表中摊入费用合计
            if (null!=transferOfPowerLine.getPowerStandInFee()){
                standInFeeAll+=Double.valueOf(transferOfPowerLine.getPowerStandInFee());
            }
            //交付使用资产价值合计
            if (null!=transferOfPowerLine.getPowerDeliveredAssets()){
                powerDeliveredAssetsAll+=Double.valueOf(transferOfPowerLine.getPowerDeliveredAssets());
            }
        }
        transferOf.setPowerTenKvLineLength(decimalFormat.format(powerTenKvLineLengthAll));
        transferOf.setPowerBuildFee(decimalFormat.format(buildFeeAll));
        transferOf.setPowerInstallFee(decimalFormat.format(installFeeAll));
        transferOf.setPowerStandInFee(decimalFormat.format(standInFeeAll));
        transferOf.setPowerDeliveredAssets(decimalFormat.format(powerDeliveredAssetsAll));
        lineList.add(transferOf);
        return lineList;
    }
    /**
     * 移交资产—配电网资产一览表(设备)  合计
     * @param erpCredits
     * @return
     */
    public static List<TransferOfEquipment> getTransferOfPowerEquList(List<ErpCredit> erpCredits){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        DecimalFormat decimalFormat1   = new DecimalFormat("#0");
        List<TransferOfEquipment> transferOfPower2s =  new ArrayList<TransferOfEquipment>();//得到设备表集合
        List<ErpCreditEquipment> erpCreditEquipmentList = null;//定义设备 费对象
        TransferOfEquipment transferOfPower2 = null;//定义设备表对象
        Double powerInstall = 0.0;//安装费
        Double materialTotal = 0.0;//材料费总计
        Double powerValueTotal = 0.0;//当前erp账户 所有设备费合计
        Double powerValue = 0.0;//设备价值
        Double eqmFtIntalls = 0.0;//获取分摊安装费
        Double powerInstallFee = 0.0;//安装费用(线路)
        Double powerEqmMater=0.0;//物料材料合计
        Double powerStandInFee = 0.0;//摊入费用
        for (ErpCredit erpCredit:erpCredits){
            powerValueTotal = 0.0;//当前erp账户 所有设备费合计 赋值初始值


            //材料费合计
            materialTotal = getErpCreditMaterial(erpCredit);//得到材料费
            erpCreditEquipmentList = getEquNumbers(erpCredit.getErpCreditChild1s());//特殊情况处理得到最终设备费
            //所有设备费累加
            powerValueTotal = getEqmValueTotal(erpCreditEquipmentList);

            Double allBookedFee = 0.0;

            //获取当前项目定义号的所有设备价值总和
            for (ErpCreditEquipment e :
                    erpCreditEquipmentList) {
                allBookedFee += e.getEqmBookedFee1();
            }

            //得到安装费用 移交生产总值 一览表1线路
            if (null!=erpCredit.getConsInstallFee()){ //安装费（x线路表）
                powerInstallFee = erpCredit.getConsInstallFee()+materialTotal;
            }
            if (null!=erpCredit.getConsBuildingFee()){
                powerInstallFee = erpCredit.getConsBuildingFee()+materialTotal;//赋值powerInstallfe为建筑费
            }
            if (null==erpCredit.getTenKvModel()){
                powerInstallFee = 0.0;
            }
            if (null!=erpCreditEquipmentList && erpCreditEquipmentList.size()>=0){//
                for (ErpCreditEquipment erpCreditEquipment:erpCreditEquipmentList){
                    transferOfPower2 = new TransferOfEquipment();//定义设备表对象
                    transferOfPower2.setPowerId(erpCredit.getItemId());//赋值项目定义编号
                    transferOfPower2.setPowerName(erpCredit.getItemName());//添加项目名称
                    transferOfPower2.setPowerArea("-");//线路起始地带
                    transferOfPower2.setPowerDepartment(ExportUtilCalculate.POWER_DEPARTMENT);//添加保管
                    if (null!=erpCredit.getConsInstallFee()&& null != erpCreditEquipment.getEqmBookedFee1()){//得到分摊后安装费
                        eqmFtIntalls = erpCreditEquipment.getEqmBookedFee1()/powerValueTotal;
                        eqmFtIntalls = eqmFtIntalls*powerInstallFee;
                    }
                    if (null!=erpCreditEquipment.getEqmNumbers()){//得到配变数量（erp账户中设备台数）
                        transferOfPower2.setPowerNumber(decimalFormat1.format(erpCreditEquipment.getEqmNumbers()));
                    }
                    if (null!=erpCreditEquipment.getEqmUnit()){//计量单位
                        transferOfPower2.setPowerUnit(erpCreditEquipment.getEqmUnit());
                    }
                    //配电容量
                    transferOfPower2.setPowerCapacity(erpCreditEquipment.getEqmMaterialDetails1());
                    //设备价值
                    if (null!=erpCreditEquipment.getEqmBookedFee1()){
                        powerValue = erpCreditEquipment.getEqmBookedFee1();
                        transferOfPower2.setPowerValue(decimalFormat.format(powerValue));//赋值设备价值
                    }
                    //安装费用
                    if (null!=transferOfPower2.getPowerCapacity()&&null==erpCredit.getTenKvModel()){
                        //未分摊安装费
                        double v = allBookedFee;
                        double buildFee = materialTotal - powerValueTotal + erpCredit.getConsTotalFee();
                        Double aDouble = Double.valueOf(transferOfPower2.getPowerValue());
                        double v1 = aDouble / v;
                        double v2 = buildFee * v1;
                        //安装费
                        powerInstall = v2;
                        transferOfPower2.setPowerInstallFee(decimalFormat.format(powerInstall));
                    }else {
//                        powerInstall = 0.0;
                        transferOfPower2.setPowerInstallFee("0");
                    }
                    //摊入
//                    powerEqmMater = powerValueTotal+powerInstallFee;//===所有的设备价值+安装费+材料费

                    //线路为空，分摊规则与04-2安装规则一致
                    if (null == erpCredit.getTenKvModel()) {
                        if (Double.valueOf(transferOfPower2.getPowerValue())!=0.0){
                            double v = allBookedFee;
                            Double aDouble = Double.valueOf(transferOfPower2.getPowerValue());
                            Double otherTotalFee = erpCredit.getOtherTotalFee();
                            double v1 = aDouble / v;
                            double v2 = otherTotalFee * v1;
                            powerStandInFee = v2;
                        }else {
                            powerStandInFee =0.0;
                        }
                    }else {
                        if (Double.valueOf(transferOfPower2.getPowerValue())!=0.0){
                            double v = materialTotal - powerValueTotal + erpCredit.getConsTotalFee() +allBookedFee;
                            Double aDouble = Double.valueOf(transferOfPower2.getPowerValue());
                            Double otherTotalFee = erpCredit.getOtherTotalFee();
                            double v1 = aDouble / v;
                            double v2 = otherTotalFee * v1;
                            powerStandInFee = v2;
                        }else {
                            powerStandInFee =0.0;
                        }
                    }
                    transferOfPower2.setPowerStandInFee(decimalFormat.format(powerStandInFee));//赋值设备分摊费用
                    //添加资产类别
                    if (transferOfPower2.getPowerCapacity().contains(POWER_BDZ)){
                        transferOfPower2.setPowerAssetClass(POWER_BDZCOUNT);
                    }
                    if (transferOfPower2.getPowerCapacity().contains(POWER_BOXBDZ)){
                        transferOfPower2.setPowerAssetClass(POWER_BOXCOUNT);
                    }
                    if (transferOfPower2.getPowerCapacity().contains(POWER_HGW)&&!transferOfPower2.getPowerCapacity().contains(POWER_WU)
                        &&!transferOfPower2.getPowerCapacity().contains(POWER_YU)){
                        transferOfPower2.setPowerAssetClass(POWER_HGWCOUNT);
                    }
                    if (transferOfPower2.getPowerCapacity().contains(POWER_KGG)){
                        transferOfPower2.setPowerAssetClass(POWER_KGGCOUNT);
                    }
                    transferOfPower2s.add(transferOfPower2);
                }
            }
        }
        return transferOfPower2s;
    }
    //移交生产资产2 - 设备表 交付使用价值 安装费+设备费+摊入费用
    public static List<TransferOfEquipment> getPowerDeliveredAssets2(List<TransferOfEquipment> transferOfEquipments){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        Double powerDeliveredAssets=0.0;//交付使用资产价值
        for (TransferOfEquipment transferOfEquipment:transferOfEquipments){
            powerDeliveredAssets = 0.0;
            //如果安装费不为空
            if (null!=transferOfEquipment.getPowerInstallFee()){
                powerDeliveredAssets+=Double.valueOf(transferOfEquipment.getPowerInstallFee());
            }
            //如果设备价值不为空
            if (null!=transferOfEquipment.getPowerValue()){
                powerDeliveredAssets+=Double.valueOf(transferOfEquipment.getPowerValue());
            }
            //如果摊入费用不为空
            if (null!=transferOfEquipment.getPowerStandInFee()){
                powerDeliveredAssets+=Double.valueOf(transferOfEquipment.getPowerStandInFee());
            }
            //如果交付使用资产总值
            transferOfEquipment.setPowerDeliveredAssets(decimalFormat.format(powerDeliveredAssets));
        }
        return transferOfEquipments;
    }

    //移交生产总值-设备表 安装费合计 建筑费合计 交付使用价值合计
    public static List<TransferOfEquipment> getTransferOfPowerTotal(List<TransferOfEquipment> transferOfEquipments){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        Double eqmNumbersAll = 0.0;//数量合计
        Double powerValueAll=0.0;//设备价值合计
        Double powerStandInFeeAll=0.0;//摊入费用
        Double powerDeliveredAssetsAll=0.0;//交付使用资产价值
        Double powerInstallAll = 0.0;//安装费合计
        for (TransferOfEquipment transferOfEquipment:transferOfEquipments){
            //如果配变数量合计
            if (null!=transferOfEquipment.getPowerNumber()){
                eqmNumbersAll+=Double.valueOf(transferOfEquipment.getPowerNumber());
            }
            //安装费合计
            if (null!=transferOfEquipment.getPowerInstallFee()){//如果安装费用不为空
                powerInstallAll+=Double.valueOf(transferOfEquipment.getPowerInstallFee());
            }
            //设备价值合计
            if (null!=transferOfEquipment.getPowerValue()){
                powerValueAll+=Double.valueOf(transferOfEquipment.getPowerValue());
            }
            //摊入费用合计
            if (null!=transferOfEquipment.getPowerStandInFee()){
                powerStandInFeeAll+=Double.valueOf(transferOfEquipment.getPowerStandInFee());
            }
            //交付使用价值合计
            if (null!=transferOfEquipment.getPowerDeliveredAssets()){
                powerDeliveredAssetsAll+=Double.valueOf(transferOfEquipment.getPowerDeliveredAssets());
            }
        }
        TransferOfEquipment transferOfEquipment1 = new TransferOfEquipment();
        transferOfEquipment1.setPowerNumber(decimalFormat.format(eqmNumbersAll));//配电数量合计
        transferOfEquipment1.setPowerInstallFee(decimalFormat.format(powerInstallAll));//安装费用合计
        transferOfEquipment1.setPowerValue(decimalFormat.format(powerValueAll));//设备价值合计
        transferOfEquipment1.setPowerDeliveredAssets(decimalFormat.format(powerDeliveredAssetsAll));//交付使用价值合计
        transferOfEquipment1.setPowerStandInFee(decimalFormat.format(powerStandInFeeAll));//摊入费用合计
        transferOfEquipments.add(transferOfEquipment1);
        return transferOfEquipments;
    }
    //概况表
    public static GeneralSituation getFeneraSituation(List<ErpCredit> erpCredits, List<MaintainData> maintainDataList){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        GeneralSituation generalSituation = new GeneralSituation();
        Double lineLength=0.0;//线路长度
        Double tenKvLength=0.0;//其中：10kv线路
        Double fourKvLength=0.0;//400v及以下
        List<String> organDataList = new ArrayList<String>();//预算批准机关、日期、文号
        TreeSet<Date>  commencementDateSet =new TreeSet<Date>();//开工日期
        TreeSet<Date> completionDateSet = new TreeSet<Date>();//竣工时间
        Date beforeDate=null;
        Date endDate = null;
        String organData = "";//文号
        Double investmentBudget=0.0;//预算投资
        Double actualInvestment=0.0;//实际投资
        if (null!=erpCredits&&erpCredits.size()>0){
            for (ErpCredit erpCredit:erpCredits){
                //如果线路长度不为空
                if (null!=erpCredit.getTenKvLineLength()){
                    lineLength+=erpCredit.getTenKvLineLength();//得到线路长度（线路）
                }
                //其中：10kv线路
                if (null!=erpCredit.getTenKvItsLine()){
                    tenKvLength+=erpCredit.getTenKvItsLine();
                }
                //400kv
                if (null!=erpCredit.getTenKvItesFhvFollow()){
                    fourKvLength+=erpCredit.getTenKvItesFhvFollow();
                }
                //实际投资
                if (null!=erpCredit.getActualInvestment()){
                    actualInvestment+=erpCredit.getActualInvestment();
                }
            }
            generalSituation.setLineLength(decimalFormat.format(lineLength));
            generalSituation.setTenKvLength(decimalFormat.format(tenKvLength));
            generalSituation.setFourKvLength(decimalFormat.format(fourKvLength));
            generalSituation.setActualInvestment(decimalFormat.format(actualInvestment));
            generalSituation.setTransferAssets(decimalFormat.format(actualInvestment));
            generalSituation.setHasItsOwn(decimalFormat.format(actualInvestment));
        }
        if (null!=maintainDataList&&maintainDataList.size()>0){
            for (MaintainData maintainData:maintainDataList){
                if (null!=maintainData.getApprovalNumber()){//如果审批文号不为空
                    organDataList.add(maintainData.getApprovalNumber());
                }
                if (null!=maintainData.getStartDate()&&maintainData.getStartDate().toString().length()>0){
                    commencementDateSet.add(maintainData.getStartDate());
                }
                if (null!=maintainData.getEndDate()&&maintainData.getEndDate().toString().length()>0){
                    completionDateSet.add(maintainData.getEndDate());
                }
                investmentBudget+=maintainData.getDesignFee()+maintainData.getSupervisionFee()+maintainData.getPreliminaryWorkFee()
                        +maintainData.getDamages()+maintainData.getManagementFee()+maintainData.getBuildingFee()+maintainData.getInstallFee()
                        +maintainData.getEquipmentFee()+maintainData.getMaterialFee();
            }
            if (null!=organDataList&&organDataList.size()>0){
                Set set1 = new HashSet();
                List<String> newList1 = new ArrayList();
                for (String string:organDataList) {
                    if(set1.add(string)) {
                        newList1.add(string);
                    }
                }
                for (String str:newList1){
                    organData+=str+"、";
                }
                String substring = organData.substring(0, organData.length() - 1);
                generalSituation.setOrganDataNumber(substring);
            }
            if (null!=commencementDateSet&&commencementDateSet.size()>0){
                generalSituation.setCommencementDate(sdf.format(commencementDateSet.first()));
            }
            if (null!=completionDateSet&&completionDateSet.size()>0){
                generalSituation.setCompletionDate(sdf.format(completionDateSet.last()));
            }
            generalSituation.setInvestmentBudget(decimalFormat.format(investmentBudget));
        }
        return generalSituation;
    }
    //资产总表
    public static List<TheAssetsTotal> getTheAssetsTotal(List<ErpCredit> erpCredits,List<MaintainData> maintainDataList){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        List<TheAssetsTotal> theAssetsTotals = new ArrayList<TheAssetsTotal>();
        TheAssetsTotal theAssetsTotal1 = new TheAssetsTotal();
        TheAssetsTotal theAssetsTotal2 = new TheAssetsTotal();
        TheAssetsTotal theAssetsTotal3 = new TheAssetsTotal();
        Double buildProject=0.0;//建筑工程
        Double intallProject=0.0;//安装工程费
        Double intallProject2 = 0.0;//安装工程费2
        Double equipmentValue=0.0;//设备价值
        Double costSharing=0.0;//分摊费用
        Double deliveryCostValue=0.0;//交付使用资产价值
        Double equipmentValue1=0.0;//设备价值
        Double costSharing1=0.0;//分摊费用
        Double deliveryCostValue1=0.0;//交付使用资产价值
        List<TransferOfPowerLine> transferOfPower1s = getTransFerPower1(erpCredits);//得到线路表
        List<TransferOfEquipment> transferOfPower2s = getTransferOfPower2(erpCredits,maintainDataList);//得到设备表
        for (int i = 0; i < transferOfPower1s.size(); i++) {
            if (i==transferOfPower1s.size()-1){
                if (null!= transferOfPower1s.get(i).getPowerBuildFee()){//建筑
                    buildProject=Double.valueOf(transferOfPower1s.get(i).getPowerBuildFee());
                }
                if (null!=transferOfPower1s.get(i).getPowerInstallFee()&&!transferOfPower1s.get(i).getPowerInstallFee().equals("-")){
                    intallProject=Double.valueOf(transferOfPower1s.get(i).getPowerInstallFee());
                }
                if (null!=transferOfPower1s.get(i).getPowerStandInFee()&&!transferOfPower1s.get(i).getPowerStandInFee().equals("-")){//分摊费用
                    costSharing=Double.valueOf(transferOfPower1s.get(i).getPowerStandInFee());
                }
                if (null!=transferOfPower1s.get(i).getPowerDeliveredAssets()){
                    deliveryCostValue=Double.valueOf(transferOfPower1s.get(i).getPowerDeliveredAssets());
                }
            }
        }
        theAssetsTotal1.setBuildProject(decimalFormat.format(buildProject));//添加建筑费
        theAssetsTotal1.setIntallProject(decimalFormat.format(intallProject));//添加设备费
        theAssetsTotal1.setCostSharing(decimalFormat.format(costSharing));
        theAssetsTotal1.setDeliveryCostValue(decimalFormat.format(deliveryCostValue));
        for (int i = 0; i < transferOfPower2s.size(); i++) {
            if (i==transferOfPower2s.size()-1){
                if (null!=transferOfPower2s.get(i).getPowerInstallFee()){
                    intallProject2 = Double.valueOf(transferOfPower2s.get(i).getPowerInstallFee());
                }
                if (null!=transferOfPower2s.get(i).getPowerValue()){//设备价值
                    equipmentValue1=Double.valueOf(transferOfPower2s.get(i).getPowerValue());
                }
                if (null!=transferOfPower2s.get(i).getPowerStandInFee()){
                    costSharing1=Double.valueOf(transferOfPower2s.get(i).getPowerStandInFee());
                }
                if (null!=transferOfPower2s.get(i).getPowerDeliveredAssets()){
                    deliveryCostValue1 = Double.valueOf(transferOfPower2s.get(i).getPowerDeliveredAssets());
                }
            }
        }
        theAssetsTotal2.setIntallProject(decimalFormat.format(intallProject2));
        theAssetsTotal2.setEquipmentValue(decimalFormat.format(equipmentValue1));
        theAssetsTotal2.setCostSharing(decimalFormat.format(costSharing1));
        theAssetsTotal2.setDeliveryCostValue(decimalFormat.format(deliveryCostValue1));
        //添加三
        theAssetsTotal3.setBuildProject(decimalFormat.format(buildProject));
        theAssetsTotal3.setIntallProject(decimalFormat.format(intallProject+intallProject2));
        theAssetsTotal3.setEquipmentValue(decimalFormat.format(equipmentValue1));
        theAssetsTotal3.setCostSharing(decimalFormat.format(costSharing+costSharing1));
        theAssetsTotal3.setDeliveryCostValue(decimalFormat.format(deliveryCostValue+deliveryCostValue1));
        theAssetsTotals.add(theAssetsTotal3);
        theAssetsTotals.add(theAssetsTotal1);
        theAssetsTotals.add(theAssetsTotal2);
        return theAssetsTotals;
    }
    //竣 工 工 程 财 务 决 算 表
    public static MCompletionProject getMCompletionProject(List<ErpCredit> erpCredits, List<MaintainData> maintainDataList){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        MCompletionProject mCompletionProject = new MCompletionProject();
        Double appropriatedFunds=0.0;//上级拨入资金
        Double vatDeduction=0.0;//、抵扣增值税
        Double fixedAssets=0.0;//交付使用固定资产
        Double transferAssets=0.0;//移交资产合计
        Double completed=0.0;//含税投资完成
        for (ErpCredit erpCredit:erpCredits){
            if (null!=erpCredit.getActualInvestment()){
                fixedAssets+=erpCredit.getActualInvestment();
                transferAssets+=erpCredit.getActualInvestment();
            }
            if (null!=erpCredit.getTheInputTax()){
                vatDeduction+=erpCredit.getTheInputTax();
            }
        }
        completed=transferAssets+vatDeduction;
        for (MaintainData maintainData:maintainDataList){
            appropriatedFunds+=maintainData.getDesignFee()+maintainData.getSupervisionFee()+maintainData.getPreliminaryWorkFee()
                    +maintainData.getDamages()+maintainData.getManagementFee()+maintainData.getBuildingFee()+maintainData.getInstallFee()
                    +maintainData.getEquipmentFee()+maintainData.getMaterialFee();
        }
        mCompletionProject.setTransferAssets(decimalFormat.format(transferAssets));
        mCompletionProject.setAppropriatedFunds(decimalFormat.format(appropriatedFunds));
        mCompletionProject.setFixedAssets(decimalFormat.format(fixedAssets));
        mCompletionProject.setVatDeduction(decimalFormat.format(vatDeduction));
        mCompletionProject.setCompleted(decimalFormat.format(completed));
        return mCompletionProject;
    }

    //设备分摊（差额）
    public static List<TransferOfEquipment> getTransFerEquipmentStandin(List<ErpCredit> erpCreditList, List<MaintainData> maintainDataList){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        Double installDiff = 0.0;//安装差额
        Double standInDiff = 0.0;//分摊差额
        String itemId = "";
        Double installALL=0.0;//一览表安装费用
        Double otherAll=0.0;//一览表其他费用
        Double installLine=0.0;//线路安装费用
        Double installEqu = 0.0;//设备安装费用
        Double standFeeLine = 0.0;//线路摊入费用
        Double standFeeEqu = 0.0;//设备摊入费用

        Double totalInstallLineEqu = 0.0;//线路设备的合计金额
        Double totalStandLineEqu = 0.0;//线路设备的合计金额
        List<Schedule> scheduleList = getScheduleList(erpCreditList, maintainDataList);//得到一览表
        List<TransferOfPowerLine> transFerPowerLines = getTransFerPower1(erpCreditList);//得到线路表
        List<TransferOfEquipment> transferOfPowerEquipments = getTransferOfPowerEquList(erpCreditList);//得到设备表
        for (Schedule schedule:scheduleList){
            if (null!=schedule.getItemId()){
                if (!schedule.getItemId().equals("")){
                    installDiff = 0.0;//安装差额
                    standInDiff = 0.0;//分摊差额
                    if (null!=schedule.getActualIntallProject()){
                        installALL=Double.valueOf(schedule.getActualIntallProject());//安装费用
                    }
                    String actualOtherFee = schedule.getActualOtherFee();
                    if (null == actualOtherFee) {
                        actualOtherFee = "0";
                    }
                    otherAll = Double.valueOf(actualOtherFee);
                    installLine=0.0;//线路表安装费用
                    standFeeLine=0.0;//线路表分摊费用
                    installEqu = 0.0;//设备安装费用
                    standFeeEqu = 0.0;//设备摊入费用
                    totalInstallLineEqu = 0.0;//线路设备的合计金额
                    totalStandLineEqu = 0.0;//线路设备的合计金额
                    //得到项目id
                    itemId = schedule.getItemId();
                    //循环线路表
                    for (int i = 0; i < transFerPowerLines.size(); i++) {
                        if (transFerPowerLines.get(i).getPowerId().equals(itemId)){
                            if (null!=transFerPowerLines.get(i).getPowerInstallFee()){
                                if (!transFerPowerLines.get(i).getPowerInstallFee().equals("-")){
                                    installLine = Double.valueOf(transFerPowerLines.get(i).getPowerInstallFee());//一览表安装费用
                                }
                            }
                            if (null!=transFerPowerLines.get(i).getPowerStandInFee()){
                                standFeeLine = Double.valueOf(transFerPowerLines.get(i).getPowerStandInFee());//一览表分摊费用
                            }
                            break;
                        }
                    }
                    //循环设备表
                    for (int i = 0; i < transferOfPowerEquipments.size(); i++) {
                        if (transferOfPowerEquipments.get(i).getPowerId().equals(itemId)){
                            if (Double.valueOf(transferOfPowerEquipments.get(i).getPowerInstallFee())>0){
                                installEqu += Double.valueOf(transferOfPowerEquipments.get(i).getPowerInstallFee());//设备安装费用
                            }
                            if (null!=transferOfPowerEquipments.get(i).getPowerStandInFee()){
                                standFeeEqu += Double.valueOf(transferOfPowerEquipments.get(i).getPowerStandInFee());//设备分摊费用
                            }
                        }
                    }
                    //安装差额计算
                    totalInstallLineEqu = installLine+installEqu;
                    if (totalInstallLineEqu!=installALL){
                        if (totalInstallLineEqu>=installALL-0.1&&totalInstallLineEqu<=installALL+0.1){
                            if (totalInstallLineEqu>installALL){
                                installDiff = totalInstallLineEqu - installALL;
                                getEquDiffenert(transferOfPowerEquipments,itemId,installDiff,true,true);
                            }else {
                                installDiff = installALL -totalInstallLineEqu;
                                getEquDiffenert(transferOfPowerEquipments,itemId,installDiff,true,false);
                            }

                        }
                    }
                    //分摊差额计算
                    totalStandLineEqu = standFeeLine+standFeeEqu;
                    if (totalStandLineEqu!=otherAll){
                        if (totalStandLineEqu>=otherAll-0.1&&totalStandLineEqu<=otherAll+0.1){
                            if (totalStandLineEqu>otherAll){
                                standInDiff = totalStandLineEqu-otherAll;
                                getEquDiffenert(transferOfPowerEquipments,itemId,standInDiff,false,true);
                            }else {
                                standInDiff = otherAll-totalStandLineEqu;
                                getEquDiffenert(transferOfPowerEquipments,itemId,standInDiff,false,false);
                            }

                        }
                    }
                }
            }
        }
        return transferOfPowerEquipments;
    }
    //分摊差额进入设备费
    public static void getEquDiffenert(List<TransferOfEquipment> transferOfPowerEquipments,String itemId,Double diff,Boolean flag,Boolean flag1){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        boolean judge = false;
        Double install = 0.0;
        Double standin = 0.0;
        for (TransferOfEquipment transferOfEquipment:transferOfPowerEquipments){
            install = 0.0;
            standin = 0.0;
            judge = false;
            if (transferOfEquipment.getPowerId().equals(itemId)){
               if (flag){//如果flag为true  安装费分摊
                   if (!transferOfEquipment.getPowerInstallFee().equals("0")){
                       if (flag1){
                           install = Double.valueOf(transferOfEquipment.getPowerInstallFee())-diff;
                       }else {
                           install = Double.valueOf(transferOfEquipment.getPowerInstallFee())+diff;
                       }
                       transferOfEquipment.setPowerInstallFee(decimalFormat.format(install));
                       judge = true;
                   }
               }else {
                   if (!transferOfEquipment.getPowerStandInFee().equals("0")){
                       if (flag1){
                           standin = Double.valueOf(transferOfEquipment.getPowerStandInFee())-diff;
                       }else {
                           standin = Double.valueOf(transferOfEquipment.getPowerStandInFee())+diff;
                       }
                       transferOfEquipment.setPowerStandInFee(decimalFormat.format(standin));
                       judge = true;
                   }
               }
            }
            if (judge){
                break;
            }
        }
    }

    /**
     * 其 他 费 用 明 细 表
     * @param erpCreditList
     * @param maintainDataList
     * @return
     */
    public static List<OtherExpenses> getOtherExpenses(List<ErpCredit> erpCreditList, List<MaintainData> maintainDataList){
        DecimalFormat decimalFormat   = new DecimalFormat("#0.00");
        String itemNameAll="合计";//工程名称
        Double managementFunds=0.0;//项目管理经费
        Double supervisionFee=0.0;//监理费
        Double designFee=0.0;//设计费
        Double loanInterest=0.0;//贷款利息
        Double damages=0.0;//赔偿费
        Double total=0.0;//合计
        Double deduction=0.0;//抵扣增值税
        Double includingTax=0.0;//实际含税投资合计
        OtherExpenses otherExpenses = new OtherExpenses();
        List<OtherExpenses> otherExpensesList = new ArrayList<OtherExpenses>();
        OtherExpenses expenses  = null;
        if (null!=erpCreditList&&erpCreditList.size()>0){
            for (ErpCredit erpCredit:erpCreditList){
                expenses = new OtherExpenses();
                expenses = getExpenses(expenses,erpCredit,maintainDataList);//得到一览表实际金额
                otherExpensesList.add(expenses);
            }
        }
        for (OtherExpenses s:otherExpensesList){
            if (null!=s.getManagementFunds()){
                managementFunds+=Double.parseDouble(s.getManagementFunds());
                otherExpenses.setManagementFunds(decimalFormat.format(managementFunds));
            }
            if (null!=s.getSupervisionFee()){
                supervisionFee+=Double.parseDouble(s.getSupervisionFee());
                otherExpenses.setSupervisionFee(decimalFormat.format(supervisionFee));
            }
            if (null!=s.getDesignFee()){
                designFee+=Double.parseDouble(s.getDesignFee());
                otherExpenses.setDesignFee(decimalFormat.format(designFee));
            }
            if (null!=s.getLoanInterest()){
                loanInterest+=Double.parseDouble(s.getLoanInterest());
                otherExpenses.setLoanInterest(decimalFormat.format(loanInterest));
            }
            if (null!=s.getDamages()){
                damages+=Double.parseDouble(s.getDamages());
                otherExpenses.setDamages(decimalFormat.format(damages));
            }
            if (null!=s.getTotal()){
                total+=Double.parseDouble(s.getTotal());
                otherExpenses.setTotal(decimalFormat.format(total));
            }
            if (null!=s.getDeduction()){
                deduction+=Double.parseDouble(s.getDeduction());
                otherExpenses.setDeduction(decimalFormat.format(deduction));
            }
            if (null!=s.getIncludingTax()){
                includingTax+=Double.parseDouble(s.getIncludingTax());
                otherExpenses.setIncludingTax(decimalFormat.format(includingTax));
            }
        }
        otherExpenses.setProjectName(itemNameAll);
        otherExpensesList.add(otherExpenses);
        return otherExpensesList;
    }


    /*
        设备费统计
     */
    public static List<TransferOfEquipment> getTransferOfPower2(List<ErpCredit> erpCreditList, List<MaintainData> maintainDataList){
        List<TransferOfEquipment> transFerEquipmentStandin = getTransFerEquipmentStandin(erpCreditList, maintainDataList);
        List<TransferOfEquipment> powerDeliveredAssets2 = getPowerDeliveredAssets2(transFerEquipmentStandin);
        List<TransferOfEquipment> transferOfPowerTotal = getTransferOfPowerTotal(powerDeliveredAssets2);
        return transferOfPowerTotal;
    }

    //材料费合计
    public  static Double getErpCreditMaterial(ErpCredit erpCredit){
        Double materialTotal = 0.0;
        List<ErpCreditMaterial> erpCreditMaterials = erpCredit.getErpCreditChild2s();//材料费
        if (null!=erpCreditMaterials&& erpCreditMaterials.size()>0){//得到材料费合计
            for (ErpCreditMaterial erpCreditMaterial:erpCreditMaterials){
                materialTotal+=erpCreditMaterial.getMtlBookedFee2();
            }
        }
        return materialTotal;
    }

    /*
        所有设备费合计
     */
    public static Double getEqmValueTotal(List<ErpCreditEquipment> erpCreditEquipments){
        Double eqmValue = 0.0;
        if (null!=erpCreditEquipments&&erpCreditEquipments.size()>=0){
            eqmValue = 0.0;//赋予初始值
            for (ErpCreditEquipment erpCreditEquipment:erpCreditEquipments){//所有设备费合计
                eqmValue+=erpCreditEquipment.getEqmBookedFee1();
            }
        }
        return eqmValue;
    }
    /*
        特殊清理处理:得到只且中只有一条的配变数量为0的设备费合计
     */
    public static Double getEqmOneFlagValue(List<ErpCreditEquipment> erpCreditEquipments){
        Double d = 0.0;//定义初始值
        boolean flag = false;
        for (ErpCreditEquipment erpCreditEquipment:erpCreditEquipments){
            if (erpCreditEquipment.getEqmNumbers()==0.00){//如果台数为0
                d+=erpCreditEquipment.getEqmBookedFee1();//设备金额累加
            }
        }
        for (ErpCreditEquipment erpCreditEquipment:erpCreditEquipments){
            if (erpCreditEquipment.getEqmNumbers()!=0.00){//如果台数不为0
                flag = true;//设置为true
            }
        }
        if (flag){
            d = 0.0;
        }
        return d;
    }
    //得到大于一条的设备费合计
    public static List<ErpCreditEquipment> getEquNumbers(List<ErpCreditEquipment> equipments) {
//        List<ErpCreditEquipment> erpCreditEquipmentList = new ArrayList<ErpCreditEquipment>();
        List<ErpCreditEquipment> erpCreditEquipmentList = new ArrayList<ErpCreditEquipment>();
        try {
            erpCreditEquipmentList = deepCopy(equipments);
            Double eqmBooksFee = 0.0;
            for (ErpCreditEquipment erpCreditEquipment:erpCreditEquipmentList){
                if (erpCreditEquipment.getEqmNumbers()==0.00){
                    eqmBooksFee += erpCreditEquipment.getEqmBookedFee1();//入账金额
                }
            }
            for (ErpCreditEquipment erpCreditEquipment1:erpCreditEquipmentList){
                if (erpCreditEquipment1.getEqmNumbers()!=0.00){
                    eqmBooksFee=eqmBooksFee+erpCreditEquipment1.getEqmBookedFee1();
                    erpCreditEquipment1.setEqmBookedFee1(eqmBooksFee);
                    break;
                }
            }
            for (int i = 0; i < erpCreditEquipmentList.size(); i++) {
                if (erpCreditEquipmentList.get(i).getEqmNumbers()==0.00){
                    erpCreditEquipmentList.remove(i);
                    i--;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return erpCreditEquipmentList;
    }
    public static List deepCopy(List src) throws IOException, ClassNotFoundException{
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);
        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in =new ObjectInputStream(byteIn);
        List dest = (List)in.readObject();
        return dest;
    }
}
