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

import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.common.utils.sg.ExcelUtil;
import com.jeeplus.modules.sg.overheadline.entity.JkxlAdjustFee;
import com.jeeplus.modules.sg.overheadline.entity.JkxlDetailFee;
import com.jeeplus.modules.sg.overheadline.entity.Settlement;
import com.jeeplus.modules.sg.overheadline.entity.WbsProject;
import com.jeeplus.modules.sg.overheadline.mapper.OverheadLineMapper;
import com.jeeplus.modules.sg.overheadline.service.FeeAdjustService;
import com.jeeplus.modules.sg.overheadline.service.RuleRatioService;
import com.jeeplus.modules.sg.overheadline.service.SettlementService;
import com.jeeplus.modules.sg.overheadline.util.BashInfo;
import com.jeeplus.modules.sg.overheadline.util.SettementUtil;
import com.jeeplus.modules.sg.project.entity.WbsItem;
import com.jeeplus.modules.sg.project.service.ItemService;
import com.jeeplus.modules.sg.substation.util.SubstationInfo;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

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

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



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


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


   @Transactional(readOnly = false)
    public void save(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(SubstationInfo.RATIO_RGTC).getValue()/100;
        tcRate[1] = ruleRatioService.getValueByName(SubstationInfo.RATIO_CLTC).getValue()/100;
        tcRate[2] = ruleRatioService.getValueByName(SubstationInfo.RATIO_JXTC).getValue()/100;

        //读取表格
        ImportExcel total = new ImportExcel(file,1,new String[]{SubstationInfo.SHEET_HZB,SubstationInfo.SHEET_HZB1} );  //工程项目竣工结算汇总表
        ImportExcel csqd1 = new ImportExcel(file,1,new String[]{SubstationInfo.SHEET_CSB,SubstationInfo.SHEET_CSB1});  // 措施项目清单计价表（一）
        ImportExcel csqd2 = new ImportExcel(file,1,SubstationInfo.SHEET_CSB2);  // 措施项目清单计价表（二）
        ImportExcel gf = new ImportExcel(file,1,SubstationInfo.SHEET_GFQD);  //规费项目清单计价表
        ImportExcel qt = new ImportExcel(file,1,SubstationInfo.SHEET_QTFY);  //其他项目清单计价表
        ImportExcel fbrcg = new ImportExcel(file,1,SubstationInfo.SHEET_ZBRCGSB );   //招标人采购材料设备计价表
        ImportExcel tbrsb = new ImportExcel(file,1,SubstationInfo.SHEET_TBRCGSB);//投标人采购设备计价表
        double jzgc = ExcelUtil.getDouble(total,SubstationInfo.HZ_JZGC,1,2);//汇总表建筑金额
        double azgc = ExcelUtil.getDouble(total,SubstationInfo.HZ_AZGC,1,2);//汇总表安装金额
        double texRate = ExcelUtil.getTexRate(total); //获取税率
        double fbfxFee = ExcelUtil.getDouble(total,SubstationInfo.JS_FBFX,1,2);  //分部分项工程费
        double jzcsRate = BashInfo.getJzRate(csqd1); //获取建筑措施费率
        double[] azcsRate = BashInfo.getAzRate(csqd1); //获取安装措施费率
        double[] jzgfRate = BashInfo.getJzGfRate(gf);  //获取建筑规费率
        double[] azgfRate = BashInfo.getAzGfRate(gf);  //获取安装规费率
        double[] qtFees = BashInfo.getQtFees(qt);  //获取其他费用
        double gcjsFee = BashInfo.getGcjsFee(total);  //获取工程结算价
       //获取未识别费用
        List<JkxlAdjustFee> wsbFees = BashInfo.getWsbFee(total);
        double wsbfee = 0.00;
        double tax = 0.00; //税金
        double totalfee = 0.00;  //合计
        //变电站 结算汇总
        List<Settlement> settlementsBdz =  SettementUtil.getComparison(total);
        double cbperson = ExcelUtil.getDouble(tbrsb,BashInfo.QT_HJ,0,6);//获取投标人设备表合计
        double word1total =  ExcelUtil.getDouble(csqd1,BashInfo.QT_HJ,0,4);//获取措施费清单计价表一
        double word2total=ExcelUtil.getDouble(csqd2,SubstationInfo.CS_JZ,0,10);//获取措施费二表措施项目合计金额
        double otherBdzTotal =  ExcelUtil.getDouble(qt,BashInfo.QT_HJ,0,2);//获取其他项目费合计
        double feesBdzCount =  ExcelUtil.getDouble(gf,BashInfo.QT_HJ,0,4);//获取规费合计
        if(jzgc !=0.00 && azgc != 0.00){
            ImportExcel jzqd = new ImportExcel(file, 1, SubstationInfo.SHEET_JZFBFXJJ);  //建筑分部分项工程量清单计价表
            ImportExcel azqd = new ImportExcel(file, 1, SubstationInfo.SHEET_AZFBFXJJ);  //安装分部分项工程量清单计价表
            ImportExcel jzhz = new ImportExcel(file, 1, SubstationInfo.SHEET_JZHZB);   //建筑工程费用汇总表
            ImportExcel azhz = new ImportExcel(file, 1, SubstationInfo.SHEET_HZHZB);   //安装工程费用汇总表
            //变电站结算汇总
            int[] comlumn2 = {12,15};//定义所要数组
            double built1Total = ExcelUtil.getDouble(jzhz,BashInfo.QT_HJ,0,2);//变电站建筑
            double built2Total = ExcelUtil.getDouble(azhz,BashInfo.QT_HJ,0,2);//变电站安装
            double[] builtTotal={built1Total,built2Total};
            double[] division1total=ExcelUtil.getDoubleArray(jzqd,SubstationInfo.JZMX_BJZ,2,comlumn2);//建筑分部分项比对数据清单合计
            double[] division2total=ExcelUtil.getDoubleArray(azqd,SubstationInfo.AZMX_BDZAZ,2,comlumn2);//安装分部分项比对数据清单合计
            double[] division = {division1total[0],division1total[1],division2total[0],division2total[1]};
            List<Settlement> settlementBdzs = SettementUtil.getDLComparison(settlementsBdz,builtTotal,division,word1total,word2total,otherBdzTotal,feesBdzCount,cbperson);
            settlementService.save(settlementBdzs,id);
            double zjgcJzTotal = ExcelUtil.getDouble(jzhz,SubstationInfo.QT_HJ,0,3);//建筑直接工程费合计
            double zjgcAzTotal = ExcelUtil.getDouble(azhz,SubstationInfo.QT_HJ,0,3);//安装直接工程费合计
            double csJzFee = SubstationInfo.getCsJzFee(csqd2);//建筑措施分摊费
            double csAzFee = SubstationInfo.getCsAzFee(csqd2);//安装措施分摊费
            double tbrJzFee = BashInfo.getFbrJzFee(tbrsb); //投标人建筑费
            double tbrAzFee = BashInfo.getFbrAzFee(tbrsb); //投标人安装费
            double zjgcTotal = zjgcAzTotal+zjgcJzTotal;
            double[] aqwmJzFees = ExcelUtil.getDoubleArray(csqd1,SubstationInfo.CS_SGZJ,1, new int[]{3, 4});  //建筑安全文明施工费
            double[] aqwmAzFees = BashInfo.getAqwfAzFee(csqd1);  //安装安全文明施工费
            double fbrJzFee = ExcelUtil.getDouble(jzqd,SubstationInfo.JZMX_BJZ,2,15);
            double fbrAzFee = ExcelUtil.getDouble(azqd,SubstationInfo.AZMX_BDZAZ,2,15);
            double zgJz = zjgcJzTotal-aqwmJzFees[1]/aqwmJzFees[0]*100;
            double zgAz = zjgcAzTotal-aqwmAzFees[1]/aqwmAzFees[0]*100;
            double jxslJz = zgJz/(fbrJzFee-zgJz); //建筑进项税率
            double jxslAz = zgAz/(fbrAzFee-zgAz); //安装进项税率
            //建筑部分基础数据
            JkxlDetailFee jz = SubstationInfo.getTolalInfo(jzqd,jzhz,"21000000",SubstationInfo.JZMX_BJZ);  //建筑合计
            JkxlDetailFee jzsc = SubstationInfo.getBashInfo(jzqd,jzhz,"21100000",SubstationInfo.JZMX_ZYGC);  //主要生产工程
            List<JkxlDetailFee> scjz = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.ZYSCWBS,SubstationInfo.ZYSCJZSTR); //主要生产建筑
            List<JkxlDetailFee> pdzz = SubstationInfo.getPdzzjz(jzqd,jzhz);    //配电装置
            List<JkxlDetailFee> gsxt  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.GSXTWBS,SubstationInfo.GSXTSTR); //供水系统建筑
            List<JkxlDetailFee> xfxt  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.XFXTWBS,SubstationInfo.XFXTSTR); //消防系统
            JkxlDetailFee jzfz = SubstationInfo.getBashInfo(jzqd,jzhz,"21200000",SubstationInfo.JZMX_FZSCGC);  //辅助生产工程
            List<JkxlDetailFee> fzscjz  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.FZSCWBS,SubstationInfo.FZSCSTR); //辅助生产建筑
            List<JkxlDetailFee> zqxjz  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.ZQXWBS,SubstationInfo.ZQXSTR); //站区性建筑
            List<JkxlDetailFee> tsgzw  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.TXGJWWBS,SubstationInfo.TXGJWSTR); //特殊构筑物
            JkxlDetailFee zqlh = SubstationInfo.getBashInfo(jzqd,jzhz,"21240000",SubstationInfo.JZMX_ZQLH);  //站区绿化
            List<JkxlDetailFee> dxgc  = SubstationInfo.getBashInfo(jzqd,jzhz,SubstationInfo.YDZYGWBS,SubstationInfo.YDZYGSTR); //与站址有关的单项工程
            List<JkxlDetailFee> jzTotal = new ArrayList<>();
            jzTotal.add(jz);
            jzTotal.add(jzsc);
            jzTotal.addAll(scjz);
            jzTotal.addAll(pdzz);
            jzTotal.addAll(gsxt);
            jzTotal.addAll(xfxt);
            jzTotal.add(jzfz);
            jzTotal.addAll(fzscjz);
            jzTotal.addAll(zqxjz);
            jzTotal.addAll(tsgzw);
            jzTotal.add(zqlh);
            jzTotal.addAll(dxgc);
            //安装部分基础数据
            JkxlDetailFee az = SubstationInfo.getTolalInfo(azqd,azhz,"23000000",SubstationInfo.AZMX_BDZAZ);  //安装合计
            JkxlDetailFee azsc = SubstationInfo.getBashInfo(azqd,azhz,"23100000",SubstationInfo.AZMX_ZYSCGC);  //主要生产工程
            List<JkxlDetailFee> zbyq  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.ZBYQWBS,SubstationInfo.ZBYQSTR); //主变压器系统
            List<JkxlDetailFee> pdzzs  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.PDZZWBS,SubstationInfo.PDZZSTR); //配电装置
            List<JkxlDetailFee> wgbc  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.WGBCWBS,SubstationInfo.WGBCSTR); //无功补偿
            List<JkxlDetailFee> kzzl = SubstationInfo.getKzzl(azqd,azhz);   //控制直流
            List<JkxlDetailFee> zyd  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.ZYDWBS,SubstationInfo.ZYDSTR); //站用电系统
            List<JkxlDetailFee> dljd = SubstationInfo.getDljd(azqd,azhz);    //电缆接地
            List<JkxlDetailFee> txyd  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.TXYDWBS,SubstationInfo.TXYDSTR); //通信及远动系统
            List<JkxlDetailFee> yzts  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.QZTSWBS,SubstationInfo.QZTSSTR); //全站调试
            List<JkxlDetailFee> fzsc  = SubstationInfo.getBashInfo(azqd,azhz,SubstationInfo.AFZSCWBS,SubstationInfo.AFZSCSTR); //辅助生产工程
            JkxlDetailFee azdx = SubstationInfo.getBashInfo(azqd,azhz,"23300000",SubstationInfo.AZMX_ZZDXGC);  //与站址有关的单项工程
            List<JkxlDetailFee> zwdy  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.ZWDYWBS,SubstationInfo.ZWDYSTR); //站外电源
            List<JkxlDetailFee> zwtx  = SubstationInfo.getBashInfos(azqd,azhz,SubstationInfo.ZWTXWBS,SubstationInfo.ZWTXSTR); //站外通信
            List<JkxlDetailFee> azTotal = new ArrayList<>();
            azTotal.add(az);
            azTotal.add(azsc);
            azTotal.addAll(zbyq);
            azTotal.addAll(pdzzs);
            azTotal.addAll(wgbc);
            azTotal.addAll(kzzl);
            azTotal.addAll(zyd);
            azTotal.addAll(dljd);
            azTotal.addAll(txyd);
            azTotal.addAll(yzts);
            azTotal.addAll(fzsc);
            azTotal.add(azdx);
            azTotal.addAll(zwdy);
            azTotal.addAll(zwtx);
            boolean flagJz = SubstationInfo.getFlag(jzTotal);
            boolean flagAz = SubstationInfo.getFlag(azTotal);
            for(JkxlDetailFee jkxlDetailFee:jzTotal){
                 double originalTotalCost = jkxlDetailFee.getOriginalTotalCost();
                 double originalFbrCost = jkxlDetailFee.getOriginalFbrCost();
                 double zjgcJzFee = jkxlDetailFee.getZjgcFee();
                 double rateCs = zjgcJzFee/zjgcJzTotal;  //措施费分摊比例
                 double rateQt = zjgcJzFee/zjgcTotal;    //其他费用分摊比例
                 jkxlDetailFee.setRgCost(jkxlDetailFee.getOriginalRgCost()/(1+tcRate[0]));
                 jkxlDetailFee.setCbrCost(jkxlDetailFee.getOriginalCbrCost()/(1+tcRate[1]));
                 jkxlDetailFee.setFbrCost(originalFbrCost/(1+jxslJz));
                 jkxlDetailFee.setJxCost(jkxlDetailFee.getOriginalJxCost()/(1+tcRate[2]));
                 double measuresFee1 = (zjgcJzFee -(originalFbrCost/(1+jxslJz)*jxslJz))*jzcsRate; //措施费一（1）
                 double measuresFee2 = csJzFee*rateCs;   //措施费一（2）
                 double measuresFee3 = 0.00; //措施费二
                 double otherFee =qtFees[2]*rateQt; //其他费用
                 double fees = (zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*(jzgfRate[0]+jzgfRate[1]*0.2);  //规费
                if(flagJz && flagAz){
                    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; //合计
                }
//                 double tax = (originalTotalCost+measuresFee1+measuresFee2+fees+otherFee-originalFbrCost)*texRate; //税金
//                 double totalfee = originalTotalCost+measuresFee1+measuresFee2+fees+otherFee+tax-originalFbrCost; //合计
                 jkxlDetailFee.setMeasuresFee1(measuresFee1);
                 jkxlDetailFee.setMeasuresFee2(measuresFee2);
                 jkxlDetailFee.setMeasuresFee3(measuresFee3);
                 jkxlDetailFee.setFees(fees);
                 jkxlDetailFee.setOtherCost(otherFee);
                 jkxlDetailFee.setTex(tax);
                 jkxlDetailFee.setTotalFee(totalfee);
                 jkxlDetailFee.setId(id);
            }
            //建筑部门求和
            JkxlDetailFee jzDetail = jzTotal.get(0);
            double jztotalFee =  jzDetail.getTotalFee()+tbrJzFee*(1+texRate);
            double jztex =  jzDetail.getTex()+tbrJzFee*texRate;
            jzDetail.setTotalFee(jztotalFee);
            jzDetail.setTex(jztex);
            jzTotal.set(0,jzDetail);
            for(JkxlDetailFee jkxlDetailFee:azTotal){
                double originalTotalCost = jkxlDetailFee.getOriginalTotalCost();
                double originalFbrCost = jkxlDetailFee.getOriginalFbrCost();
                double rgAzFee = jkxlDetailFee.getOriginalRgCost();
                double zjgcAzFee = jkxlDetailFee.getZjgcFee();
                double rateCs = zjgcAzFee/zjgcAzTotal;  //措施费分摊比例
                double rateQt = zjgcAzFee/zjgcTotal;    //其他费用分摊比例
                jkxlDetailFee.setRgCost(jkxlDetailFee.getOriginalRgCost()/(1+tcRate[0]));
                jkxlDetailFee.setCbrCost(jkxlDetailFee.getOriginalCbrCost()/(1+tcRate[1]));
                jkxlDetailFee.setFbrCost(originalFbrCost/(1+jxslAz));
                jkxlDetailFee.setJxCost(jkxlDetailFee.getOriginalJxCost()/(1+tcRate[2]));
                double measuresFee1 = (zjgcAzFee-originalFbrCost/(1+jxslAz)*jxslAz)*azcsRate[0]+(rgAzFee/(1+tcRate[0])*azcsRate[1]); //措施费一（2）
                double measuresFee2 = csAzFee*rateCs;   //措施费一（2）
                double measuresFee3 = 0.00; //措施费二
                double otherFee =qtFees[2]*rateQt; //其他费用
                double fees = rgAzFee/(1+tcRate[0])*azgfRate[0]+rgAzFee/(1+tcRate[0])*azgfRate[1]*1.6;  //规费
//                double tax = (originalTotalCost+measuresFee1+measuresFee2+fees+otherFee-originalFbrCost)*texRate; //税金
//                double totalfee = originalTotalCost+measuresFee1+measuresFee2+fees+otherFee+tax-originalFbrCost; //合计
                if(flagJz && flagAz){
                    tax = (originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee-originalFbrCost)*texRate; //税金
                    totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax-originalFbrCost; //合计
                } else {
                    tax = (originalTotalCost + measuresFee1 + measuresFee2 + measuresFee3 + fees + otherFee) * texRate; //税金
                    totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax; //合计
                }
                jkxlDetailFee.setMeasuresFee1(measuresFee1);
                jkxlDetailFee.setMeasuresFee2(measuresFee2);
                jkxlDetailFee.setMeasuresFee3(measuresFee3);
                jkxlDetailFee.setFees(fees);
                jkxlDetailFee.setOtherCost(otherFee);
                jkxlDetailFee.setTex(tax);
                jkxlDetailFee.setTotalFee(totalfee);
                jkxlDetailFee.setId(id);
            }
            //安装求和
            JkxlDetailFee azDetail = azTotal.get(0);
            double aztotalFee =  azDetail.getTotalFee()+tbrAzFee*(1+texRate);
            azDetail.setTotalFee(aztotalFee);
            azTotal.set(0,azDetail);

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

            if(wsbFees != null && wsbFees.size()>0){
                for(JkxlAdjustFee jkxlAdjustFee : wsbFees){
                    jkxlAdjustFee.setId(id);
                    feeAdjustService.save(jkxlAdjustFee);
                    wsbfee += jkxlAdjustFee.getFee();
                }
            }
            double gxFee = Math.abs(gcjsFee-tz-qtFees[0]-qtFees[1] -wsbfee);
            //将其他费用存入集合
            JkxlDetailFee jkxlDetailFee1 = new JkxlDetailFee();
            JkxlDetailFee jkxlDetailFee2 = new JkxlDetailFee();
            jkxlDetailFee1.setId(id);
            jkxlDetailFee1.setWbsCode("24120000");
            jkxlDetailFee1.setTotalFee(qtFees[0]);
            jkxlDetailFee2.setWbsCode("24220000");
            jkxlDetailFee2.setTotalFee(qtFees[1]);
            jkxlDetailFee2.setId(id);
            azTotal.add(jkxlDetailFee1);
            azTotal.add(jkxlDetailFee2);
            overheadLineMapper.saveList(jzTotal);
            overheadLineMapper.saveList(azTotal);
            JkxlAdjustFee jkxlAdjustFee = new JkxlAdjustFee();
            jkxlAdjustFee.setId(id);
            jkxlAdjustFee.setType(BashInfo.CE);
            jkxlAdjustFee.setFee(gxFee);
            feeAdjustService.save(jkxlAdjustFee);
        }
        if(jzgc !=0.00 && azgc == 0.00){
            ImportExcel jzqd = new ImportExcel(file, 1, SubstationInfo.SHEET_JZFBFXJJ);  //建筑分部分项工程量清单计价表
            ImportExcel jzhz = new ImportExcel(file, 1, SubstationInfo.SHEET_JZHZB);   //建筑工程费用汇总表
            //变电站结算汇总
            int[] comlumn2 = {12,15};//定义所要数组
            double built1Total = ExcelUtil.getDouble(jzhz,BashInfo.QT_HJ,0,2);//变电站建筑
            double[] builtTotal={built1Total,0.00};
            double[] division1total=ExcelUtil.getDoubleArray(jzqd,SubstationInfo.JZMX_BJZ,2,comlumn2);//建筑分部分项比对数据清单合计
            double[] division = {division1total[0],division1total[1],0.00,0.00};
            List<Settlement> settlementBdzs = SettementUtil.getDLComparison(settlementsBdz,builtTotal,division,word1total,word2total,otherBdzTotal,feesBdzCount,cbperson);
            settlementService.save(settlementBdzs,id);
            double zjgcJzTotal = ExcelUtil.getDouble(jzhz,SubstationInfo.QT_HJ,0,3);//建筑直接工程费合计
            double csJzFee = SubstationInfo.getCsJzFee(csqd2);//建筑措施分摊费
            double tbrJzFee = BashInfo.getFbrJzFee(tbrsb); //投标人建筑费
            double[] aqwmJzFees = ExcelUtil.getDoubleArray(csqd1,SubstationInfo.CS_SGZJ,1, new int[]{3, 4});  //建筑安全文明施工费
            double fbrJzFee = ExcelUtil.getDouble(jzqd,SubstationInfo.JZMX_BJZ,2,15);
            double zgJz = zjgcJzTotal-aqwmJzFees[1]/aqwmJzFees[0]*100;
            double jxslJz = zgJz/(fbrJzFee-zgJz); //建筑进项税率
            //建筑部分基础数据
            JkxlDetailFee jz = SubstationInfo.getTolalInfo(jzqd,jzhz,"21000000",SubstationInfo.JZMX_BJZ);  //建筑合计
            JkxlDetailFee jzsc = SubstationInfo.getBashInfo(jzqd,jzhz,"21100000",SubstationInfo.JZMX_ZYGC);  //主要生产工程
            List<JkxlDetailFee> scjz = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.ZYSCWBS,SubstationInfo.ZYSCJZSTR); //主要生产建筑
            List<JkxlDetailFee> pdzz = SubstationInfo.getPdzzjz(jzqd,jzhz);    //配电装置
            List<JkxlDetailFee> gsxt  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.GSXTWBS,SubstationInfo.GSXTSTR); //供水系统建筑
            List<JkxlDetailFee> xfxt  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.XFXTWBS,SubstationInfo.XFXTSTR); //消防系统
            JkxlDetailFee jzfz = SubstationInfo.getBashInfo(jzqd,jzhz,"21200000",SubstationInfo.JZMX_FZSCGC);  //辅助生产工程
            List<JkxlDetailFee> fzscjz  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.FZSCWBS,SubstationInfo.FZSCSTR); //辅助生产建筑
            List<JkxlDetailFee> zqxjz  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.ZQXWBS,SubstationInfo.ZQXSTR); //站区性建筑
            List<JkxlDetailFee> tsgzw  = SubstationInfo.getBashInfos(jzqd,jzhz,SubstationInfo.TXGJWWBS,SubstationInfo.TXGJWSTR); //特殊构筑物
            JkxlDetailFee zqlh = SubstationInfo.getBashInfo(jzqd,jzhz,"21240000",SubstationInfo.JZMX_ZQLH);  //站区绿化
            List<JkxlDetailFee> dxgc  = SubstationInfo.getBashInfo(jzqd,jzhz,SubstationInfo.YDZYGWBS,SubstationInfo.YDZYGSTR); //与站址有关的单项工程
            List<JkxlDetailFee> jzTotal = new ArrayList<>();
            jzTotal.add(jz);
            jzTotal.add(jzsc);
            jzTotal.addAll(scjz);
            jzTotal.addAll(pdzz);
            jzTotal.addAll(gsxt);
            jzTotal.addAll(xfxt);
            jzTotal.add(jzfz);
            jzTotal.addAll(fzscjz);
            jzTotal.addAll(zqxjz);
            jzTotal.addAll(tsgzw);
            jzTotal.add(zqlh);
            jzTotal.addAll(dxgc);
            boolean flagJz = SubstationInfo.getFlag(jzTotal);
            for(JkxlDetailFee jkxlDetailFee:jzTotal){
                double originalTotalCost = jkxlDetailFee.getOriginalTotalCost();
                double originalFbrCost = jkxlDetailFee.getOriginalFbrCost();
                double zjgcJzFee = jkxlDetailFee.getZjgcFee();
                double rateCs = zjgcJzFee/zjgcJzTotal;  //措施费分摊比例
                double rateQt = zjgcJzFee/zjgcJzTotal;    //其他费用分摊比例
                jkxlDetailFee.setRgCost(jkxlDetailFee.getOriginalRgCost()/(1+tcRate[0]));
                jkxlDetailFee.setCbrCost(jkxlDetailFee.getOriginalCbrCost()/(1+tcRate[1]));
                jkxlDetailFee.setFbrCost(originalFbrCost/(1+jxslJz));
                jkxlDetailFee.setJxCost(jkxlDetailFee.getOriginalJxCost()/(1+tcRate[2]));
                double measuresFee1 = (zjgcJzFee -(originalFbrCost/(1+jxslJz)*jxslJz))*jzcsRate; //措施费一（1）
                double measuresFee2 = csJzFee*rateCs;   //措施费一（2）
                double measuresFee3 = 0.00; //措施费二
                double otherFee =qtFees[2]*rateQt; //其他费用
                double fees = (zjgcJzFee-(originalFbrCost/(1+jxslJz)*jxslJz))*(jzgfRate[0]+jzgfRate[1]*0.2);  //规费
//                double tax = (originalTotalCost+measuresFee1+measuresFee2+fees+otherFee-originalFbrCost)*texRate; //税金
//                double totalfee = originalTotalCost+measuresFee1+measuresFee2+fees+otherFee+tax-originalFbrCost; //合计
                if(flagJz){
                    tax = (originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee-originalFbrCost)*texRate; //税金
                    totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax-originalFbrCost; //合计
                } else {
                    tax = (originalTotalCost + measuresFee1 + measuresFee2 + measuresFee3 + fees + otherFee) * texRate; //税金
                    totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax; //合计
                }
                jkxlDetailFee.setMeasuresFee1(measuresFee1);
                jkxlDetailFee.setMeasuresFee2(measuresFee2);
                jkxlDetailFee.setMeasuresFee3(measuresFee3);
                jkxlDetailFee.setFees(fees);
                jkxlDetailFee.setOtherCost(otherFee);
                jkxlDetailFee.setTex(tax);
                jkxlDetailFee.setTotalFee(totalfee);
                jkxlDetailFee.setId(id);
            }
            //建筑部门求和
            JkxlDetailFee jzDetail = jzTotal.get(0);
            double jztotalFee =  jzDetail.getTotalFee()+tbrJzFee*(1+texRate);
            double jztex =  jzDetail.getTex()+tbrJzFee*texRate;
            jzDetail.setTotalFee(jztotalFee);
            jzDetail.setTex(jztex);
            jzTotal.set(0,jzDetail);

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

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

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

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



    }








}
