shiyunteng
2026-06-08 a0974f04b5a83e3ab152ce79e53f180e7b496165
platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java
@@ -36,6 +36,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
@@ -58,7 +59,7 @@
   private final MeterReadRecordMapper meterReadRecordMapper;
   private final ContractExecDateMapper contractExecDateMapper;
   private final RemoteFlowProcessService remoteFlowProcessService;
   private final ContractTemplateMapper contractTemplateMapper;
   private final ProductMapper productMapper;
   private final RemoteDeptService remoteDeptService;
   @Value("${file.local.base-path}")
@@ -235,6 +236,10 @@
      detailVo.setContractSubjectMatter(subjectMatterList);
      List<ContractPaymentSchedule> paymentScheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, contract.getId()));
      detailVo.setContractPaymentSchedule(paymentScheduleList);
      BusinessCustomer a = businessCustomerMapper.selectById(contract.getPartyAId());
      if (ObjUtil.isNotNull(a)){
         detailVo.setPartyAOrgCode(a.getCreditCode());
      }
      return detailVo;
   }
@@ -657,7 +662,7 @@
      try {
         // 1. 加载模版
         // 假设模版在 resources/templates/template.docx
         ClassPathResource resource = new ClassPathResource("template/ymjjgclht.docx");
         ClassPathResource resource = new ClassPathResource("template/ymj/ymjjgclht.docx");
         // 配置列表策略
         Configure config = Configure.builder()
               .bind("items", new LoopRowTableRenderPolicy()) // 将 items 绑定到行循环策略
@@ -760,4 +765,312 @@
      }
      return R.ok();
   }
   @Override
   public void exportContractYMJGYPMMWord(Long id, HttpServletResponse response) {
      Contract contract = baseMapper.selectById(id);
      List<ContractSubjectMatter> subjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery()
            .eq(ContractSubjectMatter::getContractId, id).orderByAsc(ContractSubjectMatter::getCreateTime));
      Map<String, Object> map = new HashMap<>();
      //合同基本信息
      map.put("partyA", contract.getPartyA());
      map.put("partyB", contract.getPartyB());
      map.put("contractNo", contract.getContractNo());
      map.put("signPlace", contract.getSignPlace());
      map.put("signDate", DateUtil.formatDate(contract.getSignDate()));
      map.put("signDateYMD", DateUtil.format(contract.getSignDate(), DatePattern.CHINESE_DATE_PATTERN));
      map.put("total", contract.getAmount());
      map.put("amountWords", Convert.digitToChinese(contract.getAmount()));
      //标的物
      List<Map<String, Object>> items = new ArrayList<>();
      List<RowRenderData> dataList = new ArrayList<>();
      AtomicReference<Integer> no = new AtomicReference<>(0);
      subjectMatterList.stream().forEach(contractSubjectMatter -> {
         Map<String, Object> item = new HashMap<>();
         item.put("no", no);
         item.put("materialName", contractSubjectMatter.getMaterialName());
         item.put("guige", contractSubjectMatter.getSpecification());
         item.put("unit", contractSubjectMatter.getUnit());
         item.put("quantity", contractSubjectMatter.getQuantity());
         item.put("danzhong", contractSubjectMatter.getSingleWight());
         item.put("zongzhong", contractSubjectMatter.getTotalWight());
         item.put("unitPrice", contractSubjectMatter.getUnitPrice());
         item.put("price", contractSubjectMatter.getTotalAmount());
         if (no.get().equals(Integer.valueOf(0))){
            item.put("deliveryCycle",contract.getDeliveryCycle());
         }
         items.add(item);
         no.updateAndGet(v -> v + 1);
      });
      map.put("items", items);
      //定制方
      BusinessCustomer customer = businessCustomerMapper.selectById(contract.getPartyAId());
      map.put("companyName", customer.getCompanyName());
      map.put("legalPerson", customer.getLegalPerson());
      map.put("contactPhone", customer.getContactPhone());
      map.put("bankName", customer.getBankName());
      map.put("bankAccount", customer.getBankAccount());
      //承揽方
      R<SysDept> r = remoteDeptService.getById(contract.getPartyBId());
      SysDept dept = r.getData();
      map.put("orgName", dept.getOrgName());
      map.put("legalPerson1", StrUtil.isNotEmpty(dept.getLegalPerson()) ? dept.getLegalPerson() : "");
      map.put("entrustedAgent", StrUtil.isNotEmpty(dept.getLegalPerson()) ? "" : dept.getEntrustedAgent());
      map.put("orgContact", dept.getOrgContact());
      map.put("orgBank", dept.getOrgBank());
      map.put("orgBankAccount", dept.getOrgBankAccount());
      //生成文件名
      Long time = new Date().getTime();
      // 生成的word格式
      String formatSuffix = ".docx";
      // 拼接后的文件名
      String fileName = time + formatSuffix;//文件名  带后缀
      //导出word
      try {
         // 1. 加载模版
         // 假设模版在 resources/templates/template.docx
         ClassPathResource resource = new ClassPathResource("template/ymj/ymjgypmmht.docx");
         // 配置列表策略
         Configure config = Configure.builder()
               .bind("items", new LoopRowTableRenderPolicy()) // 将 items 绑定到行循环策略
               .build();
         // 2. 编译并渲染数据
         XWPFTemplate xwpfTemplate = XWPFTemplate.compile(resource.getInputStream(), config).render(map);
         // 3. 设置响应头
         response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
         response.setCharacterEncoding("utf-8");
         response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".docx");
         // 4. 写入输出流
         OutputStream out = response.getOutputStream();
         xwpfTemplate.write(out);
         out.flush();
         out.close();
         xwpfTemplate.close(); // 重要:关闭模版释放资源
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   @Override
   public void exportContractYMJGKCPMMWord(Long id, HttpServletResponse response) {
      Contract contract = baseMapper.selectById(id);
      List<ContractSubjectMatter> subjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery()
            .eq(ContractSubjectMatter::getContractId, id).orderByAsc(ContractSubjectMatter::getCreateTime));
      Map<String, Object> map = new HashMap<>();
      //合同基本信息
      map.put("partyA", contract.getPartyA());
      map.put("partyB", contract.getPartyB());
      map.put("contractNo", contract.getContractNo());
      map.put("signPlace", contract.getSignPlace());
      map.put("signDate", DateUtil.formatDate(contract.getSignDate()));
      map.put("signDateYMD", DateUtil.format(contract.getSignDate(), DatePattern.CHINESE_DATE_PATTERN));
      map.put("total", contract.getAmount());
      map.put("amountWords", Convert.digitToChinese(contract.getAmount()));
      map.put("deliveryCycle",contract.getDeliveryCycle());
      //标的物
      List<Map<String, Object>> items = new ArrayList<>();
      AtomicReference<Integer> no = new AtomicReference<>(0);
      subjectMatterList.stream().forEach(contractSubjectMatter -> {
         Map<String, Object> item = new HashMap<>();
         item.put("no", no);
         item.put("materialName", contractSubjectMatter.getMaterialName());
         item.put("guige", contractSubjectMatter.getSpecification());
         item.put("unit", contractSubjectMatter.getUnit());
         item.put("quantity", contractSubjectMatter.getQuantity());
         item.put("danzhong", contractSubjectMatter.getSingleWight());
         item.put("zongzhong", contractSubjectMatter.getTotalWight());
         item.put("unitPrice", contractSubjectMatter.getUnitPrice());
         item.put("price", contractSubjectMatter.getTotalAmount());
         items.add(item);
         no.updateAndGet(v -> v + 1);
      });
      map.put("items", items);
      //定制方
      BusinessCustomer customer = businessCustomerMapper.selectById(contract.getPartyAId());
      map.put("companyName", customer.getCompanyName());
      map.put("legalPerson", customer.getLegalPerson());
      map.put("contactPhone", customer.getContactPhone());
      map.put("bankName", customer.getBankName());
      map.put("bankAccount", customer.getBankAccount());
      //承揽方
      R<SysDept> r = remoteDeptService.getById(contract.getPartyBId());
      SysDept dept = r.getData();
      map.put("orgName", dept.getOrgName());
      map.put("legalPerson1", StrUtil.isNotEmpty(dept.getLegalPerson()) ? dept.getLegalPerson() : "");
      map.put("entrustedAgent", StrUtil.isNotEmpty(dept.getLegalPerson()) ? "" : dept.getEntrustedAgent());
      map.put("orgContact", dept.getOrgContact());
      map.put("orgBank", dept.getOrgBank());
      map.put("orgBankAccount", dept.getOrgBankAccount());
      //生成文件名
      Long time = new Date().getTime();
      // 生成的word格式
      String formatSuffix = ".docx";
      // 拼接后的文件名
      String fileName = time + formatSuffix;//文件名  带后缀
      //导出word
      try {
         // 1. 加载模版
         // 假设模版在 resources/templates/template.docx
         ClassPathResource resource = new ClassPathResource("template/ymj/ymjgkcpmmht.docx");
         // 配置列表策略
         Configure config = Configure.builder()
               .bind("items", new LoopRowTableRenderPolicy()) // 将 items 绑定到行循环策略
               .build();
         // 2. 编译并渲染数据
         XWPFTemplate xwpfTemplate = XWPFTemplate.compile(resource.getInputStream(), config).render(map);
         // 3. 设置响应头
         response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
         response.setCharacterEncoding("utf-8");
         response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".docx");
         // 4. 写入输出流
         OutputStream out = response.getOutputStream();
         xwpfTemplate.write(out);
         out.flush();
         out.close();
         xwpfTemplate.close(); // 重要:关闭模版释放资源
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
   @Override
   public void exportContractYMJCGWord(Long id, HttpServletResponse response) {
      Contract contract = baseMapper.selectById(id);
      List<ContractSubjectMatter> subjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery()
            .eq(ContractSubjectMatter::getContractId, id).orderByAsc(ContractSubjectMatter::getCreateTime));
      Map<String, Object> map = new HashMap<>();
      //合同基本信息
      map.put("partyA", contract.getPartyA());
      map.put("partyB", contract.getPartyB());
      map.put("contractNo", contract.getContractNo());
      map.put("signPlace", contract.getSignPlace());
      map.put("signDate", DateUtil.formatDate(contract.getSignDate()));
      map.put("signDateYMD", DateUtil.format(contract.getSignDate(), DatePattern.CHINESE_DATE_PATTERN));
      map.put("total", contract.getAmount());
      map.put("amountWords", Convert.digitToChinese(contract.getAmount()));
      map.put("environment",contract.getEnvironment());
      map.put("standard",contract.getStandard());
      map.put("useMonth",contract.getUseMonth());
      map.put("afterShipMonth",contract.getAfterShipMonth());
      map.put("shipMethod",contract.getShipMethod());
      map.put("shipAddress",contract.getShipAddress());
      map.put("packaging",contract.getPackaging());
      map.put("repairPeriod",contract.getRepairPeriod());
      map.put("repairBreachAmount",contract.getRepairBreachAmount());
      map.put("goodsShort",contract.getGoodsShort());
      map.put("goodsShortBreachAmount",contract.getGoodsShortBreachAmount());
      map.put("overdueBreachAmount",contract.getOverdueBreachAmount());
      map.put("terminateContract",contract.getTerminateContract());
      map.put("court",contract.getCourt());
      map.put("contractAttchment",contract.getContractAttchment());
      map.put("invoiceNotice",contract.getInvoiceNotice());
      //标的物
      List<Map<String, Object>> items = new ArrayList<>();
      AtomicReference<Integer> no = new AtomicReference<>(0);
      subjectMatterList.stream().forEach(contractSubjectMatter -> {
         Product product = productMapper.selectOne(Wrappers.<Product>lambdaQuery().eq(Product::getErpCode,contractSubjectMatter.getMaterialCode()).last("limit 1"));
         Map<String, Object> item = new HashMap<>();
         item.put("no", no);
         item.put("materialName", contractSubjectMatter.getMaterialName());
         item.put("guige", contractSubjectMatter.getSpecification());
         item.put("unit", contractSubjectMatter.getUnit());
         item.put("quantity", contractSubjectMatter.getQuantity());
         item.put("unitPrice", contractSubjectMatter.getUnitPrice());
         item.put("priceTax", contractSubjectMatter.getUnitPrice().multiply(contractSubjectMatter.getQuantity()).setScale(2, RoundingMode.HALF_UP));
         item.put("priceNoTax", contractSubjectMatter.getUnitPrice().multiply(contractSubjectMatter.getQuantity())
               .subtract(contractSubjectMatter.getUnitPrice().multiply(contractSubjectMatter.getQuantity())
                     .divide(new BigDecimal("100").add(new BigDecimal(product.getTaxRate())),2, RoundingMode.HALF_UP).multiply(new BigDecimal(product.getTaxRate())))
               .setScale(2, RoundingMode.HALF_UP));
         item.put("deliveryDate", DateUtil.formatDate(contractSubjectMatter.getPlannedDeliveryDate()));
         item.put("remarks", contractSubjectMatter.getRemarks());
         map.put("taxRate",product.getTaxRate());
         items.add(item);
         no.updateAndGet(v -> v + 1);
      });
      //付款
      List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
            .eq(ContractPaymentSchedule::getContractId, id).orderByAsc(ContractPaymentSchedule::getCreateTime));
      scheduleList.stream().forEach(contractPaymentSchedule -> {
         if (contractPaymentSchedule.getStageName().equals("合同签订")){
            map.put("htqdbl",contractPaymentSchedule.getPaymentRatio());
            map.put("htqdje",contractPaymentSchedule.getPlannedAmount());
            map.put("htqdjr",contractPaymentSchedule.getAgreedDays());
         }
         if (contractPaymentSchedule.getStageName().equals("发货前")){
            map.put("fhqbl",contractPaymentSchedule.getPaymentRatio());
            map.put("fhqje",contractPaymentSchedule.getPlannedAmount());
         }
         if (contractPaymentSchedule.getStageName().equals("调试完成或验收")){
            map.put("ysbl",contractPaymentSchedule.getPaymentRatio());
            map.put("ysje",contractPaymentSchedule.getPlannedAmount());
            map.put("ysjr",contractPaymentSchedule.getAgreedDays());
         }
         if (contractPaymentSchedule.getStageName().equals("质保金")){
            map.put("zbjbl",contractPaymentSchedule.getPaymentRatio());
            map.put("zbjje",contractPaymentSchedule.getPlannedAmount());
            map.put("zbjjr",contractPaymentSchedule.getAgreedDays());
         }
      });
      map.put("items", items);
      //定制方
      BusinessCustomer customer = businessCustomerMapper.selectById(contract.getPartyAId());
      map.put("companyName", customer.getCompanyName());
      map.put("legalPerson", customer.getLegalPerson());
      map.put("contactPhone", customer.getContactPhone());
      map.put("bankName", customer.getBankName());
      map.put("bankAccount", customer.getBankAccount());
      //承揽方
      R<SysDept> r = remoteDeptService.getById(contract.getPartyBId());
      SysDept dept = r.getData();
      map.put("orgName", dept.getOrgName());
      map.put("legalPerson1", StrUtil.isNotEmpty(dept.getLegalPerson()) ? dept.getLegalPerson() : "");
      map.put("entrustedAgent", StrUtil.isNotEmpty(dept.getLegalPerson()) ? "" : dept.getEntrustedAgent());
      map.put("orgContact", dept.getOrgContact());
      map.put("orgBank", dept.getOrgBank());
      map.put("orgBankAccount", dept.getOrgBankAccount());
      //生成文件名
      Long time = new Date().getTime();
      // 生成的word格式
      String formatSuffix = ".docx";
      // 拼接后的文件名
      String fileName = time + formatSuffix;//文件名  带后缀
      //导出word
      try {
         // 1. 加载模版
         // 假设模版在 resources/templates/template.docx
         ClassPathResource resource = new ClassPathResource("template/ymj/ymjcght.docx");
         // 配置列表策略
         Configure config = Configure.builder()
               .bind("items", new LoopRowTableRenderPolicy()) // 将 items 绑定到行循环策略
               .build();
         // 2. 编译并渲染数据
         XWPFTemplate xwpfTemplate = XWPFTemplate.compile(resource.getInputStream(), config).render(map);
         // 3. 设置响应头
         response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
         response.setCharacterEncoding("utf-8");
         response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".docx");
         // 4. 写入输出流
         OutputStream out = response.getOutputStream();
         xwpfTemplate.write(out);
         out.flush();
         out.close();
         xwpfTemplate.close(); // 重要:关闭模版释放资源
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}