package com.jeeplus.modules.sg.overheadline.util;

import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.common.utils.sg.ExcelUtil;
import com.jeeplus.modules.sg.overheadline.entity.JkxlAdjustFee;
import com.jeeplus.modules.sg.overheadline.entity.Settlement;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.ArrayList;
import java.util.List;
public class SettementUtil {
    private static final NumberFormat nf= NumberFormat.getInstance();
    public static Boolean getFlag = false;
    public static final String RESON_TRUE="【范围内】&nbsp;金额<span style='color:#0000FF'>&nbsp;一致。</span>";
    public static final String RESON_FLASE="【范围内】金额<span style='color:#FF0000'>&nbsp;不一致。</span>";
    public static final String BDZ_TBRNAME="投标人采购设备费";
    public static final String BDZ_ZBRNAME="招标人采购材料费";
    public static final String BDZ_TBNAME="投标报价";
    public static final String SE= "税额";
    public static final String ST = "税金调差";
    public static final String SE1= "税额调差";
    public static final String RESON_KYJ="扣";
    public static final String RESON_KYJ1="减";
    public static final String RESON_ZZJS="最终结算价";
    public static final String RESON_ZZFC="最终工程造价";
    public static final String RESON_ZGTB="最高投标限价";
    public static final String RESON_FBFXGCF1="建筑工程费";
    public static final String RESON_FBFXGCF2="安装工程费";
    public static final String RESON_XF="下浮";
    public static final String RESON_XFHZJ="下浮后造价";

    /*
        分布分项比对
     */
    public static final String RESON_FBFX1="《工程项目竣工结算汇总表》,分部分项工程费,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_FBFX2="《架空线路工程费用汇总表》,分部分项汇总金额,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_FBFX3="《分部分项工程量清单计价表》,分部分项汇总金额,<span style='font-weight: bold;'>xxx</span>,";
    //电缆
    public static final String RESON_JZFBFX="《电缆线路建筑工程费用汇总表》,合计金额,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_AZFBFX="《电缆线路安装工程费用汇总表》,合计金额,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_JZFBFX3="《建筑分部分项工程量清单计价表》,分部分项汇总金额,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_AZFBFX3="《安装分部分项工程量清单计价表》,分部分项汇总金额,<span style='font-weight: bold;'>xxx</span>,";
    //变电站
    public static final String BDZ_FXFX="《工程项目投标报价汇总表》,分部分项工程费,<span style='font-weight: bold;'>xxx</span>,";
    public static final String BDZ_FBFX1="《变电站建筑工程费用汇总表》,分部分项汇总金额,<span style='font-weight: bold;'>xxx</span>,";
    public static final String BDZ_FBFX2="《变电站安装工程费用汇总表》,分部分项汇总金额,<span style='font-weight: bold;'>xxx</span>,";
    /*
        承包人采购比对
     */
    public static final String RESON_CBRF="《工程项目竣工结算汇总表》,承包人采购设备费,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_CBRF1="《承包人采购设备计价表》,合计金额,<span style='font-weight: bold;'>xxx</span>,";
    //变电站
    public static final String BDZ_TBRCG="《工程项目投标报价汇总表》,投标人采购设备费,<span style='font-weight: bold;'>xxx</span>,";
    public static final String BDZ_TBRCG1="《投标人采购设备计价表》,合计金额,<span style='font-weight: bold;'>xxx</span>,";
    /*
        措施费比对
     */
    public static final String RESON_CSXM="《工程项目竣工结算汇总表》,措施项目费,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_CSXM1="《措施项目清单计价表(一)》,合计金额,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_CSXM2="《措施项目清单计价表(二)》,合计金额,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_CSXM3="&nbsp,小计,<span style='font-weight: bold;'>xxx</span>,";
   //变电站
    public static final String BDZ_CSF="《工程项目投标报价汇总表》,措施项目费,<span style='font-weight: bold;'>xxx</span>,";
    /*
        其他项目费比对
     */
    public static final String RESON_QTXM="《工程项目竣工结算汇总表》,其他项目费,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_QTXM1="《其他项目清单计价表》,合计金额,<span style='font-weight: bold;'>xxx</span>,";
    //变电站
    public static final String BDZ_QTXM="《工程项目投标报价汇总表》,其他项目费,<span style='font-weight: bold;'>xxx</span>,";
    public static final String BDZ_QTXM1="《其他项目清单计价表》,合计金额,<span style='font-weight: bold;'>xxx</span>,";
    /*
        规费比对
     */
    public static final String RESON_GF="《工程项目竣工结算汇总表》,规费,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_GF1="《规费项目清单计价表》,合计金额,<span style='font-weight: bold;'>xxx</span>,";
    //变电站
    public static final String BDZ_GF="《工程项目投标报价汇总表》,规费,<span style='font-weight: bold;'>xxx</span>,";
    /*
        税率 不验证
     */
    public static final String RESON_SJ="《工程项目竣工结算汇总表》,税金 (税率: 9%)";
    public static final String BDZ_SJ="《工程项目投标报价汇总表》,税金 (税率: 9%)";
    /*
        发包人采购费
     */
    public static final String RESON_FBRF="《工程项目竣工结算汇总表》,发包人采购材料费,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_FBRF1="《分布分项工程量清单计价表》,发包人采购汇总金额,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_FBRF2="《分布分项工程量清单计价表》,招标人采购汇总金额,<span style='font-weight: bold;'>xxx</span>,";
    //电缆
    public static final String RESON_DLFBRF1="《建筑分布分项工程量清单计价表》,发包人采购汇总金额,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_DLFBRF2="《安装分布分项工程量清单计价表》,发包人采购汇总金额,<span style='font-weight: bold;'>xxx</span>,";
    //变电
    public static final String BDZ_FBRF="《工程项目投标报价汇总表》,招标人采购材料费,<span style='font-weight: bold;'>xxx</span>,";
    public static final String BDZ_FBRF1="《建筑分布分项工程量清单计价表》,招标人采购金额,<span style='font-weight: bold;'>xxx</span>,";
    public static final String BDZ_FBRF2="《安装分布分项工程量清单计价表》,招标人采购金额,<span style='font-weight: bold;'>xxx</span>,";
    //变电站
    /*
        竣工结算
     */
    public static final String RESON_JGJS="《工程项目竣工结算汇总表》,竣工结算价合计金额,<span style='font-weight: bold;'>xxx</span>,";
    public static final String RESON_JGJS8=",小计,<span style='font-weight: bold;'>xxx</span>,";
    //变电站
    public static final String BDZ_TBBJ="《工程项目投标报价汇总表》,投标报价,<span style='font-weight: bold;'>xxx</span>,";

    /*
       工程结算
    */
    public static final String RESON_GCJS="工程项目汇总表,结算价,<span style='font-weight: bold;'>xxx</span>,";
    public static final String[] DLBDZ_FBFX2 = {RESON_FBFX1,BDZ_FBFX1,BDZ_FBFX2,RESON_JGJS8,RESON_JZFBFX3,RESON_AZFBFX3,RESON_JGJS8};
    public static final String[] DLBDZ_FBFX1 = {RESON_FBFX1,RESON_JZFBFX,RESON_AZFBFX,RESON_JGJS8,RESON_JZFBFX3,RESON_AZFBFX3,RESON_JGJS8};
    /*
        设置转换参数
     */
    static {
        nf.setGroupingUsed(false);
    }
    /**
     * 《工程竣工表》、《工程项目投标表》获取所有数据
     * @param importExcel 表格对象
     * @return 《工程竣工表》/《工程项目投资表》 list集合
     */
    public static List<Settlement> getComparison(ImportExcel importExcel){
        List<JkxlAdjustFee> jkxlAdjustFees = BashInfo.getWsbFee(importExcel);
        boolean flag = false;//判断 是否是变电站
        boolean flagTotal= true;//判断是否有分部分项
        double countTotal=0.0;//安装/建筑合计金额
        Settlement settlement;
        List<Settlement> settlementList = new ArrayList<Settlement>();
        int lastDataRowNum =  importExcel.getLastDataRowNum();
        String key = "";//获取结算名称
        Integer count = 0; //计数器
        for (int i = 0; i < lastDataRowNum; i++) {
            settlement=new Settlement();
            key =  importExcel.getValue(i+1,1);
            if (key.contains(BashInfo.JS_SJ)){
                if (key.contains(ST)){
                    key=ST;
                }else {
                    key=BashInfo.JS_SJ;
                }
            }
            if(key.contains(SE)){
                if (key.contains(SE1)){
                    key=SE1;
                }else {
                    key=BashInfo.JS_SJ;
                }
            }
            if(key.equals(BDZ_TBRNAME)){
                flag=true;
                key=BashInfo.JS_CBRF;
            }
            if (key.equals(BDZ_ZBRNAME)){
                flag=true;
                key=BashInfo.JS_FBRF;
            }
            if (key.equals(BDZ_TBNAME)){
                flag=true;
                key=BashInfo.JS_JGJS;
            }
            switch (key){
                case RESON_FBFXGCF1:
                    countTotal+=importExcel.getDouble(i+1,2);
                    break;
                case RESON_FBFXGCF2:
                    countTotal+=importExcel.getDouble(i+1,2);
                    break;
                case BashInfo.JS_FBFX:
                    flagTotal=false;
                    settlementList.add(getSettlement(settlement,key,importExcel,i));
                    break;
                case BashInfo.JS_CBRF:
                    if (flag){
                        key=BDZ_TBRNAME;
                        flag=false;
                    }
                    settlementList.add(getSettlement(settlement,key,importExcel,i));
                    break;
                case BashInfo.JS_CSXM:
                    settlementList.add(getSettlement(settlement,key,importExcel,i));
                    break;
                case BashInfo.JS_QTXM:
                    settlementList.add(getSettlement(settlement,key,importExcel,i));
                    break;
                case BashInfo.JS_GF:
                    settlementList.add(getSettlement(settlement,key,importExcel,i));
                    break;
                case BashInfo.JS_SJ:
                    count+=1;//计数器
                    if (count>1){
                        for (Settlement st:settlementList){
                            if (st.getEntryName().equals(BashInfo.JS_SJ)){
                                double s = importExcel.getDouble(i+1,2);
                                double total = st.getMoney()+s;
                                st.setMoney(total);
                            }
                        }
                    }
                    if (count==1){
                        settlementList.add(getSettlement(settlement,key,importExcel,i));
                        for (JkxlAdjustFee jkxlAdjustFee:jkxlAdjustFees){
                            settlement=new Settlement();
                            settlement.setEntryName(jkxlAdjustFee.getType());
                            settlement.setMoney(jkxlAdjustFee.getFee());
                            settlement.setRemarks("");
                            settlementList.add(settlement);
                        }
                    }
                    break;
                case BashInfo.JS_FBRF:
                    if (flag){
                        key=BDZ_ZBRNAME;
                        flag=false;
                    }
                    settlementList.add(getSettlement(settlement,key,importExcel,i));
                    break;
                case ST:
                    settlementList.add(getSettlement(settlement,ST,importExcel,i));
                    break;
                case SE1:
                    settlementList.add(getSettlement(settlement,ST,importExcel,i));
                    break;
                case BashInfo.JS_JGJS:
                    if (flag){
                        key=BDZ_TBNAME;
                        flag=false;
                    }
                    settlementList.add(getSettlement(settlement,key,importExcel,i));
                    break;
                case BashInfo.JS_GCJS:
                    settlementList.add(getSettlement(settlement,key,importExcel,i));
                    break;
                case RESON_ZZFC:
                    settlementList.add(getSettlement(settlement,key,importExcel,i));
                    break;
                case RESON_ZZJS:
                    settlementList.add(getSettlement(settlement,key,importExcel,i));
                    break;
                case RESON_ZGTB:
                    settlementList.add(getSettlement(settlement,key,importExcel,i));
                    break;
                case RESON_XFHZJ:
                    settlementList.add(getSettlement(settlement,key,importExcel,i));
                    break;
            }
        }
        if (flagTotal){
            settlement=new Settlement();
            settlement.setEntryName(BashInfo.JS_FBFX);
            settlement.setMoney(countTotal);
            settlementList.add(0,settlement);
        }
        return settlementList;
    }

    /**
     * 架空项目  校验
     * @param settlements 工程竣工结算list集合
     * @param builtTotal 获取架空线路工程费用汇总表 分部分项 合计
     * @param divisiontotal 获取分部分项比对数据清单
     * @param wordtotal1 //获取措施费清单计价表1
     * @param wordtotal2 //获取措施费清单计价表2
     * @param otherTotal 获取其他项目费合计
     * @param feescount 获取规费合计
     * @param cbperson 获取承包人采购合计
     * @return 导入架空项目校验后的集合。
     */
    public static List<Settlement> getComparison(List<Settlement> settlements, double builtTotal, double[] divisiontotal, double wordtotal1,double wordtotal2, double otherTotal, double feescount,double cbperson){
        double wordtotal=wordtotal1+wordtotal2;//措施费相加
        String name="";//获取当前汇总表名称
        double money=0.0;//获取当前汇总表金额
        Settlement settlement;//对象
        double moneyComparison1;//范围金额+5.0元
        double moneyComparison2;//范围金额-5.0元
        for (int i = 0; i < settlements.size(); i++) {
            settlement=settlements.get(i);
            name=settlement.getEntryName();
            //读取字段区分
            if(name.equals(BDZ_TBRNAME)){
                name=BashInfo.JS_CBRF;
            }
            if (name.equals(BDZ_ZBRNAME)){
                name=BashInfo.JS_FBRF;
            }
            if (name.equals(BDZ_TBNAME)){
                name=BashInfo.JS_JGJS;
            }
            if (name.contains(RESON_KYJ)){
                name = RESON_KYJ;
            }
            if (name.contains(RESON_KYJ1)){
                name = RESON_KYJ1;
            }
            if (name.equals(RESON_ZZJS)){
                name = BashInfo.JS_GCJS;
            }
            if (name.equals(RESON_ZZFC)){
                name = BashInfo.JS_GCJS;
            }
            if (name.equals(RESON_ZGTB)){
                name = BashInfo.JS_GCJS;
            }
            if (name.equals(RESON_XFHZJ)){
                name = BashInfo.JS_GCJS;
            }
            money=settlement.getMoney();
            moneyComparison1 = money+5.0;
            moneyComparison2 = money-5.0;
            settlement.setStatus("1");
            switch (name){
                case BashInfo.JS_FBFX:
                    if(builtTotal<=moneyComparison1&&builtTotal>=moneyComparison2&&divisiontotal[0]<=moneyComparison1&&divisiontotal[0]>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    settlement.setTheAmount(String.valueOf(nf.format(builtTotal)+","+nf.format(divisiontotal[0])));
                    break;
                case  BashInfo.JS_CBRF:
                    if(cbperson<=moneyComparison1&&cbperson>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    settlement.setTheAmount(String.valueOf(nf.format(cbperson)));
                    break;
                case BashInfo.JS_CSXM:
                    if(wordtotal<=moneyComparison1&&wordtotal>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    settlement.setTheAmount(String.valueOf(nf.format(wordtotal1)+","+nf.format(wordtotal2)+","+nf.format(wordtotal)));
                    break;
                case BashInfo.JS_QTXM:
                    if(otherTotal<=moneyComparison1&&otherTotal>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    settlement.setTheAmount(String.valueOf(nf.format(otherTotal)));
                    break;
                case BashInfo.JS_GF:
                    if (feescount<=moneyComparison1&&feescount>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    settlement.setTheAmount(String.valueOf(nf.format(feescount)));
                    break;
                case BashInfo.JS_SJ:
                    settlement.setStatus("2");
                    settlement.setTheAmount(String.valueOf(nf.format(money)));
                    break;
                case BashInfo.JS_FBRF:
                    if(divisiontotal[1]<=moneyComparison1&&divisiontotal[1]>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    settlement.setTheAmount(String.valueOf(nf.format(divisiontotal[1])));
                    break;
                case BashInfo.JS_JGJS:
                    double doubles = getJgjsComparison(settlements);
                    if (doubles<=moneyComparison1&&doubles>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    String str = String.valueOf(nf.format(doubles));
                    settlement.setTheAmount(str);
                    break;
                case BashInfo.JS_GCJS:
                    double gcComparison =getGcComparison(settlements);
                    if (gcComparison<=moneyComparison1&&gcComparison>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    settlement.setTheAmount(String.valueOf(nf.format(gcComparison)));
                    break;
                default:
                    settlement.setStatus("2");
                    break;
            }
        }
        return settlements;
    }

    /**
     * 电缆/变电站 核对 校验规则
     * @param settlements 校验表数据list集合
     * @param builtTotal  建筑、安装 分布分项数组
     * @param divisiontotal 建筑、安装 清单数组
     * @param wordtotal1  措施费一
     * @param wordtotal2  措施费二
     * @param otherTotal  其他项目费
     * @param feescount   规费
     * @param cbperson    发包人 /  招标人 费用
     * @return  电缆/变电站 项目 导入后 校验规则  list集合
     */
    public static List<Settlement> getDLComparison(List<Settlement> settlements, double builtTotal[], double[] divisiontotal, double wordtotal1,double wordtotal2, double otherTotal, double feescount,double cbperson){
        double wordtotal=wordtotal1+wordtotal2;//措施费相加
        String name="";//获取当前汇总表名称
        double money=0.0;//获取当前汇总表金额
        Settlement settlement;//对象
        double moneyComparison1;//范围金额+5.0元
        double moneyComparison2;//范围金额-5.0元
        for (int i = 0; i < settlements.size(); i++) {
            settlement=settlements.get(i);
            name=settlement.getEntryName();
            //读取字段区分
            if(name.equals(BDZ_TBRNAME)){
                name=BashInfo.JS_CBRF;
            }
            if (name.equals(BDZ_ZBRNAME)){
                name=BashInfo.JS_FBRF;
            }
            if (name.equals(BDZ_TBNAME)){
                name=BashInfo.JS_JGJS;
            }
            if (name.contains(RESON_KYJ)){
                name = RESON_KYJ;
            }
            if (name.contains(RESON_KYJ1)){
                name = RESON_KYJ1;
            }
            if (name.equals(RESON_ZZJS)){
                name = BashInfo.JS_GCJS;
            }
            if (name.equals(RESON_ZZFC)){
                name = BashInfo.JS_GCJS;
            }
            if (name.equals(RESON_ZGTB)){
                name = BashInfo.JS_GCJS;
            }
            if (name.equals(RESON_XFHZJ)){
                name = BashInfo.JS_GCJS;
            }
            //获取金额
            money=settlement.getMoney();
            moneyComparison1 = money+5.0;
            moneyComparison2 = money-5.0;
            //默认初始状态为 1-不一致
            settlement.setStatus("1");
            switch (name){
                case BashInfo.JS_FBFX:
                    double division=divisiontotal[0]+divisiontotal[2];//分工清单分部分项
                    double built = builtTotal[0]+builtTotal[1];//安装合计总金额
                    if(built<=moneyComparison1&&built>=moneyComparison2&&division<=moneyComparison1&&division>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    settlement.setTheAmount(String.valueOf(nf.format(built)+","+nf.format(division)+","+nf.format(builtTotal[0])+","+nf.format(builtTotal[1])+","+nf.format(divisiontotal[0])+","+nf.format(divisiontotal[2])));
                    break;
                case  BashInfo.JS_CBRF:
                    if(cbperson<=moneyComparison1&&cbperson>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    settlement.setTheAmount(String.valueOf(nf.format(cbperson)));
                    break;
                case BashInfo.JS_CSXM:
                    if (wordtotal<=moneyComparison1&&wordtotal>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    settlement.setTheAmount(String.valueOf(nf.format(wordtotal1)+","+nf.format(wordtotal2)+","+nf.format(wordtotal)));
                    break;
                case BashInfo.JS_QTXM:
                    if(otherTotal<=moneyComparison1&&otherTotal>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    settlement.setTheAmount(String.valueOf(nf.format(otherTotal)));
                    break;
                case BashInfo.JS_GF:
                    if (feescount<=moneyComparison1&&feescount>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    settlement.setTheAmount(String.valueOf(nf.format(feescount)));
                    break;
                case BashInfo.JS_SJ:
                    settlement.setStatus("2");
                    settlement.setTheAmount(String.valueOf(nf.format(money)));
                    break;
                case BashInfo.JS_FBRF:
                    double divisionFbr =divisiontotal[1]+divisiontotal[3];
                    if(divisionFbr<=moneyComparison1&&divisionFbr>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    settlement.setTheAmount(String.valueOf(nf.format(divisionFbr)+","+nf.format(divisiontotal[1])+","+nf.format(divisiontotal[3])));
                    break;
                case BashInfo.JS_JGJS:
                    double doubles = getJgjsComparison(settlements);
                    if (doubles<=moneyComparison1&&doubles>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    String str = String.valueOf(nf.format(doubles));
                    settlement.setTheAmount(str);
                    break;
                case BashInfo.JS_GCJS:
                    double gcComparison =getGcComparison(settlements);
                    if (gcComparison<=moneyComparison1&&gcComparison>=moneyComparison2){
                        settlement.setStatus("0");
                    }
                    settlement.setTheAmount(String.valueOf(nf.format(gcComparison)));
                    break;
                default:
                    settlement.setStatus("2");
                    break;
            }
        }
        return settlements;
    }
    /**
     * 架空、电缆、变电站 竣工结算/工程投标 合计计算
     * @param settlements 《工程项目投标报价汇总表》/《工程项目结算汇总表》 集合
     */
    public static Double getJgjsComparison(List<Settlement> settlements){
        double[] doubles = new double[8];
        boolean flag= false;
        String name="";
        double money=0.0;
        double minus = 0.0;
        double addcount = 0.0;
        double jgjeCount = 0.0;
        for (Settlement settlement:settlements){
            name = settlement.getEntryName();
            money = settlement.getMoney();
            if (name.equals(BDZ_TBNAME)){
                name=BashInfo.JS_JGJS;
            }
            if(name.equals(BDZ_TBRNAME)){
                name=BashInfo.JS_CBRF;
            }
            if (name.equals(BDZ_ZBRNAME)){
                name=BashInfo.JS_FBRF;
            }
            if (name.equals(BDZ_TBNAME)){
                name=BashInfo.JS_JGJS;
            }
            if (name.contains(RESON_KYJ)||name.contains(RESON_XF)||name.contains(RESON_KYJ1)){
                if (!name.equals(RESON_XFHZJ)){
                    name = RESON_KYJ;
                }
            }
//            if (){
//                name = RESON_KYJ1;
//            }
            if (name.equals(RESON_ZZJS)){
                name = BashInfo.JS_GCJS;
            }
            if (name.equals(RESON_ZZFC)){
                name = BashInfo.JS_GCJS;
            }
            if (name.equals(RESON_ZGTB)){
                name = BashInfo.JS_GCJS;
            }
            if (name.equals(RESON_XFHZJ)){
                name = BashInfo.JS_GCJS;
            }
            switch (name) {
                case BashInfo.JS_FBFX:
                    doubles[1]=money;
                    break;
                case BashInfo.JS_CBRF:
                    doubles[2]=money;
                    break;
                case BashInfo.JS_CSXM:
                    doubles[3]=money;
                    break;
                case BashInfo.JS_QTXM:
                    doubles[4]=money;
                    break;
                case BashInfo.JS_GF:
                    doubles[5]=money;
                    break;
                case BashInfo.JS_SJ:
                    doubles[6]=money;
                    break;
                case BashInfo.JS_FBRF:
                    doubles[7]=money;
                    break;
                case RESON_KYJ:
                    minus += money;
                    break;
//                case RESON_KYJ1:
//                    minus += money;
//                    break;
                default:
                    if (name.equals(BashInfo.JS_JGJS)){
                        jgjeCount=money;
                    }else if (!name.equals(BashInfo.JS_GCJS)){
                        addcount+=money;
                    }
                    if (name.equals(BashInfo.JS_GCJS)){
                        flag=true;
                    }
                    break;
            }
        }
        doubles[0]=doubles[1]+doubles[2]+doubles[3]+doubles[4]+doubles[5]+doubles[6]-minus+addcount;
        getFlag = false;
        if (flag){
            doubles[0] = doubles[0]+ minus - addcount;
        }
        if ((jgjeCount-5.0)>=doubles[0]||(jgjeCount+5.0)<=doubles[0]){
            doubles[0]=doubles[1]+doubles[2]+doubles[3]+doubles[4]+doubles[5]+doubles[6]-doubles[7]-minus+addcount;
            getFlag = true;
        }
        if (flag){
            doubles[0] = doubles[0]+ minus - addcount;
        }
        return doubles[0];
    }
    /**
     * 工程结算价 校验
     * @param settlements 《工程项目结算汇总表》 集合
     * @return double[] 计算匹配数据集合
     */
    public static double getGcComparison(List<Settlement> settlements){
        double fbrMoney = 0.0;
        String name="";
        double money=0.0;
        double minus = 0.0;
        double addcount = 0.0;
        double count = 0.0;
        for (Settlement settlement:settlements){
            name = settlement.getEntryName();
            if (name.equals(BDZ_TBNAME)){
                name=BashInfo.JS_JGJS;
            }
            if(name.equals(BDZ_TBRNAME)){
                name=BashInfo.JS_CBRF;
            }
            if (name.equals(BDZ_ZBRNAME)){
                name=BashInfo.JS_FBRF;
            }
            if (name.equals(BDZ_TBNAME)){
                name=BashInfo.JS_JGJS;
            }
            if (name.contains(RESON_KYJ)||name.contains(RESON_XF)||name.contains(RESON_KYJ1)){
                if (!name.equals(RESON_XFHZJ)){
                    name = RESON_KYJ;
                }
            }
//            if (name.contains(RESON_KYJ1)){
//                name = RESON_KYJ1;
//            }
            if (name.equals(RESON_ZZJS)){
                name = BashInfo.JS_GCJS;
            }
            if (name.equals(RESON_ZZFC)){
                name = BashInfo.JS_GCJS;
            }
            if (name.equals(RESON_ZGTB)){
                name = BashInfo.JS_GCJS;
            }
            if (name.equals(RESON_XFHZJ)){
                name = BashInfo.JS_GCJS;
            }
            money =settlement.getMoney();
            switch (name) {
                case BashInfo.JS_FBRF:
                    fbrMoney+=money;
                    break;
                case RESON_KYJ:
                    minus += money;
                    break;
//                case RESON_KYJ1:
//                    minus += money;
//                    break;
                default:
                    if (!name.equals(BashInfo.JS_JGJS)&&!name.equals(BashInfo.JS_GCJS)){
                        addcount+=money;
                    }
                    break;
            }
        }
        if (getFlag){
            count =addcount-fbrMoney-minus;
        }else {
            count=addcount-minus;
        }
        return count;
    }
    /**
     * 前台显示提示数据匹配：架空线路
     * @param settlementList 数据库  集合
     * @return 数据库匹配后返回  集合
     */
    public static List<Settlement> getSelectSettlement(List<Settlement> settlementList){
        boolean flag = false;//定义开关
        String key="";
        String reason="";
        for(Settlement settlement:settlementList){
            key = settlement.getEntryName();
            if (key.equals(BDZ_TBRNAME)){
                flag=true;
                key = BashInfo.JS_CBRF;
            }
            if (key.equals(BDZ_ZBRNAME)){
                flag=true;
                key=BashInfo.JS_FBRF;
            }
            if (key.equals(BDZ_TBNAME)){
                flag=true;
                key = BashInfo.JS_JGJS;
            }
            if (key.equals(RESON_ZZJS)){
                key = BashInfo.GCJS;
            }
            switch (key){
                case BashInfo.JS_FBFX:
                    reason = getFbfxJoint(settlement);
                    settlement.setReason(reason);
                    break;
                case  BashInfo.JS_CBRF:
                    if(flag){
                        settlement.setEntryName(BDZ_TBRNAME);
                        reason = getJoint(BDZ_TBRCG,BDZ_TBRCG1,settlement);
                        flag= false; //默认关闭
                    }else {
                        reason = getJoint(RESON_CBRF,RESON_CBRF1,settlement);
                    }
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_CSXM:
                    reason =getCsfJoint(RESON_CSXM,settlement);
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_QTXM:
                    reason=getJoint(RESON_QTXM,RESON_QTXM1,settlement);
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_GF:
                    reason=getJoint(RESON_GF,RESON_GF1,settlement);
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_SJ:
                    reason=RESON_SJ;
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_FBRF:
                    if(flag){
                        settlement.setEntryName(BashInfo.JS_FBRF);
                        reason=getJoint(BDZ_FBRF,RESON_FBRF2,settlement);
                        flag= false; //默认关闭
                    }else {
                        reason=getJoint(RESON_FBRF,RESON_FBRF1,settlement);
                    }
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_JGJS:
                    if(flag){
                        settlement.setEntryName(BDZ_TBNAME);
                        reason = getJgJoint(BDZ_TBBJ,settlement,settlementList);
                        flag = false;
                    }else {
                        reason = getJgJoint(RESON_JGJS,settlement,settlementList);
                    }
                    settlement.setReason(reason);
                    break;
                case BashInfo.GCJS:
                    reason=getJshzJoint(RESON_GCJS,settlement,settlementList);
                    settlement.setReason(reason);
                    break;
                case RESON_ZZFC:
                    reason=getJshzJoint(RESON_GCJS,settlement,settlementList);
                    settlement.setReason(reason);
                    break;
                case RESON_ZGTB:
                    reason=getJshzJoint(RESON_GCJS,settlement,settlementList);
                    settlement.setReason(reason);
                    break;
                case RESON_XFHZJ:
                    reason=getJshzJoint(RESON_GCJS,settlement,settlementList);
                    settlement.setReason(reason);
                    break;

            }
        }
        return settlementList;
    }
    /**
     * 前台显示 提示数据匹配：电缆
     * @param settlementList 数据库  集合
     * @return 数据库匹配后返回  集合
     */
    public static List<Settlement> getSelectDlSettlement(List<Settlement> settlementList){
        boolean flag = false;//定义开关
        String key="";
        String reason="";
        for(Settlement settlement:settlementList){
            key = settlement.getEntryName();
            if (key.equals(BDZ_TBRNAME)){
                flag=true;
                key = BashInfo.JS_CBRF;
            }
            if (key.equals(BDZ_ZBRNAME)){
                flag=true;
                key=BashInfo.JS_FBRF;
            }
            if (key.equals(BDZ_TBNAME)){
                flag=true;
                key = BashInfo.JS_JGJS;
            }
            if (key.equals(RESON_ZZJS)){
                key = BashInfo.GCJS;
            }
            switch (key){
                case BashInfo.JS_FBFX:
                    reason = getDJoint(settlement,DLBDZ_FBFX1);
                    settlement.setReason(reason);
                    break;
                case  BashInfo.JS_CBRF:
                    if(flag){
                        settlement.setEntryName(BDZ_TBRNAME);
                        reason = getJoint(BDZ_TBRCG,BDZ_TBRCG1,settlement);
                        flag= false; //默认关闭
                    }else {
                        reason = getJoint(RESON_CBRF,RESON_CBRF1,settlement);
                    }
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_CSXM:
                    reason =getCsfJoint(RESON_CSXM,settlement);
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_QTXM:
                    reason=getJoint(RESON_QTXM,RESON_QTXM1,settlement);
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_GF:
                    reason=getJoint(RESON_GF,RESON_GF1,settlement);
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_SJ:
                    reason=RESON_SJ;
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_FBRF:
                    if(flag){
                        settlement.setEntryName(BDZ_ZBRNAME);
                        reason=getDlJoint(BDZ_FBRF,BDZ_FBRF1,BDZ_FBRF2,settlement);
                        flag = false;
                    }else {
                        reason=getDlJoint(RESON_FBRF,RESON_DLFBRF1,RESON_DLFBRF2,settlement);
                    }
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_JGJS:
                    if(flag){
                        settlement.setEntryName(BDZ_TBNAME);
                        reason = getJgJoint(BDZ_TBBJ,settlement,settlementList);
                        flag =false;
                    }else {
                        reason = getJgJoint(RESON_JGJS,settlement,settlementList);
                    }
                    settlement.setReason(reason);
                    break;
                case BashInfo.GCJS:
                    reason=getJshzJoint(RESON_GCJS,settlement,settlementList);
                    settlement.setReason(reason);
                    break;
                case RESON_ZZFC:
                    reason=getJshzJoint(RESON_GCJS,settlement,settlementList);
                    settlement.setReason(reason);
                    break;
                case RESON_ZGTB:
                    reason=getJshzJoint(RESON_GCJS,settlement,settlementList);
                    settlement.setReason(reason);
                    break;
                case RESON_XFHZJ:
                    reason=getJshzJoint(RESON_GCJS,settlement,settlementList);
                    settlement.setReason(reason);
                    break;
            }
        }
        return settlementList;
    }

    /**
     * 前台显示提示数据匹配：变电站
     * @param settlementList 数据库获取所有数据
     * @return 数据库匹配后返回  集合
     */
    public static List<Settlement> getSelectBdzSettlement(List<Settlement> settlementList){
        boolean flag = false;//定义开关
        String key=""; //匹配字段
        String reason=""; //提示 字段
        for(Settlement settlement:settlementList){
            key = settlement.getEntryName();
            //字段 匹配
            if (key.equals(BashInfo.JS_CBRF)){
                flag=true;
                key = BDZ_TBRNAME;
            }
            if (key.equals(BashInfo.JS_FBRF)){
                flag=true;
                key=BDZ_ZBRNAME;
            }
            if (key.equals(BashInfo.JS_JGJS)){
                flag=true;
                key = BDZ_TBNAME;
            }
            switch (key){
                case BashInfo.JS_FBFX:
                    reason = getDJoint(settlement,DLBDZ_FBFX2);
                    settlement.setReason(reason);
                    break;
                case  BDZ_TBRNAME:
                    if(flag){ //如果为true 变电站字段为工程竣工
                        settlement.setEntryName(BashInfo.JS_CBRF);
                        reason = getJoint(RESON_CBRF,RESON_CBRF1,settlement);
                        flag= false; //默认关闭
                    }else {
                        reason = getJoint(BDZ_TBRCG,BDZ_TBRCG1,settlement);
                    }
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_CSXM:
                    reason =getCsfJoint(BDZ_CSF,settlement);
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_QTXM:
                    reason=getJoint(BDZ_QTXM,BDZ_QTXM1,settlement);
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_GF:
                    reason=getJoint(BDZ_GF,RESON_GF1,settlement);
                    settlement.setReason(reason);
                    break;
                case BashInfo.JS_SJ:
                    reason=BDZ_SJ;
                    settlement.setReason(reason);
                    break;
                case SettementUtil.BDZ_ZBRNAME:
                    if(flag){
                        settlement.setEntryName(BashInfo.JS_FBRF);
                        reason=getDlJoint(RESON_FBRF,RESON_DLFBRF1,RESON_DLFBRF2,settlement);
                        flag= false; //默认关闭
                    }else {
                        reason=getDlJoint(BDZ_FBRF,BDZ_FBRF1,BDZ_FBRF2,settlement);
                    }
                    settlement.setReason(reason);
                    break;
                case SettementUtil.BDZ_TBNAME:
                    if(flag){
                        settlement.setEntryName(BashInfo.JS_JGJS);
                        reason = getJgJoint(RESON_JGJS,settlement,settlementList);
                        flag= false; //默认关闭
                    }else {
                        reason = getJgJoint(BDZ_TBBJ,settlement,settlementList);
                    }
                    settlement.setReason(reason);
                    break;
                case BashInfo.GCJS:
                    reason=getJshzJoint(RESON_GCJS,settlement,settlementList);
                    settlement.setReason(reason);
                    break;
                case RESON_ZZFC:
                    reason=getJshzJoint(RESON_GCJS,settlement,settlementList);
                    settlement.setReason(reason);
                    break;
                case RESON_ZZJS:
                    reason=getJshzJoint(RESON_GCJS,settlement,settlementList);
                    settlement.setReason(reason);
                    break;
                case RESON_ZGTB:
                    reason=getJshzJoint(RESON_GCJS,settlement,settlementList);
                    settlement.setReason(reason);
                    break;
                case RESON_XFHZJ:
                    reason=getJshzJoint(RESON_GCJS,settlement,settlementList);
                    settlement.setReason(reason);
                    break;
            }
        }
        return settlementList;
    }
    /**
     * 架空项目 分部分项对比
     * @param settlement bean
     * @return String reason
     */
    public static String getFbfxJoint(Settlement settlement){
        String reason="";
        String[] amout=settlement.getTheAmount().split(",");
        String reason1=SettementUtil.RESON_FBFX1.replace("xxx",String.valueOf(nf.format(settlement.getMoney())))+SettementUtil.RESON_FBFX2.replace("xxx",amout[0]);
        String reason2=SettementUtil.RESON_FBFX1.replace("xxx",String.valueOf(nf.format(settlement.getMoney())))+SettementUtil.RESON_FBFX3.replace("xxx",amout[1]);
        String status =settlement.getStatus();
        if(status.equals("0")){
            reason=reason1+SettementUtil.RESON_TRUE+","+","+","+reason2+SettementUtil.RESON_TRUE;
        }
        if(status.equals("1")){
            double money = settlement.getMoney();
            double moneyComparison1=money+5.0;//范围金额+5.0元
            double moneyComparison2=money-5.0;//范围金额-5.0元
            double amout1 = Double.valueOf(amout[0]);
            double amout2 = Double.valueOf(amout[1]);
            if (amout1>=moneyComparison1||amout1<=moneyComparison2){
                if (amout2>=moneyComparison1||amout2<=moneyComparison2){
                    reason=reason1+SettementUtil.RESON_FLASE+","+","+","+reason2+SettementUtil.RESON_FLASE;
                }
                if(amout2<=moneyComparison1&&amout2>=moneyComparison2){
                    reason=reason1+SettementUtil.RESON_FLASE+","+","+","+reason2+SettementUtil.RESON_TRUE;
                }

            }
            if (amout2>=moneyComparison1||amout2<=moneyComparison2){
                if(amout1<=moneyComparison1&&amout1>=moneyComparison2){
                    reason=reason1+SettementUtil.RESON_TRUE+","+","+","+reason2+SettementUtil.RESON_FLASE;
                }
            }
        }
        return reason;
    }
    /**
     * 获取 电缆/变电站 分部分项  reason 值
     * @param settlement bean
     * @return reason
     */
    public static String getDJoint(Settlement settlement,String[] tips){
        String reason="";
        String[] amout=settlement.getTheAmount().split(",");
        String reason1=tips[0].replace("xxx",String.valueOf(nf.format(settlement.getMoney())))
                +tips[1].replace("xxx",amout[2])
                +tips[2].replace("xxx",amout[3])
                +tips[3].replace("xxx",amout[0]);
        String reason2=tips[0].replace("xxx",String.valueOf(nf.format(settlement.getMoney())))
                +tips[4].replace("xxx",amout[4])
                +tips[5].replace("xxx",amout[5])
                +tips[6].replace("xxx",amout[1]);
        String status =settlement.getStatus();
        if(status.equals("0")){
            reason=reason1+SettementUtil.RESON_TRUE+","+","+","+reason2+SettementUtil.RESON_TRUE;
        }
        if(status.equals("1")){
            double money = settlement.getMoney();
            double amout1 = Double.valueOf(amout[0]);
            double amout2 = Double.valueOf(amout[1]);
            double moneyComparison1=money+5.0;//范围金额+5.0元
            double moneyComparison2=money-5.0;//范围金额-5.0元
            if (amout1>=moneyComparison1||amout1<=moneyComparison2){
                if (amout2>=moneyComparison1||amout2<=moneyComparison2){
                    reason=reason1+SettementUtil.RESON_FLASE+","+","+","+reason2+SettementUtil.RESON_FLASE;
                }
                if(amout2<=moneyComparison1&&amout2>=moneyComparison2){
                    reason=reason1+SettementUtil.RESON_FLASE+","+","+","+reason2+SettementUtil.RESON_TRUE;
                }

            }
            if (amout2>=moneyComparison1||amout2<=moneyComparison2){
                if(amout1<=moneyComparison1&&amout1>=moneyComparison2){
                    reason=reason1+SettementUtil.RESON_TRUE+","+","+","+reason2+SettementUtil.RESON_FLASE;
                }
            }
        }
        return reason;
    }

    /**
     *  规费/其他费 等 计算 数据 常量匹配
     * @param constant1   匹配的常量one
     * @param constant2   匹配的常量two
     * @param settlement  Settlement 对象
     * @return Settlement reason 属性 赋值
     */
    public static String getJoint(String constant1,String constant2,Settlement settlement){
        String reason="";
        String status=settlement.getStatus();
        reason=constant1.replace("xxx",String.valueOf(nf.format(settlement.getMoney())))
                +constant2.replace("xxx",settlement.getTheAmount());
        if (status.equals("0")){
            reason+=SettementUtil.RESON_TRUE;
        }else {
            reason+=SettementUtil.RESON_FLASE;
        }
        return reason;
    }

    /**
     * 措施费对比
     * @param csxm  措施费常量
     * @param settlement bean
     * @return reason
     */
    public static String getCsfJoint(String csxm,Settlement settlement){
        String status =settlement.getStatus();
        String[] amout=settlement.getTheAmount().split(",");
        String reason=csxm.replace("xxx",String.valueOf(nf.format(settlement.getMoney())))
                +RESON_CSXM1.replace("xxx",amout[0])+
                RESON_CSXM2.replace("xxx",amout[1])+
                RESON_CSXM3.replace("xxx",amout[2]);
        if (status.equals("0")){
            reason+=RESON_TRUE;
        }else {
            reason+=RESON_FLASE;
        }
        return reason;
    }
    /**
     * 电缆/变电站 发包人采购对比
     * @param s 发包人采购 常量 s1
     * @param settlement bean
     * @return reason
     */
    public static String getDlJoint(String s,String s1,String s2,Settlement settlement){
        String reason="";
        String[] amout = settlement.getTheAmount().split(",");
        String status=settlement.getStatus();
        reason=s.replace("xxx",String.valueOf(nf.format(settlement.getMoney())))
                +s1.replace("xxx",amout[1])
                +s2.replace("xxx",amout[2])
                +RESON_JGJS8.replace("xxx",amout[0]);
        if (status.equals("0")){
            reason+=SettementUtil.RESON_TRUE;
        }else {
            reason+=SettementUtil.RESON_FLASE;
        }
        return reason;
    }
    /**
     * 工程竣工 常量匹配
     * @param tops //常量拼接
     * @param settlement bean
     * @param  settlements  集合
     * @return string
     */
    public static String getJgJoint(String tops,Settlement settlement,List<Settlement> settlements){
        String reason="";
        reason+=tops.replace("xxx",String.valueOf(nf.format(settlement.getMoney())));
        String amout=settlement.getTheAmount();
        String name = "";
        for (int i = 0; i < settlements.size(); i++) {
            name = settlements.get(i).getEntryName();
            if (name.equals(BashInfo.JS_JGJS)||
                    name.equals(BDZ_TBNAME)){
                reason+=RESON_JGJS8.replace("xxx",amout);
                if (settlement.getStatus().equals("0")){
                    reason+=SettementUtil.RESON_TRUE;
                }else {
                    reason+=SettementUtil.RESON_FLASE;
                }
                return reason;
            }
            reason += ","+settlements.get(i).getEntryName()+",<span style='font-weight: bold;'>"+nf.format(settlements.get(i).getMoney())+"</span>,";
        }
        reason+=RESON_JGJS8.replace("xxx",amout);
        if (settlement.getStatus().equals("0")){
            reason+=SettementUtil.RESON_TRUE;
        }else {
            reason+=SettementUtil.RESON_FLASE;
        }
        return reason;
    }
    /**
     * 工程结算
     * @param tops
     * @param settlement
     * @param settlements
     * @return String
     */
    public static String getJshzJoint(String tops,Settlement settlement,List<Settlement> settlements){
        String reason="";
        String name="";
        reason = tops.replace("xxx",String.valueOf(nf.format(settlement.getMoney())));
        String amout=settlement.getTheAmount();
        for (int i = 0; i < settlements.size(); i++) {
            name = settlements.get(i).getEntryName();
            if (name.equals(BashInfo.JS_JGJS)|| name.equals(BDZ_TBNAME)){
                continue;
            }
            if (name.equals(BashInfo.JS_GCJS)|| name.equals(RESON_ZZJS)|| name.equals(RESON_ZZFC)|| name.equals(RESON_ZGTB)||
                    name.equals(RESON_XFHZJ)){
                reason+=RESON_JGJS8.replace("xxx",amout);
                if (settlement.getStatus().equals("0")){
                    reason+=SettementUtil.RESON_TRUE;
                }else {
                    reason+=SettementUtil.RESON_FLASE;
                }
                return reason;
            }
            reason += ","+settlements.get(i).getEntryName()+",<span style='font-weight: bold;'>"+nf.format(settlements.get(i).getMoney())+"</span>,";
        }
        reason+=RESON_JGJS8.replace("xxx",amout);
        if (settlement.getStatus().equals("0")){
            reason+=SettementUtil.RESON_TRUE;
        }else {
            reason+=SettementUtil.RESON_FLASE;
        }
        return reason;
    }
    /**
     *  添加获取数据到settlement 对象
     * @param settlement bean
     * @param key    当前循环到的key值
     * @param importExcel importExcel
     * @param i 当前循环到到的下标
     * @return 对象
     */
    public static Settlement getSettlement(Settlement settlement,String key,ImportExcel importExcel,int i){
        settlement.setEntryName(key);
        settlement.setMoney(importExcel.getDouble(i+1,2));
        settlement.setRemarks(importExcel.getValue(i+1,3));
        return settlement;
    }
}
