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.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.text.SimpleDateFormat;
import java.util.*;

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




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


public List<JkxlDetailFee> findList(String id,String type){
    List<JkxlDetailFee> list = overheadLineMapper.findList(id,type);
    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.getOriginalTotalCost() == null){
            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("3")) {
        for (JkxlDetailFee jkxlDetailFee : list) {
            if (jkxlDetailFee.getWbsCode().equals("21000000")) {
                jz1 = jkxlDetailFee.getTotalFee()!=null? jkxlDetailFee.getTotalFee():0.00;
                jz2 = jkxlDetailFee.getJzFee()!=null? jkxlDetailFee.getJzFee():0.00;
            }
            if (jkxlDetailFee.getWbsCode().equals("23000000")) {
                az1 = jkxlDetailFee.getTotalFee()!=null? jkxlDetailFee.getTotalFee():0.00;
                az2 = jkxlDetailFee.getAzFee()!=null? jkxlDetailFee.getAzFee():0.00;
            }
            if (jkxlDetailFee.getOriginalTotalCost() == null && jkxlDetailFee.getTotalFee() != null) {
                qtTotal += jkxlDetailFee.getTotalFee();
            }
        }
        for (JkxlDetailFee jkxlDetailFee : list) {
            if(jkxlDetailFee.getWbsCode().equals("20000000")){
                jkxlDetailFee.setJzFee(jz2);
                jkxlDetailFee.setAzFee(az2);
                jkxlDetailFee.setTotalFee(jz1+az1);
                jkxlDetailFee.setQtFee(qtTotal);
            }
            if(jkxlDetailFee.getWbsCode().equals("10000000")){
                jkxlDetailFee.setJzFee(jz2);
                jkxlDetailFee.setAzFee(az2);
                jkxlDetailFee.setTotalFee(jz1+az1);
                jkxlDetailFee.setQtFee(qtTotal);
            }
            if(jkxlDetailFee.getWbsCode().equals("24000000")){
                jkxlDetailFee.setTotalFee(qtTotal);
            }
        }
    }
    //电缆导出
    if(type.equals("2")){
        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.getOriginalTotalCost() == null && jkxlDetailFee.getTotalFee() != null) {
                qtTotal += jkxlDetailFee.getTotalFee();
            }
        }
        for (JkxlDetailFee jkxlDetailFee : list) {
            if(jkxlDetailFee.getWbsCode().equals("40000000")){
                jkxlDetailFee.setJzFee(jz2);
                jkxlDetailFee.setAzFee(az2);
                jkxlDetailFee.setTotalFee(jz1+az1);
                jkxlDetailFee.setQtFee(qtTotal);
            }
            if(jkxlDetailFee.getWbsCode().equals("10000000")){
                jkxlDetailFee.setJzFee(jz2);
                jkxlDetailFee.setAzFee(az2);
                jkxlDetailFee.setTotalFee(jz1+az1);
                jkxlDetailFee.setQtFee(qtTotal);
            }
            if(jkxlDetailFee.getWbsCode().equals("49100000")){
                jkxlDetailFee.setTotalFee(jz1);
                jkxlDetailFee.setJzFee(jz2);
            }
            if(jkxlDetailFee.getWbsCode().equals("49300000")){
                jkxlDetailFee.setTotalFee(az1);
                jkxlDetailFee.setAzFee(az2);

            }
            if(jkxlDetailFee.getWbsCode().equals("49000000")){
                jkxlDetailFee.setJzFee(jz2);
                jkxlDetailFee.setAzFee(az2);
                jkxlDetailFee.setTotalFee(jz1+az1);
            }
            if(jkxlDetailFee.getWbsCode().equals("44000000")){
                jkxlDetailFee.setTotalFee(qtTotal);
            }
        }

    }
    //架空导出
    if(type.equals("1")) {
        for (JkxlDetailFee jkxlDetailFee : list) {
            //本体费用求和
            if (jkxlDetailFee.getOriginalTotalCost() != null) {
                az1 += jkxlDetailFee.getTotalFee();
            }
            //其他费用求和
            if (jkxlDetailFee.getOriginalTotalCost() == null && jkxlDetailFee.getTotalFee() != null) {
                qtTotal += jkxlDetailFee.getTotalFee();
            }
        }
        for (JkxlDetailFee jkxlDetailFee : list) {
            if(jkxlDetailFee.getWbsCode().equals("30000000")){
                jkxlDetailFee.setTotalFee(az1);
                jkxlDetailFee.setAzFee(az1);
                jkxlDetailFee.setQtFee(qtTotal);
            }
            if(jkxlDetailFee.getWbsCode().equals("10000000")){
                jkxlDetailFee.setTotalFee(az1);
                jkxlDetailFee.setAzFee(az1);
                jkxlDetailFee.setQtFee(qtTotal);
            }
            if(jkxlDetailFee.getWbsCode().equals("39000000")){
                jkxlDetailFee.setTotalFee(az1);
                jkxlDetailFee.setAzFee(az1);
            }
            if(jkxlDetailFee.getWbsCode().equals("34000000")){
                jkxlDetailFee.setTotalFee(qtTotal);
            }
        }
    }
    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 saveProject(WbsProject wbsProject){
       overheadLineMapper.saveProject(wbsProject);
   }

   //架空线路保存数据
    @Transactional(readOnly = false)
   public void saveJk(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);
       }
//       List<JkxlDetailFee> jkxlDetailFees = overheadLineMapper.findList(id,type);
//       if(jkxlDetailFees != null && jkxlDetailFees.size()>0){
//           overheadLineMapper.delete(id);
//           feeAdjustService.delete(id);
//           settlementService.delete(id);
//       }
       //获取调差系数
//       double[] tcRate = new double[3];
//       tcRate[0] = ruleRatioService.getValueByName(BashInfo.RATIO_RGTC).getValue()/100;
       //读取表格
       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 total = new ImportExcel(file,1,new String[]{BashInfo.SHEET_HZB,BashInfo.SHEET_HZB1,BashInfo.SHEET_HZB2,BashInfo.SHEET_HZB3},true );  //工程项目竣工结算汇总表
       ImportExcel gzhz = new ImportExcel(file,1,new String[]{BashInfo.SHEET_GCHZ,BashInfo.SHEET_GCHZ1},true );   //架空线路工程费用汇总
//       ImportExcel fbrcg = new ImportExcel(file,1,new String[]{BashInfo.SHEET_FBRCG,BashInfo.SHEET_FBRCG1,BashInfo.SHEET_FBRCG2,BashInfo.SHEET_ZBRCG,BashInfo.SHEET_ZBRCG1},true);   //发包人采购材料设备计价表
        ImportExcel cbrsb = new ImportExcel(file,1,new String[]{BashInfo.SHEET_TBRCG,BashInfo.SHEET_CBRCG},false);//承包人采购设备计价表
//        ImportExcel zyrg = new ImportExcel(file,1,BashInfo.SHEET_ZYRG,true); //主要日工价格表
        //数据核验数据
        List<Settlement> settlementList =  SettementUtil.getComparison(total);
       double builtTotal = ExcelUtil.getDouble(gzhz,BashInfo.QT_HJ,0,2);//获取架空线路工程费用汇总表 分部分项 合计
//       double wordtotal1 =  ExcelUtil.getDouble(csqd,BashInfo.QT_HJ,0,4);//获取措施费清单计价表一
       double wordtotal1 =  BashInfo.getJkCs(csqd);//获取措施费清单计价表一
       double otherTotal =  ExcelUtil.getDouble(qt,BashInfo.QT_HJ,0,2);//获取其他项目费合计
        double cbperson =  0.0;
       double[] divisiontotal=ExcelUtil.getDoubleArray(importExcel,BashInfo.PROJECTNAME,2,new int[]{12,15});//获取分部分项比对数据清单
//        divisiontotal[0] = divisiontotal[0]+csxmHj;
        double gfFees = BashInfo.getJkGf(gf); //规费
//       double feescount =  ExcelUtil.getDouble(gf,BashInfo.QT_HJ,0,4);//获取规费合计
       //数据核验并保存数据库
        double wordtotal2 =  0.0;  //措施费二合计
        if(ImportExcel.getSheet(csqd2) != null){
            wordtotal2 = SubstationInfo.getCsJzFee(csqd2);
        }
        double csFees2 = ExcelUtil.getDouble(total,BashInfo.CSXM2,1,2);//措施费二
       //获取未识别费用
        List<JkxlAdjustFee> wsbFees = BashInfo.getWsbFee(total);
        double wsbfee = 0.00;
        //获取补税额
        double btex = BashInfo.getBtex(total);
       //承包人采购设备费
        if(ImportExcel.getSheet(cbrsb) != null){
            cbperson = ExcelUtil.getDouble(cbrsb,BashInfo.QT_HJ,0,6);//承包人合计
        }
        double cbrfee = ExcelUtil.getDouble(total,BashInfo.QT_CBRSB,1,2);
       double[] originalTotalFees = ExcelUtil.getDoubleArray(gzhz,BashInfo.HZ_ALL,1,2); //获取合计费
       double[] zjgcFees = ExcelUtil.getDoubleArray(gzhz,BashInfo.HZ_ALL,1,3);  //获取直接工程费
       double[][] originalFees = ExcelUtil.getDoubleArrays(importExcel,BashInfo.MX_ALL,2, new int[]{13, 14, 15, 17}); //获取原始费用
       double texRate = ExcelUtil.getTexRate(total); //获取税率
//        double rgRate = ExcelUtil.getRgRate(zyrg);   //获取人工调差系数
       double totalRgFee = ExcelUtil.getDouble(importExcel,BashInfo.PROJECTNAME,2,13);  //获取人工费合计金额
       double[] csRate = BashInfo.getCsRate(csqd); //获取措施费率
//       double[] gfRate = BashInfo.getGfRate(gf);  //获取规费率
       double[] qtFees = BashInfo.getQtFees(qt);  //获取其他费用
       double gcjsFee = BashInfo.getGcjsFee(total);  //获取工程结算价
//       double fbrFee = ExcelUtil.getDouble(fbrcg,BashInfo.QT_HJ,0,6); //发包人采购计价表合计金额
       double fbrFee = ExcelUtil.getDouble(importExcel,BashInfo.PROJECTNAME,2,15); //发包人采购计价表合计金额
       double zjgcTotal = ExcelUtil.getDouble(gzhz,BashInfo.QT_HJ,0,3);//直接工程费合计
       double[] aqwmFees = ExcelUtil.getDoubleArray(csqd,BashInfo.CS_AQWM,1, new int[]{3, 4});  //安全文明施工费
       double zg = zjgcTotal-(aqwmFees[1]/(aqwmFees[0]/100));
       double jxsl = zg/(fbrFee-zg); //进项税率
       double csFees = BashInfo.getCsFee(csqd); //安装措施费
//       double gfFees = ExcelUtil.getDouble(gf,BashInfo.QT_HJ,0,4); //规费

       //结算汇总
        List<Settlement> settlements=SettementUtil.getComparison(settlementList,builtTotal,divisiontotal,wordtotal1,wordtotal2,otherTotal,gfFees,cbperson);
        Boolean f = SettementUtil.getFlag;
        settlementService.save(settlements,id);
        //获取结算表中基础数据
        double jsCs = ExcelUtil.getDouble(total,BashInfo.JS_CSXM,1,2);
        double jsGf = ExcelUtil.getDouble(total,BashInfo.JS_GF,1,2);
        double textotal = ExcelUtil.getTex(total); //获取税金
        double fbrFees = ExcelUtil.getFbr(total); //获取发包人费
        //措施项目
        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<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 orRate = new BigDecimal(otherRate).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入
           double otherFee = qtFees[2]*otherRate;  //其他费用
           double zjgcFee = zjgcFees[i];//直接工程费
           double originalRgCost = originalFees[i][0];// 原人工费
           double rgfRate = originalRgCost/totalRgFee; //人工费比例
//           double rgCost = originalRgCost/(1+tcRate[0]);  //后人工费
           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.setOriginalRate(orRate);
           jkxlDetailFee.setOriginalRgCost(originalRgCost);
//           jkxlDetailFee.setRgCost(rgCost);
           jkxlDetailFee.setOriginalCbrCost(originalCbrCost);
           jkxlDetailFee.setOriginalFbrCost(originalFbrCost);
           jkxlDetailFee.setFbrCost(fbrCost);
           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 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;
                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 ftRate = jkxlDetailFee1.getZjgcFee()/zjgcTotal;
           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);
                }
            }

        }
        //计算差额
        for(JkxlDetailFee jkxlDetailFee : list){
            tz += jkxlDetailFee.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("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();
        String type = wbsItem.getType();
        WbsItem wbsItem1 = itemService.get(id);
        if(wbsItem1 == null){
            wbsItem.setDelFlag("0");
            //保存项目
            itemService.save(wbsItem);
        }
//        List<JkxlDetailFee> jkxlDetailFees = overheadLineMapper.findList(id,type);
//        if(jkxlDetailFees != null && jkxlDetailFees.size()>0){
//            overheadLineMapper.delete(id);
//            feeAdjustService.delete(id);
//            settlementService.delete(id);
//        }
        //获取调差系数
//        double[] tcRate = new double[3];
//        tcRate[0] = ruleRatioService.getValueByName(BashInfo.RATIO_RGTC).getValue()/100;
        //读取表格
        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 fbrcg = new ImportExcel(file,1,new String[]{BashInfo.SHEET_FBRCG,BashInfo.SHEET_FBRCG1,BashInfo.SHEET_FBRCG2,BashInfo.SHEET_ZBRCG,BashInfo.SHEET_ZBRCG1},true);   //发包人采购材料设备计价表
        ImportExcel cbrsb = new ImportExcel(file,1,new String[]{BashInfo.SHEET_TBRCG,BashInfo.SHEET_CBRCG},false);//承包人采购设备计价表
        List<Settlement> settlementList =  SettementUtil.getComparison(total);
        double cbperson = 0.0;
        //措施费合计
        double jzcs = BashInfo.getJzCs(csqd);
        double azcs = BashInfo.getAzCs(csqd);
//        double word1total =  ExcelUtil.getDouble(csqd,BashInfo.QT_HJ,0,4);//获取措施费清单计价表一
        double word1total = jzcs+azcs;//获取措施费清单计价表一
        double word2total=0.0;
        double csFees2 = ExcelUtil.getDouble(total,BashInfo.CSXM2,1,2);//措施费二
        double otherDlTotal =  ExcelUtil.getDouble(qt,BashInfo.QT_HJ,0,2);//获取其他项目费合计
        double feesDlCount =  ExcelUtil.getDouble(gf,BashInfo.QT_HJ,0,4);//获取规费合计
        int[] comlumn2 = {12,15};//定义所要数组
        List<JkxlAdjustFee> wsbFees = BashInfo.getWsbFee(total);
        double wsbfee = 0.00;
        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 jzcsRate = BashInfo.getJzRate(csqd); //获取建筑措施费率
        double[] azcsRate = BashInfo.getAzRate(csqd); //获取安装措施费率
        double[] jzgfRate = BashInfo.getJzGfRate(gf);  //获取建筑规费率
//        double[] azgfRate = BashInfo.getAzGfRate(gf);  //获取安装规费率
        double[] qtFees = BashInfo.getQtFees(qt);  //获取其他费用
        double gcjsFee = BashInfo.getGcjsFee(total);  //获取工程结算价
        double tax = 0.00; //税金
        double totalfee = 0.00;  //合计
        //获取补税额
        double btex = BashInfo.getBtex(total);
        //承包人采购设备费
        if(ImportExcel.getSheet(cbrsb) != null){
            cbperson = ExcelUtil.getDouble(cbrsb,BashInfo.QT_HJ,0,6);//投标人合计
        }
        double cbrfee = ExcelUtil.getDouble(total,BashInfo.QT_CBRSB,1,2);
        //安装措施费
        double azCsFee = BashInfo.getAzCsfee(csqd);
        double[] csJzFee = BashInfo.getJzCsfee(csqd); //建筑措施费
        //规费
        double azGf = BashInfo.getAzGf(gf);
        //获取结算表中基础数据
        double jsCs = ExcelUtil.getDouble(total,BashInfo.JS_CSXM,1,2);
        double jsGf = ExcelUtil.getDouble(total,BashInfo.JS_GF,1,2);
        double textotal = ExcelUtil.getTex(total);
        double fbrFees = ExcelUtil.getFbr(total); //获取发包人费
        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(fbrcg,BashInfo.QT_XJ,0,6); //建筑发包人采购计价表合计金额
            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 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.getFlag;
            //数据核验并保存数据库
            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.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.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.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 = BashInfo.getFbrAzFee(fbrcg);  //安装发包人采购计价表合计金额
            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.getFlag;
            //数据核验并保存数据库
            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.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.setTotalFee(jkxlDetailFee1.getTotalFee() - cs * rate);
                    }
                }
            }

        } else {
            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(fbrcg,BashInfo.QT_XJ,0,6); //建筑发包人采购计价表合计金额
////            double fbrAzFee = BashInfo.getFbrAzFee(fbrcg);  //安装发包人采购计价表合计金额
            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[] 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.getFlag;
            //数据核验并保存数据库
            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.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 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.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.setTotalFee(jkxlDetailFee1.getTotalFee() - cs * rate);
                    }
                }
            }
        }
        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 textotal = 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 ftRate = jkxlDetailFee1.getZjgcFee()/zjgcTotal;
            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);
        }
        //分摊税
        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);

        }
        //计算差额
        for(JkxlDetailFee jkxlDetailFee : list){
            tz += jkxlDetailFee.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("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);
        }
//        List<JkxlDetailFee> jkxlDetailFees = overheadLineMapper.findList(id,type);
//        if(jkxlDetailFees != null && jkxlDetailFees.size()>0){
//            overheadLineMapper.delete(id);
//            feeAdjustService.delete(id);
//            settlementService.delete(id);
//        }
        //获取调差系数
//        double[] tcRate = new double[3];
//        tcRate[0] = ruleRatioService.getValueByName(BashInfo.RATIO_RGTC).getValue()/100;
        //读取表格
        ImportExcel total = new ImportExcel(file,1,new String[]{BashInfo.SHEET_HZB,BashInfo.SHEET_HZB1,BashInfo.SHEET_HZB2},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 fbrcg = new ImportExcel(file,1,new String[]{BashInfo.SHEET_FBRCG,BashInfo.SHEET_FBRCG1,BashInfo.SHEET_FBRCG2,BashInfo.SHEET_ZBRCG,BashInfo.SHEET_ZBRCG1},true);   //发包人采购材料设备计价表
        ImportExcel cbrsb = new ImportExcel(file,1,new String[]{BashInfo.SHEET_TBRCG,BashInfo.SHEET_CBRCG},false);//承包人采购设备计价表
        List<Settlement> settlementList =  SettementUtil.getComparison(total);
        double cbperson = 0.0;
        double word1total =  ExcelUtil.getDouble(csqd,BashInfo.QT_HJ,0,4);//获取措施费清单计价表一
        double word2total=0.0;
        double csFees2 = ExcelUtil.getDouble(total,BashInfo.CSXM2,1,2);//措施费二
        double otherDlTotal =  ExcelUtil.getDouble(qt,BashInfo.QT_HJ,0,2);//获取其他项目费合计
        //规费
        double jzGf = BashInfo.getJzGf(gf);
        double azGf = BashInfo.getAzGf(gf);
//        double feesDlCount =  ExcelUtil.getDouble(gf,BashInfo.QT_HJ,0,4);//获取规费合计
        double feesDlCount =  jzGf+azGf;//获取规费合计
        int[] comlumn2 = {12,15};//定义所要数组
        List<JkxlAdjustFee> wsbFees = BashInfo.getWsbFee(total);
        double wsbfee = 0.00;
        double jzgc = ExcelUtil.getDouble(total,BashInfo.HZ_JZGC,1,2);//汇总表建筑金额
        double azgc = ExcelUtil.getDouble(total,BashInfo.HZ_AZGC,1,2);//汇总表安装金额
        double texRate = ExcelUtil.getTexRate(total); //获取税率
        double fbfxFee = ExcelUtil.getDouble(total,BashInfo.JS_FBFX,1,2);  //分部分项工程费
//        double jzcsRate = BashInfo.getJzRate(csqd); //获取建筑措施费率
        double[] azcsRate = BashInfo.getAzRate(csqd); //获取安装措施费率
        double[] jzgfRate = BashInfo.getJzGfRate(gf);  //获取建筑规费率
//        double[] azgfRate = BashInfo.getAzGfRate(gf);  //获取安装规费率
        double[] qtFees = BashInfo.getQtFees(qt);  //获取其他费用
        double gcjsFee = BashInfo.getGcjsFee(total);  //获取工程结算价
        double tax = 0.00; //税金
        double totalfee = 0.00;  //合计
        //获取补税额
        double btex = BashInfo.getBtex(total);
        //承包人采购设备费
        if(ImportExcel.getSheet(cbrsb) != null){
            cbperson = ExcelUtil.getDouble(cbrsb,BashInfo.QT_HJ,0,6);//投标人合计
        }
        double cbrfee = ExcelUtil.getDouble(total,BashInfo.QT_CBRSB,1,2);
        //安装措施费
        double azCsFee = BashInfo.getAzCsfee(csqd);
        double[] csJzFee = BashInfo.getJzCsfee(csqd); //建筑措施费
        //获取结算表中基础数据
        double jsCs = ExcelUtil.getDouble(total,BashInfo.JS_CSXM,1,2);
        double jsGf = ExcelUtil.getDouble(total,BashInfo.JS_GF,1,2);
        double textotal = ExcelUtil.getTex(total);
        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.getFlag;
            //数据核验并保存数据库
            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.setOriginalRate(orRate);
                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);
            }

        } 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.getFlag;
            //数据核验并保存数据库
            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 {
            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(fbrcg,BashInfo.QT_XJ,0,6); //建筑发包人采购计价表合计金额
////            double fbrAzFee = BashInfo.getFbrAzFee(fbrcg);  //安装发包人采购计价表合计金额
            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.getFlag;
            //数据核验并保存数据库
            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 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);
            }
        }
        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 textotal = 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 ftRate = jkxlDetailFee1.getZjgcFee()/zjgcTotal;
            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);
        }
        //分摊税
        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);

        }
        //计算差额
        for(JkxlDetailFee jkxlDetailFee : list){
            tz += jkxlDetailFee.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("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);
    }


}
