platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractInvoiceServiceImpl.java
@@ -21,6 +21,7 @@
import com.by4cloud.platformx.business.api.feign.RemoteFlowProcessService;
import com.by4cloud.platformx.business.constant.FlowNameEnum;
import com.by4cloud.platformx.business.dto.ContractInvoiceQueryDTO;
import com.by4cloud.platformx.business.dto.ContractQueryDTO;
import com.by4cloud.platformx.business.dto.GenInvoiceInfoDTO;
import com.by4cloud.platformx.business.entity.*;
import com.by4cloud.platformx.business.mapper.*;
@@ -56,6 +57,10 @@
   private final ContractMapper contractMapper;
   private final BusinessCustomerMapper businessCustomerMapper;
   private final ContractOutBoundMapper contractOutBoundMapper;
   private final PaymentConfirmMapper paymentConfirmMapper;
   private final ContractExecDateMapper contractExecDateMapper;
   private final ContractPaymentScheduleMapper contractPaymentScheduleMapper;
   private final MeterReadRecordMapper meterReadRecordMapper;
   private final ContractSubjectMatterMapper subjectMatterMapper;
   private final ProductMapper productMapper;
   private final BwRequestRecordMapper bwRequestRecordMapper;
@@ -87,7 +92,7 @@
            .selectAll(ContractOutBound.class)
            .eq(ContractOutBound::getContractId, contractId);
      List<ContractInvoiceOutBoundVo> invoiceOutBoundVoList = contractOutBoundMapper.selectJoinList(ContractInvoiceOutBoundVo.class, wrapper);
      if (ArrayUtil.isNotEmpty(invoiceOutBoundVoList.toArray())){
      if (ArrayUtil.isNotEmpty(invoiceOutBoundVoList.toArray())) {
         invoiceOutBoundVoList.stream().forEach(contractInvoiceOutBoundVo -> {
            contractInvoiceOutBoundVo.setInvoiceNum(contractInvoiceOutBoundVo.getOutBoundNum().subtract(contractInvoiceOutBoundVo.getInvoiceNum()));
            contractInvoiceOutBoundVo.setToInvoiceNum(contractInvoiceOutBoundVo.getInvoiceNum());
@@ -97,12 +102,29 @@
   }
   @Override
   public R getContractHouseWater(Long contractId) {
      MPJLambdaWrapper<ContractPaymentSchedule> wrapper = new MPJLambdaWrapper<ContractPaymentSchedule>()
            .selectAll(ContractPaymentSchedule.class)
            .eq(ContractPaymentSchedule::getContractId, contractId)
            .orderByAsc(ContractPaymentSchedule::getCreateTime);
      List<ContractHouseWaterVo> invoiceOutBoundVoList = contractPaymentScheduleMapper.selectJoinList(ContractHouseWaterVo.class, wrapper);
      return R.ok(invoiceOutBoundVoList);
   }
   @Override
   public R genInvoiceInfo(GenInvoiceInfoDTO genInvoiceInfoDTO) {
      if (ArrayUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList())) {
         return R.failed("请选择开票出库单后再操作");
      }
      ContractOutBound outBound = contractOutBoundMapper.selectById(genInvoiceInfoDTO.getOutBoundInvoiceList().get(0).getContractOutBoundId());
      Contract contract = contractMapper.selectById(outBound.getContractId());
      Long contractId = null;
      if (!StrUtil.equals(genInvoiceInfoDTO.getContractCategory(),"water_house")) {
         ContractOutBound outBound = contractOutBoundMapper.selectById(genInvoiceInfoDTO.getOutBoundInvoiceList().get(0).getContractOutBoundId());
         contractId = outBound.getContractId();
      }else {
         ContractPaymentSchedule schedule = contractPaymentScheduleMapper.selectById(genInvoiceInfoDTO.getOutBoundInvoiceList().get(0).getContractOutBoundId());
         contractId = schedule.getContractId();
      }
      Contract contract = contractMapper.selectById(contractId);
      if (ObjUtil.isNull(contract)) {
         return R.failed("合同信息异常,请联系技术人员");
      }
@@ -132,57 +154,129 @@
      List<ContractOutBoundMergeVo> mergeOutBounds = new ArrayList<>();
//      List<ContractOutBound> outBounds = contractOutBoundMapper.selectList(Wrappers.<ContractOutBound>lambdaQuery()
//            .in(ContractOutBound::getId, genInvoiceInfoDTO.getOutBoundInvoiceList().stream().map(item->item.getContractOutBoundId()).collect(Collectors.toList())));
      if (ArrayUtil.isNotEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList().toArray())) {
         genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
            ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
      if (!StrUtil.equals(contract.getContractCategory(), "water_house")){
         if (ArrayUtil.isNotEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList().toArray())) {
            genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
               ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
            if (!ArrayUtil.contains(erpCode.toArray(), contractOutBound.getSubjectMatterCode())) {
               erpCode.add(contractOutBound.getSubjectMatterCode());
               ContractOutBoundMergeVo mergeVo = new ContractOutBoundMergeVo();
               mergeVo.setMaterialCode(contractOutBound.getSubjectMatterCode());
               mergeVo.setMaterialInternalName(contractOutBound.getSubjectMatterName());
               mergeVo.setQuantity(contractOutBoundInvoiceVo.getInvoiceNum());
               Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, contractOutBound.getSubjectMatterCode())
                     .eq(Product::getCompId, SecurityUtils.getUser().getCompId()).last("limit 1"));
               if (ObjUtil.isNotNull(product)) {
                  mergeVo.setTaxClass(product.getTaxClass());
                  mergeVo.setSpecification(product.getProductType());
                  mergeVo.setTaxRate(new BigDecimal(product.getTaxRate()));
                  mergeVo.setTaxCode(product.getTaxCode());
                  Product productClass = productMapper.selectById(product.getParentId());
               if (!ArrayUtil.contains(erpCode.toArray(), contractOutBound.getSubjectMatterCode())) {
                  erpCode.add(contractOutBound.getSubjectMatterCode());
                  ContractOutBoundMergeVo mergeVo = new ContractOutBoundMergeVo();
                  mergeVo.setMaterialCode(contractOutBound.getSubjectMatterCode());
                  mergeVo.setMaterialInternalName(contractOutBound.getSubjectMatterName());
                  mergeVo.setQuantity(contractOutBoundInvoiceVo.getInvoiceNum());
                  Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, contractOutBound.getSubjectMatterCode())
                        .eq(Product::getCompId, SecurityUtils.getUser().getCompId()).last("limit 1"));
                  if (ObjUtil.isNotNull(product)) {
                     mergeVo.setItemName("*" + product.getTaxClass() + "*" + productClass.getProductName());
                     mergeVo.setTaxClass(product.getTaxClass());
                     mergeVo.setSpecification(product.getProductType());
                     mergeVo.setTaxRate(new BigDecimal(product.getTaxRate()));
                     mergeVo.setTaxCode(product.getTaxCode());
                     Product productClass = productMapper.selectById(product.getParentId());
                     if (ObjUtil.isNotNull(product)) {
                        if (StrUtil.equals(product.getErpCode(), "water_bill")) {
                           mergeVo.setItemName("*" + product.getTaxClass() + "*水费");
                        } else if (StrUtil.equals(product.getErpCode(), "electricity_bill")) {
                           mergeVo.setItemName("*" + product.getTaxClass() + "*电费");
                        } else if (StrUtil.equals(product.getErpCode(), "house_rental")) {
                           mergeVo.setItemName("*" + product.getTaxClass() + "*房屋租赁");
                        } else {
                           mergeVo.setItemName("*" + product.getTaxClass() + "*" + productClass.getProductName());
                        }
                     }
                  }
                  ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
                        .eq(ContractSubjectMatter::getMaterialCode, contractOutBound.getSubjectMatterCode())
                        .eq(ContractSubjectMatter::getContractId, contract.getId()).last("limit 1"));
                  if (ObjUtil.isNotNull(subjectMatter)) {
                     mergeVo.setUnitPrice(subjectMatter.getUnitPrice());
                     mergeVo.setUnit(subjectMatter.getUnit());
                  }
                  mergeOutBounds.add(mergeVo);
               } else {
                  List<ContractOutBoundMergeVo> filteredList = mergeOutBounds.stream()
                        .filter(item -> contractOutBound.getSubjectMatterCode().equals(item.getMaterialCode())) // 防止空指针,建议常量在前
                        .collect(Collectors.toList());
                  if (ArrayUtil.isNotEmpty(filteredList.toArray())) {
                     filteredList.get(0).setQuantity(filteredList.get(0).getQuantity().add(contractOutBound.getOutBoundNum()));
                  }
               }
               ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
                     .eq(ContractSubjectMatter::getMaterialCode, contractOutBound.getSubjectMatterCode())
                     .eq(ContractSubjectMatter::getContractId, contract.getId()).last("limit 1"));
               if (ObjUtil.isNotNull(subjectMatter)) {
                  mergeVo.setUnitPrice(subjectMatter.getUnitPrice());
                  mergeVo.setUnit(subjectMatter.getUnit());
               }
               mergeOutBounds.add(mergeVo);
            } else {
               List<ContractOutBoundMergeVo> filteredList = mergeOutBounds.stream()
                     .filter(item -> contractOutBound.getSubjectMatterCode().equals(item.getMaterialCode())) // 防止空指针,建议常量在前
                     .collect(Collectors.toList());
               if (ArrayUtil.isNotEmpty(filteredList.toArray())) {
                  filteredList.get(0).setQuantity(filteredList.get(0).getQuantity().add(contractOutBound.getOutBoundNum()));
               }
            }
         });
         if (ArrayUtil.isNotEmpty(mergeOutBounds.toArray())) {
            invoiceItemVoList = BeanUtil.copyToList(mergeOutBounds, InvoiceItemVo.class);
            invoiceItemVoList.stream().forEach(invoiceItemVo -> {
//            for (InvoiceItemVo invoiceItemVo:invoiceItemVoList) {
               BigDecimal itemPrice = invoiceItemVo.getUnitPrice().multiply(invoiceItemVo.getQuantity());
               BigDecimal itemPriceTax = itemPrice.divide(new BigDecimal("100").add(invoiceItemVo.getTaxRate()), 2, RoundingMode.HALF_UP).multiply(invoiceItemVo.getTaxRate());
               invoiceItemVo.setPriceTax(itemPriceTax);
               invoiceItemVo.setPriceNoTax(itemPrice.subtract(itemPriceTax));
            });
            vo.setTotalPrice(invoiceItemVoList.stream().map(item -> item.getPriceTax().add(item.getPriceNoTax())).reduce(BigDecimal.ZERO, BigDecimal::add));
            vo.setInvoiceItemList(invoiceItemVoList);
            vo.setPriceWords(Convert.digitToChinese(vo.getTotalPrice()));
            if (ArrayUtil.isNotEmpty(mergeOutBounds.toArray())) {
               invoiceItemVoList = BeanUtil.copyToList(mergeOutBounds, InvoiceItemVo.class);
               invoiceItemVoList.stream().forEach(invoiceItemVo -> {
//            for (InvoiceItemVo invoiceItemVo:invoiceItemVoList) {
                  BigDecimal itemPrice = invoiceItemVo.getUnitPrice().multiply(invoiceItemVo.getQuantity());
                  BigDecimal itemPriceTax = itemPrice.divide(new BigDecimal("100").add(invoiceItemVo.getTaxRate()), 2, RoundingMode.HALF_UP).multiply(invoiceItemVo.getTaxRate());
                  invoiceItemVo.setPriceTax(itemPriceTax);
                  invoiceItemVo.setPriceNoTax(itemPrice.subtract(itemPriceTax));
               });
               vo.setTotalPrice(invoiceItemVoList.stream().map(item -> item.getPriceTax().add(item.getPriceNoTax())).reduce(BigDecimal.ZERO, BigDecimal::add));
               vo.setInvoiceItemList(invoiceItemVoList);
               vo.setPriceWords(Convert.digitToChinese(vo.getTotalPrice()));
            }
         }
      }else {
         if (ArrayUtil.isNotEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList().toArray())) {
            genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
               ContractPaymentSchedule schedule = contractPaymentScheduleMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
               if (!ArrayUtil.contains(erpCode.toArray(), schedule.getSubjectMatterCode())) {
                  erpCode.add(schedule.getSubjectMatterCode());
                  ContractOutBoundMergeVo mergeVo = new ContractOutBoundMergeVo();
                  mergeVo.setMaterialCode(schedule.getSubjectMatterCode());
                  mergeVo.setMaterialInternalName(schedule.getSubjectMatterName());
                  mergeVo.setPriceAndTax(schedule.getPlannedAmount());
                  Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, schedule.getSubjectMatterCode())
                        .eq(Product::getCompId, SecurityUtils.getUser().getCompId()).last("limit 1"));
                  if (ObjUtil.isNotNull(product)) {
                     mergeVo.setTaxClass(product.getTaxClass());
                     mergeVo.setSpecification(product.getProductType());
                     mergeVo.setTaxRate(new BigDecimal(product.getTaxRate()));
                     mergeVo.setTaxCode(product.getTaxCode());
                     Product productClass = productMapper.selectById(product.getParentId());
                     if (ObjUtil.isNotNull(product)) {
                        if (StrUtil.equals(product.getErpCode(), "water_bill")) {
                           mergeVo.setItemName("*" + product.getTaxClass() + "*水费");
                        } else if (StrUtil.equals(product.getErpCode(), "electricity_bill")) {
                           mergeVo.setItemName("*" + product.getTaxClass() + "*电费");
                        } else if (StrUtil.equals(product.getErpCode(), "house_rental")) {
                           mergeVo.setItemName("*" + product.getTaxClass() + "*房屋租赁");
                        } else {
                           mergeVo.setItemName("*" + product.getTaxClass() + "*" + productClass.getProductName());
                        }
                     }
                  }
                  ContractSubjectMatter subjectMatter = subjectMatterMapper.selectOne(Wrappers.<ContractSubjectMatter>lambdaQuery()
                        .eq(ContractSubjectMatter::getMaterialCode, schedule.getSubjectMatterCode())
                        .eq(ContractSubjectMatter::getContractId, contract.getId()).last("limit 1"));
                  if (ObjUtil.isNotNull(subjectMatter)) {
                     mergeVo.setUnitPrice(subjectMatter.getUnitPrice());
                  }
                  mergeOutBounds.add(mergeVo);
               }else {
                  List<ContractOutBoundMergeVo> filterMergeVo = mergeOutBounds.stream().filter(item->item.getMaterialCode()
                        .equals(schedule.getSubjectMatterCode())).collect(Collectors.toList());
                  if (ArrayUtil.isNotEmpty(filterMergeVo.toArray())){
                     filterMergeVo.get(0).setPriceAndTax(schedule.getPlannedAmount().add(filterMergeVo.get(0).getPriceAndTax()));
                  }
               }
            });
            if (ArrayUtil.isNotEmpty(mergeOutBounds.toArray())) {
               invoiceItemVoList = BeanUtil.copyToList(mergeOutBounds, InvoiceItemVo.class);
               invoiceItemVoList.stream().forEach(invoiceItemVo -> {
//            for (InvoiceItemVo invoiceItemVo:invoiceItemVoList) {
                  BigDecimal itemPrice = invoiceItemVo.getPriceAndTax();
                  BigDecimal itemPriceTax = itemPrice.divide(new BigDecimal("100").add(invoiceItemVo.getTaxRate()), 2, RoundingMode.HALF_UP).multiply(invoiceItemVo.getTaxRate());
                  invoiceItemVo.setPriceTax(itemPriceTax);
                  invoiceItemVo.setPriceNoTax(itemPrice.subtract(itemPriceTax));
               });
               vo.setTotalPrice(invoiceItemVoList.stream().map(item -> item.getPriceAndTax()).reduce(BigDecimal.ZERO, BigDecimal::add));
               vo.setInvoiceItemList(invoiceItemVoList);
               vo.setPriceWords(Convert.digitToChinese(vo.getTotalPrice()));
            }
         }
      }
      return vo;
@@ -193,8 +287,15 @@
      if (ArrayUtil.isEmpty(genInvoiceInfoDTO.getOutBoundInvoiceList().toArray())) {
         return R.failed("请选择开票出库单后再操作");
      }
      ContractOutBound outBound = contractOutBoundMapper.selectById(genInvoiceInfoDTO.getOutBoundInvoiceList().get(0).getContractOutBoundId());
      Contract contract = contractMapper.selectById(outBound.getContractId());
      Long contractId = null;
      if (!StrUtil.equals(genInvoiceInfoDTO.getContractCategory(),"water_house")) {
         ContractOutBound outBound = contractOutBoundMapper.selectById(genInvoiceInfoDTO.getOutBoundInvoiceList().get(0).getContractOutBoundId());
         contractId = outBound.getContractId();
      }else {
         ContractPaymentSchedule schedule = contractPaymentScheduleMapper.selectById(genInvoiceInfoDTO.getOutBoundInvoiceList().get(0).getContractOutBoundId());
         contractId = schedule.getContractId();
      }
      Contract contract = contractMapper.selectById(contractId);
      if (ObjUtil.isNull(contract)) {
         return R.failed("合同信息异常,请联系技术人员");
      }
@@ -209,19 +310,31 @@
      //启动流程
      ProcessInstanceParamDto dto = new ProcessInstanceParamDto();
      Map<String, Object> map = BeanUtil.beanToMap(invoice);
      if (StrUtil.equals(genInvoiceInfoDTO.getContractCategory(),"water_house")) {
         map.put("contractCategory","water_house");
      }
      dto.setParamMap(map);
      dto.setFlowName(FlowNameEnum.销售挂账审批.name());
      R r1 = remoteFlowProcessService.startProcessInstance(dto);
      if (r1.getCode() == 1) {
         return R.failed("流程启动失败");
      }
      //更新合同出库单状态
      genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
         ContractOutBound bound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
         bound.setInvoiceStatus("1");
         bound.setInvoiceNum(bound.getInvoiceNum().add(contractOutBoundInvoiceVo.getInvoiceNum()));
         contractOutBoundMapper.updateById(bound);
      });
      if (!StrUtil.equals(contract.getContractCategory(),"water_house")) {
         //更新合同出库单状态
         genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
            ContractOutBound bound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
            bound.setInvoiceStatus("1");
            bound.setInvoiceNum(bound.getInvoiceNum().add(contractOutBoundInvoiceVo.getInvoiceNum()));
            contractOutBoundMapper.updateById(bound);
         });
      }else {
         //更新合同房租 水电 费开票状态
         genInvoiceInfoDTO.getOutBoundInvoiceList().stream().forEach(contractOutBoundInvoiceVo -> {
               ContractPaymentSchedule schedule = contractPaymentScheduleMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
               schedule.setInvoiceFlag("1");
               contractPaymentScheduleMapper.updateById(schedule);
            });
      }
      return R.ok();
   }
@@ -259,7 +372,11 @@
         invoiceDetail.put("goodsUnit", invoiceItemVo.getUnit());
         invoiceDetail.put("goodsQuantity", invoiceItemVo.getQuantity());
         invoiceDetail.put("goodsPrice", invoiceItemVo.getUnitPrice());
         invoiceDetail.put("goodsTotalPrice", invoiceItemVo.getUnitPrice().multiply(invoiceItemVo.getQuantity()));
         if (!StrUtil.equals(vo.getContractCategory(),"water_house")) {
            invoiceDetail.put("goodsTotalPrice", invoiceItemVo.getUnitPrice().multiply(invoiceItemVo.getQuantity()));
         }else {
            invoiceDetail.put("goodsTotalPrice", invoiceItemVo.getPriceAndTax());
         }
         invoiceDetail.put("goodsTotalTax", invoiceItemVo.getPriceTax());
         invoiceDetailsList.add(invoiceDetail);
@@ -316,6 +433,7 @@
      ContractInvoice blueInvoice = baseMapper.selectById(invoiceId);
      blueInvoice.setRedReversal("2");
      baseMapper.updateById(blueInvoice);
      Contract contract = contractMapper.selectById(blueInvoice.getContractId());
      ContractInvoice invoice = new ContractInvoice();
      invoice.setContractId(blueInvoice.getContractId());
      invoice.setTexNo(blueInvoice.getTexNo());
@@ -342,6 +460,10 @@
      ProcessInstanceParamDto dto = new ProcessInstanceParamDto();
      Map<String, Object> map = BeanUtil.beanToMap(invoice);
      dto.setParamMap(map);
      if (StrUtil.equals(contract.getContractCategory(),"water_house")) {
         map.put("contractCategory","water_house");
      }
      dto.setParamMap(map);
      dto.setFlowName(FlowNameEnum.销售挂账审批.name());
      R r1 = remoteFlowProcessService.startProcessInstance(dto);
      if (r1.getCode() == 1) {
@@ -351,7 +473,7 @@
      return R.ok();
   }
   private JSONObject genRedInvoice(ContractInvoice blueInvoice, String redConfirmSerialNo) {
   private JSONObject genRedInvoice(Contract contract,ContractInvoice blueInvoice, String redConfirmSerialNo) {
      JSONObject jsonObject = new JSONObject();
      jsonObject.put("taxNo", blueInvoice.getTexNo());
      jsonObject.put("redConfirmSerialNo", redConfirmSerialNo);
@@ -380,7 +502,11 @@
         invoiceDetail.put("goodsUnit", invoiceItemVo.getUnit());
         invoiceDetail.put("goodsQuantity", invoiceItemVo.getQuantity());
         invoiceDetail.put("goodsPrice", invoiceItemVo.getUnitPrice());
         invoiceDetail.put("goodsTotalPrice", invoiceItemVo.getUnitPrice().multiply(invoiceItemVo.getQuantity()));
         if (!StrUtil.equals(contract.getContractCategory(),"water_house")) {
            invoiceDetail.put("goodsTotalPrice", invoiceItemVo.getUnitPrice().multiply(invoiceItemVo.getQuantity()));
         }else {
            invoiceDetail.put("goodsTotalPrice", invoiceItemVo.getPriceAndTax());
         }
         invoiceDetail.put("goodsTotalTax", invoiceItemVo.getPriceTax());
         invoiceDetailsList.add(invoiceDetail);
@@ -549,7 +675,7 @@
      ContractInvoice invoice = baseMapper.selectById(id);
      Contract contract = contractMapper.selectById(invoice.getContractId());
      GenInvoiceInfoVo vo = BeanUtil.copyProperties(JSONObject.parseObject(invoice.getInvoicePreview()), GenInvoiceInfoVo.class);
      vo.setContractCategory(contract.getContractCategory());
      //蓝票
      if (invoice.getInvoiceCategory().equals("1")) {
         // TODO 开蓝票接口
@@ -572,37 +698,77 @@
               invoice.setInvoiceNo(model.getString("invoiceNo"));
            }
         }
         if (!StrUtil.equals(contract.getContractCategory(),"water_house")) {
            //合同状态
            if (StrUtil.equals(contract.getBillingStatus(), "2")) {
               if (ObjUtil.isNull(contract.getBillingAmout())) {
                  contract.setBillingAmout(vo.getTotalPrice());
                  if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0) {
                     contract.setBillingStatus("3");
                  }
                  if (contract.getBillingAmout().compareTo(contract.getAmount()) < 0) {
                     contract.setBillingStatus("1");
                  }
                  contractMapper.updateById(contract);
               } else {
                  contract.setBillingAmout(vo.getTotalPrice().add(contract.getBillingAmout()));
                  if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0) {
                     contract.setBillingStatus("3");
                  }
                  if (contract.getBillingAmout().compareTo(contract.getAmount()) < 0) {
                     contract.setBillingStatus("1");
                  }
                  contractMapper.updateById(contract);
               }
            } else {
               if (ObjUtil.isNull(contract.getBillingAmout())) {
                  contract.setBillingAmout(vo.getTotalPrice());
                  contractMapper.updateById(contract);
               } else {
                  contract.setBillingAmout(vo.getTotalPrice().add(contract.getBillingAmout()));
                  contractMapper.updateById(contract);
               }
            }
         }else {
            //判断 循环是否完成
            List<ContractExecDate> execDateList = contractExecDateMapper.selectList(Wrappers.<ContractExecDate>lambdaQuery().eq(ContractExecDate::getContractId,contract.getId())
                  .eq(ContractExecDate::getGenFlag,"0"));
            List<MeterReadRecord> recordList = meterReadRecordMapper.selectList(Wrappers.<MeterReadRecord>lambdaQuery()
                  .eq(MeterReadRecord::getContractId,contract.getId())
                  .isNull(MeterReadRecord::getMeterReadNum));
            if (ArrayUtil.isEmpty(execDateList.toArray())&&ArrayUtil.isEmpty(recordList.toArray())) {
               //判断是否有可开票记录
               List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
                     .eq(ContractPaymentSchedule::getContractId,contract.getId()).eq(ContractPaymentSchedule::getInvoiceFlag,"0"));
               if (ArrayUtil.isEmpty(scheduleList.toArray())){
                  contract.setBillingStatus("3");
               }else {
                  contract.setBillingStatus("2");
               }
         //合同状态
         if (StrUtil.equals(contract.getBillingStatus(), "2")) {
            }else {
               //判断是否有可开票记录
               List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
                     .eq(ContractPaymentSchedule::getContractId,contract.getId()));
               if (ArrayUtil.isEmpty(scheduleList.toArray())){
                  contract.setBillingStatus("1");
               }else {
                  contract.setBillingStatus("2");
               }
            }
            if (ObjUtil.isNull(contract.getBillingAmout())) {
               contract.setBillingAmout(vo.getTotalPrice());
               if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0) {
                  contract.setBillingStatus("3");
               }
               contractMapper.updateById(contract);
            } else {
               contract.setBillingAmout(vo.getTotalPrice().add(contract.getBillingAmout()));
               if (contract.getBillingAmout().compareTo(contract.getAmount()) == 0) {
                  contract.setBillingStatus("3");
               }
               contractMapper.updateById(contract);
            }
         } else {
            if (ObjUtil.isNull(contract.getBillingAmout())) {
               contract.setBillingAmout(vo.getTotalPrice());
               contractMapper.updateById(contract);
            } else {
               contract.setBillingAmout(vo.getTotalPrice().add(contract.getBillingAmout()));
               contractMapper.updateById(contract);
            }
            contractMapper.updateById(contract);
         }
      }
      //红票
      if (invoice.getInvoiceCategory().equals("2")) {
         // TODO 开红票接口
         ContractInvoice blueInvoice = baseMapper.selectById(invoice.getBlueInvoiceId());
         JSONObject request = genRedInvoice(blueInvoice, invoice.getSerialNo());
         JSONObject request = genRedInvoice(contract,blueInvoice, invoice.getSerialNo());
         String requestId = IdUtil.fastUUID();
         if (bwFlag) {
            log.info("红字确认单请求接口入参", request.toJSONString());
@@ -629,23 +795,32 @@
               }
            }
         }
         contract.setBillingAmout(contract.getBillingAmout().subtract(invoice.getInvoiceTotalPriceTax()));
         //更新合同开票状态 前提出库完成
         if (StrUtil.equals(contract.getBillingStatus(),"3")) {
            contract.setBillingStatus("2");
            contract.setBillingAmout(contract.getBillingAmout().subtract(invoice.getInvoiceTotalPriceTax()));
            contractMapper.updateById(contract);
         }
         contract.setBillingStatus("2");
         contractMapper.updateById(contract);
         blueInvoice.setRedReversal("1");
         baseMapper.updateById(blueInvoice);
         //更新合同出库单状态
         List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList = BeanUtil.copyToList(JSONArray.parseArray(blueInvoice.getInvoicePreviewParam()),ContractOutBoundInvoiceVo.class);
         if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList.toArray())){
            outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> {
               ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
               contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum()));
               contractOutBoundMapper.updateById(contractOutBound);
            });
         if (!StrUtil.equals(contract.getContractCategory(),"water_house")) {
            //更新合同出库单状态
            List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList = BeanUtil.copyToList(JSONArray.parseArray(blueInvoice.getInvoicePreviewParam()), ContractOutBoundInvoiceVo.class);
            if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList.toArray())) {
               outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> {
                  ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
                  contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum()));
                  contractOutBoundMapper.updateById(contractOutBound);
               });
            }
         }else {
            //更新合同房租 水电 费开票状态
            List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList = BeanUtil.copyToList(JSONArray.parseArray(blueInvoice.getInvoicePreviewParam()), ContractOutBoundInvoiceVo.class);
            if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList.toArray())) {
               outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> {
                  ContractPaymentSchedule schedule = contractPaymentScheduleMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
                  schedule.setInvoiceFlag("0");
                  contractPaymentScheduleMapper.updateById(schedule);
               });
            }
         }
      }
      invoice.setApprovalStatus("1");
@@ -657,16 +832,31 @@
      ContractInvoice invoice = baseMapper.selectById(id);
      invoice.setApprovalStatus("2");
      baseMapper.updateById(invoice);
      Contract contract = contractMapper.selectById(invoice.getContractId());
      //蓝票
      if (invoice.getInvoiceCategory().equals("1")) {
         //更新合同出库单状态
         List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList = BeanUtil.copyToList(JSONArray.parseArray(invoice.getInvoicePreviewParam()),ContractOutBoundInvoiceVo.class);
         if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList.toArray())){
            outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> {
               ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
               contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum()));
               contractOutBoundMapper.updateById(contractOutBound);
            });
         if (!StrUtil.equals(contract.getContractCategory(),"water_house")) {
            //更新合同出库单状态
            List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList = BeanUtil.copyToList(JSONArray.parseArray(invoice.getInvoicePreviewParam()), ContractOutBoundInvoiceVo.class);
            if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList.toArray())) {
               outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> {
                  ContractOutBound contractOutBound = contractOutBoundMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
                  contractOutBound.setInvoiceNum(contractOutBound.getInvoiceNum().subtract(contractOutBoundInvoiceVo.getInvoiceNum()));
                  contractOutBoundMapper.updateById(contractOutBound);
               });
            }
         }else {
            //更新合同房租 水电 费开票状态
            List<ContractOutBoundInvoiceVo> outBoundInvoiceVoList = BeanUtil.copyToList(JSONArray.parseArray(invoice.getInvoicePreviewParam()), ContractOutBoundInvoiceVo.class);
            if (ArrayUtil.isNotEmpty(outBoundInvoiceVoList.toArray())) {
               outBoundInvoiceVoList.stream().forEach(contractOutBoundInvoiceVo -> {
                  ContractPaymentSchedule schedule = contractPaymentScheduleMapper.selectById(contractOutBoundInvoiceVo.getContractOutBoundId());
                  schedule.setInvoiceFlag("0");
                  contractPaymentScheduleMapper.updateById(schedule);
               });
            }
            contract.setBillingStatus("2");
            contractMapper.updateById(contract);
         }
      }
      //红票
@@ -681,4 +871,9 @@
   public Page pageInvoice(Page page, ContractInvoiceQueryDTO queryDTO) {
      return baseMapper.pageInvoice(page,queryDTO, DataScope.of("comp_id"));
   }
   @Override
   public Page pageScope(Page page, ContractQueryDTO queryDTO) {
      return baseMapper.pageScope(page,queryDTO, DataScope.of("comp_id"));
   }
}