package com.jeeplus.modules.sg.overheadline.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.util.BashInfo;
import com.jeeplus.modules.sg.overheadline.util.DoubleUtil;
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.commons.lang3.ArrayUtils;
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.*;

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




   public List<JkxlDetailFee> getList(String id,String type){
       return overheadLineMapper.getList(id,type);
   }

    /**
     * 获取导出数据
     * @param id
     * @param type
     * @return
     */
    public List<JkxlDetailFee> findList(String id,String type){
        List<JkxlDetailFee> list = overheadLineMapper.findList(id,type);
        double integrityFee = 0.00; //诚信扣款
        double decFund = 0.00;//甲供材超供抵扣款
        double texDeal = 0.00;//税金处理
        double totalDeal = 0.00;//需处理税金与甲供材抵扣款和
    //    WbsItem wbsItem = itemService.getState(id);
        WbsItem wbsItem = itemService.get(id);
        if(wbsItem != null){
            if(wbsItem.getIntegrityFee() != null){
                integrityFee = wbsItem.getIntegrityFee() * -1;
            }
            if(wbsItem.getDecFund() != null){
                decFund = wbsItem.getDecFund();
            }
            if(wbsItem.getTexDeal() != null){
                texDeal = wbsItem.getTexDeal();
            }
            totalDeal = decFund + texDeal;
        }

//        根据节点编码特征判断阶段费用类型为“建筑费”、“安装费”或者“其他费用”，并设置
        for(JkxlDetailFee jkxlDetailFee : list){
            double totalFee  = jkxlDetailFee.getTotalFee()!=null? jkxlDetailFee.getTotalFee():0.00;
            //建筑
            if(jkxlDetailFee.getWbsCode().substring(0,2).equals("21") || jkxlDetailFee.getWbsCode().substring(0,3).equals("491")){
                jkxlDetailFee.setJzFee(totalFee);
            }
            //安装
            if(jkxlDetailFee.getWbsCode().substring(0,2).equals("23") || jkxlDetailFee.getWbsCode().substring(0,3).equals("493") || jkxlDetailFee.getWbsCode().substring(0,2).equals("39")){
                jkxlDetailFee.setAzFee(totalFee);
            }
            //其他费用
            if(jkxlDetailFee.getTotalFee() != null && jkxlDetailFee.getFeeType().equals("0")){
                double qtFee = jkxlDetailFee.getTotalFee()!=null? jkxlDetailFee.getTotalFee():0.00;
                jkxlDetailFee.setQtFee(qtFee);
            }

        }
        double jz1 = 0.00;
        double az1 = 0.00;
        double jz2 = 0.00;
        double az2 = 0.00;
        double qtTotal = 0.00;
        //变电站导出
        if(type.equals(BashInfo.PROJECT_BDZ)) {
//            根据totalFee进行分摊，分摊至最后剩下的 找一个最低层级wbs扣减，并逐层扣减上级wbs
            String lastShareWbs = null;
            for (JkxlDetailFee jkxlDetailFee : list) {
                Double totalFee = jkxlDetailFee.getTotalFee();
                //建筑费和
                if (jkxlDetailFee.getWbsCode().equals("21000000")) {
                    jz1 = totalFee!=null? jkxlDetailFee.getTotalFee():0.00;
                    jz2 = jkxlDetailFee.getJzFee()!=null? jkxlDetailFee.getJzFee():0.00;
                }
                //安装费和
                if (jkxlDetailFee.getWbsCode().equals("23000000")) {
                    az1 = totalFee!=null? jkxlDetailFee.getTotalFee():0.00;
                    az2 = jkxlDetailFee.getAzFee()!=null? jkxlDetailFee.getAzFee():0.00;
                }
                //其他费用和
                if (jkxlDetailFee.getFeeType().equals("0")&& totalFee != null) {
                    qtTotal += totalFee;
                }
//                wbs简明表示8位，最低层次wbs最后两位为0
                String six = jkxlDetailFee.getWbsCode().substring(5,6);
                if(lastShareWbs==null && jkxlDetailFee.getFeeType().equals("1")&&!six.equals("0")&&totalFee!=null&&totalFee!=0.00){
                    lastShareWbs = jkxlDetailFee.getWbsCode();
                }

            }

            if(totalDeal!=0.00){
                double totalJzShare = DoubleUtil.round2(totalDeal*jz1/(jz1+az1));
                double totalAzShare = DoubleUtil.round2(totalDeal*az1/(jz1+az1));
                //实际分摊金额与需分摊金额是否有差额
                double diff = DoubleUtil.sub(totalDeal,(DoubleUtil.add(totalJzShare,totalAzShare)));
                //实际分摊与需分摊存在差额情况下 将差额分配的wbs节点及其父节点
                String wbs1 = null;
                String wbs2 = null;
                String wbs3 = null;
                String wbs4 = null;
                if(diff != 0.00&&lastShareWbs!=null){
                    wbs1 = lastShareWbs.substring(0,2)+"000000";
                    wbs2 = lastShareWbs.substring(0,3)+"00000";
                    wbs3 = lastShareWbs.substring(0,4)+"0000";
                    wbs4 = lastShareWbs.substring(0,5)+"000";
                }
                //根据合计分摊至本体部分 看代码jz1=jz2 az1=az2不知道为什么要分开。
                for(JkxlDetailFee jkxlDetailFee:list){
                    String wbs = jkxlDetailFee.getWbsCode();
                    Double totalFee = jkxlDetailFee.getTotalFee();
                    if(wbs.startsWith("21")&&totalFee!=null&&totalFee!=0.00){
                        double share = DoubleUtil.round2(totalDeal*totalFee/(jz1+az1));
                        jkxlDetailFee.setTotalFee(DoubleUtil.sub(totalFee,share));
                        jkxlDetailFee.setJzFee((DoubleUtil.sub(totalFee,share)));
                    }
                    if(wbs.startsWith("23")&&totalFee!=null&&totalFee!=0.00){
                        double share = DoubleUtil.round2(totalDeal*totalFee/(jz1+az1));
                        jkxlDetailFee.setTotalFee(DoubleUtil.sub(totalFee,share));
                        jkxlDetailFee.setAzFee(DoubleUtil.sub(totalFee,share));
                    }
                    if(diff!=0.00&&lastShareWbs!=null){
                        if(wbs.equals(wbs1)||wbs.equals(wbs2)||wbs.equals(wbs3)||wbs.equals(wbs4)||wbs.equals(lastShareWbs)){
                            jkxlDetailFee.setTotalFee(DoubleUtil.sub(jkxlDetailFee.getTotalFee(),diff));
                            if(lastShareWbs.startsWith("21")){
                                jkxlDetailFee.setJzFee(DoubleUtil.sub(jkxlDetailFee.getJzFee(),diff));
                            }else{
                                jkxlDetailFee.setAzFee(DoubleUtil.sub(jkxlDetailFee.getAzFee(),diff));
                            }
                        }

                    }else if(diff!=0.00&& lastShareWbs==null){
                        if(jz1>0.00){
                            lastShareWbs = "21000000";
                        }else{
                            lastShareWbs = "23000000";
                        }
                    }

                }
                if(lastShareWbs.startsWith("21")){
                    totalJzShare = DoubleUtil.add(totalJzShare,diff);
                }else{
                    totalAzShare = DoubleUtil.add(totalAzShare,diff);
                }
                jz1 = DoubleUtil.sub(jz1,totalJzShare);
                jz2 = DoubleUtil.sub(jz2,totalJzShare);
                az1 = DoubleUtil.sub(az1,totalAzShare);
                az2 = DoubleUtil.sub(az2,totalAzShare);
            }


            qtTotal = qtTotal + integrityFee;
            for (JkxlDetailFee jkxlDetailFee : list) {
                //WBS首层数据
                if(jkxlDetailFee.getWbsCode().equals("20000000")){
                    jkxlDetailFee.setJzFee(jz2);
                    jkxlDetailFee.setAzFee(az2);
                    jkxlDetailFee.setTotalFee(jz1+az1+qtTotal);
                    jkxlDetailFee.setQtFee(qtTotal);
                }
                //WBS变电站合计
                if(jkxlDetailFee.getWbsCode().equals("10000000")){
                    jkxlDetailFee.setJzFee(jz2);
                    jkxlDetailFee.setAzFee(az2);
                    jkxlDetailFee.setTotalFee(jz1+az1+qtTotal);
                    jkxlDetailFee.setQtFee(qtTotal);
                }
                //WBS变电站其他费用合计
                if(jkxlDetailFee.getWbsCode().equals("24000000")){
                    jkxlDetailFee.setTotalFee(qtTotal);
                    jkxlDetailFee.setQtFee(qtTotal);
                }
                //WBS变电站工程结算审核费（诚信扣款）
                if(jkxlDetailFee.getWbsCode().equals("24260000")){
                    jkxlDetailFee.setTotalFee(integrityFee);
                    jkxlDetailFee.setQtFee(integrityFee);
                }
            }
        }
        //电缆导出
        if(type.equals(BashInfo.PROJECT_DLXL)){
            for (JkxlDetailFee jkxlDetailFee : list) {
                String wbs = jkxlDetailFee.getWbsCode().substring(0,3);
                String wbs1 = jkxlDetailFee.getWbsCode().substring(3,4);
                //建筑和
                if(wbs.equals("491") && !wbs1.equals("0")){
                    jz1 += jkxlDetailFee.getTotalFee()!=null? jkxlDetailFee.getTotalFee():0.00;
                    jz2 += jkxlDetailFee.getJzFee()!=null? jkxlDetailFee.getJzFee():0.00;
                }
                //安装和
                if(wbs.equals("493") && !wbs1.equals("0")){
                    az1 += jkxlDetailFee.getTotalFee()!=null? jkxlDetailFee.getTotalFee():0.00;
                    az2 += jkxlDetailFee.getAzFee()!=null? jkxlDetailFee.getAzFee():0.00;
                }
                //其他费用和
                if (jkxlDetailFee.getFeeType().equals("0")&& jkxlDetailFee.getTotalFee() != null) {
                    qtTotal += jkxlDetailFee.getTotalFee();
                }
            }

            //处理税金和甲供材超供抵扣款
            if(totalDeal!=0.00){
                double totalJzShare = 0.00;
                double totalAzShare = 0.00;
                //根据合计分摊至本体部分 看代码jz1=jz2 az1=az2不知道为什么要分开。
                for(JkxlDetailFee jkxlDetailFee:list){
                    String wbs = jkxlDetailFee.getWbsCode().substring(0,3);
                    String wbs1 = jkxlDetailFee.getWbsCode().substring(3,4);
                    Double totalFee = jkxlDetailFee.getTotalFee();
                    if(wbs.equals("491") && !wbs1.equals("0")&&totalFee!=null&&totalFee!=0.00){
                        double share = DoubleUtil.round2(totalDeal*totalFee/(jz1+az1));
                        jkxlDetailFee.setTotalFee(DoubleUtil.sub(totalFee,share));
                        jkxlDetailFee.setJzFee((DoubleUtil.sub(totalFee,share)));
                        totalJzShare = DoubleUtil.add(totalJzShare,share);
                    }
                    if(wbs.equals("493") && !wbs1.equals("0")&&totalFee!=null&&totalFee!=0.00){
                        double share = DoubleUtil.round2(totalDeal*totalFee/(jz1+az1));
                        jkxlDetailFee.setTotalFee(DoubleUtil.sub(totalFee,share));
                        jkxlDetailFee.setAzFee(DoubleUtil.sub(totalFee,share));
                        totalAzShare = DoubleUtil.add(totalAzShare,share);
                    }

                }
                //将分摊后的差额放入第一个分部分项
                if(totalDeal!=(totalJzShare+totalAzShare)){
                    for(JkxlDetailFee jkxlDetailFee:list){
                        String wbs = jkxlDetailFee.getWbsCode().substring(0,3);
                        String wbs1 = jkxlDetailFee.getWbsCode().substring(3,4);
                        Double totalFee = jkxlDetailFee.getTotalFee();
                        if(wbs.equals("491") && !wbs1.equals("0")&&totalFee!=null&&totalFee!=0.00){
                            jkxlDetailFee.setTotalFee(DoubleUtil.sub(totalFee,DoubleUtil.sub(totalDeal,(DoubleUtil.add(totalJzShare,totalAzShare)))));
                            jkxlDetailFee.setJzFee(DoubleUtil.sub(totalFee,DoubleUtil.sub(totalDeal,(DoubleUtil.add(totalJzShare,totalAzShare)))));
                            totalJzShare = DoubleUtil.add(totalJzShare,DoubleUtil.sub(totalDeal,(DoubleUtil.add(totalJzShare,totalAzShare))));
                            break;
                        }
                        if(wbs.equals("493") && !wbs1.equals("0")&&totalFee!=null&&totalFee!=0.00){
                            jkxlDetailFee.setTotalFee(DoubleUtil.sub(totalFee,DoubleUtil.sub(totalDeal,(DoubleUtil.add(totalJzShare,totalAzShare)))));
                            jkxlDetailFee.setAzFee(DoubleUtil.sub(totalFee,DoubleUtil.sub(totalDeal,(DoubleUtil.add(totalJzShare,totalAzShare)))));
                            totalAzShare = DoubleUtil.add(totalAzShare,DoubleUtil.sub(totalDeal,(DoubleUtil.add(totalJzShare,totalAzShare))));
                            break;
                        }

                    }
                }
                jz1 = DoubleUtil.sub(jz1,totalJzShare);
                jz2 = DoubleUtil.sub(jz2,totalJzShare);
                az1 = DoubleUtil.sub(az1,totalAzShare);
                az2 = DoubleUtil.sub(az2,totalAzShare);
            }

            //诚信扣款
            qtTotal = qtTotal + integrityFee;
            for (JkxlDetailFee jkxlDetailFee : list) {
                //WBS电缆和
                if(jkxlDetailFee.getWbsCode().equals("40000000")){
                    jkxlDetailFee.setJzFee(jz2);
                    jkxlDetailFee.setAzFee(az2);
                    jkxlDetailFee.setTotalFee(jz1+az1+qtTotal);
                    jkxlDetailFee.setQtFee(qtTotal);
                }
                //WBS首层
                if(jkxlDetailFee.getWbsCode().equals("10000000")){
                    jkxlDetailFee.setJzFee(jz2);
                    jkxlDetailFee.setAzFee(az2);
                    jkxlDetailFee.setTotalFee(jz1+az1+qtTotal);
                    jkxlDetailFee.setQtFee(qtTotal);
                }
                //WBS电缆建筑
                if(jkxlDetailFee.getWbsCode().equals("49100000")){
                    jkxlDetailFee.setTotalFee(jz1);
                    jkxlDetailFee.setJzFee(jz2);
                }
                //WBS电缆安装
                if(jkxlDetailFee.getWbsCode().equals("49300000")){
                    jkxlDetailFee.setTotalFee(az1);
                    jkxlDetailFee.setAzFee(az2);

                }
                //WBS电缆本体工程
                if(jkxlDetailFee.getWbsCode().equals("49000000")){
                    jkxlDetailFee.setJzFee(jz2);
                    jkxlDetailFee.setAzFee(az2);
                    jkxlDetailFee.setTotalFee(jz1+az1);
                }
                //WBS电缆其他费用
                if(jkxlDetailFee.getWbsCode().equals("44000000")){
                    jkxlDetailFee.setTotalFee(qtTotal);
                    jkxlDetailFee.setQtFee(qtTotal);
                }
                //诚信扣款
                if(jkxlDetailFee.getWbsCode().equals("44260000")){
                    jkxlDetailFee.setTotalFee(integrityFee);
                    jkxlDetailFee.setQtFee(integrityFee);
                }
            }

        }
        //架空导出
        if(type.equals(BashInfo.PROJECT_JKXL)) {
            for (JkxlDetailFee jkxlDetailFee : list) {
                //本体费用求和
                if (jkxlDetailFee.getFeeType().equals("1")&& jkxlDetailFee.getTotalFee() != null) {
                    az1 += jkxlDetailFee.getTotalFee();
                }
                //其他费用求和
                if (jkxlDetailFee.getFeeType().equals("0")&& jkxlDetailFee.getTotalFee() != null) {
                    qtTotal += jkxlDetailFee.getTotalFee();
                }
            }

            //处理税金和甲供材超供抵扣款
            if(totalDeal!=0.00){
                double totalShare = 0.00;
                //根据合计分摊至本体部分
                for(JkxlDetailFee jkxlDetailFee:list){
                    Double totalFee = jkxlDetailFee.getTotalFee();
                    if(jkxlDetailFee.getFeeType().equals("1") && totalFee != null && totalFee!=0.00){
                        double share = DoubleUtil.round2(totalDeal*totalFee/az1);
                        jkxlDetailFee.setTotalFee(DoubleUtil.sub(totalFee,share));
                        jkxlDetailFee.setAzFee(DoubleUtil.sub(totalFee,share));
                        totalShare = DoubleUtil.add(totalShare,share);
                    }
                }
                //将分摊后的差额放入第一个分部分项
                if(totalDeal!=totalShare){
                    for(JkxlDetailFee jkxlDetailFee:list){
                        Double totalFee = jkxlDetailFee.getTotalFee();
                        if(jkxlDetailFee.getFeeType().equals("1") && totalFee != null && totalFee!=0.00){
                            jkxlDetailFee.setTotalFee(DoubleUtil.sub(totalFee,DoubleUtil.sub(totalDeal,totalShare)));
                            jkxlDetailFee.setAzFee(DoubleUtil.sub(totalFee,DoubleUtil.sub(totalDeal,totalShare)));
                            break;
                        }
                    }
                }
                az1 = DoubleUtil.sub(az1,totalDeal);
            }

            qtTotal = qtTotal + integrityFee;
            for (JkxlDetailFee jkxlDetailFee : list) {
                //WBS架空合计
                if(jkxlDetailFee.getWbsCode().equals("30000000")){
                    jkxlDetailFee.setTotalFee(az1+qtTotal);
                    jkxlDetailFee.setAzFee(az1);
                    jkxlDetailFee.setQtFee(qtTotal);
                }
                //WBS首层
                if(jkxlDetailFee.getWbsCode().equals("10000000")){
                    jkxlDetailFee.setTotalFee(az1+qtTotal);
                    jkxlDetailFee.setAzFee(az1);
                    jkxlDetailFee.setQtFee(qtTotal);
                }
                //WBS架空本体工程
                if(jkxlDetailFee.getWbsCode().equals("39000000")){
                    jkxlDetailFee.setTotalFee(az1);
                    jkxlDetailFee.setAzFee(az1);
                }
                //WBS架空其他费用
                if(jkxlDetailFee.getWbsCode().equals("34000000")){
                    jkxlDetailFee.setTotalFee(qtTotal);
                    jkxlDetailFee.setQtFee(qtTotal);
                }
                //WBS架空诚信扣款
                if(jkxlDetailFee.getWbsCode().equals("34260000")){
                    jkxlDetailFee.setTotalFee(integrityFee);
                    jkxlDetailFee.setQtFee(integrityFee);
                }
            }
        }
        return list;

    }

   public JkxlDetailFee getDate(String id, String wbsCode){
       return overheadLineMapper.getData(id,wbsCode);
   }

   public List<JkxlDetailFee> getAdjust(String id){
       return overheadLineMapper.getAdjust(id);
   }


    @Transactional(readOnly = false)
    public void updataDate(JkxlDetailFee jkxlDetailFee){
        overheadLineMapper.updateDate(jkxlDetailFee);
    }

    @Transactional(readOnly = false)
    public void updateList(List<JkxlDetailFee> list){
        overheadLineMapper.updateList(list);
    }

    @Transactional(readOnly = false)
    public void delete(String id){
       overheadLineMapper.delete(id);
    }



   //架空线路保存数据
    @Transactional(readOnly = false)
    public void saveJk(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 importExcel = new ImportExcel(file,1,BashInfo.SHEET_FBFXJJ,true);  //分部分项工程量清单计价表
        ImportExcel csqd = 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 gzhz = new ImportExcel(file,1,new String[]{BashInfo.SHEET_GCHZ,BashInfo.SHEET_GCHZ1},true );   //架空线路工程费用汇总
        ImportExcel cbrsb = new ImportExcel(file,1,new String[]{BashInfo.SHEET_TBRCG,BashInfo.SHEET_CBRCG},false);//承包人采购设备计价表

//        获取《工程项目竣工结算汇总表》数据
        double jsCs = ExcelUtil.getDouble(total,BashInfo.JS_CSXM,1,2); //获取措施费
        double csFees2 = ExcelUtil.getDouble(total,BashInfo.CSXM2,1,2);//措施费二
        double jsGf = ExcelUtil.getDouble(total,BashInfo.JS_GF,1,2);  //获取规费
        double cbrfee = ExcelUtil.getDouble(total,BashInfo.QT_CBRSB,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[] originalTotalFees = ExcelUtil.getDoubleArray(gzhz,BashInfo.HZ_ALL,1,2); //获取合计费
        double[] zjgcFees = ExcelUtil.getDoubleArray(gzhz,BashInfo.HZ_ALL,1,3);  //获取直接工程费
        double zjgcTotal = ExcelUtil.getDouble(gzhz,BashInfo.QT_HJ,0,3);//直接工程费合计

//        获取《分部分项工程量清单计价表》详细费用数据
        double totalRgFee = ExcelUtil.getDouble(importExcel,BashInfo.PROJECTNAME,2,13);  //获取人工费合计金额
        double[][] originalFees = ExcelUtil.getDoubleArrays(importExcel,BashInfo.MX_ALL,2, new int[]{13, 14, 15, 17}); //获取分部分项费用
        double fbrFee = ExcelUtil.getDouble(importExcel,BashInfo.PROJECTNAME,2,15); //发包人采购计价表合计金额

//        从《 措施项目清单计价表（一）》《（二）》获取措施费
        double csFees = BashInfo.getCsFee(csqd); //获取措施费
        double[] csRate = BashInfo.getCsRate(csqd); //获取措施费率
        double wordtotal2 =  0.0;  //措施费二合计
        if(ImportExcel.getSheet(csqd2) != null){
            wordtotal2 = SubstationInfo.getCsJzFee(csqd2);
        }
//        double[] aqwmFees = ExcelUtil.getDoubleArray(csqd,BashInfo.CS_AQWM,1, new int[]{3, 4});  //安全文明施工费
//        double[] aqwmJzFees = BashInfo.getAqwfJzFee(csqd1);  //建筑安全文明施工费
        double[] aqwmFees = BashInfo.getAqwfJzFee(csqd);  //安全文明施工费

//        获取《规费项目清单计价表》合计
        double gfFees = BashInfo.getJkGf(gf); //规费合计

//        获取《承包人采购设备计价表》合计
        double cbperson =  0.0; //承包人采购设备费
        if(ImportExcel.getSheet(cbrsb) != null){
            cbperson = ExcelUtil.getDouble(cbrsb,BashInfo.QT_HJ,0,6);
        }

//        获取《其他项目清单计价表》中其他费用和
        double otherTotal =  ExcelUtil.getDouble(qt,BashInfo.QT_HJ,0,2);//获取其他项目费合计
        double[] qtFees = BashInfo.getQtFees(qt);  //获取其他费用中场地租用费和招标代理费

//        根据安全文明施工费、费率、直接工程费合计计算进项税率
        double zg = zjgcTotal-(aqwmFees[1]/(aqwmFees[0]/100));
        double jxsl = zg/(fbrFee-zg); //进项税率

//        部分结算书的《架空线路工程费用汇总》和《分部分项工程量清单计价表》中多出措施项目的分部分项，处理方法是将该部分数据放到“辅助工程”中
        double csxmHj = ExcelUtil.getDouble(gzhz,BashInfo.HZ_CH,1,2);//措施项目合计
        double[] csxmFees = ExcelUtil.getDoubleArray(importExcel,BashInfo.CS,2,new int[]{13, 14, 15, 17});//措施项目费
        originalTotalFees[5] = originalTotalFees[5] + csxmHj;
        originalFees[5][0] = originalFees[5][0] + csxmFees[0];
        originalFees[5][1] = originalFees[5][1] + csxmFees[1];
        originalFees[5][2] = originalFees[5][2] + csxmFees[2];
        originalFees[5][3] = originalFees[5][3] + csxmFees[3];

        //数据核验数据
        List<Settlement> settlementList =  SettementUtil.getComparison(total);
        double builtTotal = ExcelUtil.getDouble(gzhz,BashInfo.QT_HJ,0,2);//获取架空线路工程费用汇总表 分部分项 合计
        double wordtotal1 =  BashInfo.getJkCs(csqd);//获取措施费清单计价表一合计
        double[] divisiontotal=ExcelUtil.getDoubleArray(importExcel,BashInfo.PROJECTNAME,2,new int[]{12,15});//获取分部分项比对数据清单
        List<Settlement> settlements=SettementUtil.getComparison(settlementList,builtTotal,divisiontotal,wordtotal1,wordtotal2,otherTotal,gfFees,cbperson);
        Boolean f = SettementUtil.getJgjsDeveloper(settlementList);
        settlementService.save(settlements,id);

        //将本体工程费用明细存入集合
       List<JkxlDetailFee> list = new ArrayList<>();
       for(int i=0;i<originalTotalFees.length;i++){
           double tax = 0.00; //税金
           double totalfee = 0.00;  //合计
           JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
           double originalTotalCost = originalTotalFees[i]; //原合计
           double otherRate = originalTotalCost/builtTotal;   //其他费用分摊比例
           double otherFee = qtFees[2]*otherRate;  //除场地租用费、招标代理费外费用根据分部分项合计费用所占比例分摊
           double zjgcFee = zjgcFees[i];//直接工程费
           double originalRgCost = originalFees[i][0];// 原人工费
           double rgfRate = originalRgCost/totalRgFee; //人工费比例
           double originalCbrCost = originalFees[i][1];  //原承包人采购
           double originalFbrCost = originalFees[i][2];  //原发包人采购
           double fbrCost = originalFbrCost/(1+jxsl); //后发包人采购
           double originalJxrCost = originalFees[i][3];   //原机械费
           double measuresFee1 = (zjgcFee-fbrCost*jxsl)*csRate[0]; //措施费一（1）
//           double measuresFee2 = rgCost*csRate[1];   //措施费一（2）
           double measuresFee2 = csFees*rgfRate;
           double measuresFee3 = csFees2*otherRate; //措施费二
//           double fees = rgCost*gfRate[1]*1.12+rgCost*gfRate[0];  //规费
           double fees = gfFees*rgfRate;  //规费
           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.setOriginalTotalCost(originalTotalCost);
           jkxlDetailFee.setOriginalRgCost(originalRgCost);
           jkxlDetailFee.setOriginalCbrCost(originalCbrCost);
           jkxlDetailFee.setOriginalFbrCost(originalFbrCost);
           jkxlDetailFee.setOriginalJxCost(originalJxrCost);
           jkxlDetailFee.setMeasuresFee1(measuresFee1);
           jkxlDetailFee.setMeasuresFee2(measuresFee2);
           jkxlDetailFee.setMeasuresFee3(measuresFee3);
           jkxlDetailFee.setFees(fees);
           jkxlDetailFee.setOtherCost(otherFee);
           jkxlDetailFee.setTex(tax);
           jkxlDetailFee.setTotalFee(totalfee);
           jkxlDetailFee.setZjgcFee(zjgcFee);
           jkxlDetailFee.setWbsCode(BashInfo.WBSIDS[i]);
           jkxlDetailFee.setId(id);
           list.add(jkxlDetailFee);
       }

        double texTotal = 0.00; //税合计金额
        //其他费用补税
        if(qtFees[0]+qtFees[1] != 0.00) {
            for(JkxlDetailFee jkxlDetailFee : list){
                texTotal += jkxlDetailFee.getTex();
            }
            for (JkxlDetailFee jkxlDetailFee : list) {
                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 texTotal1 = 0.00;
            for(JkxlDetailFee jkxlDetailFee : list){
                texTotal1 += jkxlDetailFee.getTex();
            }
            for(JkxlDetailFee jkxlDetailFee : list){
                double tex = jkxlDetailFee.getTex();
                double total1 = jkxlDetailFee.getTotalFee();
                double rate = tex/texTotal1;
                double tztex = btex*rate;
                jkxlDetailFee.setTex(tex+tztex);
                jkxlDetailFee.setTotalFee(total1+tztex);
            }
        }
        //承包人采购设备费分摊
        if(cbrfee != 0.00){
            for(JkxlDetailFee jkxlDetailFee : list){
                if(jkxlDetailFee.getWbsCode().equals("39400000")){
                    double total1 = jkxlDetailFee.getTotalFee();
                    jkxlDetailFee.setTotalFee(cbrfee+total1);
                }
            }
        }
        //分摊差额
        double cstotal1 = 0.00;
        double cstotal2 = 0.00;
        double cstotal3 = 0.00;
        double gftotal = 0.00;
        for(JkxlDetailFee jkxlDetailFee2 :list){
            cstotal1 += jkxlDetailFee2.getMeasuresFee1();
            cstotal2 += jkxlDetailFee2.getMeasuresFee2();
            cstotal3 += jkxlDetailFee2.getMeasuresFee3();
            gftotal  += jkxlDetailFee2.getFees();
        }
        for(JkxlDetailFee jkxlDetailFee1 : list){
            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 sgft = (csft+gfft)*texRate;
           jkxlDetailFee1.setMeasuresFee1(jkxlDetailFee1.getMeasuresFee1()+csft);
           jkxlDetailFee1.setFees(jkxlDetailFee1.getFees()+gfft);
           jkxlDetailFee1.setTex(jkxlDetailFee1.getTex()+sgft);
           jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee()+csft+gfft+sgft);
        }
        //分摊税
        double texh = textotal+btex;//总税额
        double jktex = 0.00;
        for(JkxlDetailFee jkxlDetailFee : list){
            jktex += jkxlDetailFee.getTex();
        }
        double texr = texh-jktex;
        for(JkxlDetailFee jkxlDetailFee : list){
            double texrate = jkxlDetailFee.getTex()/jktex;
            double tex = texr*texrate;
            jkxlDetailFee.setTex(jkxlDetailFee.getTex()+tex);
            jkxlDetailFee.setTotalFee(jkxlDetailFee.getTotalFee()+tex);

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

        }
        //获取未识别费用
        List<JkxlAdjustFee> wsbFees = BashInfo.getWsbFee(total);
        double wsbfee = 0.00;
       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 tz = 0.00; //总合计金额
        for(JkxlDetailFee jkxlDetailFee : list){
            tz += jkxlDetailFee.getTotalFee();
        }
       double gxFee = Math.abs(gcjsFee-tz-qtFees[0]-qtFees[1]-wsbfee);
       //将其他费用存入集合
       JkxlDetailFee jkxlDetailFee1 = new JkxlDetailFee();
       JkxlDetailFee jkxlDetailFee2 = new JkxlDetailFee();
       jkxlDetailFee1.setId(id);
       jkxlDetailFee1.setWbsCode("34120000");
       jkxlDetailFee1.setTotalFee(qtFees[0]);
       jkxlDetailFee2.setWbsCode("34220000");
       jkxlDetailFee2.setTotalFee(qtFees[1]);
       jkxlDetailFee2.setId(id);
       list.add(jkxlDetailFee1);
       list.add(jkxlDetailFee2);
       //保存费用明细
        overheadLineMapper.saveList(list);
       //保存未识别费用
       JkxlAdjustFee jkxlAdjustFee2 = new JkxlAdjustFee();
       jkxlAdjustFee2.setId(id);
       jkxlAdjustFee2.setType(BashInfo.CE);
       jkxlAdjustFee2.setFee(gxFee);
       feeAdjustService.save(jkxlAdjustFee2);
   }

    //电缆线路保存数据
    @Transactional(readOnly = false)
    public void saveDl(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 csqd = 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 cbrsb = new ImportExcel(file,1,new String[]{BashInfo.SHEET_TBRCG,BashInfo.SHEET_CBRCG},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 texRate = ExcelUtil.getTexRate(total); //获取税率
        double fbfxFee = ExcelUtil.getDouble(total,BashInfo.JS_FBFX,1,2);  //分部分项工程费
        double jsCs = ExcelUtil.getDouble(total,BashInfo.JS_CSXM,1,2);  //措施费合计
        double csFees2 = ExcelUtil.getDouble(total,BashInfo.CSXM2,1,2);//措施费二合计
        double jsGf = ExcelUtil.getDouble(total,BashInfo.JS_GF,1,2);  //规费合计
        double textotal = ExcelUtil.getTex(total);   //税金合计
        double btex = BashInfo.getBtex(total); //获取补税额
        double fbrFees = ExcelUtil.getFbr(total); //获取发包人费
        double cbrfee = ExcelUtil.getDouble(total,BashInfo.QT_CBRSB,1,2);//承包人采购设备费
        double gcjsFee = BashInfo.getGcjsFee(total);  //获取工程结算价
        double jzcs = BashInfo.getJzCs(csqd); //建筑措施费合计
        double azcs = BashInfo.getAzCs(csqd);  //安装措施费合计
        double word1total = jzcs+azcs;//获取措施费清单计价表一
        double azCsFee = BashInfo.getAzCsfee(csqd); //安装措施费除临时施工费和安全文明施工费合计
        double[] csJzFee = BashInfo.getJzCsfee(csqd); //建筑措施费除临时施工费和安全文明施工费以及除两项合计
        double[] azcsRate = BashInfo.getAzRate(csqd); //获取安装措施费率
        double word2total=0.0; //清单表措施费二合计
        if(ImportExcel.getSheet(csqd2) != null){
            word2total = SubstationInfo.getCsJzFee(csqd2);
        }
        double cbperson = 0.0;  //承包人采购设备费
        if(ImportExcel.getSheet(cbrsb) != null){
            cbperson = ExcelUtil.getDouble(cbrsb,BashInfo.QT_HJ,0,6);
        }
        double otherDlTotal =  ExcelUtil.getDouble(qt,BashInfo.QT_HJ,0,2);//获取其他项目费合计
        double[] qtFees = BashInfo.getQtFees(qt);  //获取场地租用费和招标代理费
        double feesDlCount =  ExcelUtil.getDouble(gf,BashInfo.QT_HJ,0,4);//获取规费合计
        double azGf = BashInfo.getAzGf(gf); //获取安装规费
        double[] jzgfRate = BashInfo.getJzGfRate(gf);  //获取建筑规费率
        List<Settlement> settlementList =  SettementUtil.getComparison(total);
        int[] comlumn2 = {12,15};//定义所要数组
        double tax = 0.00; //税金
        double totalfee = 0.00;  //合计金额
        List<JkxlDetailFee> list = new ArrayList<>();
        if(jzgc !=0.00 && azgc == 0.00){
            ImportExcel jzqd = new ImportExcel(file,1,BashInfo.SHEET_JZFBFXJJ,true);  //建筑分部分项工程量清单计价表
            ImportExcel jzhz = new ImportExcel(file,1,BashInfo.SHEET_JZHZB,true );   //电缆线路建筑工程费用汇总表
            double built1Total = ExcelUtil.getDouble(jzhz,BashInfo.QT_HJ,0,2);//建筑分布分项工程量清单计价表合计
            double[] builtTotal={built1Total,0.00};
            double[] division1total=ExcelUtil.getDoubleArray(jzqd,BashInfo.DLJZ,2,comlumn2);//建筑分部分项比对数据清单合计
            double[] division = {division1total[0],division1total[1],0.00,0.00};
            //---结算取值汇总结束
            double[] originalTotalJzFees = ExcelUtil.getDoubleArray(jzhz,BashInfo.HZ_JZ,1,2); //获取建筑合计费
            double[] zjgcJzFees = ExcelUtil.getDoubleArray(jzhz,BashInfo.HZ_JZ,1,3);  //获取建筑直接工程费
            double[][] originalJzFees = ExcelUtil.getDoubleArrays(jzqd,BashInfo.MX_JZ,2, new int[]{13, 14, 15, 17}); //获取建筑原始费用
            double fbrJzFee = ExcelUtil.getDouble(jzqd,BashInfo.DLJZ,2,15);
            double zjgcJzTotal = ExcelUtil.getDouble(jzhz,BashInfo.QT_HJ,0,3);//建筑直接工程费合计
//            double[] aqwmJzFees = ExcelUtil.getDoubleArray(csqd,BashInfo.CS_AQWM,1, new int[]{3, 4});  //建筑安全文明施工费
            double[] aqwmJzFees = BashInfo.getAqwfJzFee(csqd);  //建筑安全文明施工费
            double jxslJz = 0.00;
            if(fbrJzFee != 0.00){
                double zgJz = zjgcJzTotal-(aqwmJzFees[1]/(aqwmJzFees[0]/100));
                jxslJz = zgJz/(fbrJzFee-zgJz); //建筑进项税率
            }
            //措施项目
            double csxmHj = ExcelUtil.getDouble(jzhz,BashInfo.HZ_CH,1,2);//措施项目合计
            double[] csxmFees = ExcelUtil.getDoubleArray(jzqd,BashInfo.CS,2,new int[]{13, 14, 15, 17});//措施项目费
            originalTotalJzFees[2] = originalTotalJzFees[2] + csxmHj;
            originalJzFees[2][0] = originalJzFees[2][0] + csxmFees[0];
            originalJzFees[2][1] = originalJzFees[2][1] + csxmFees[1];
            originalJzFees[2][2] = originalJzFees[2][2] + csxmFees[2];
            originalJzFees[2][3] = originalJzFees[2][3] + csxmFees[3];
            //电缆结算汇总
            List<Settlement> dlSettlements = SettementUtil.getDLComparison(settlementList,builtTotal,division,word1total,word2total,otherDlTotal,feesDlCount,cbperson);
            Boolean f = SettementUtil.getJgjsDeveloper(settlementList);
            //数据核验并保存数据库
            settlementService.save(dlSettlements,id);
            for(int i=0;i<originalTotalJzFees.length;i++){
                JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
                double originalTotalCost = originalTotalJzFees[i]; //原合计
                double otherFee = qtFees[2]/fbfxFee*originalTotalCost;  //其他费用
                double zjgcJzFee = zjgcJzFees[i];//直接工程费
                double originalRgCost = originalJzFees[i][0];// 原人工费
//                double rgCost = originalRgCost/(1+tcRate[0]);  //后人工费
                double originalCbrCost = originalJzFees[i][1];  //原承包人采购
                double originalFbrCost = originalJzFees[i][2];  //原发包人采购
                double originalJxrCost = originalJzFees[i][3];   //原机械费
                double rate = (zjgcJzFee-originalFbrCost)/(zjgcJzTotal-fbrJzFee); //比例
                double measuresFee1 = (zjgcJzFee-originalFbrCost/(1+jxslJz)*jxslJz)*csJzFee[0]+csJzFee[1]*rate; //措施费一（1）
//                double measuresFee1 = (zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*jzcsRate; //措施费一（2）
                double measuresFee2 = 0.00;   //措施费一（1）
                double measuresFee3 = csFees2*originalTotalCost/fbfxFee; //措施费二
                double fees = (zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*jzgfRate[0]+(zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*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.setOriginalTotalCost(originalTotalCost);
                jkxlDetailFee.setOriginalRgCost(originalRgCost);
                jkxlDetailFee.setOriginalCbrCost(originalCbrCost);
                jkxlDetailFee.setOriginalFbrCost(originalFbrCost);
                jkxlDetailFee.setOriginalJxCost(originalJxrCost);
                jkxlDetailFee.setMeasuresFee1(measuresFee1);
                jkxlDetailFee.setMeasuresFee2(measuresFee2);
                jkxlDetailFee.setMeasuresFee3(measuresFee3);
                jkxlDetailFee.setFees(fees);
                jkxlDetailFee.setOtherCost(otherFee);
                jkxlDetailFee.setTex(tax);
                jkxlDetailFee.setTotalFee(totalfee);
                jkxlDetailFee.setZjgcFee(zjgcJzFee);
                jkxlDetailFee.setWbsCode(BashInfo.JZWBSIDS [i]);
                jkxlDetailFee.setId(id);
                list.add(jkxlDetailFee);
            }
            //分摊发包人
            if(f){
               double fbrfee = 0.00;
               for(JkxlDetailFee jkxlDetailFee : list){
                fbrfee += jkxlDetailFee.getOriginalFbrCost();
               }
               double cs = fbrFees-fbrfee;
               if(cs != 0.00) {
                 for (JkxlDetailFee jkxlDetailFee1 : list) {
                    double rate = jkxlDetailFee1.getOriginalFbrCost() / fbrfee;
                    jkxlDetailFee1.setOriginalFbrCost(jkxlDetailFee1.getOriginalFbrCost() + cs * rate);
                     jkxlDetailFee1.setTex(jkxlDetailFee1.getTex() - (cs * rate)*texRate);
                    jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee() - cs * rate);
                  }
               }
           }
        } else if(jzgc ==0.00 && azgc != 0.00){
            ImportExcel azqd = new ImportExcel(file,1,BashInfo.SHEET_AZFBFXJJ,true);  //安装分部分项工程量清单计价表
            ImportExcel azhz = new ImportExcel(file,1,BashInfo.SHEET_HZHZB ,true);   //电缆线路安装工程费用汇总表
            double built2Total = ExcelUtil.getDouble(azhz,BashInfo.QT_HJ,0,2);//安装分部分项工程量清单计合计
            double[] builtTotal={0.00,built2Total};
            double[] division2total=ExcelUtil.getDoubleArray(azqd,BashInfo.DLAZ,2,comlumn2);//安装分部分项比对数据清单合计
            double[] division = {0.00,0.00,division2total[0],division2total[1]};
           //结算汇总取值结束
            double[] originalTotalAzFees = ExcelUtil.getDoubleArray(azhz,BashInfo.HZ_AZ,1,2); //获取安装合计费
            double[] zjgcAzFees = ExcelUtil.getDoubleArray(azhz,BashInfo.HZ_AZ,1,3);  //获取安装直接工程费
            double[] rgFees = ExcelUtil.getDoubleArray(azhz,BashInfo.HZ_AZ,1,4);  //人工费
            double[][] originalAzFees = ExcelUtil.getDoubleArrays(azqd,BashInfo.MX_AZ,2, new int[]{13, 14, 15, 17}); //获取安装原始费用
            double fbrAzFee = ExcelUtil.getDouble(azqd,BashInfo.DLAZ,2,15);
            double zjgcAzTotal = ExcelUtil.getDouble(azhz,BashInfo.QT_HJ,0,3);//安装直接工程费合计
            double rgAzTotal = ExcelUtil.getDouble(azhz,BashInfo.QT_HJ,0,4);//安装人工费费合计
            double[] aqwmAzFees = BashInfo.getAqwfAzFee(csqd);  //建筑安全文明施工费
            double jxslAz = 0.00;
            if(fbrAzFee != 0.00){
                double zgAz = zjgcAzTotal-(aqwmAzFees[1]/(aqwmAzFees[0]/100));
                jxslAz = zgAz/(fbrAzFee-zgAz); //安装进项税率
            }
            //结算汇总
            List<Settlement> dlSettlements = SettementUtil.getDLComparison(settlementList,builtTotal,division,word1total,word2total,otherDlTotal,feesDlCount,cbperson);
            Boolean f = SettementUtil.getJgjsDeveloper(settlementList);
            //数据核验并保存数据库
            settlementService.save(dlSettlements,id);
            for(int i=0;i<originalTotalAzFees.length;i++){
                JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
                double originalTotalCost = originalTotalAzFees[i]; //原合计
                double otherFee = qtFees[2]/fbfxFee*originalTotalCost;  //其他费用
                double zjgcAzFee = zjgcAzFees[i];//直接工程费
                double rgAzFee = rgFees[i];//人工费
                double originalRgCost = originalAzFees[i][0];// 原人工费
                double rgRate = originalRgCost/rgAzTotal; //比例
//                double rgCost = originalRgCost/(1+tcRate[0]);  //后人工费
                double originalCbrCost = originalAzFees[i][1];  //原承包人采购
                double originalFbrCost = originalAzFees[i][2];  //原发包人采购
                double originalJxrCost = originalAzFees[i][3];   //原机械费
//                double measuresFee1 = (zjgcAzFee-originalFbrCost/(1+jxslAz)*jxslAz)*azcsRate[0]+(rgAzFee/(1+tcRate[0])*azcsRate[1]); //措施费一（1）
                double measuresFee1 = (zjgcAzFee-originalFbrCost/(1+jxslAz)*jxslAz)*azcsRate[0]+azCsFee*rgRate; //措施费一（1）
                double measuresFee2 = 0.00;   //措施费一（2）
                double measuresFee3 = csFees2*originalTotalCost/fbfxFee; //措施费二
//                double fees = rgAzFee/(1+tcRate[0])*azgfRate[0]+rgAzFee/(1+tcRate[0])*azgfRate[1]*1.2;  //规费
                double fees = rgRate*azGf;  //规费
                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.setOriginalTotalCost(originalTotalCost);
                jkxlDetailFee.setOriginalRgCost(originalRgCost);
                jkxlDetailFee.setOriginalCbrCost(originalCbrCost);
                jkxlDetailFee.setOriginalFbrCost(originalFbrCost);
                jkxlDetailFee.setOriginalJxCost(originalJxrCost);
                jkxlDetailFee.setMeasuresFee1(measuresFee1);
                jkxlDetailFee.setMeasuresFee2(measuresFee2);
                jkxlDetailFee.setMeasuresFee3(measuresFee3);
                jkxlDetailFee.setFees(fees);
                jkxlDetailFee.setOtherCost(otherFee);
                jkxlDetailFee.setTex(tax);
                jkxlDetailFee.setTotalFee(totalfee);
                jkxlDetailFee.setZjgcFee(zjgcAzFee);
                jkxlDetailFee.setWbsCode(BashInfo.AZWBSIDS[i]);
                jkxlDetailFee.setId(id);
                list.add(jkxlDetailFee);
            }
            //分摊发包人
            if(f){
                double fbrfee = 0.00;
                for(JkxlDetailFee jkxlDetailFee : list){
                    fbrfee += jkxlDetailFee.getOriginalFbrCost();
                }
                double cs = fbrFees-fbrfee;
                if(cs != 0.00) {
                    for (JkxlDetailFee jkxlDetailFee1 : list) {
                        double rate = jkxlDetailFee1.getOriginalFbrCost() / fbrfee;
                        jkxlDetailFee1.setOriginalFbrCost(jkxlDetailFee1.getOriginalFbrCost() + cs * rate);
                        jkxlDetailFee1.setTex(jkxlDetailFee1.getTex() - (cs * rate)*texRate);
                        jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee() - cs * rate);
                    }
                }
            }

        } else 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, BashInfo.SHEET_JZHZB,true);   //电缆线路建筑工程费用汇总表
            ImportExcel azhz = new ImportExcel(file, 1, BashInfo.SHEET_HZHZB,true);   //电缆线路安装工程费用汇总表
            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,BashInfo.DLJZ,2,comlumn2);//建筑分部分项比对数据清单合计
            double[] division2total=ExcelUtil.getDoubleArray(azqd,BashInfo.DLAZ,2,comlumn2);//安装分部分项比对数据清单合计
            double[] division = {division1total[0],division1total[1],division2total[0],division2total[1]};
            double[] originalTotalJzFees = ExcelUtil.getDoubleArray(jzhz,BashInfo.HZ_JZ,1,2); //获取建筑合计费
            double[] originalTotalAzFees = ExcelUtil.getDoubleArray(azhz,BashInfo.HZ_AZ,1,2); //获取安装合计费
            double[] zjgcJzFees = ExcelUtil.getDoubleArray(jzhz,BashInfo.HZ_JZ,1,3);  //获取建筑直接工程费
            double[] zjgcAzFees = ExcelUtil.getDoubleArray(azhz,BashInfo.HZ_AZ,1,3);  //获取安装直接工程费
            double[] rgAzFees = ExcelUtil.getDoubleArray(azhz,BashInfo.HZ_AZ,1,4);  //获取安装人工费
            double[][] originalJzFees = ExcelUtil.getDoubleArrays(jzqd,BashInfo.MX_JZ,2, new int[]{13, 14, 15, 17}); //获取建筑原始费用
            double[][] originalAzFees = ExcelUtil.getDoubleArrays(azqd,BashInfo.MX_AZ,2, new int[]{13, 14, 15, 17}); //获取安装原始费用
            double fbrJzFee = ExcelUtil.getDouble(jzqd,BashInfo.DLJZ,2,15);
            double fbrAzFee = ExcelUtil.getDouble(azqd,BashInfo.DLAZ,2,15);
            double zjgcJzTotal = ExcelUtil.getDouble(jzhz,BashInfo.QT_HJ,0,3);//建筑直接工程费合计
            double zjgcAzTotal = ExcelUtil.getDouble(azhz,BashInfo.QT_HJ,0,3);//安装直接工程费合计
            double rgAzTotal = ExcelUtil.getDouble(azhz,BashInfo.QT_HJ,0,4);//安装人工费费合计
//            double[] aqwmJzFees = ExcelUtil.getDoubleArray(csqd,BashInfo.CS_AQWM,1, new int[]{3, 4});  //建筑安全文明施工费
            double[] aqwmJzFees = BashInfo.getAqwfJzFee(csqd);  //建筑安全文明施工费
            double[] aqwmAzFees = BashInfo.getAqwfAzFee(csqd);  //安装安全文明施工费
            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); //安装进项税率
            }
            //措施项目
            double csxmHj = ExcelUtil.getDouble(jzhz,BashInfo.HZ_CH,1,2);//措施项目合计
            double[] csxmFees = ExcelUtil.getDoubleArray(jzqd,BashInfo.MX_CSXM,2,new int[]{13, 14, 15, 17});//措施项目费
            originalTotalJzFees[2] = originalTotalJzFees[2] + csxmHj;
            originalJzFees[2][0] = originalJzFees[2][0] + csxmFees[0];
            originalJzFees[2][1] = originalJzFees[2][1] + csxmFees[1];
            originalJzFees[2][2] = originalJzFees[2][2] + csxmFees[2];
            originalJzFees[2][3] = originalJzFees[2][3] + csxmFees[3];

            List<Settlement> dlSettlements = SettementUtil.getDLComparison(settlementList,builtTotal,division,word1total,word2total,otherDlTotal,feesDlCount,cbperson);
            Boolean f = SettementUtil.getJgjsDeveloper(settlementList);
            //数据核验并保存数据库
            settlementService.save(dlSettlements,id);
            //将本体工程费用明细存入集合
            for(int i=0;i<originalTotalJzFees.length;i++){
                JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
                double originalTotalCost = originalTotalJzFees[i]; //原合计
                double otherFee = qtFees[2]/fbfxFee*originalTotalCost;  //其他费用
                double zjgcJzFee = zjgcJzFees[i];//直接工程费
                double originalRgCost = originalJzFees[i][0];// 原人工费
                double originalCbrCost = originalJzFees[i][1];  //原承包人采购
                double originalFbrCost = originalJzFees[i][2];  //原发包人采购
                double originalJxrCost = originalJzFees[i][3];   //原机械费
                double rate = (zjgcJzFee-originalFbrCost)/(zjgcJzTotal-fbrJzFee); //比例
//                double measuresFee1 = (zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*jzcsRate; //措施费一（2）
                double measuresFee1 = (zjgcJzFee-originalFbrCost/(1+jxslJz)*jxslJz)*csJzFee[0]+csJzFee[1]*rate; //措施费一（1）
                double measuresFee2 = 0.00;   //措施费一（1）
                double measuresFee3 = csFees2*originalTotalCost/fbfxFee; //措施费二
                double fees = (zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*jzgfRate[0]+(zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*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.setOriginalTotalCost(originalTotalCost);
                jkxlDetailFee.setOriginalRgCost(originalRgCost);
                jkxlDetailFee.setOriginalCbrCost(originalCbrCost);
                jkxlDetailFee.setOriginalFbrCost(originalFbrCost);
                jkxlDetailFee.setOriginalJxCost(originalJxrCost);
                jkxlDetailFee.setMeasuresFee1(measuresFee1);
                jkxlDetailFee.setMeasuresFee2(measuresFee2);
                jkxlDetailFee.setMeasuresFee3(measuresFee3);
                jkxlDetailFee.setFees(fees);
                jkxlDetailFee.setOtherCost(otherFee);
                jkxlDetailFee.setTex(tax);
                jkxlDetailFee.setTotalFee(totalfee);
                jkxlDetailFee.setZjgcFee(zjgcJzFee);
                jkxlDetailFee.setWbsCode(BashInfo.JZWBSIDS [i]);
                jkxlDetailFee.setId(id);
                list.add(jkxlDetailFee);
            }
            for(int i=0;i<originalTotalAzFees.length;i++){
                JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
                double originalTotalCost = originalTotalAzFees[i]; //原合计
                double otherFee = qtFees[2]/fbfxFee*originalTotalCost;  //其他费用
                double zjgcAzFee = zjgcAzFees[i];//直接工程费
                double rgAzFee = rgAzFees[i];//人工费
                double originalRgCost = originalAzFees[i][0];// 原人工费
                double rgRate = originalRgCost/rgAzTotal; //比例
                double originalCbrCost = originalAzFees[i][1];  //原承包人采购
                double originalFbrCost = originalAzFees[i][2];  //原发包人采购
                double originalJxrCost = originalAzFees[i][3];   //原机械费
//                double measuresFee1 = (zjgcAzFee-originalFbrCost/(1+jxslAz)*jxslAz)*azcsRate[0]+(rgAzFee/(1+tcRate[0])*azcsRate[1]); //措施费一（1）
                double measuresFee1 = (zjgcAzFee-originalFbrCost/(1+jxslAz)*jxslAz)*azcsRate[0]+azCsFee*rgRate; //措施费一（1）
                double measuresFee2 = 0.00;   //措施费一（2）
                double measuresFee3 = csFees2*originalTotalCost/fbfxFee; //措施费二
//                double fees = rgAzFee/(1+tcRate[0])*azgfRate[0]+rgAzFee/(1+tcRate[0])*azgfRate[1]*1.2;  //规费
                double fees = rgRate*azGf;  //规费
                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.setOriginalTotalCost(originalTotalCost);
                jkxlDetailFee.setOriginalRgCost(originalRgCost);
                jkxlDetailFee.setOriginalCbrCost(originalCbrCost);
                jkxlDetailFee.setOriginalFbrCost(originalFbrCost);
                jkxlDetailFee.setOriginalJxCost(originalJxrCost);
                jkxlDetailFee.setMeasuresFee1(measuresFee1);
                jkxlDetailFee.setMeasuresFee2(measuresFee2);
                jkxlDetailFee.setMeasuresFee3(measuresFee3);
                jkxlDetailFee.setFees(fees);
                jkxlDetailFee.setOtherCost(otherFee);
                jkxlDetailFee.setTex(tax);
                jkxlDetailFee.setTotalFee(totalfee);
                jkxlDetailFee.setZjgcFee(zjgcAzFee);
                jkxlDetailFee.setWbsCode(BashInfo.AZWBSIDS[i]);
                jkxlDetailFee.setId(id);
                list.add(jkxlDetailFee);
            }
            //分摊发包人
            if(f){
                double fbrfee = 0.00;
                for(JkxlDetailFee jkxlDetailFee : list){
                    fbrfee += jkxlDetailFee.getOriginalFbrCost();
                }
                double cs = fbrFees-fbrfee;
                if(cs != 0.00) {
                    for (JkxlDetailFee jkxlDetailFee1 : list) {
                        double rate = jkxlDetailFee1.getOriginalFbrCost() / fbrfee;
                        jkxlDetailFee1.setOriginalFbrCost(jkxlDetailFee1.getOriginalFbrCost() + cs * rate);
                        jkxlDetailFee1.setTex(jkxlDetailFee1.getTex() - (cs * rate)*texRate);
                        jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee() - cs * rate);
                    }
                }
            }
        }else {
            throw new RuntimeException(BashInfo.NUMBERERROR);
        }
        double tz = 0.00; //总合计金额
        double texTotal = 0.00; //税合计金额

        //其他费用补税
        if(qtFees[0]+qtFees[1] != 0.00) {
            for(JkxlDetailFee jkxlDetailFee : list){
                texTotal += jkxlDetailFee.getTex();
            }
            for (JkxlDetailFee jkxlDetailFee : list) {
                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 texTotal1 = 0.00;
            for(JkxlDetailFee jkxlDetailFee : list){
                texTotal1 += jkxlDetailFee.getTex();
            }
            for(JkxlDetailFee jkxlDetailFee : list){
                double tex = jkxlDetailFee.getTex();
                double total1 = jkxlDetailFee.getTotalFee();
                double rate = tex/texTotal1;
                jkxlDetailFee.setTex(tex+btex*rate);
                jkxlDetailFee.setTotalFee(total1+btex*rate);
            }
        }
        //承包人设备费分摊
        if(cbrfee != 0.00){
            for(JkxlDetailFee jkxlDetailFee : list){
                double total1 = jkxlDetailFee.getTotalFee();
                if(jkxlDetailFee.getWbsCode().equals("49320000") && jkxlDetailFee.getTotalFee() != 0.00){
                    jkxlDetailFee.setTotalFee(total1+cbrfee);
                } else {
                    double rate = jkxlDetailFee.getOriginalTotalCost()/fbfxFee;
                    jkxlDetailFee.setTotalFee(total1+cbrfee*rate);
                }
            }
        }
        //分摊差额
        double cstotal1 = 0.00;
        double cstotal2 = 0.00;
        double cstotal3 = 0.00;
        double gftotal = 0.00;
        double zjgcTotal = 0.00;
        for(JkxlDetailFee jkxlDetailFee2 :list){
            cstotal1 += jkxlDetailFee2.getMeasuresFee1();
            cstotal2 += jkxlDetailFee2.getMeasuresFee2();
            cstotal3 += jkxlDetailFee2.getMeasuresFee3();
            gftotal  += jkxlDetailFee2.getFees();
            zjgcTotal += jkxlDetailFee2.getZjgcFee();
        }
        for(JkxlDetailFee jkxlDetailFee1 : list){
            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 sgft = (csft+gfft)*texRate;
            jkxlDetailFee1.setMeasuresFee1(jkxlDetailFee1.getMeasuresFee1()+csft);
            jkxlDetailFee1.setFees(jkxlDetailFee1.getFees()+gfft);
            jkxlDetailFee1.setTex(jkxlDetailFee1.getTex()+sgft);
            jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee()+csft+gfft+sgft);
        }
        //分摊税
        double texh = textotal+btex;//总税额
        double jktex = 0.00;
        for(JkxlDetailFee jkxlDetailFee : list){
            jktex += jkxlDetailFee.getTex();
        }
        double texr = texh-jktex;
        for(JkxlDetailFee jkxlDetailFee : list){
            double texrate = jkxlDetailFee.getTex()/jktex;
            double tex = texr*texrate;
            jkxlDetailFee.setTex(jkxlDetailFee.getTex()+tex);
            jkxlDetailFee.setTotalFee(jkxlDetailFee.getTotalFee()+tex);

        }

        //获取未识别费用
        List<JkxlAdjustFee> wsbFees = BashInfo.getWsbFee(total);
        double wsbfee = 0.00;
        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;
            }
        }
        //计算差额
        for(JkxlDetailFee jkxlDetailFee : list){
            tz += jkxlDetailFee.getTotalFee();
        }
        double gxFee = Math.abs(gcjsFee-tz-qtFees[0]-qtFees[1]-wsbfee);
        //将其他费用存入集合
        JkxlDetailFee jkxlDetailFee1 = new JkxlDetailFee();
        JkxlDetailFee jkxlDetailFee2 = new JkxlDetailFee();
        jkxlDetailFee1.setId(id);
        jkxlDetailFee1.setWbsCode("44120000");
        jkxlDetailFee1.setTotalFee(qtFees[0]);
        jkxlDetailFee2.setWbsCode("44220000");
        jkxlDetailFee2.setTotalFee(qtFees[1]);
        jkxlDetailFee2.setId(id);
        list.add(jkxlDetailFee1);
        list.add(jkxlDetailFee2);
        overheadLineMapper.saveList(list);
        JkxlAdjustFee jkxlAdjustFee2 = new JkxlAdjustFee();
        jkxlAdjustFee2.setId(id);
        jkxlAdjustFee2.setType(BashInfo.CE);
        jkxlAdjustFee2.setFee(gxFee);
        feeAdjustService.save(jkxlAdjustFee2);
    }

    //通信线路保存数据
    @Transactional(readOnly = false)
    public void saveTx(MultipartFile file,WbsItem wbsItem) throws IOException, InvalidFormatException {
        String id = wbsItem.getId();
        String type = wbsItem.getType();
        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 csqd = 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 cbrsb = new ImportExcel(file,1,new String[]{BashInfo.SHEET_TBRCG,BashInfo.SHEET_CBRCG},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 texRate = ExcelUtil.getTexRate(total); //获取税率
        double fbfxFee = ExcelUtil.getDouble(total,BashInfo.JS_FBFX,1,2);  //分部分项工程费
        double jsCs = ExcelUtil.getDouble(total,BashInfo.JS_CSXM,1,2);  //措施费合计
        double csFees2 = ExcelUtil.getDouble(total,BashInfo.CSXM2,1,2);//措施费二合计
        double jsGf = ExcelUtil.getDouble(total,BashInfo.JS_GF,1,2);  //规费合计
        double textotal = ExcelUtil.getTex(total);   //税金合计
        double btex = BashInfo.getBtex(total); //获取补税额
        double fbrFees = ExcelUtil.getFbr(total); //获取发包人费
        double cbrfee = ExcelUtil.getDouble(total,BashInfo.QT_CBRSB,1,2);//承包人采购设备费
        double gcjsFee = BashInfo.getGcjsFee(total);  //获取工程结算价
        double jzcs = BashInfo.getJzCs(csqd); //建筑措施费合计
        double azcs = BashInfo.getAzCs(csqd);  //安装措施费合计
        double word1total = jzcs+azcs;//获取措施费清单计价表一
        double azCsFee = BashInfo.getAzCsfee(csqd); //安装措施费除临时施工费和安全文明施工费合计
        double[] csJzFee = BashInfo.getJzCsfee(csqd); //建筑措施费除临时施工费和安全文明施工费以及除两项合计
        double[] azcsRate = BashInfo.getAzRate(csqd); //获取安装措施费率
        double word2total=0.0; //清单表措施费二合计
        if(ImportExcel.getSheet(csqd2) != null){
            word2total = SubstationInfo.getCsJzFee(csqd2);
        }
        double cbperson = 0.0;  //承包人采购设备费
        if(ImportExcel.getSheet(cbrsb) != null){
            cbperson = ExcelUtil.getDouble(cbrsb,BashInfo.QT_HJ,0,6);
        }
        double otherDlTotal =  ExcelUtil.getDouble(qt,BashInfo.QT_HJ,0,2);//获取其他项目费合计
        double[] qtFees = BashInfo.getQtFees(qt);  //获取场地租用费和招标代理费
        double feesDlCount =  ExcelUtil.getDouble(gf,BashInfo.QT_HJ,0,4);//获取规费合计
        double azGf = BashInfo.getAzGf(gf); //获取安装规费
        double[] jzgfRate = BashInfo.getJzGfRate(gf);  //获取建筑规费率
        List<Settlement> settlementList =  SettementUtil.getComparison(total);
        int[] comlumn2 = {12,15};//定义所要数组
        double tax = 0.00; //税金
        double totalfee = 0.00;  //合计金额
        List<JkxlDetailFee> list = new ArrayList<>();
        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 );   //通信线路建筑工程费用汇总表
            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[] originalTotalJzFees = ExcelUtil.getDoubleArray(jzhz,BashInfo.TXHZ_JZ,1,2); //获取建筑合计费
            double[] zjgcJzFees = ExcelUtil.getDoubleArray(jzhz,BashInfo.TXHZ_JZ,1,3);  //获取建筑直接工程费
            double[][] originalJzFees = ExcelUtil.getDoubleArrays(jzqd,BashInfo.TXMX_JZ,2, new int[]{13, 14, 15, 17}); //获取建筑原始费用
//            double fbrJzFee = ExcelUtil.getDouble(fbrcg,BashInfo.QT_XJ,0,6); //建筑发包人采购计价表合计金额
            double fbrJzFee = ExcelUtil.getDouble(jzqd,SubstationInfo.JZMX_BJZ,2,15);
            double zjgcJzTotal = ExcelUtil.getDouble(jzhz,BashInfo.QT_HJ,0,3);//建筑直接工程费合计
            double[] aqwmJzFees = ExcelUtil.getDoubleArray(csqd,BashInfo.CS_AQWM,1, new int[]{3, 4});  //建筑安全文明施工费
            double jxslJz = 0.00;
            if(fbrJzFee != 0.00){
                double zgJz = zjgcJzTotal-(aqwmJzFees[1]/(aqwmJzFees[0]/100));
                jxslJz = zgJz/(fbrJzFee-zgJz); //建筑进项税率
            }
            //电缆结算汇总
            List<Settlement> dlSettlements = SettementUtil.getDLComparison(settlementList,builtTotal,division,word1total,word2total,otherDlTotal,feesDlCount,cbperson);
            Boolean f = SettementUtil.getJgjsDeveloper(settlementList);
            //数据核验并保存数据库
            settlementService.save(dlSettlements,id);
            for(int i=0;i<originalTotalJzFees.length;i++){
                JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
                double originalTotalCost = originalTotalJzFees[i]; //原合计
                double otherFee = qtFees[2]/fbfxFee*originalTotalCost;  //其他费用
                double zjgcJzFee = zjgcJzFees[i];//直接工程费
                double originalRgCost = originalJzFees[i][0];// 原人工费
//                double rgCost = originalRgCost/(1+tcRate[0]);  //后人工费
                double originalCbrCost = originalJzFees[i][1];  //原承包人采购
                double originalFbrCost = originalJzFees[i][2];  //原发包人采购
                double originalJxrCost = originalJzFees[i][3];   //原机械费
                double rate = (zjgcJzFee-originalFbrCost)/(zjgcJzTotal-fbrJzFee); //比例
                double measuresFee1 = (zjgcJzFee-originalFbrCost/(1+jxslJz)*jxslJz)*csJzFee[0]+csJzFee[1]*rate; //措施费一（1）
                double measuresFee2 = 0.00;   //措施费一（2）
                double measuresFee3 = csFees2*originalTotalCost/fbfxFee; //措施费二
                double fees = (zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*jzgfRate[0]+(zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*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.setOriginalTotalCost(originalTotalCost);
                jkxlDetailFee.setOriginalRgCost(originalRgCost);
                jkxlDetailFee.setOriginalCbrCost(originalCbrCost);
                jkxlDetailFee.setOriginalFbrCost(originalFbrCost);
                jkxlDetailFee.setOriginalJxCost(originalJxrCost);
                jkxlDetailFee.setMeasuresFee1(measuresFee1);
                jkxlDetailFee.setMeasuresFee2(measuresFee2);
                jkxlDetailFee.setMeasuresFee3(measuresFee3);
                jkxlDetailFee.setFees(fees);
                jkxlDetailFee.setOtherCost(otherFee);
                jkxlDetailFee.setTex(tax);
                jkxlDetailFee.setTotalFee(totalfee);
                jkxlDetailFee.setZjgcFee(zjgcJzFee);
                jkxlDetailFee.setWbsCode(BashInfo.JZTXWBSIDS [i]);
                jkxlDetailFee.setId(id);
                list.add(jkxlDetailFee);
            }

        } else 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);   //电缆线路安装工程费用汇总表
            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[] originalTotalAzFees = ExcelUtil.getDoubleArray(azhz,BashInfo.HZ_AZ,1,2); //获取安装合计费
            double[] zjgcAzFees = ExcelUtil.getDoubleArray(azhz,BashInfo.TXHZ_AZ,1,3);  //获取安装直接工程费
            double[] rgFees = ExcelUtil.getDoubleArray(azhz,BashInfo.TXHZ_AZ,1,4);  //人工费
            double[][] originalAzFees = ExcelUtil.getDoubleArrays(azqd,BashInfo.TXMX_AZ,2, new int[]{13, 14, 15, 17}); //获取安装原始费用
//            double fbrAzFee = BashInfo.getFbrAzFee(fbrcg);  //安装发包人采购计价表合计金额
            double fbrAzFee = ExcelUtil.getDouble(azqd,SubstationInfo.AZMX_BDZAZ,2,15);
            double zjgcAzTotal = ExcelUtil.getDouble(azhz,BashInfo.QT_HJ,0,3);//安装直接工程费合计
            double rgAzTotal = ExcelUtil.getDouble(azhz,BashInfo.QT_HJ,0,4);//安装人工费费合计
            double[] aqwmAzFees = BashInfo.getAqwfAzFee(csqd);  //建筑安全文明施工费
            double jxslAz = 0.00;
            if(fbrAzFee != 0.00){
                double zgAz = zjgcAzTotal-(aqwmAzFees[1]/(aqwmAzFees[0]/100));
                jxslAz = zgAz/(fbrAzFee-zgAz); //安装进项税率
            }
            //结算汇总
            List<Settlement> dlSettlements = SettementUtil.getDLComparison(settlementList,builtTotal,division,word1total,word2total,otherDlTotal,feesDlCount,cbperson);
            Boolean f = SettementUtil.getJgjsDeveloper(settlementList);
            //数据核验并保存数据库
            settlementService.save(dlSettlements,id);
            for(int i=0;i<originalTotalAzFees.length;i++){
                JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
                double originalTotalCost = originalTotalAzFees[i]; //原合计
                double otherFee = qtFees[2]/fbfxFee*originalTotalCost;  //其他费用
                double zjgcAzFee = zjgcAzFees[i];//直接工程费
                double rgAzFee = rgFees[i];//人工费
                double originalRgCost = originalAzFees[i][0];// 原人工费
                double rgRate = originalRgCost/rgAzTotal; //比例
//                double rgCost = originalRgCost/(1+tcRate[0]);  //后人工费
                double originalCbrCost = originalAzFees[i][1];  //原承包人采购
                double originalFbrCost = originalAzFees[i][2];  //原发包人采购
                double originalJxrCost = originalAzFees[i][3];   //原机械费
//                double measuresFee1 = (zjgcAzFee-originalFbrCost/(1+jxslAz)*jxslAz)*azcsRate[0]+(rgAzFee/(1+tcRate[0])*azcsRate[1]); //措施费一（1）
                double measuresFee1 = (zjgcAzFee-originalFbrCost/(1+jxslAz)*jxslAz)*azcsRate[0]+azCsFee*rgRate; //措施费一（1）
                double measuresFee2 = 0.00;   //措施费一（2）
                double measuresFee3 = csFees2*originalTotalCost/fbfxFee; //措施费二
//                double fees = rgAzFee/(1+tcRate[0])*azgfRate[0]+rgAzFee/(1+tcRate[0])*azgfRate[1]*1.2;  //规费
                double fees = rgRate*azGf;  //规费
                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.setOriginalTotalCost(originalTotalCost);
                jkxlDetailFee.setOriginalRgCost(originalRgCost);
//                jkxlDetailFee.setRgCost(rgCost);
                jkxlDetailFee.setOriginalCbrCost(originalCbrCost);
                jkxlDetailFee.setOriginalFbrCost(originalFbrCost);
                jkxlDetailFee.setOriginalJxCost(originalJxrCost);
                jkxlDetailFee.setMeasuresFee1(measuresFee1);
                jkxlDetailFee.setMeasuresFee2(measuresFee2);
                jkxlDetailFee.setMeasuresFee3(measuresFee3);
                jkxlDetailFee.setFees(fees);
                jkxlDetailFee.setOtherCost(otherFee);
                jkxlDetailFee.setTex(tax);
                jkxlDetailFee.setTotalFee(totalfee);
                jkxlDetailFee.setZjgcFee(zjgcAzFee);
                jkxlDetailFee.setWbsCode(BashInfo.AZTXWBSIDS[i]);
                jkxlDetailFee.setId(id);
                list.add(jkxlDetailFee);
            }

        } else 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);   //电缆线路安装工程费用汇总表
            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[] originalTotalJzFees = ExcelUtil.getDoubleArray(jzhz,BashInfo.TXHZ_JZ,1,2); //获取建筑合计费
            double[] originalTotalAzFees = ExcelUtil.getDoubleArray(azhz,BashInfo.TXHZ_AZ,1,2); //获取安装合计费
            double[] zjgcJzFees = ExcelUtil.getDoubleArray(jzhz,BashInfo.TXHZ_JZ,1,3);  //获取建筑直接工程费
            double[] zjgcAzFees = ExcelUtil.getDoubleArray(azhz,BashInfo.TXHZ_AZ,1,3);  //获取安装直接工程费
            double[] rgAzFees = ExcelUtil.getDoubleArray(azhz,BashInfo.TXHZ_AZ,1,4);  //获取安装人工费
            double[][] originalJzFees = ExcelUtil.getDoubleArrays(jzqd,BashInfo.TXMX_JZ,2, new int[]{13, 14, 15, 17}); //获取建筑原始费用
            double[][] originalAzFees = ExcelUtil.getDoubleArrays(azqd,BashInfo.TXMX_AZ,2, new int[]{13, 14, 15, 17}); //获取安装原始费用
            double fbrJzFee = ExcelUtil.getDouble(jzqd,SubstationInfo.JZMX_BJZ,2,15);
            double fbrAzFee = ExcelUtil.getDouble(azqd,SubstationInfo.AZMX_BDZAZ,2,15);
            double zjgcJzTotal = ExcelUtil.getDouble(jzhz,BashInfo.QT_HJ,0,3);//建筑直接工程费合计
            double zjgcAzTotal = ExcelUtil.getDouble(azhz,BashInfo.QT_HJ,0,3);//安装直接工程费合计
            double rgAzTotal = ExcelUtil.getDouble(azhz,BashInfo.QT_HJ,0,4);//安装人工费费合计
            double[] aqwmJzFees = ExcelUtil.getDoubleArray(csqd,BashInfo.CS_AQWM,1, new int[]{3, 4});  //建筑安全文明施工费
            double[] aqwmAzFees = BashInfo.getAqwfAzFee(csqd);  //安装安全文明施工费
            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); //安装进项税率
            }
            List<Settlement> dlSettlements = SettementUtil.getDLComparison(settlementList,builtTotal,division,word1total,word2total,otherDlTotal,feesDlCount,cbperson);
            Boolean f = SettementUtil.getJgjsDeveloper(settlementList);
            //数据核验并保存数据库
            settlementService.save(dlSettlements,id);
            //将本体工程费用明细存入集合
            for(int i=0;i<originalTotalJzFees.length;i++){
                JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
                double originalTotalCost = originalTotalJzFees[i]; //原合计
                double otherFee = qtFees[2]/fbfxFee*originalTotalCost;  //其他费用
                double zjgcJzFee = zjgcJzFees[i];//直接工程费
                double originalRgCost = originalJzFees[i][0];// 原人工费
//                double rgCost = originalRgCost/(1+tcRate[0]);  //后人工费
                double originalCbrCost = originalJzFees[i][1];  //原承包人采购
                double originalFbrCost = originalJzFees[i][2];  //原发包人采购
                double originalJxrCost = originalJzFees[i][3];   //原机械费
                double rate = (zjgcJzFee-originalFbrCost)/(zjgcJzTotal-fbrJzFee); //比例
//                double measuresFee1 = (zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*jzcsRate; //措施费一（2）
                double measuresFee1 = (zjgcJzFee-originalFbrCost/(1+jxslJz)*jxslJz)*csJzFee[0]+csJzFee[1]*rate; //措施费一（1）
                double measuresFee2 = 0.00;   //措施费一（1）
                double measuresFee3 = csFees2*originalTotalCost/fbfxFee; //措施费二
                double fees = (zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*jzgfRate[0]+(zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*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.setOriginalTotalCost(originalTotalCost);
                jkxlDetailFee.setOriginalRgCost(originalRgCost);
//                jkxlDetailFee.setRgCost(rgCost);
                jkxlDetailFee.setOriginalCbrCost(originalCbrCost);
                jkxlDetailFee.setOriginalFbrCost(originalFbrCost);
                jkxlDetailFee.setOriginalJxCost(originalJxrCost);
                jkxlDetailFee.setMeasuresFee1(measuresFee1);
                jkxlDetailFee.setMeasuresFee2(measuresFee2);
                jkxlDetailFee.setMeasuresFee3(measuresFee3);
                jkxlDetailFee.setFees(fees);
                jkxlDetailFee.setOtherCost(otherFee);
                jkxlDetailFee.setTex(tax);
                jkxlDetailFee.setTotalFee(totalfee);
                jkxlDetailFee.setZjgcFee(zjgcJzFee);
                jkxlDetailFee.setWbsCode(BashInfo.JZTXWBSIDS [i]);
                jkxlDetailFee.setId(id);
                list.add(jkxlDetailFee);
            }
            for(int i=0;i<originalTotalAzFees.length;i++){
                JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
                double originalTotalCost = originalTotalAzFees[i]; //原合计
                double otherFee = qtFees[2]/fbfxFee*originalTotalCost;  //其他费用
                double zjgcAzFee = zjgcAzFees[i];//直接工程费
                double rgAzFee = rgAzFees[i];//人工费
                double originalRgCost = originalAzFees[i][0];// 原人工费
                double rgRate = originalRgCost/rgAzTotal; //比例
                double originalCbrCost = originalAzFees[i][1];  //原承包人采购
                double originalFbrCost = originalAzFees[i][2];  //原发包人采购
                double originalJxrCost = originalAzFees[i][3];   //原机械费
                double measuresFee1 = (zjgcAzFee-originalFbrCost/(1+jxslAz)*jxslAz)*azcsRate[0]+azCsFee*rgRate; //措施费一（1）
                double measuresFee2 = 0.00;   //措施费一（2）
                double measuresFee3 = csFees2*originalTotalCost/fbfxFee; //措施费二
                double fees = rgRate*azGf;  //规费
                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.setOriginalTotalCost(originalTotalCost);
                jkxlDetailFee.setOriginalRgCost(originalRgCost);
                jkxlDetailFee.setOriginalCbrCost(originalCbrCost);
                jkxlDetailFee.setOriginalFbrCost(originalFbrCost);
                jkxlDetailFee.setOriginalJxCost(originalJxrCost);
                jkxlDetailFee.setMeasuresFee1(measuresFee1);
                jkxlDetailFee.setMeasuresFee2(measuresFee2);
                jkxlDetailFee.setMeasuresFee3(measuresFee3);
                jkxlDetailFee.setFees(fees);
                jkxlDetailFee.setOtherCost(otherFee);
                jkxlDetailFee.setTex(tax);
                jkxlDetailFee.setTotalFee(totalfee);
                jkxlDetailFee.setZjgcFee(zjgcAzFee);
                jkxlDetailFee.setWbsCode(BashInfo.AZTXWBSIDS[i]);
                jkxlDetailFee.setId(id);
                list.add(jkxlDetailFee);
            }
        } else {
            throw new RuntimeException(BashInfo.NUMBERERROR);
        }
        double tz = 0.00; //总合计金额
        double texTotal = 0.00; //税合计金额
        for(JkxlDetailFee jkxlDetailFee : list){
            texTotal += jkxlDetailFee.getTex();
        }
        //其他费用补税
        if(qtFees[0]+qtFees[1] != 0.00) {
            for (JkxlDetailFee jkxlDetailFee : list) {
                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 = 0.00;
            for(JkxlDetailFee jkxlDetailFee : list){
                tex1 += jkxlDetailFee.getTex();
            }
            for(JkxlDetailFee jkxlDetailFee : list){
                double tex = jkxlDetailFee.getTex();
                double total1 = jkxlDetailFee.getTotalFee();
                double rate = tex/tex1;
                jkxlDetailFee.setTex(tex+btex*rate);
                jkxlDetailFee.setTotalFee(total1+btex*rate);
            }
        }
        //承包人设备费
        if(cbrfee != 0.00){
            for(JkxlDetailFee jkxlDetailFee : list){
                double total1 = jkxlDetailFee.getTotalFee();
                if(jkxlDetailFee.getWbsCode().equals("49320000") && jkxlDetailFee.getTotalFee() != 0.00){
                    jkxlDetailFee.setTotalFee(total1+cbrfee);
                } else {
                    double rate = jkxlDetailFee.getOriginalTotalCost()/fbfxFee;
                    jkxlDetailFee.setTotalFee(total1+cbrfee*rate);
                }
            }
        }
        //分摊差额
        double cstotal1 = 0.00;
        double cstotal2 = 0.00;
        double cstotal3 = 0.00;
        double gftotal = 0.00;
        double zjgcTotal = 0.00;
        for(JkxlDetailFee jkxlDetailFee2 :list){
            cstotal1 += jkxlDetailFee2.getMeasuresFee1();
            cstotal2 += jkxlDetailFee2.getMeasuresFee2();
            cstotal3 += jkxlDetailFee2.getMeasuresFee3();
            gftotal  += jkxlDetailFee2.getFees();
            zjgcTotal += jkxlDetailFee2.getZjgcFee();
        }
        for(JkxlDetailFee jkxlDetailFee1 : list){
            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 sgft = (csft+gfft)*texRate;
            jkxlDetailFee1.setMeasuresFee1(jkxlDetailFee1.getMeasuresFee1()+csft);
            jkxlDetailFee1.setFees(jkxlDetailFee1.getFees()+gfft);
            jkxlDetailFee1.setTex(jkxlDetailFee1.getTex()+sgft);
            jkxlDetailFee1.setTotalFee(jkxlDetailFee1.getTotalFee()+csft+gfft+sgft);
        }
        //分摊税
        double texh = textotal+btex;//总税额
        double jktex = 0.00;
        for(JkxlDetailFee jkxlDetailFee : list){
            jktex += jkxlDetailFee.getTex();
        }
        double texr = texh-jktex;
        for(JkxlDetailFee jkxlDetailFee : list){
            double texrate = jkxlDetailFee.getTex()/jktex;
            double tex = texr*texrate;
            jkxlDetailFee.setTex(jkxlDetailFee.getTex()+tex);
            jkxlDetailFee.setTotalFee(jkxlDetailFee.getTotalFee()+tex);

        }

        //获取未识别费用
        List<JkxlAdjustFee> wsbFees = BashInfo.getWsbFee(total);
        double wsbfee = 0.00;
        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;
            }
        }
        //计算差额
        for(JkxlDetailFee jkxlDetailFee : list){
            tz += jkxlDetailFee.getTotalFee();
        }
        double gxFee = Math.abs(gcjsFee-tz-qtFees[0]-qtFees[1]-wsbfee);
        //将其他费用存入集合
        JkxlDetailFee jkxlDetailFee1 = new JkxlDetailFee();
        JkxlDetailFee jkxlDetailFee2 = new JkxlDetailFee();
        jkxlDetailFee1.setId(id);
        jkxlDetailFee1.setWbsCode("44120000");
        jkxlDetailFee1.setTotalFee(qtFees[0]);
        jkxlDetailFee2.setWbsCode("44220000");
        jkxlDetailFee2.setTotalFee(qtFees[1]);
        jkxlDetailFee2.setId(id);
        list.add(jkxlDetailFee1);
        list.add(jkxlDetailFee2);
        overheadLineMapper.saveList(list);
        JkxlAdjustFee jkxlAdjustFee2 = new JkxlAdjustFee();
        jkxlAdjustFee2.setId(id);
        jkxlAdjustFee2.setType(BashInfo.CE);
        jkxlAdjustFee2.setFee(gxFee);
        feeAdjustService.save(jkxlAdjustFee2);
    }


}
