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 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);
       JkxlAdjustFee jkxlAdjustFee = new JkxlAdjustFee();
       jkxlAdjustFee.setId(id);
       jkxlAdjustFee.setType(BashInfo.CE);
       List<JkxlAdjustFee> jkxlAdjustFees = feeAdjustService.getTzFee(jkxlAdjustFee);
       JkxlAdjustFee jkxlAdjustFee1 = jkxlAdjustFees.get(0);
       double cFee = jkxlAdjustFee1.getFee();
       double btTotal = 0.00;
       double qtTotal = 0.00;
       double azTotal = 0.00;
       double otherTotal = 0.00;
       for(JkxlDetailFee jkxlDetailFee : list){
           double measuresFee1 = jkxlDetailFee.getMeasuresFee1()!=null? jkxlDetailFee.getMeasuresFee1():0.00;
           double measuresFee2 = jkxlDetailFee.getMeasuresFee2()!=null? jkxlDetailFee.getMeasuresFee2():0.00;
           double measuresFee3 = jkxlDetailFee.getMeasuresFee3()!=null? jkxlDetailFee.getMeasuresFee3():0.00;
           double Tex = jkxlDetailFee.getTex()!=null? jkxlDetailFee.getTex():0.00;
           double otherCost = jkxlDetailFee.getOtherCost()!=null? jkxlDetailFee.getOtherCost():0.00;
           double Fees = jkxlDetailFee.getFees()!=null? jkxlDetailFee.getFees():0.00;
           double qtFee = measuresFee1 + measuresFee2 + measuresFee3 + otherCost + Fees + Tex;
           double rgFee = jkxlDetailFee.getRgCost()!=null? jkxlDetailFee.getRgCost():0.00;
           double cbrFee = jkxlDetailFee.getCbrCost()!=null? jkxlDetailFee.getCbrCost():0.00;
           double fbrFee = jkxlDetailFee.getFbrCost()!=null? jkxlDetailFee.getFbrCost():0.00;
           double jxFee = jkxlDetailFee.getJxCost()!=null? jkxlDetailFee.getJxCost():0.00;
           double azFee = rgFee + cbrFee + jxFee;
           jkxlDetailFee.setQtFee(qtFee);
           jkxlDetailFee.setAzFee(azFee);
       }
       for(JkxlDetailFee jkxlDetailFee : list){
           //本体费用求和
           if(jkxlDetailFee.getOriginalTotalCost() != null){
               btTotal += jkxlDetailFee.getTotalFee();
               azTotal += jkxlDetailFee.getAzFee();
               otherTotal += jkxlDetailFee.getQtFee();
           }
           //其他费用求和
           if(jkxlDetailFee.getOriginalTotalCost() == null && jkxlDetailFee.getTotalFee() != null){
               qtTotal += jkxlDetailFee.getTotalFee();
           }
       }
       for(JkxlDetailFee jkxlDetailFee : list){
           if(jkxlDetailFee.getWbsCode().equals("30000000") || jkxlDetailFee.getWbsCode().equals("40000000")){
               jkxlDetailFee.setTotalFee(btTotal+qtTotal);
               jkxlDetailFee.setAzFee(azTotal);
               jkxlDetailFee.setQtFee(otherTotal);
               jkxlDetailFee.setcFee(cFee);
               jkxlDetailFee.setJsFee(btTotal+qtTotal+cFee);
           }
           if(jkxlDetailFee.getWbsCode().equals("39000000") || jkxlDetailFee.getWbsCode().equals("49000000")){
               jkxlDetailFee.setTotalFee(btTotal);
               jkxlDetailFee.setAzFee(azTotal);
               jkxlDetailFee.setQtFee(otherTotal);
           }
           if(jkxlDetailFee.getWbsCode().equals("44000000") ||jkxlDetailFee.getWbsCode().equals("34000000")){
               jkxlDetailFee.setTotalFee(qtTotal);

           }
       }
       for (int i=0;i<list.size();i++){
           list.get(i).setIndex(i+1);
       }
       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 saveProject(WbsProject wbsProject){
       overheadLineMapper.saveProject(wbsProject);
   }

   //架空线路保存数据
    @Transactional(readOnly = false)
   public void saveJk(MultipartFile file,String name,String id,String projectId,String type) throws IOException, InvalidFormatException {
       WbsItem wbsItem = itemService.get(id);
       if(wbsItem == null){
           WbsItem wbsItem1 = new WbsItem();
           wbsItem1.setProjectId(projectId);
           wbsItem1.setId(id);
           wbsItem1.setItemName(name);
           wbsItem1.setType(type);
           //保存项目
           itemService.save(wbsItem1);
       }
       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;
       tcRate[1] = ruleRatioService.getValueByName(BashInfo.RATIO_CLTC).getValue()/100;
       tcRate[2] = ruleRatioService.getValueByName(BashInfo.RATIO_JXTC).getValue()/100;
       //读取表格
       ImportExcel importExcel = new ImportExcel(file,1,BashInfo.SHEET_FBFXJJ);  //分部分项工程量清单计价表
       ImportExcel csqd = new ImportExcel(file,1,BashInfo.SHEET_CSB);  // 措施项目清单计价表（一）
       ImportExcel gf = new ImportExcel(file,1,BashInfo.SHEET_GFQD);  //规费项目清单计价表
       ImportExcel qt = new ImportExcel(file,1,BashInfo.SHEET_QTFY);  //其他项目清单计价表
       ImportExcel total = new ImportExcel(file,1,BashInfo.SHEET_HZB );  //工程项目竣工结算汇总表
       ImportExcel gzhz = new ImportExcel(file,1,BashInfo.SHEET_GCHZ );   //架空线路工程费用汇总
       ImportExcel fbrcg = new ImportExcel(file,1,BashInfo.SHEET_FBRCG);   //发包人采购材料设备计价表
        ImportExcel cbrcgsb = new ImportExcel(file,1,BashInfo.SHEET_FBRCG);   //发包人采购设备计价表
        //数据核验数据
        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 otherTotal =  ExcelUtil.getDouble(qt,BashInfo.QT_HJ,0,2);//获取其他项目费合计
        double cbperson =  0.0;
       int[] comlumn2 = {12,15};//定义所要数组
       double[] divisiontotal=ExcelUtil.getDoubleArray(importExcel,BashInfo.PROJECTNAME,2,comlumn2);//获取分部分项比对数据清单
       double feescount =  ExcelUtil.getDouble(gf,BashInfo.QT_HJ,0,4);//获取规费合计
       //数据核验并保存数据库
        double wordtotal2 =  0.0;
        List<Settlement> settlements=SettementUtil.getComparison(settlementList,builtTotal,divisiontotal,wordtotal1,wordtotal2,otherTotal,feescount,cbperson);
        settlementService.save(settlements,id);
      //获取补增值税税率调整差额和扣减甲供材超供材料费
       double[] wsbFees = ExcelUtil.getDoubleArray(total, new String[]{BashInfo.BZZSSL,BashInfo.KJJGCC},1,2);
       double csFee = ExcelUtil.getDouble(gzhz,BashInfo.HZ_CH,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 totalFee = ExcelUtil.getDouble(importExcel,BashInfo.PROJECTNAME,2,12);  //获取架空线路合计金额
       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 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 = new BigDecimal(zg/(fbrFee-zg)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); //进项税率
       //将本体工程费用明细存入集合
       List<JkxlDetailFee> list = new ArrayList<>();
       for(int i=0;i<originalTotalFees.length;i++){
           JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
           double originalTotalCost = originalTotalFees[i]; //原合计
           double otherRate = originalTotalCost/totalFee;   //其他费用分摊比例
           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 rgCost = originalRgCost/(1+tcRate[0]);  //后人工费
           double originalCbrCost = originalFees[i][1];  //原承包人采购
           double cbrCost = originalCbrCost/(1+tcRate[1]);  //后承包人采购
           double originalFbrCost = originalFees[i][2];  //原发包人采购
           double fbrCost = originalFbrCost/(1+jxsl); //后发包人采购
           double originalJxrCost = originalFees[i][3];   //原机械费
           double jxCost = originalJxrCost/(1+tcRate[2]); //后机械费
           double measuresFee1 = (zjgcFee-fbrCost*jxsl)*csRate[0]; //措施费一（2）
           double measuresFee2 = rgCost*csRate[1];   //措施费一（1）
           double measuresFee3 = 0.00; //措施费二
           double fees = rgCost*gfRate[1]*1.12+rgCost*gfRate[0];  //规费
           double tax = (originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee-originalFbrCost)*texRate; //税金
           double totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax-originalFbrCost; //合计
           jkxlDetailFee.setOriginalTotalCost(originalTotalCost);
           jkxlDetailFee.setOriginalRate(orRate);
           jkxlDetailFee.setOriginalRgCost(originalRgCost);
           jkxlDetailFee.setRgCost(rgCost);
           jkxlDetailFee.setOriginalCbrCost(originalCbrCost);
           jkxlDetailFee.setCbrCost(cbrCost);
           jkxlDetailFee.setOriginalFbrCost(originalFbrCost);
           jkxlDetailFee.setFbrCost(fbrCost);
           jkxlDetailFee.setOriginalJxCost(originalJxrCost);
           jkxlDetailFee.setJxCost(jxCost);
           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; //总合计金额
       for(JkxlDetailFee jkxlDetailFee : list){
           tz += jkxlDetailFee.getTotalFee();
       }
       double gxFee = Math.abs(gcjsFee-tz-qtFees[0]-qtFees[1]-wsbFees[0]-wsbFees[1]);
       //将其他费用存入集合
       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);
       //保存费用明细
       for(JkxlDetailFee jkxlDetailFee:list){
           overheadLineMapper.save(jkxlDetailFee);
       }
       //保存未识别费用
       JkxlAdjustFee jkxlAdjustFee = new JkxlAdjustFee();
       jkxlAdjustFee.setId(id);
       jkxlAdjustFee.setType(BashInfo.BZZSSL);
       jkxlAdjustFee.setFee(wsbFees[0]);
       feeAdjustService.save(jkxlAdjustFee);
       JkxlAdjustFee jkxlAdjustFee1 = new JkxlAdjustFee();
       jkxlAdjustFee1.setId(id);
       jkxlAdjustFee1.setType(BashInfo.KJJGCC);
       jkxlAdjustFee1.setFee(wsbFees[1]);
       feeAdjustService.save(jkxlAdjustFee1);
       JkxlAdjustFee jkxlAdjustFee2 = new JkxlAdjustFee();
       jkxlAdjustFee2.setId(id);
       jkxlAdjustFee2.setType(BashInfo.CE);
       jkxlAdjustFee2.setFee(gxFee);
       feeAdjustService.save(jkxlAdjustFee2);
       if(csFee!= 0.00){
           JkxlAdjustFee jkxlAdjustFee3 = new JkxlAdjustFee();
           jkxlAdjustFee3.setId(id);
           jkxlAdjustFee3.setType(BashInfo.HZ_CH);
           jkxlAdjustFee3.setFee(csFee);
           feeAdjustService.save(jkxlAdjustFee3);
       }
   }

    //电缆线路保存数据
    @Transactional(readOnly = false)
    public void saveDl(MultipartFile file,String name,String id,String projectId,String type) throws IOException, InvalidFormatException {
        WbsItem wbsItem = itemService.get(id);
        if(wbsItem == null){
            WbsItem wbsItem1 = new WbsItem();
            wbsItem1.setProjectId(projectId);
            wbsItem1.setId(id);
            wbsItem1.setItemName(name);
            wbsItem1.setType(type);
            //保存项目
            itemService.save(wbsItem1);
        }
        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;
        tcRate[1] = ruleRatioService.getValueByName(BashInfo.RATIO_CLTC).getValue()/100;
        tcRate[2] = ruleRatioService.getValueByName(BashInfo.RATIO_JXTC).getValue()/100;
        //读取表格
        ImportExcel jzqd = new ImportExcel(file,1,BashInfo.SHEET_JZFBFXJJ);  //建筑分部分项工程量清单计价表
        ImportExcel azqd = new ImportExcel(file,1,BashInfo.SHEET_AZFBFXJJ);  //安装分部分项工程量清单计价表
        ImportExcel csqd = new ImportExcel(file,1,BashInfo.SHEET_CSB);  // 措施项目清单计价表（一）
        ImportExcel gf = new ImportExcel(file,1,BashInfo.SHEET_GFQD);  //规费项目清单计价表
        ImportExcel qt = new ImportExcel(file,1,BashInfo.SHEET_QTFY);  //其他项目清单计价表
        ImportExcel total = new ImportExcel(file,1,BashInfo.SHEET_HZB );  //工程项目竣工结算汇总表
        ImportExcel jzhz = new ImportExcel(file,1,BashInfo.SHEET_JZHZB );   //电缆线路建筑工程费用汇总表
        ImportExcel azhz = new ImportExcel(file,1,BashInfo.SHEET_HZHZB );   //电缆线路安装工程费用汇总表
        ImportExcel fbrcg = new ImportExcel(file,1,new String[]{BashInfo.SHEET_FBRCG,BashInfo.SHEET_FBRCG1});   //发包人采购材料设备计价表
        List<Settlement> settlementList =  SettementUtil.getComparison(total);
        double built1Total = ExcelUtil.getDouble(jzhz,BashInfo.QT_HJ,0,2);//建筑分布分项工程量清单计价表合计
        double built2Total = ExcelUtil.getDouble(azhz,BashInfo.QT_HJ,0,2);//安装分部分项工程量清单计合计
        double[] builtTotal={built1Total,built2Total};
        List<Settlement> settlementDLList =  SettementUtil.getComparison(total);
        double wordtotal1 =  ExcelUtil.getDouble(csqd,BashInfo.QT_HJ,0,4);//获取措施费清单计价表
        double otherTotal =  ExcelUtil.getDouble(qt,BashInfo.QT_HJ,0,2);//获取其他项目费合计
        double cbperson = 0.0;
        double word1total =  ExcelUtil.getDouble(csqd,BashInfo.QT_HJ,0,4);//获取措施费清单计价表一
        double word2total=0.0;
        double world2total=0.0;//措施费二
        double otherDlTotal =  ExcelUtil.getDouble(qt,BashInfo.QT_HJ,0,2);//获取其他项目费合计
        double feesDlCount =  ExcelUtil.getDouble(gf,BashInfo.QT_HJ,0,4);//获取规费合计
        int[] comlumn2 = {12,15};//定义所要数组
        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 feescount =  ExcelUtil.getDouble(gf,BashInfo.QT_HJ,0,4);//获取规费合计
        List<Settlement> dlSettlements = SettementUtil.getDLComparison(settlementList,builtTotal,division,word1total,word2total,otherDlTotal,feesDlCount,cbperson);
        //数据核验并保存数据库
        settlementService.save(dlSettlements,id);
        //获取补增值税税率调整差额和扣减甲供材超供材料费
        double wsbFees = ExcelUtil.getDouble(total, BashInfo.KJYJSG,1,2);
//        double csFee = ExcelUtil.getDouble(gzhz,BashInfo.HZ_CH,1,2);  //措施项目费
        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.getDoubleArrays(azhz,BashInfo.HZ_AZ,1,new int[]{3,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 texRate = ExcelUtil.getTexRate(total); //获取税率
        double fbfxFee = ExcelUtil.getDouble(total,BashInfo.JS_FBFX,1,2);  //分部分项工程费
        double totalJzFee = ExcelUtil.getDouble(jzqd,BashInfo.DLJZ,2,12);  //获取电缆建筑合计金额
        double totalAzFee = ExcelUtil.getDouble(jzqd,BashInfo.DLAZ,2,12);  //获取电缆安装合计金额
//        double totalFee = totalJzFee + totalAzFee;
        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 fbrJzFee = ExcelUtil.getDouble(fbrcg,BashInfo.QT_XJ,0,6); //建筑发包人采购计价表合计金额
        double fbrAzFee = BashInfo.getFbrAzFee(fbrcg);  //安装发包人采购计价表合计金额
        double zjgcJzTotal = ExcelUtil.getDouble(jzhz,BashInfo.QT_HJ,0,3);//建筑直接工程费合计
        double zjgcAzTotal = ExcelUtil.getDouble(azhz,BashInfo.QT_HJ,0,3);//安装直接工程费合计
        double[] aqwmJzFees = ExcelUtil.getDoubleArray(csqd,BashInfo.CS_AQWM,1, new int[]{3, 4});  //建筑安全文明施工费
        double[] aqwmAzFees = BashInfo.getAqwfAzFee(csqd);  //建筑安全文明施工费
        double zgJz = zjgcJzTotal-(aqwmJzFees[1]/(aqwmJzFees[0]/100));
        double zgAz = zjgcAzTotal-(aqwmAzFees[1]/(aqwmAzFees[0]/100));
        double jxslJz = new BigDecimal(zgJz/(fbrJzFee-zgJz)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); //建筑进项税率
        double jxslAz = new BigDecimal(zgAz/(fbrAzFee-zgAz)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); //安装进项税率
        //将本体工程费用明细存入集合
        List<JkxlDetailFee> list = new ArrayList<>();
        for(int i=0;i<originalTotalJzFees.length;i++){
            JkxlDetailFee jkxlDetailFee = new JkxlDetailFee();
            double originalTotalCost = originalTotalJzFees[i]; //原合计
//            double otherRate = originalTotalCost/totalFee;   //其他费用分摊比例
//            double orRate = new BigDecimal(otherRate).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue(); //四舍五入
            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 cbrCost = originalCbrCost/(1+tcRate[1]);  //后承包人采购
            double originalFbrCost = originalJzFees[i][2];  //原发包人采购
            double fbrCost = originalFbrCost/(1+jxslJz); //后发包人采购
            double originalJxrCost = originalJzFees[i][3];   //原机械费
            double jxCost = originalJxrCost/(1+tcRate[2]); //后机械费
            double measuresFee1 = (zjgcJzFee-originalFbrCost)*jzcsRate; //措施费一（2）
            double measuresFee2 = 0.00;   //措施费一（1）
            double measuresFee3 = 0.00; //措施费二
            double fees = (zjgcJzFee-originalFbrCost)*jzgfRate[0]+(zjgcJzFee-originalFbrCost)*jzgfRate[1]*0.2;  //规费
            double tax = (originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee)*texRate; //税金
            double totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax; //合计
            jkxlDetailFee.setOriginalTotalCost(originalTotalCost);
//            jkxlDetailFee.setOriginalRate(orRate);
            jkxlDetailFee.setOriginalRgCost(originalRgCost);
            jkxlDetailFee.setRgCost(rgCost);
            jkxlDetailFee.setOriginalCbrCost(originalCbrCost);
            jkxlDetailFee.setCbrCost(cbrCost);
            jkxlDetailFee.setOriginalFbrCost(originalFbrCost);
            jkxlDetailFee.setFbrCost(fbrCost);
            jkxlDetailFee.setOriginalJxCost(originalJxrCost);
            jkxlDetailFee.setJxCost(jxCost);
            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][0];//直接工程费
            double rgAzFee = zjgcAzFees[i][1];//人工费
            double originalRgCost = originalAzFees[i][0];// 原人工费
            double rgCost = originalRgCost/(1+tcRate[0]);  //后人工费
            double originalCbrCost = originalAzFees[i][1];  //原承包人采购
            double cbrCost = originalCbrCost/(1+tcRate[1]);  //后承包人采购
            double originalFbrCost = originalAzFees[i][2];  //原发包人采购
            double fbrCost = originalFbrCost/(1+jxslJz); //后发包人采购
            double originalJxrCost = originalAzFees[i][3];   //原机械费
            double jxCost = originalJxrCost/(1+tcRate[2]); //后机械费
            double measuresFee1 = (zjgcAzFee-originalFbrCost/(1+jxslAz)*jxslAz)*azcsRate[0]+(rgAzFee/(1+tcRate[0])*azcsRate[1]); //措施费一（2）
            double measuresFee2 = 0.00;   //措施费一（1）
            double measuresFee3 = 0.00; //措施费二
            double fees = rgAzFee/(1+tcRate[0])*azgfRate[0]+rgAzFee/(1+tcRate[0])*azgfRate[1]*1.2;  //规费
            double tax = (originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee)*texRate; //税金
            double totalfee = originalTotalCost+measuresFee1+measuresFee2+measuresFee3+fees+otherFee+tax; //合计
            jkxlDetailFee.setOriginalTotalCost(originalTotalCost);
            jkxlDetailFee.setOriginalRgCost(originalRgCost);
            jkxlDetailFee.setRgCost(rgCost);
            jkxlDetailFee.setOriginalCbrCost(originalCbrCost);
            jkxlDetailFee.setCbrCost(cbrCost);
            jkxlDetailFee.setOriginalFbrCost(originalFbrCost);
            jkxlDetailFee.setFbrCost(fbrCost);
            jkxlDetailFee.setOriginalJxCost(originalJxrCost);
            jkxlDetailFee.setJxCost(jxCost);
            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);
        }
        double tz = 0.00; //总合计金额
        for(JkxlDetailFee jkxlDetailFee : list){
            tz += jkxlDetailFee.getTotalFee();
        }
        double gxFee = Math.abs(gcjsFee-tz-qtFees[0]-qtFees[1]-wsbFees);
        //将其他费用存入集合
        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);
        //保存费用明细
        for(JkxlDetailFee jkxlDetailFee:list){
            overheadLineMapper.save(jkxlDetailFee);
        }
        //保存未识别费用
        JkxlAdjustFee jkxlAdjustFee = new JkxlAdjustFee();
        jkxlAdjustFee.setId(id);
        jkxlAdjustFee.setType(BashInfo.KJYJSG);
        jkxlAdjustFee.setFee(wsbFees);
        feeAdjustService.save(jkxlAdjustFee);
//        JkxlAdjustFee jkxlAdjustFee1 = new JkxlAdjustFee();
//        jkxlAdjustFee1.setId(id);
//        jkxlAdjustFee1.setType(BashInfo.KJJGCC);
//        jkxlAdjustFee1.setFee(wsbFees[1]);
//        feeAdjustService.save(jkxlAdjustFee1);
        JkxlAdjustFee jkxlAdjustFee2 = new JkxlAdjustFee();
        jkxlAdjustFee2.setId(id);
        jkxlAdjustFee2.setType(BashInfo.CE);
        jkxlAdjustFee2.setFee(gxFee);
        feeAdjustService.save(jkxlAdjustFee2);
//        if(csFee!= 0.00){
//            JkxlAdjustFee jkxlAdjustFee3 = new JkxlAdjustFee();
//            jkxlAdjustFee3.setId(id);
//            jkxlAdjustFee3.setType(BashInfo.HZ_CH);
//            jkxlAdjustFee3.setFee(csFee);
//            feeAdjustService.save(jkxlAdjustFee3);
//        }
    }


}
