package com.jeeplus.modules.sg.substation.service;

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.JkxlDetailFee;
import com.jeeplus.modules.sg.overheadline.entity.Settlement;
import com.jeeplus.modules.sg.overheadline.entity.WbsProject;
import com.jeeplus.modules.sg.overheadline.mapper.OverheadLineMapper;
import com.jeeplus.modules.sg.overheadline.service.FeeAdjustService;
import com.jeeplus.modules.sg.overheadline.service.SettlementService;
import com.jeeplus.modules.sg.overheadline.util.BashInfo;
import com.jeeplus.modules.sg.overheadline.util.SettementUtil;
import com.jeeplus.modules.sg.project.entity.WbsItem;
import com.jeeplus.modules.sg.project.service.ItemService;
import com.jeeplus.modules.sg.substation.util.SubstationInfo;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class SubstationService {
    @Autowired
    private OverheadLineMapper overheadLineMapper;
    @Autowired
    private FeeAdjustService feeAdjustService;
    @Autowired
    private SettlementService settlementService;
    @Autowired
    private ItemService itemService;


    /**
     * 变电站工程分析保存
     * @param file   项目文件
     * @param wbsItem 项目信息
     */
   @Transactional(readOnly = false)
    public void save(MultipartFile file,WbsItem wbsItem) throws IOException, InvalidFormatException {
        String id = wbsItem.getId();
        WbsItem wbsItem1 = itemService.get(id);
        if(wbsItem1 == null){
            wbsItem.setDelFlag("0");
            //保存项目
            itemService.save(wbsItem);
        }
        //读取表格
        ImportExcel total = new ImportExcel(file,1,new String[]{BashInfo.SHEET_HZB,BashInfo.SHEET_HZB1,BashInfo.SHEET_HZB2,BashInfo.SHEET_HZB3},true );  //工程项目竣工结算汇总表
        ImportExcel csqd1 = new ImportExcel(file,1,new String[]{BashInfo.SHEET_CSB,BashInfo.SHEET_CSB1},true);  // 措施项目清单计价表（一）
        ImportExcel csqd2 = new ImportExcel(file,1,BashInfo.SHEET_CSB2,false);  // 措施项目清单计价表（二）
        ImportExcel gf = new ImportExcel(file,1,BashInfo.SHEET_GFQD,true);  //规费项目清单计价表
        ImportExcel qt = new ImportExcel(file,1,BashInfo.SHEET_QTFY,true);  //其他项目清单计价表
        ImportExcel tbrsb = new ImportExcel(file,1,new String[]{BashInfo.SHEET_CBRCG,BashInfo.SHEET_TBRCG},false);//投标人采购设备计价表

//        从《工程项目竣工结算汇总表》获取相关值
        double jzgc = ExcelUtil.getDouble(total,new String[]{BashInfo.HZ_JZGC,BashInfo.HZ_JZGCF},1,2);//汇总表建筑金额
        double azgc = ExcelUtil.getDouble(total,new String[]{BashInfo.HZ_AZGC,BashInfo.HZ_AZGCF},1,2);//汇总表安装金额
        double cbrFee = ExcelUtil.getDouble(total,new String[]{BashInfo.JS_CBRF,BashInfo.JS_TBRF},1,2);//承包人采购设备费
        double jsCs = ExcelUtil.getDouble(total,BashInfo.JS_CSXM,1,2); //获取措施费合计
        double jsGf = ExcelUtil.getDouble(total,BashInfo.JS_GF,1,2);  //获取规费合计
        double texRate = ExcelUtil.getTexRate(total); //获取税率
        double textotal = ExcelUtil.getTex(total);  //获取税金合计
        double btex = BashInfo.getBtex(total);  //获取补税额
        double fbrFees = ExcelUtil.getFbr(total); //获取发包人采购设备费
        double gcjsFee = BashInfo.getGcjsFee(total);  //获取工程结算价

//        从《措施项目清单计价表（一）》中获取建筑和安装的措施费
        double jzcsRate = BashInfo.getJzRate(csqd1); //获取建筑措施费率
        double[] azcsRate = BashInfo.getAzRate(csqd1); //获取安装措施费率
        double jzcs = BashInfo.getJzCs(csqd1); //建筑措施费
        double azcs = BashInfo.getAzCs(csqd1);  //安装措施费
        double azCsFee = BashInfo.getAzCsfee(csqd1); //安装措施费 除去临时施工费和安全文明施工费剩下措施费合计
        double word1total =  jzcs+azcs;//获取措施费合计


        double csJzFee = 0.00;  //建筑措施分摊费
        double csAzFee = 0.00;  //安装措施分摊费
        double word2total=0.00;  //获取措施费二表措施项目合计金额
        if(ImportExcel.getSheet(csqd2) != null){
             csJzFee = SubstationInfo.getCsJzFee(csqd2);
             csAzFee = SubstationInfo.getCsAzFee(csqd2);
             word2total=csJzFee+csAzFee;
        }

//       从《承包人采购设备计价表》中，获取承包人采购设备费
        double tbrJzFee = 0.00; //投标人建筑费
        double tbrAzFee = 0.00; //投标人安装费
        double cbperson = 0.00; //投标人合计
        if(ImportExcel.getSheet(tbrsb) != null){
            tbrJzFee = BashInfo.getFbrJzFee(tbrsb);
            tbrAzFee = BashInfo.getFbrAzFee(tbrsb);
//            cbperson = ExcelUtil.getDouble(tbrsb,BashInfo.QT_HJ,0,6);
            cbperson = tbrJzFee+tbrAzFee;
        }

//        从《规费项目清单计价表》中获取规费
        double jzGf = BashInfo.getJzGf(gf);//建筑规费合计
        double azGf = BashInfo.getAzGf(gf); //安装规费合计
        double feesBdzCount =  jzGf+azGf;//获取规费合计

//       从《规费项目清单计价表》中获取建筑规费费率，包括“危险作业意外伤害保险费”和剩余费率和
        double[] jzgfRate = BashInfo.getJzGfRate(gf);  //获取建筑规费率

//        从《其他费用清单计价表》中获取，“施工场地租用费”，“招标代理”，以及除这两项外其他费用和
        double[] qtFees = BashInfo.getQtFees(qt);  //获取其他费
//       从《其他费用清单计价表》中获取其他费用合计
        double otherBdzTotal =  ExcelUtil.getDouble(qt,BashInfo.QT_HJ,0,2);//获取其他项目费合计

//       从《工程项目竣工结算汇总表》中查找未识别费用
        List<JkxlAdjustFee> wsbFees = BashInfo.getWsbFee(total);
        double wsbfee = 0.00;
        double tax = 0.00; //税金
        double totalfee = 0.00;  //合计
        //变电站 结算汇总
        List<Settlement> settlementsBdz =  SettementUtil.getComparison(total);

//        《工程项目竣工结算汇总表》中“建筑工程”、“安装工程”金额都不为空时处理方式
        if(jzgc !=0.00 && azgc != 0.00){
            ImportExcel jzqd = new ImportExcel(file, 1, BashInfo.SHEET_JZFBFXJJ,true);  //建筑分部分项工程量清单计价表
            ImportExcel azqd = new ImportExcel(file, 1, BashInfo.SHEET_AZFBFXJJ,true);  //安装分部分项工程量清单计价表
            ImportExcel jzhz = new ImportExcel(file, 1, SubstationInfo.SHEET_JZHZB,true);   //建筑工程费用汇总表
            ImportExcel azhz = new ImportExcel(file, 1, SubstationInfo.SHEET_HZHZB,true);   //安装工程费用汇总表
            //变电站结算汇总
            int[] comlumn2 = {12,15};//定义所要数组

//            从《工程费用汇总表》中获取“合计”行的合计金额
            double built1Total = ExcelUtil.getDouble(jzhz,BashInfo.QT_HJ,0,2);//变电站建筑
            double built2Total = ExcelUtil.getDouble(azhz,BashInfo.QT_HJ,0,2);//变电站安装
            double[] builtTotal={built1Total,built2Total};

//            从《分部分项工程量清单计价表》分别获取“变电站建筑工程”、“变电站安装工程”的“合计”、“招标人采购”
            double[] division1total=ExcelUtil.getDoubleArray(jzqd,SubstationInfo.JZMX_BJZ,2,comlumn2);//建筑分部分项比对数据清单合计
            double[] division2total=ExcelUtil.getDoubleArray(azqd,SubstationInfo.AZMX_BDZAZ,2,comlumn2);//安装分部分项比对数据清单合计
            double[] division = {division1total[0],division1total[1],division2total[0],division2total[1]};


            //获取甲供材进项税额
//            从《工程费用汇总表》中获取“合计”行的直接工程费
            double zjgcJzTotal = ExcelUtil.getDouble(jzhz,SubstationInfo.QT_HJ,0,3);//建筑直接工程费合计
            double zjgcAzTotal = ExcelUtil.getDouble(azhz,SubstationInfo.QT_HJ,0,3);//安装直接工程费合计
//            从《安装工程费用汇总表》中获取“合计”行的人工费
            double rgAzTotal = ExcelUtil.getDouble(azhz,SubstationInfo.QT_HJ,0,4);//安装人工费合计
//            double csJzFee = SubstationInfo.getCsJzFee(csqd2);//建筑措施分摊费
//            double csAzFee = SubstationInfo.getCsAzFee(csqd2);//安装措施分摊费
//            double tbrJzFee = BashInfo.getFbrJzFee(tbrsb); //投标人建筑费
//            double tbrAzFee = BashInfo.getFbrAzFee(tbrsb); //投标人安装费
            double zjgcTotal = zjgcAzTotal+zjgcJzTotal;
//            double[] aqwmJzFees = ExcelUtil.getDoubleArray(csqd1,SubstationInfo.CS_AQWM,1, new int[]{3, 4});  //建筑安全文明施工费
            double[] aqwmJzFees = BashInfo.getAqwfJzFee(csqd1);  //建筑安全文明施工费、费率
            double[] aqwmAzFees = BashInfo.getAqwfAzFee(csqd1);  //安装安全文明施工费、费率
//            从两个《分部分项工程量清单计价表》的“变电站建筑工程”、“变电站安装工程”获取建筑和安装的发包人采购材料
            double fbrJzFee = ExcelUtil.getDouble(jzqd,SubstationInfo.JZMX_BJZ,2,15); //
            double fbrAzFee = ExcelUtil.getDouble(azqd,SubstationInfo.AZMX_BDZAZ,2,15);
            double jxslJz = 0.00;
            double jxslAz = 0.00;
            if (fbrJzFee != 0.00) {
                    double zgJz = zjgcJzTotal - aqwmJzFees[1] / (aqwmJzFees[0]/100);
                    jxslJz = zgJz / (fbrJzFee - zgJz); //建筑甲供材进项税率
            }
            if (fbrAzFee != 0.00) {
                    double zgAz = zjgcAzTotal - aqwmAzFees[1] / (aqwmAzFees[0]/100);
                    jxslAz = zgAz / (fbrAzFee - zgAz); //安装甲供材进项税率
            }

            //建筑部分基础数据
            JkxlDetailFee jz = SubstationInfo.getTolalInfo(jzqd,jzhz,"21000000",SubstationInfo.JZMX_BJZ);  //建筑合计
            JkxlDetailFee jzsc = SubstationInfo.getBashInfo(jzqd,jzhz,"21100000",SubstationInfo.JZMX_ZYGC);  //主要生产工程
            List<JkxlDetailFee> scjz = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.ZYSCWBS,SubstationInfo.ZYSCJZSTR,"1"); //主要生产建筑
            List<JkxlDetailFee> pdzz = SubstationInfo.getPdzzjz(jzqd,jzhz);    //配电装置
            List<JkxlDetailFee> gsxt  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.GSXTWBS,SubstationInfo.GSXTSTR,"1"); //供水系统建筑
            List<JkxlDetailFee> xfxt  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.XFXTWBS,SubstationInfo.XFXTSTR,"1"); //消防系统
            JkxlDetailFee jzfz = SubstationInfo.getBashInfo(jzqd,jzhz,"21200000",SubstationInfo.JZMX_FZSCGC);  //辅助生产工程
            List<JkxlDetailFee> fzscjz  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.FZSCWBS,SubstationInfo.FZSCSTR,"2"); //辅助生产建筑
            List<JkxlDetailFee> zqxjz  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.ZQXWBS,SubstationInfo.ZQXSTR,"2"); //站区性建筑
            List<JkxlDetailFee> tsgzw  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.TXGJWWBS,SubstationInfo.TXGJWSTR,"2"); //特殊构筑物
            JkxlDetailFee zqlh = SubstationInfo.getBashInfo(jzqd,jzhz,"21240000",SubstationInfo.JZMX_ZQLH);  //站区绿化
            JkxlDetailFee ydxgc = SubstationInfo.getBashInfo(jzqd,jzhz,"21300000",SubstationInfo.JZMX_ZZDXGC);  //单项工程
            List<JkxlDetailFee> dxgc  = SubstationInfo.getDxInfo(jzqd,jzhz,SubstationInfo.YDZYGWBS,SubstationInfo.YDZYGSTR); //与站址有关的单项工程
            List<JkxlDetailFee> jzTotal = new ArrayList<>();
            jzTotal.add(jz);
            jzTotal.add(jzsc);
            jzTotal.addAll(scjz);
            jzTotal.addAll(pdzz);
            jzTotal.addAll(gsxt);
            jzTotal.addAll(xfxt);
            jzTotal.add(jzfz);
            jzTotal.addAll(fzscjz);
            jzTotal.addAll(zqxjz);
            jzTotal.addAll(tsgzw);
            jzTotal.add(zqlh);
            jzTotal.add(ydxgc);
            jzTotal.addAll(dxgc);
            //安装部分基础数据
            JkxlDetailFee az = SubstationInfo.getTolalInfo(azqd,azhz,"23000000",SubstationInfo.AZMX_BDZAZ);  //安装合计
            JkxlDetailFee azsc = SubstationInfo.getBashInfo(azqd,azhz,"23100000",SubstationInfo.AZMX_ZYSCGC);  //主要生产工程
            List<JkxlDetailFee> zbyq  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.ZBYQWBS,SubstationInfo.ZBYQSTR); //主变压器系统
            List<JkxlDetailFee> pdzzs  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.PDZZWBS,SubstationInfo.PDZZSTR); //配电装置
            List<JkxlDetailFee> wgbc  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.WGBCWBS,SubstationInfo.WGBCSTR); //无功补偿
            List<JkxlDetailFee> kzzl = SubstationInfo.getKzzl(azqd,azhz);   //控制直流
            List<JkxlDetailFee> zyd  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.ZYDWBS,SubstationInfo.ZYDSTR); //站用电系统
            List<JkxlDetailFee> dljd = SubstationInfo.getDljd(azqd,azhz);    //电缆接地
            List<JkxlDetailFee> txyd  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.TXYDWBS,SubstationInfo.TXYDSTR); //通信及远动系统
            List<JkxlDetailFee> yzts  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.QZTSWBS,SubstationInfo.QZTSSTR); //全站调试
            List<JkxlDetailFee> fzsc  = SubstationInfo.getBashInfo(azqd,azhz,SubstationInfo.AFZSCWBS,SubstationInfo.AFZSCSTR); //辅助生产工程
            JkxlDetailFee azdx = SubstationInfo.getBashInfo(azqd,azhz,"23300000",SubstationInfo.AZMX_ZZDXGC);  //与站址有关的单项工程
            List<JkxlDetailFee> zwdy  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.ZWDYWBS,SubstationInfo.ZWDYSTR); //站外电源
            List<JkxlDetailFee> zwtx  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.ZWTXWBS,SubstationInfo.ZWTXSTR); //站外通信
            List<JkxlDetailFee> azTotal = new ArrayList<>();
            azTotal.add(az);
            azTotal.add(azsc);
            azTotal.addAll(zbyq);
            azTotal.addAll(pdzzs);
            azTotal.addAll(wgbc);
            azTotal.addAll(kzzl);
            azTotal.addAll(zyd);
            azTotal.addAll(dljd);
            azTotal.addAll(txyd);
            azTotal.addAll(yzts);
            azTotal.addAll(fzsc);
            azTotal.add(azdx);
            azTotal.addAll(zwdy);
            azTotal.addAll(zwtx);
            //结算汇总
            List<Settlement> settlementBdzs = SettementUtil.getDLComparison(settlementsBdz,builtTotal,division,word1total,word2total,otherBdzTotal,feesBdzCount,cbperson);
            Boolean f = SettementUtil.getJgjsDeveloper(settlementsBdz);
            settlementService.save(settlementBdzs,id);
            for(JkxlDetailFee jkxlDetailFee:jzTotal){
                 double originalTotalCost = jkxlDetailFee.getOriginalTotalCost();
                 double originalFbrCost = jkxlDetailFee.getOriginalFbrCost();
                 double zjgcJzFee = jkxlDetailFee.getZjgcFee();
                 double rateCs = zjgcJzFee/zjgcJzTotal;  //措施费分摊比例
                 double rateQt = zjgcJzFee/zjgcTotal;    //其他费用分摊比例
                 double measuresFee1 = (zjgcJzFee -(originalFbrCost/(1+jxslJz)*jxslJz))*jzcsRate; //措施费一（1）
                 double measuresFee2 = 0.00;   //措施费一（2）
                 double measuresFee3 = csJzFee*rateCs; //措施费二
                 double otherFee =qtFees[2]*rateQt; //其他费用
                 double fees = (zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*(jzgfRate[0]+jzgfRate[1]*0.2);  //规费
                if(f){
                    tax = (originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee-originalFbrCost)*texRate; //税金
                    totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax-originalFbrCost; //合计
                } else {
                    tax = (originalTotalCost + measuresFee1 + measuresFee2 + measuresFee3 + fees + otherFee) * texRate; //税金
                    totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax; //合计
                }
                 jkxlDetailFee.setMeasuresFee1(measuresFee1);
                 jkxlDetailFee.setMeasuresFee2(measuresFee2);
                 jkxlDetailFee.setMeasuresFee3(measuresFee3);
                 jkxlDetailFee.setFees(fees);
                 jkxlDetailFee.setOtherCost(otherFee);
                 jkxlDetailFee.setTex(tax);
                 jkxlDetailFee.setTotalFee(totalfee);
                 jkxlDetailFee.setId(id);
            }
            //建筑部门求和
//            JkxlDetailFee jzDetail = jzTotal.get(0);
//            double jztotalFee =  jzDetail.getTotalFee()+tbrJzFee*(1+texRate);
//            double jztex =  jzDetail.getTex()+tbrJzFee*texRate;
//            jzDetail.setTotalFee(jztotalFee);
//            jzDetail.setTex(jztex);
//            jzTotal.set(0,jzDetail);


            for(JkxlDetailFee jkxlDetailFee:azTotal){
                double originalTotalCost = jkxlDetailFee.getOriginalTotalCost();
                double originalFbrCost = jkxlDetailFee.getOriginalFbrCost();
                double rgAzFee = jkxlDetailFee.getOriginalRgCost();
                double zjgcAzFee = jkxlDetailFee.getZjgcFee();
                double rateCs = zjgcAzFee/zjgcAzTotal;  //措施费分摊比例
                double rateQt = zjgcAzFee/zjgcTotal;    //其他费用分摊比例
                double rateRg = rgAzFee/rgAzTotal;  //人工费比例
//                double measuresFee1 = (zjgcAzFee-originalFbrCost/(1+jxslAz)*jxslAz)*azcsRate[0]+(rgAzFee/(1+tcRate[0])*azcsRate[1]); //措施费一（2）
                double measuresFee1 = (zjgcAzFee-originalFbrCost/(1+jxslAz)*jxslAz)*azcsRate[0]+azCsFee*rateRg; //措施费一（2）
                double measuresFee2 = 0.00;   //措施费一（2）
                double measuresFee3 = csAzFee*rateCs; //措施费二
                double otherFee =qtFees[2]*rateQt; //其他费用
//                double fees = rgAzFee/(1+tcRate[0])*azgfRate[0]+rgAzFee/(1+tcRate[0])*azgfRate[1]*1.6;  //规费
                double fees = azGf*rateRg;  //规费
                if(f){
                    tax = (originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee-originalFbrCost)*texRate; //税金
                    totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax-originalFbrCost; //合计
                } else {
                    tax = (originalTotalCost +measuresFee1 + measuresFee2 + measuresFee3 + fees + otherFee) * texRate; //税金
                    totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax; //合计
                }
                jkxlDetailFee.setMeasuresFee1(measuresFee1);
                jkxlDetailFee.setMeasuresFee2(measuresFee2);
                jkxlDetailFee.setMeasuresFee3(measuresFee3);
                jkxlDetailFee.setFees(fees);
                jkxlDetailFee.setOtherCost(otherFee);
                jkxlDetailFee.setTex(tax);
                jkxlDetailFee.setTotalFee(totalfee);
                jkxlDetailFee.setId(id);
            }
            //安装求和
//            JkxlDetailFee azDetail = azTotal.get(0);
//            double aztotalFee =  azDetail.getTotalFee()+tbrAzFee*(1+texRate);
//            azDetail.setTotalFee(aztotalFee);
//            azTotal.set(0,azDetail);

            //分摊承包人采购设备费
            if(cbrFee != 0.00){
                for (JkxlDetailFee jkxlDetailFee : azTotal) {
                    double total1 = jkxlDetailFee.getTotalFee();
                    double rate = jkxlDetailFee.getOriginalTotalCost()/ built2Total;
                    double tz = cbrFee*rate;
                    jkxlDetailFee.setTotalFee(total1+tz);
                }
            }

            //补税
            double texTotal = jzTotal.get(0).getTex()+azTotal.get(0).getTex();//税合计金额
            //其他费用补税
            if(qtFees[0]+qtFees[1] != 0.00) {
                for (JkxlDetailFee jkxlDetailFee : jzTotal) {
                    double tex = jkxlDetailFee.getTex();
                    double total1 = jkxlDetailFee.getTotalFee();
                    double rate = tex / texTotal;
                    double tzTex = (qtFees[0] + qtFees[1]) * rate * texRate;
                    jkxlDetailFee.setTex(tex + tzTex);
                    jkxlDetailFee.setTotalFee(total1 + tzTex);
                }
                for (JkxlDetailFee jkxlDetailFee : azTotal) {
                    double tex = jkxlDetailFee.getTex();
                    double total1 = jkxlDetailFee.getTotalFee();
                    double rate = tex / texTotal;
                    double tzTex = (qtFees[0] + qtFees[1]) * rate * texRate;
                    jkxlDetailFee.setTex(tex + tzTex);
                    jkxlDetailFee.setTotalFee(total1 + tzTex);
                }
            }
            //补税
            if(btex != 0.00){
                double tex1 = jzTotal.get(0).getTex()+azTotal.get(0).getTex();//税合计金额
                for(JkxlDetailFee jkxlDetailFee : jzTotal){
                    double tex = jkxlDetailFee.getTex();
                    double total1 = jkxlDetailFee.getTotalFee();
                    double rate = tex/tex1;
                    jkxlDetailFee.setTex(tex+btex*rate);
                    jkxlDetailFee.setTotalFee(total1+btex*rate);
                }
                for(JkxlDetailFee jkxlDetailFee : azTotal){
                    double tex = jkxlDetailFee.getTex();
                    double total1 = jkxlDetailFee.getTotalFee();
                    double rate = tex/tex1;
                    jkxlDetailFee.setTex(tex+btex*rate);
                    jkxlDetailFee.setTotalFee(total1+btex*rate);
                }
            }
            //分摊差额
            double cstotal1 = jzTotal.get(0).getMeasuresFee1()+azTotal.get(0).getMeasuresFee1();
            double cstotal2 = jzTotal.get(0).getMeasuresFee2()+azTotal.get(0).getMeasuresFee2();
            double cstotal3 = jzTotal.get(0).getMeasuresFee3()+azTotal.get(0).getMeasuresFee3();
            double gftotal = jzTotal.get(0).getFees()+azTotal.get(0).getFees();

            for(JkxlDetailFee jkxlDetailFee1 : jzTotal){
                double csftRate = 0.00;
                if(cstotal1 != 0.00) {
                    csftRate = jkxlDetailFee1.getMeasuresFee1() / cstotal1;
                }
                double gfRate = 0.00;
                if(gftotal != 0.00) {
                    gfRate = jkxlDetailFee1.getFees() / gftotal;
                }
                double csft = (jsCs-(cstotal1+cstotal2+cstotal3))*csftRate;
                double gfft = (jsGf-gftotal)*gfRate;
                double texft = (csft+gfft)*texRate;
                jkxlDetailFee1.setMeasuresFee1(jkxlDetailFee1.getMeasuresFee1()+csft);
                jkxlDetailFee1.setFees(jkxlDetailFee1.getFees()+gfft);
                jkxlDetailFee1.setTex(jkxlDetailFee1.getTex()+texft);
                jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee()+csft+gfft+texft);
            }
            for(JkxlDetailFee jkxlDetailFee1 : azTotal){
                double csftRate = 0.00;
                if(cstotal1 != 0.00) {
                    csftRate = jkxlDetailFee1.getMeasuresFee1() / cstotal1;
                }
                double gfRate = 0.00;
                if(gftotal != 0.00) {
                    gfRate = jkxlDetailFee1.getFees() / gftotal;
                }
                double csft = (jsCs-(cstotal1+cstotal2+cstotal3))*csftRate;
                double gfft = (jsGf-gftotal)*gfRate;
                double texft = (csft+gfft)*texRate;
                jkxlDetailFee1.setMeasuresFee1(jkxlDetailFee1.getMeasuresFee1()+csft);
                jkxlDetailFee1.setFees(jkxlDetailFee1.getFees()+gfft);
                jkxlDetailFee1.setTex(jkxlDetailFee1.getTex()+texft);
                jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee()+csft+gfft+texft);
            }
            //分摊税
            double texh = textotal+btex;//总税额
            double jktex = jzTotal.get(0).getTex()+azTotal.get(0).getTex();
            double texr = texh-jktex;
            for(JkxlDetailFee jkxlDetailFee : jzTotal){
                double texrate = jkxlDetailFee.getTex()/jktex;
                double tex = texr*texrate;
                jkxlDetailFee.setTex(jkxlDetailFee.getTex()+tex);
                jkxlDetailFee.setTotalFee(jkxlDetailFee.getTotalFee()+tex);

            }
            for(JkxlDetailFee jkxlDetailFee : azTotal){
                double texrate = jkxlDetailFee.getTex()/jktex;
                double tex = texr*texrate;
                jkxlDetailFee.setTex(jkxlDetailFee.getTex()+tex);
                jkxlDetailFee.setTotalFee(jkxlDetailFee.getTotalFee()+tex);

            }
            //分摊发包人
            if(f){
                double fbrfee = jzTotal.get(0).getOriginalFbrCost()+azTotal.get(0).getOriginalFbrCost();
                double cs = fbrFees-fbrfee;
                if(cs!=0.00) {
                    for (JkxlDetailFee jkxlDetailFee1 : jzTotal) {
                        double rate = jkxlDetailFee1.getOriginalFbrCost() / fbrfee;
                        jkxlDetailFee1.setOriginalFbrCost(jkxlDetailFee1.getOriginalFbrCost() + cs * rate);
                        jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee() - cs * rate);
                    }
                    for (JkxlDetailFee jkxlDetailFee1 : azTotal) {
                        double rate = jkxlDetailFee1.getOriginalFbrCost() / fbrfee;
                        jkxlDetailFee1.setOriginalFbrCost(jkxlDetailFee1.getOriginalFbrCost() + cs * rate);
                        jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee() - cs * rate);
                    }
                }

            }
            //计算差额
            double tz = jzTotal.get(0).getTotalFee()+azTotal.get(0).getTotalFee();//总合计金额

            if(wsbFees != null && wsbFees.size()>0){
                for(JkxlAdjustFee jkxlAdjustFee : wsbFees){
                    jkxlAdjustFee.setId(id);
                    feeAdjustService.save(jkxlAdjustFee);
                    double fee = jkxlAdjustFee.getFee();
                    if(jkxlAdjustFee.getType().contains("减") || jkxlAdjustFee.getType().contains("扣") || jkxlAdjustFee.getType().contains("下浮")){
                        fee = fee* -1;
                    }
                    wsbfee += fee;
                }
            }
            double gxFee = Math.abs(gcjsFee-tz-qtFees[0]-qtFees[1] -wsbfee);
            //将其他费用存入集合
            JkxlDetailFee jkxlDetailFee1 = new JkxlDetailFee();
            JkxlDetailFee jkxlDetailFee2 = new JkxlDetailFee();
            jkxlDetailFee1.setId(id);
            jkxlDetailFee1.setWbsCode("24120000");
            jkxlDetailFee1.setTotalFee(qtFees[0]);
            jkxlDetailFee2.setWbsCode("24220000");
            jkxlDetailFee2.setTotalFee(qtFees[1]);
            jkxlDetailFee2.setId(id);
            azTotal.add(jkxlDetailFee1);
            azTotal.add(jkxlDetailFee2);
            overheadLineMapper.saveList(jzTotal);
            overheadLineMapper.saveList(azTotal);
            JkxlAdjustFee jkxlAdjustFee = new JkxlAdjustFee();
            jkxlAdjustFee.setId(id);
            jkxlAdjustFee.setType(BashInfo.CE);
            jkxlAdjustFee.setFee(gxFee);
            feeAdjustService.save(jkxlAdjustFee);
        }
        if(jzgc !=0.00 && azgc == 0.00){
            ImportExcel jzqd = new ImportExcel(file, 1, BashInfo.SHEET_JZFBFXJJ,true);  //建筑分部分项工程量清单计价表
            ImportExcel jzhz = new ImportExcel(file, 1, SubstationInfo.SHEET_JZHZB,true);   //建筑工程费用汇总表
            //变电站结算汇总
            int[] comlumn2 = {12,15};//定义所要数组
            double built1Total = ExcelUtil.getDouble(jzhz,BashInfo.QT_HJ,0,2);//变电站建筑
            double[] builtTotal={built1Total,0.00};
            double[] division1total=ExcelUtil.getDoubleArray(jzqd,SubstationInfo.JZMX_BJZ,2,comlumn2);//建筑分部分项比对数据清单合计
            double[] division = {division1total[0],division1total[1],0.00,0.00};
            //结算汇总取值结束
            double zjgcJzTotal = ExcelUtil.getDouble(jzhz,SubstationInfo.QT_HJ,0,3);//建筑直接工程费合计
//            double csJzFee = SubstationInfo.getCsJzFee(csqd2);//建筑措施分摊费
//            double tbrJzFee = BashInfo.getFbrJzFee(tbrsb); //投标人建筑费
//            double[] aqwmJzFees = ExcelUtil.getDoubleArray(csqd1,SubstationInfo.CS_AQWM,1, new int[]{3, 4});  //建筑安全文明施工费
            double[] aqwmJzFees = BashInfo.getAqwfJzFee(csqd1);  //建筑安全文明施工费
            double fbrJzFee = ExcelUtil.getDouble(jzqd,SubstationInfo.JZMX_BJZ,2,15);
            double jxslJz = 0.00;
            if (fbrJzFee != 0.00) {
                    double zgJz = zjgcJzTotal - aqwmJzFees[1] / aqwmJzFees[0] * 100;
                    jxslJz = zgJz / (fbrJzFee - zgJz); //建筑进项税率
            }

            //建筑部分基础数据
            JkxlDetailFee jz = SubstationInfo.getTolalInfo(jzqd,jzhz,"21000000",SubstationInfo.JZMX_BJZ);  //建筑合计
            JkxlDetailFee jzsc = SubstationInfo.getBashInfo(jzqd,jzhz,"21100000",SubstationInfo.JZMX_ZYGC);  //主要生产工程
            List<JkxlDetailFee> scjz = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.ZYSCWBS,SubstationInfo.ZYSCJZSTR,"1"); //主要生产建筑
            List<JkxlDetailFee> pdzz = SubstationInfo.getPdzzjz(jzqd,jzhz);    //配电装置
            List<JkxlDetailFee> gsxt  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.GSXTWBS,SubstationInfo.GSXTSTR,"1"); //供水系统建筑
            List<JkxlDetailFee> xfxt  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.XFXTWBS,SubstationInfo.XFXTSTR,"1"); //消防系统
            JkxlDetailFee jzfz = SubstationInfo.getBashInfo(jzqd,jzhz,"21200000",SubstationInfo.JZMX_FZSCGC);  //辅助生产工程
            List<JkxlDetailFee> fzscjz  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.FZSCWBS,SubstationInfo.FZSCSTR,"2"); //辅助生产建筑
            List<JkxlDetailFee> zqxjz  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.ZQXWBS,SubstationInfo.ZQXSTR,"2"); //站区性建筑
            List<JkxlDetailFee> tsgzw  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.TXGJWWBS,SubstationInfo.TXGJWSTR,"2"); //特殊构筑物
            JkxlDetailFee zqlh = SubstationInfo.getBashInfo(jzqd,jzhz,"21240000",SubstationInfo.JZMX_ZQLH);  //站区绿化
            JkxlDetailFee ydxgc = SubstationInfo.getBashInfo(jzqd,jzhz,"21300000",SubstationInfo.JZMX_ZZDXGC);  //单项工程
            List<JkxlDetailFee> dxgc  = SubstationInfo.getDxInfo(jzqd,jzhz,SubstationInfo.YDZYGWBS,SubstationInfo.YDZYGSTR); //与站址有关的单项工程
            List<JkxlDetailFee> jzTotal = new ArrayList<>();
            jzTotal.add(jz);
            jzTotal.add(jzsc);
            jzTotal.addAll(scjz);
            jzTotal.addAll(pdzz);
            jzTotal.addAll(gsxt);
            jzTotal.addAll(xfxt);
            jzTotal.add(jzfz);
            jzTotal.addAll(fzscjz);
            jzTotal.addAll(zqxjz);
            jzTotal.addAll(tsgzw);
            jzTotal.add(zqlh);
            jzTotal.add(ydxgc);
            jzTotal.addAll(dxgc);
//            boolean flagJz = SubstationInfo.getFlag(jzTotal);
            //结算汇总保存
            List<Settlement> settlementBdzs = SettementUtil.getDLComparison(settlementsBdz,builtTotal,division,word1total,word2total,otherBdzTotal,feesBdzCount,cbperson);
            settlementService.save(settlementBdzs,id);
            Boolean f = SettementUtil.getJgjsDeveloper(settlementsBdz);
            for(JkxlDetailFee jkxlDetailFee:jzTotal){
                double originalTotalCost = jkxlDetailFee.getOriginalTotalCost();
                double originalFbrCost = jkxlDetailFee.getOriginalFbrCost();
                double zjgcJzFee = jkxlDetailFee.getZjgcFee();
                double rateCs = zjgcJzFee/zjgcJzTotal;  //措施费分摊比例
                double rateQt = zjgcJzFee/zjgcJzTotal;    //其他费用分摊比例
                double measuresFee1 = (zjgcJzFee -(originalFbrCost/(1+jxslJz)*jxslJz))*jzcsRate; //措施费一（1）
                double measuresFee2 = csJzFee*rateCs;   //措施费一（2）
                double measuresFee3 = 0.00; //措施费二
                double otherFee =qtFees[2]*rateQt; //其他费用
                double fees = (zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*(jzgfRate[0]+jzgfRate[1]*0.2);  //规费
                if(f){
                    tax = (originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee-originalFbrCost)*texRate; //税金
                    totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax-originalFbrCost; //合计
                } else {
                    tax = (originalTotalCost+measuresFee1 + measuresFee2 + measuresFee3 + fees + otherFee) * texRate; //税金
                    totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax; //合计
                }
                jkxlDetailFee.setMeasuresFee1(measuresFee1);
                jkxlDetailFee.setMeasuresFee2(measuresFee2);
                jkxlDetailFee.setMeasuresFee3(measuresFee3);
                jkxlDetailFee.setFees(fees);
                jkxlDetailFee.setOtherCost(otherFee);
                jkxlDetailFee.setTex(tax);
                jkxlDetailFee.setTotalFee(totalfee);
                jkxlDetailFee.setId(id);
            }
            //建筑部门求和
//            JkxlDetailFee jzDetail = jzTotal.get(0);
//            double jztotalFee =  jzDetail.getTotalFee()+tbrJzFee*(1+texRate);
//            double jztex =  jzDetail.getTex()+tbrJzFee*texRate;
//            jzDetail.setTotalFee(jztotalFee);
//            jzDetail.setTex(jztex);
//            jzTotal.set(0,jzDetail);

            //分摊承包人采购设备费
            if(cbrFee != 0.00){
                for (JkxlDetailFee jkxlDetailFee : jzTotal) {
                    double total1 = jkxlDetailFee.getTotalFee();
                    double rate = jkxlDetailFee.getOriginalTotalCost()/ built1Total;
                    double tz = cbrFee*rate;
                    jkxlDetailFee.setTotalFee(total1+tz);
                }
            }


            double jztexTotal = jzTotal.get(0).getTex();
            //其他费用补税
            if(qtFees[0]+qtFees[1] != 0.00) {
                for (JkxlDetailFee jkxlDetailFee : jzTotal) {
                    double tex = jkxlDetailFee.getTex();
                    double total1 = jkxlDetailFee.getTotalFee();
                    double rate = tex / jztexTotal;
                    double tzTex = (qtFees[0] + qtFees[1]) * rate * texRate;
                    jkxlDetailFee.setTex(tex + tzTex);
                    jkxlDetailFee.setTotalFee(total1 + tzTex);
                }
            }
            //补税
            if(btex != 0.00){
                double jztex1 = jzTotal.get(0).getTex();
                for(JkxlDetailFee jkxlDetailFee : jzTotal){
                    double tex = jkxlDetailFee.getTex();
                    double total1 = jkxlDetailFee.getTotalFee();
                    double rate = tex/jztex1;
                    jkxlDetailFee.setTex(tex+btex*rate);
                    jkxlDetailFee.setTotalFee(total1+btex*rate);
                }
            }
            //分摊差额
            double cstotal1 = jzTotal.get(0).getMeasuresFee1();
            double cstotal2 = jzTotal.get(0).getMeasuresFee2();
            double cstotal3 = jzTotal.get(0).getMeasuresFee3();
            double gftotal = jzTotal.get(0).getFees();

            for(JkxlDetailFee jkxlDetailFee1 : jzTotal){
                double csftRate = 0.00;
                if(cstotal1 != 0.00) {
                    csftRate = jkxlDetailFee1.getMeasuresFee1() / cstotal1;
                }
                double gfRate = 0.00;
                if(gftotal != 0.00) {
                    gfRate = jkxlDetailFee1.getFees() / gftotal;
                }
                double csft = (jsCs-(cstotal1+cstotal2+cstotal3))*csftRate;
                double gfft = (jsGf-gftotal)*gfRate;
                double texft = (csft+gfft)*texRate;
                jkxlDetailFee1.setMeasuresFee1(jkxlDetailFee1.getMeasuresFee1()+csft);
                jkxlDetailFee1.setFees(jkxlDetailFee1.getFees()+gfft);
                jkxlDetailFee1.setTex(jkxlDetailFee1.getTex()+texft);
                jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee()+csft+gfft+texft);
            }
            //分摊税
            double texh = textotal+btex;//总税额
            double jktex = jzTotal.get(0).getTex();
            double texr = texh-jktex;
            for(JkxlDetailFee jkxlDetailFee : jzTotal){
                double texrate = jkxlDetailFee.getTex()/jktex;
                double tex = texr*texrate;
                jkxlDetailFee.setTex(jkxlDetailFee.getTex()+tex);
                jkxlDetailFee.setTotalFee(jkxlDetailFee.getTotalFee()+tex);

            }
            //分摊发包人
            if(f){
                double fbrfee = jzTotal.get(0).getOriginalFbrCost();
                double cs = fbrFees-fbrfee;
                if(cs!=0.00) {
                    for (JkxlDetailFee jkxlDetailFee1 : jzTotal) {
                        double rate = jkxlDetailFee1.getOriginalFbrCost() / fbrfee;
                        jkxlDetailFee1.setOriginalFbrCost(jkxlDetailFee1.getOriginalFbrCost() + cs * rate);
                        jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee() - cs * rate);
                    }
                }

            }
            double tz = jzTotal.get(0).getTotalFee();//总合计金额
            if(wsbFees != null && wsbFees.size()>0){
                for(JkxlAdjustFee jkxlAdjustFee : wsbFees){
                    jkxlAdjustFee.setId(id);
                    feeAdjustService.save(jkxlAdjustFee);
                    double fee = jkxlAdjustFee.getFee();
                    if(jkxlAdjustFee.getType().contains("减") || jkxlAdjustFee.getType().contains("扣") || jkxlAdjustFee.getType().contains("下浮")){
                        fee = fee* -1;
                    }
                    wsbfee += fee;
                }
            }
            double gxFee = Math.abs(gcjsFee-tz-qtFees[0]-qtFees[1]-wsbfee);
            //将其他费用存入集合
            JkxlDetailFee jkxlDetailFee1 = new JkxlDetailFee();
            JkxlDetailFee jkxlDetailFee2 = new JkxlDetailFee();
            jkxlDetailFee1.setId(id);
            jkxlDetailFee1.setWbsCode("24120000");
            jkxlDetailFee1.setTotalFee(qtFees[0]);
            jkxlDetailFee2.setWbsCode("24220000");
            jkxlDetailFee2.setTotalFee(qtFees[1]);
            jkxlDetailFee2.setId(id);
            jzTotal.add(jkxlDetailFee1);
            jzTotal.add(jkxlDetailFee2);
            overheadLineMapper.saveList(jzTotal);
            JkxlAdjustFee jkxlAdjustFee = new JkxlAdjustFee();
            jkxlAdjustFee.setId(id);
            jkxlAdjustFee.setType(BashInfo.CE);
            jkxlAdjustFee.setFee(gxFee);
            feeAdjustService.save(jkxlAdjustFee);
        }
        if(jzgc ==0.00 && azgc != 0.00){
            ImportExcel azqd = new ImportExcel(file, 1, BashInfo.SHEET_AZFBFXJJ,true);  //安装分部分项工程量清单计价表
            ImportExcel azhz = new ImportExcel(file, 1, SubstationInfo.SHEET_HZHZB,true);   //安装工程费用汇总表
            //变电站结算汇总
            int[] comlumn2 = {12,15};//定义所要数组
            double built2Total = ExcelUtil.getDouble(azhz,BashInfo.QT_HJ,0,2);//变电站安装
            double[] builtTotal={0.00,built2Total};
            double[] division2total=ExcelUtil.getDoubleArray(azqd,SubstationInfo.AZMX_BDZAZ,2,comlumn2);//安装分部分项比对数据清单合计
            double[] division = {0.00,0.00,division2total[0],division2total[1]};
            //结算汇总取值结束
            double zjgcAzTotal = ExcelUtil.getDouble(azhz,SubstationInfo.QT_HJ,0,3);//安装直接工程费合计
            double rgAzTotal = ExcelUtil.getDouble(azhz,SubstationInfo.QT_HJ,0,4);//安装人工费合计
            double[] aqwmAzFees = BashInfo.getAqwfAzFee(csqd1);  //安装安全文明施工费
            double fbrAzFee = ExcelUtil.getDouble(azqd,SubstationInfo.AZMX_BDZAZ,2,15);
            double jxslAz = 0.00;
            if (fbrAzFee != 0.00) {
                    double zgAz = zjgcAzTotal - aqwmAzFees[1] / aqwmAzFees[0] * 100;
                    jxslAz = zgAz / (fbrAzFee - zgAz); //安装进项税率
            }
            //安装部分基础数据
            JkxlDetailFee az = SubstationInfo.getTolalInfo(azqd,azhz,"23000000",SubstationInfo.AZMX_BDZAZ);  //安装合计
            JkxlDetailFee azsc = SubstationInfo.getBashInfo(azqd,azhz,"23100000",SubstationInfo.AZMX_ZYSCGC);  //主要生产工程
            List<JkxlDetailFee> zbyq  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.ZBYQWBS,SubstationInfo.ZBYQSTR); //主变压器系统
            List<JkxlDetailFee> pdzzs  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.PDZZWBS,SubstationInfo.PDZZSTR); //配电装置
            List<JkxlDetailFee> wgbc  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.WGBCWBS,SubstationInfo.WGBCSTR); //无功补偿
            List<JkxlDetailFee> kzzl = SubstationInfo.getKzzl(azqd,azhz);   //控制直流
            List<JkxlDetailFee> zyd  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.ZYDWBS,SubstationInfo.ZYDSTR); //站用电系统
            List<JkxlDetailFee> dljd = SubstationInfo.getDljd(azqd,azhz);    //电缆接地
            List<JkxlDetailFee> txyd  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.TXYDWBS,SubstationInfo.TXYDSTR); //通信及远动系统
            List<JkxlDetailFee> yzts  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.QZTSWBS,SubstationInfo.QZTSSTR); //全站调试
            List<JkxlDetailFee> fzsc  = SubstationInfo.getBashInfo(azqd,azhz,SubstationInfo.AFZSCWBS,SubstationInfo.AFZSCSTR); //辅助生产工程
            JkxlDetailFee azdx = SubstationInfo.getBashInfo(azqd,azhz,"23300000",SubstationInfo.AZMX_ZZDXGC);  //与站址有关的单项工程
            List<JkxlDetailFee> zwdy  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.ZWDYWBS,SubstationInfo.ZWDYSTR); //站外电源
            List<JkxlDetailFee> zwtx  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.ZWTXWBS,SubstationInfo.ZWTXSTR); //站外通信
            List<JkxlDetailFee> azTotal = new ArrayList<>();
            azTotal.add(az);
            azTotal.add(azsc);
            azTotal.addAll(zbyq);
            azTotal.addAll(pdzzs);
            azTotal.addAll(wgbc);
            azTotal.addAll(kzzl);
            azTotal.addAll(zyd);
            azTotal.addAll(dljd);
            azTotal.addAll(txyd);
            azTotal.addAll(yzts);
            azTotal.addAll(fzsc);
            azTotal.add(azdx);
            azTotal.addAll(zwdy);
            azTotal.addAll(zwtx);
            List<Settlement> settlementBdzs = SettementUtil.getDLComparison(settlementsBdz,builtTotal,division,word1total,word2total,otherBdzTotal,feesBdzCount,cbperson);
            settlementService.save(settlementBdzs,id);
            Boolean f = SettementUtil.getJgjsDeveloper(settlementsBdz);
            for(JkxlDetailFee jkxlDetailFee:azTotal){
                double originalTotalCost = jkxlDetailFee.getOriginalTotalCost();
                double originalFbrCost = jkxlDetailFee.getOriginalFbrCost();
                double rgAzFee = jkxlDetailFee.getOriginalRgCost();
                double zjgcAzFee = jkxlDetailFee.getZjgcFee();
                double rateCs = zjgcAzFee/zjgcAzTotal;  //措施费分摊比例
                double rateQt = zjgcAzFee/zjgcAzTotal;    //其他费用分摊比例
                double rateRg = rgAzFee/rgAzTotal;   //人工费比例
//                double measuresFee1 = (zjgcAzFee-originalFbrCost/(1+jxslAz)*jxslAz)*azcsRate[0]+(rgAzFee/(1+tcRate[0])*azcsRate[1]); //措施费一（2）
                double measuresFee1 = (zjgcAzFee-originalFbrCost/(1+jxslAz)*jxslAz)*azcsRate[0]+azCsFee*rateRg; //措施费一（1）
                double measuresFee2 = 0.00;   //措施费一（2）
                double measuresFee3 = csAzFee*rateCs;   //措施费二
                double otherFee =qtFees[2]*rateQt; //其他费用
//                double fees = rgAzFee/(1+tcRate[0])*azgfRate[0]+rgAzFee/(1+tcRate[0])*azgfRate[1]*1.6;  //规费
                double fees = azGf*rateRg;  //规费
                if(f){
                    tax = (originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee-originalFbrCost)*texRate; //税金
                    totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax-originalFbrCost; //合计
                } else {
                    tax = (originalTotalCost +measuresFee1 + measuresFee2 + measuresFee3 + fees + otherFee) * texRate; //税金
                    totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax; //合计
                }
                jkxlDetailFee.setMeasuresFee1(measuresFee1);
                jkxlDetailFee.setMeasuresFee2(measuresFee2);
                jkxlDetailFee.setMeasuresFee3(measuresFee3);
                jkxlDetailFee.setFees(fees);
                jkxlDetailFee.setOtherCost(otherFee);
                jkxlDetailFee.setTex(tax);
                jkxlDetailFee.setTotalFee(totalfee);
                jkxlDetailFee.setId(id);
            }
            //安装求和
//            JkxlDetailFee azDetail = azTotal.get(0);
//            double aztotalFee =  azDetail.getTotalFee()+tbrAzFee*(1+texRate);
//            azDetail.setTotalFee(aztotalFee);
//            azTotal.set(0,azDetail);

            //分摊承包人采购设备费
            if(cbrFee != 0.00){
                for (JkxlDetailFee jkxlDetailFee : azTotal) {
                    double total1 = jkxlDetailFee.getTotalFee();
                    double rate = jkxlDetailFee.getOriginalTotalCost()/ built2Total;
                    double tz = cbrFee*rate;
                    jkxlDetailFee.setTotalFee(total1+tz);
                }
            }

            //补税
            double aztexTotal = azTotal.get(0).getTex();
            //其他费用补税
            if(qtFees[0]+qtFees[1] != 0.00) {
                for (JkxlDetailFee jkxlDetailFee : azTotal) {
                    double tex = jkxlDetailFee.getTex();
                    double total1 = jkxlDetailFee.getTotalFee();
                    double rate = tex / aztexTotal;
                    double tzTex = (qtFees[0] + qtFees[1]) * rate * texRate;
                    jkxlDetailFee.setTex(tex + tzTex);
                    jkxlDetailFee.setTotalFee(total1 + tzTex);
                }
            }
            //补税
            if(btex != 0.00){
                double tex1 = azTotal.get(0).getTex();
                for(JkxlDetailFee jkxlDetailFee : azTotal){
                    double tex = jkxlDetailFee.getTex();
                    double total1 = jkxlDetailFee.getTotalFee();
                    double rate = tex/tex1;
                    jkxlDetailFee.setTex(tex+btex*rate);
                    jkxlDetailFee.setTotalFee(total1+btex*rate);
                }
            }
            //分摊差额
            double cstotal1 = azTotal.get(0).getMeasuresFee1();
            double cstotal2 = azTotal.get(0).getMeasuresFee2();
            double cstotal3 = azTotal.get(0).getMeasuresFee3();
            double gftotal = azTotal.get(0).getFees();

            for(JkxlDetailFee jkxlDetailFee1 : azTotal){
                double csftRate = 0.00;
                if(cstotal1 != 0.00) {
                    csftRate = jkxlDetailFee1.getMeasuresFee1() / cstotal1;
                }
                double gfRate = 0.00;
                if(gftotal != 0.00) {
                    gfRate = jkxlDetailFee1.getFees() / gftotal;
                }
                double csft = (jsCs-(cstotal1+cstotal2+cstotal3))*csftRate;
                double gfft = (jsGf-gftotal)*gfRate;
                double texft = (csft+gfft)*texRate;
                jkxlDetailFee1.setMeasuresFee1(jkxlDetailFee1.getMeasuresFee1()+csft);
                jkxlDetailFee1.setFees(jkxlDetailFee1.getFees()+gfft);
                jkxlDetailFee1.setTex(jkxlDetailFee1.getTex()+texft);
                jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee()+csft+gfft+texft);
            }
            //分摊税
            double texh = textotal+btex;//总税额
            double jktex = azTotal.get(0).getTex();
            double texr = texh-jktex;
            for(JkxlDetailFee jkxlDetailFee : azTotal){
                double texrate = jkxlDetailFee.getTex()/jktex;
                double tex = texr*texrate;
                jkxlDetailFee.setTex(jkxlDetailFee.getTex()+tex);
                jkxlDetailFee.setTotalFee(jkxlDetailFee.getTotalFee()+tex);
            }
            if(f){
                double fbrfee = azTotal.get(0).getOriginalFbrCost();
                double cs = fbrFees-fbrfee;
                if(cs!=0.00) {
                    for (JkxlDetailFee jkxlDetailFee1 : azTotal) {
                        double rate = jkxlDetailFee1.getOriginalFbrCost() / fbrfee;
                        jkxlDetailFee1.setOriginalFbrCost(jkxlDetailFee1.getOriginalFbrCost() + cs * rate);
                        jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee() - cs * rate);
                    }
                }

            }
            double tz = azTotal.get(0).getTotalFee();//总合计金额

            if(wsbFees != null && wsbFees.size()>0){
                for(JkxlAdjustFee jkxlAdjustFee : wsbFees){
                    jkxlAdjustFee.setId(id);
                    feeAdjustService.save(jkxlAdjustFee);
                    double fee = jkxlAdjustFee.getFee();
                    if(jkxlAdjustFee.getType().contains("减") || jkxlAdjustFee.getType().contains("扣") || jkxlAdjustFee.getType().contains("下浮")){
                        fee = fee* -1;
                    }
                    wsbfee += fee;
                }
            }
            double gxFee = Math.abs(gcjsFee-tz-qtFees[0]-qtFees[1]-wsbfee);
            //将其他费用存入集合
            JkxlDetailFee jkxlDetailFee1 = new JkxlDetailFee();
            JkxlDetailFee jkxlDetailFee2 = new JkxlDetailFee();
            jkxlDetailFee1.setId(id);
            jkxlDetailFee1.setWbsCode("24120000");
            jkxlDetailFee1.setTotalFee(qtFees[0]);
            jkxlDetailFee2.setWbsCode("24220000");
            jkxlDetailFee2.setTotalFee(qtFees[1]);
            jkxlDetailFee2.setId(id);
            azTotal.add(jkxlDetailFee1);
            azTotal.add(jkxlDetailFee2);
            overheadLineMapper.saveList(azTotal);
            JkxlAdjustFee jkxlAdjustFee = new JkxlAdjustFee();
            jkxlAdjustFee.setId(id);
            jkxlAdjustFee.setType(BashInfo.CE);
            jkxlAdjustFee.setFee(gxFee);
            feeAdjustService.save(jkxlAdjustFee);
        }
        if(jzgc ==0.00 && azgc == 0.00){
            throw new RuntimeException(BashInfo.NUMBERERROR);
        }

    }








}
