platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java
@@ -97,6 +97,9 @@
   private final ContractApprovalMapper contractApprovalMapper;
   private final TaxCodeMapper taxCodeMapper;
   private final ContractSubjectMatterService contractSubjectMatterService;
   private final HistoryOverdueMapper historyOverdueMapper;
   private final DropLossMapper dropLossMapper;
   private final PaymentOffsetMapper paymentOffsetMapper;
   @Value("${erp.ymj}")
   private String ymjErp;
@@ -314,6 +317,13 @@
   @Override
   public R edit(ContractUpdateDTO updateDTO) {
      Contract entity = baseMapper.selectById(updateDTO.getId());
      if (StrUtil.equals(updateDTO.getContractCategory(),"paid-edit")){
         entity.setPaidAmount(updateDTO.getPaidAmount());
         entity.setBillingAmout(updateDTO.getBillingAmout());
         baseMapper.updateById(entity);
         return R.ok();
      }
      if (!StrUtil.equals(entity.getContractStatus() + "", "0")) {
         return R.failed("当前状态无法修改合同");
      }
@@ -334,23 +344,23 @@
      contract.setPaidAmount(new BigDecimal("0"));
      contract.setBillingAmout(new BigDecimal("0"));
      contract.setNickName(SecurityUtils.getUser().getNickname());
      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smj) || StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjsc) ||
            StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjzt)||
            StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjhw)) {
         contract.setContractNo(generateSMJContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", sgb)) {
         contract.setContractNo(generateGYBContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", ymj)) {
         contract.setContractNo(generateYMJContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", tfgs)) {
         contract.setContractNo(generateTFContractNo());
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", jxc)) {
         contract.setContractNo(generateJXCContractNo());
      }
//      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smj) || StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjsc) ||
//            StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjzt) ||
//            StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjhw)) {
//         contract.setContractNo(generateSMJContractNo());
//      }
//      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", sgb)) {
//         contract.setContractNo(generateGYBContractNo());
//      }
//      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", ymj)) {
//         contract.setContractNo(generateYMJContractNo());
//      }
//      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", tfgs)) {
//         contract.setContractNo(generateTFContractNo());
//      }
//      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", jxc)) {
//         contract.setContractNo(generateJXCContractNo());
//      }
      if (ObjUtil.isNotNull(updateDTO.getDeliveryCycle())) {
         contract.setExpirationDate(DateUtil.offsetDay(updateDTO.getSignDate(), updateDTO.getDeliveryCycle()));
      }
@@ -538,19 +548,35 @@
      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smj) || StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjsc) ||
            StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjzt)||
            StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjhw)) {
         if (contract.getContractCategory().equals("his_no_center")){
            requestApply.put("fd_templateId", "1a03828a8ff2b476af7805f45a88a2ed");
         }else {
         requestApply.put("fd_templateId", tempSMJ);
      }
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", sgb)) {
         requestApply.put("fd_templateId", tempSGB);
         requestApply.put("fd_templateId", "1a038430e7f207101ebd992454b8fec5");
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", ymj)) {
         if (contract.getContractCategory().equals("his_no_center")){
            requestApply.put("fd_templateId", "1a038467e32f6274f051de14080b5cde");
         }else {
         requestApply.put("fd_templateId", tempYMJ);
      }
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", tfgs)) {
         if (contract.getContractCategory().equals("his_no_center")){
            requestApply.put("fd_templateId", "1a03845b5e642092d9272d64dacba76c");
         }else {
         requestApply.put("fd_templateId", tempTFGS);
      }
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", jxc)) {
         if (contract.getContractCategory().equals("his_no_center")){
            requestApply.put("fd_templateId", "1a038450e9ab5d940be954144b2b62b1");
         }else {
         requestApply.put("fd_templateId", tempJXC);
         }
      }
      requestApply.put("fd_name", contract.getContractName());
      requestApply.put("fd_signDate", DateUtil.formatDate(contract.getSignDate()));
@@ -565,11 +591,14 @@
      requestApply.put("fd_customerName", contract.getPartyA());
      requestApply.put("fd_systemSource", "非煤销售系统");
      requestApply.put("fd_type", "00");
      if (contract.getAmount().compareTo(new BigDecimal("0"))==0){
         requestApply.put("fd_agr_property","1");
      }
      String bzr =  comp.getBaozhangren();
      String syr = baseMapper.selectYZSH();
      if (!StrUtil.equals(contract.getCompId()+"",contract.getPartyBId()+"")&& (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjsc) ||
            StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjzt)||
            StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjhw))&&StrUtil.isNotBlank(syr)) {
            StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjhw)) && StrUtil.isNotBlank(syr) && StrUtil.isNotBlank(bzr)) {
         log.info("石煤机子公司用总公司印章");
         bzr =bzr.concat(syr);
      }
@@ -667,14 +696,16 @@
      //资金明细
      savePaymentConfirm(contract);
      contract.setNextScheduleName("发货前");
      baseMapper.updateById(contract);
      //合同推送子公司
      pushSubCompany(contract,subjectMatterList);
      //合同中心推送
//      pusnContractCenter(contract,subjectMatterList);
      contract.setNextScheduleName("发货前");
      baseMapper.updateById(contract);
   }
   private void pusnContractCenter(Contract contract,List<ContractSubjectMatter> subjectMatterList) {
@@ -740,11 +771,9 @@
      }).start();
   }
   private void pushSubCompany(Contract contract,List<ContractSubjectMatter> subjectMatterList) {
   private R pushSubCompany(Contract contract, List<ContractSubjectMatter> subjectMatterList) {
      //erp推送
      new Thread(() -> {
         try {
            Thread.sleep(500);
            JSONObject request = new JSONObject();
            BusinessCustomer customer = businessCustomerMapper.selectById(contract.getPartyAId());
            request.put("contractNo", contract.getContractNo());
@@ -786,7 +815,7 @@
               }
               if (StrUtil.isEmpty(token)){
                  log.error("一煤机登录获取token失败");
                  return;
            return R.failed("一煤机登录获取token失败");
               }
               log.info("一煤机合同推送erp入参:{}", request.toJSONString());
               HttpResponse response = HttpRequest.post(ymjErp + "/api/contract-staging/v1/receive")
@@ -795,6 +824,10 @@
                     .body(request.toJSONString())                        // 设置请求体
                     .execute();                                         // 执行请求.post(ymjErp,request.toJSONString());
               log.info("一煤机合同推送erp回参:{}", response.body());
         JSONObject res = JSONObject.parseObject(response.body());
         if (res.getInteger("code")!=0){
            return R.failed(res.getString("msg"));
         }
               //保存请求记录
               saveErpRequestRecord("ymj", request.toJSONString(), response.body());
               //更新合同erp推送标识
@@ -802,9 +835,7 @@
               baseMapper.updateById(contract);
            }
            if ((StrUtil.equals(contract.getPartyBId() + "", smj) ||StrUtil.equals(contract.getPartyBId() + "", smjsc) ||
                  StrUtil.equals(contract.getPartyBId() + "", smjzt) ||
                  StrUtil.equals(contract.getPartyBId() + "", smjhw)) && StrUtil.isNotEmpty(smjErp)) {
      if ((StrUtil.equals(contract.getPartyBId() + "", smj) || StrUtil.equals(contract.getPartyBId() + "", smjhw)) && StrUtil.isNotEmpty(smjErp)) {
               request.put("customerCode", customer.getErpCompanyCode());
               request.put("currencyId", "RMB");
               request.put("signDate", DateUtil.format(contract.getSignDate(), DatePattern.NORM_DATETIME_PATTERN));
@@ -852,7 +883,7 @@
               }
               if (StrUtil.isEmpty(token)){
                  log.error("石煤机登录获取token失败");
                  return;
            return R.failed("石煤机登录获取token失败");
               }
               log.info("石煤机合同推送erp入参:{}", request.toJSONString());
@@ -862,6 +893,10 @@
                     .body(request.toJSONString())                        // 设置请求体
                     .execute();                                         // 执行请求.post(ymjErp,request.toJSONString());
               log.info("石煤机合同推送erp回参:{}", response.body());
         JSONObject res = JSONObject.parseObject(response.body());
         if (res.getInteger("code")!=0){
            return R.failed(res.getString("msg"));
         }
               //保存请求记录
               saveErpRequestRecord("smj", request.toJSONString(), response.body());
               //更新合同erp推送标识
@@ -906,7 +941,7 @@
               }
               if (StrUtil.isEmpty(token)){
                  log.error("石煤机登录获取token失败");
                  return;
            return R.failed("石煤机登录获取token失败");
               }
               log.info("石煤机随车分公司合同推送erp入参:{}", request.toJSONString());
@@ -916,6 +951,10 @@
                     .body(request.toJSONString())                        // 设置请求体
                     .execute();                                         // 执行请求.post(ymjErp,request.toJSONString());
               log.info("石煤机随车分公司合同推送erp回参:{}", response.body());
         JSONObject res = JSONObject.parseObject(response.body());
         if (res.getInteger("code")!=0){
            return R.failed(res.getString("msg"));
         }
               //保存请求记录
               saveErpRequestRecord("smj", request.toJSONString(), response.body());
               //更新合同erp推送标识
@@ -960,7 +999,7 @@
               }
               if (StrUtil.isEmpty(token)){
                  log.error("石煤机登录获取token失败");
                  return;
            return R.failed("石煤机登录获取token失败");
               }
               log.info("石煤机钻探分公司合同推送erp入参:{}", request.toJSONString());
@@ -970,6 +1009,10 @@
                     .body(request.toJSONString())                        // 设置请求体
                     .execute();                                         // 执行请求.post(ymjErp,request.toJSONString());
               log.info("石煤机钻探分公司合同推送erp回参:{}", response.body());
         JSONObject res = JSONObject.parseObject(response.body());
         if (res.getInteger("code")!=0){
            return R.failed(res.getString("msg"));
         }
               //保存请求记录
               saveErpRequestRecord("smj", request.toJSONString(), response.body());
               //更新合同erp推送标识
@@ -989,12 +1032,7 @@
            if (StrUtil.equals(contract.getPartyBId() + "", tfgs) && StrUtil.isNotEmpty(tfgsErp)) {
               //通方公司通过系统录入
            }
         } catch (Exception e) {
            e.printStackTrace();
         }
      }).start();
      return R.ok();
   }
   private String getErpToken() {
@@ -1958,11 +1996,11 @@
      contract.setPartyBId(SecurityUtils.getUser().getCompId());
      contract.setPartyB(SecurityUtils.getUser().getCompName());
      contract.setContractStatus(2);
      contract.setPaidAmount(new BigDecimal("0"));
      contract.setPaidAmount(ObjUtil.isNotNull(addDTO.getPaidAmount())?addDTO.getPaidAmount():new BigDecimal("0"));
      contract.setBillingStatus("1");
      contract.setErpPushFlag("1");
      contract.setCenterPushFlag("0");
      contract.setBillingAmout(new BigDecimal("0"));
      contract.setBillingAmout(ObjUtil.isNotNull(addDTO.getBillingAmout())?addDTO.getBillingAmout():new BigDecimal("0"));
      BusinessCustomer bc = businessCustomerMapper.selectOne(Wrappers.<BusinessCustomer>lambdaQuery().eq(BusinessCustomer::getErpCompanyCode,
            addDTO.getPartyACode()).last("limit 1"));
@@ -1997,21 +2035,30 @@
         addDTO.getContractSubjectMatter().stream().forEach(contractSubjectMatterAddDTO -> {
            ContractSubjectMatter subjectMatter = BeanUtil.copyProperties(contractSubjectMatterAddDTO, ContractSubjectMatter.class);
            subjectMatter.setContractId(contract.getId());
            Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode,subjectMatter.getMaterialCode())
                  .last("limit 1"));
            if (ObjUtil.isNull(product)){
               product = new Product();
               product.setParentId(2101559148831944705L);
               product.setProductName(subjectMatter.getMaterialInternalName());
               product.setErpCode(subjectMatter.getMaterialCode());
               product.setProductType(subjectMatter.getSpecification());
               product.setTaxCode("1090117040000000000");
               product.setTaxClass("泵");
               productMapper.insert(product);
               subjectMatter.setMaterialInternalName(product.getId()+"");
            }
//            Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode, subjectMatter.getMaterialCode())
//                  .last("limit 1"));
//            if (ObjUtil.isNull(product)) {
//               product = new Product();
//               product.setParentId(2101559148831944705L);
//               product.setProductName(subjectMatter.getMaterialInternalName());
//               product.setErpCode(subjectMatter.getMaterialCode());
//               product.setProductType(subjectMatter.getSpecification());
//               product.setTaxCode("1090117040000000000");
//               product.setTaxClass("泵");
//               productMapper.insert(product);
//               subjectMatter.setMaterialInternalName(product.getId() + "");
//            }
            subjectMatter.setDeliveredQuantity(new BigDecimal("0"));
            subjectMatter.setDeliveryStatus(0);
            if(ObjUtil.isNotNull(subjectMatter.getDeliveredQuantity())&&subjectMatter.getDeliveredQuantity().compareTo(new BigDecimal("0"))>0){
               subjectMatter.setRemainingQuantity(subjectMatter.getQuantity().subtract(subjectMatter.getDeliveredQuantity()));
               if (subjectMatter.getRemainingQuantity().compareTo(new BigDecimal("0"))==0){
                  subjectMatter.setDeliveryStatus(2);
               }
               if (subjectMatter.getRemainingQuantity().compareTo(new BigDecimal("0"))>0){
                  subjectMatter.setDeliveryStatus(1);
               }
            }
            subjectMatter.setTotalAmount(contractSubjectMatterAddDTO.getQuantity().multiply(contractSubjectMatterAddDTO.getUnitPrice()));
            contractSubjectMatterMapper.insert(subjectMatter);
            //水电类必须有初次表号
@@ -2130,6 +2177,15 @@
            subjectMatter.setDeliveredQuantity(new BigDecimal("0"));
            subjectMatter.setDeliveryStatus(0);
            if(ObjUtil.isNotNull(subjectMatter.getDeliveredQuantity())&&subjectMatter.getDeliveredQuantity().compareTo(new BigDecimal("0"))>0){
               subjectMatter.setRemainingQuantity(subjectMatter.getQuantity().subtract(subjectMatter.getDeliveredQuantity()));
               if (subjectMatter.getRemainingQuantity().compareTo(new BigDecimal("0"))==0){
                  subjectMatter.setDeliveryStatus(2);
               }
               if (subjectMatter.getRemainingQuantity().compareTo(new BigDecimal("0"))>0){
                  subjectMatter.setDeliveryStatus(1);
               }
            }
            contractSubjectMatterMapper.insert(subjectMatter);
            //水电类必须有初次表号
            if (StrUtil.isNotEmpty(contractSubjectMatterAddDTO.getMeterReadCode()) &&
@@ -2655,13 +2711,20 @@
      requestApply.put("fd_MainContent", "");
      requestApply.put("fd_settlementType", "00");
      requestApply.put("fd_applyType", "外销");
      if (contract.getAmount().compareTo(new BigDecimal("0"))==0){
         requestApply.put("fd_agr_property","1");
      }
      requestApply.put("fd_contractSubject ", subjectMatterList.stream().map(item -> item.getMaterialName()).collect(Collectors.joining(",")));
      requestApply.put("fd_docNumber", contract.getContractNo());
      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smj) || StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjsc) ||
            StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjzt)||
            StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjhw)) {
         if (contract.getContractCategory().equals("his_no_center")){
            requestApply.put("fd_templateId", "1a03828a8ff2b476af7805f45a88a2ed");
         }else {
         requestApply.put("fd_templateId", tempSMJ);
         }
      }
      if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", sgb)) {
         requestApply.put("fd_templateId", tempSGB);
@@ -2815,12 +2878,11 @@
               continue;
            }
            BusinessCustomer bc = null;
            if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smj) || StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjsc)
                  || StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjzt)|| StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjhw)){
            if (StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smj) || StrUtil.equals(SecurityUtils.getUser().getCompId() + "", smjhw)) {
               List<String> smjList = new ArrayList<>();
               smjList.add(smj);
               smjList.add(smjsc);
               smjList.add(smjzt);
//               smjList.add(smjsc);
//               smjList.add(smjzt);
               smjList.add(smjhw);
               bc = businessCustomerMapper.selectOne(Wrappers.<BusinessCustomer>lambdaQuery()
                     .eq(BusinessCustomer::getCompanyName, contractExceVO.getBcErpCode())
@@ -2971,7 +3033,12 @@
                        schedule.setActualAmount(schedule.getPlannedAmount().add(remainAmount));
                     }
                  }
                  ContractPaymentSchedule paymentSchedule = contractPaymentScheduleMapper.selectOne(Wrappers.<ContractPaymentSchedule>lambdaQuery()
                        .eq(ContractPaymentSchedule::getContractId,contract.getId())
                        .eq(ContractPaymentSchedule::getStageName,schedule.getStageName()).last("limit 1"));
                  if (ObjUtil.isNull(paymentSchedule)) {
                  contractPaymentScheduleMapper.insert(schedule);
                  }
//                  if (schedule.getStageName().equals("质保金") && contract.getAmount().subtract(ObjUtil.isNotNull(contract.getPaidAmount()) ?
//                        contract.getPaidAmount() : new BigDecimal("0")).compareTo(new BigDecimal("0")) > 0) {
//                     PaymentConfirm confirm = new PaymentConfirm();
@@ -3238,7 +3305,7 @@
      }
      if (StrUtil.equals(dto.getStatus(), "3") || StrUtil.equals(dto.getStatus(), "4")) {
         new Thread(() -> {
            contract.setContractStatus(3);
            contract.setContractStatus(4);
            baseMapper.updateById(contract);
            saveContractApproval(contract,dto);
         }).start();
@@ -3558,8 +3625,7 @@
      Contract contract = baseMapper.selectById(id);
      List<ContractSubjectMatter> subjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery()
            .eq(ContractSubjectMatter::getContractId, id));
      pushSubCompany(contract,subjectMatterList);
      return R.ok();
      return pushSubCompany(contract, subjectMatterList);
   }
   @Override
@@ -4017,4 +4083,47 @@
      return R.ok();
   }
   @Override
   public R erpPushBatch() {
      List<Long> compIds = new ArrayList<>();
      compIds.add(2089995177814495234L);
      compIds.add(2089996921978040321L);
      compIds.add(2089997578009128962L);
      compIds.add(2101966257365045249L);
      List<Contract> contractList = baseMapper.selectList(Wrappers.<Contract>lambdaQuery().in(Contract::getCompId,compIds)
            .likeRight(Contract::getContractNo,"ZB-SMJ-").eq(Contract::getErpPushFlag,"0"));
      if (CollUtil.isNotEmpty(contractList)){
         for (Contract con:contractList
             ) {
            List<ContractSubjectMatter> subjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery().
                  eq(ContractSubjectMatter::getContractId, con.getId()));
            R r = pushSubCompany(con,subjectMatterList);
            if (!r.isOk()){
               return r;
            }
         }
      }
      return R.failed("暂无需要推送的合同");
   }
   @Override
   public R delById(Long id) {
      paymentOffsetMapper.delete(Wrappers.<PaymentOffset>lambdaQuery().eq(PaymentOffset::getContractId,id));
      contractApprovalMapper.delete(Wrappers.<ContractApproval>lambdaQuery().eq(ContractApproval::getContractId,id));
      contractDelayOutMapper.delete(Wrappers.<ContractDelayOut>lambdaQuery().eq(ContractDelayOut::getContractId,id));
      contractExecDateMapper.delete(Wrappers.<ContractExecDate>lambdaQuery().eq(ContractExecDate::getContractId,id));
      dropLossMapper.delete(Wrappers.<DropLoss>lambdaQuery().eq(DropLoss::getContractId,id));
      meterReadRecordMapper.delete(Wrappers.<MeterReadRecord>lambdaQuery().eq(MeterReadRecord::getContractId,id));
      contractOutBoundMapper.delete(Wrappers.<ContractOutBound>lambdaQuery().eq(ContractOutBound::getContractId,id));
      contractInvoiceMapper.delete(Wrappers.<ContractInvoice>lambdaQuery().eq(ContractInvoice::getContractId,id));
      historyOverdueMapper.delete(Wrappers.<HistoryOverdue>lambdaQuery().eq(HistoryOverdue::getContractId,id));
      currentOverdueMapper.delete(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getContractId,id));
      paymentConfirmMapper.delete(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId,id));
      paymentSlipMapper.delete(Wrappers.<PaymentSlip>lambdaQuery().eq(PaymentSlip::getContractId,id));
      contractPaymentScheduleMapper.delete(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId,id));
      contractSubjectMatterMapper.delete(Wrappers.<ContractSubjectMatter>lambdaQuery().eq(ContractSubjectMatter::getContractId,id));
      baseMapper.deleteById(id);
      return R.ok();
   }
}