platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java
@@ -1,12 +1,16 @@
package com.by4cloud.platformx.business.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by4cloud.platformx.admin.api.entity.SysDept;
import com.by4cloud.platformx.admin.api.feign.RemoteDeptService;
import com.by4cloud.platformx.business.api.feign.RemoteFlowProcessService;
import com.by4cloud.platformx.business.constant.FlowNameEnum;
import com.by4cloud.platformx.business.dto.ContractAddDTO;
@@ -18,17 +22,23 @@
import com.by4cloud.platformx.business.vo.ContractDetailVo;
import com.by4cloud.platformx.common.core.util.R;
import com.by4cloud.platformx.common.security.util.SecurityUtils;
import com.by4cloud.platformx.flow.task.api.feign.RemoteBusinessService;
import com.by4cloud.platformx.flow.task.dto.ProcessInstanceParamDto;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import java.io.Serializable;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
/**
 * @author cd
@@ -44,7 +54,13 @@
   private final ContractPaymentScheduleProcessMapper contractPaymentScheduleProcessMapper;
   private final PaymentConfirmMapper paymentConfirmMapper;
   private final CurrentOverdueMapper currentOverdueMapper;
   private final BusinessCustomerMapper businessCustomerMapper;
   private final RemoteFlowProcessService remoteFlowProcessService;
   private final ContractTemplateMapper contractTemplateMapper;
   private final RemoteDeptService remoteDeptService;
   @Value("${file.local.base-path}")
   private String basePath;
   @Override
   public R add(ContractAddDTO addDTO) {
@@ -208,29 +224,8 @@
            .orderByAsc(ContractPaymentSchedule::getCreateTime).last("limit 1"));
      if (fitstSchedule.getStageName().equals("合同签订")){
         //新增应收
         PaymentConfirm paymentConfirm = new PaymentConfirm();
         paymentConfirm.setBusinessType("合同签订应收");
         paymentConfirm.setBusGuestId(contract.getPartyAId());
         paymentConfirm.setBusGuestName(contract.getPartyA());
         paymentConfirm.setContractId(contract.getId());
         paymentConfirm.setContractName(contract.getContractName());
         paymentConfirm.setContractNo(contract.getContractNo());
         paymentConfirm.setScheduleId(fitstSchedule.getId());
         paymentConfirm.setScheduleName(fitstSchedule.getStageName());
         paymentConfirm.setConfirmTime(new Date());
         paymentConfirm.setTransationAmount(fitstSchedule.getPlannedAmount());
         paymentConfirm.setReceivableAmount(fitstSchedule.getPlannedAmount());
         paymentConfirm.setTotalAmount(fitstSchedule.getPlannedAmount().multiply(new BigDecimal("-1")));
         PaymentConfirm lastNewConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId,contract.getId())
               .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
         BigDecimal lastNewTotal = new BigDecimal("0");
         if(ObjUtil.isNotNull(lastNewConfirm)){
            lastNewTotal = lastNewConfirm.getTotalAmount();
         }
         paymentConfirm.setTotalAmount(lastNewTotal.subtract(paymentConfirm.getReceivableAmount()));
         if (fitstSchedule.getPaymentRatio().compareTo(new BigDecimal("0"))>0){
            paymentConfirmMapper.insert(paymentConfirm);
         }
         savePaymentConfirm(contract,fitstSchedule);
         //新增合同履约记录
         ContractPaymentScheduleProcess process = new ContractPaymentScheduleProcess();
         process.setContractId(contract.getId());
@@ -239,14 +234,14 @@
         process.setScheduleName(fitstSchedule.getStageName());
         process.setProcessDate(contract.getSignDate());
         contractPaymentScheduleProcessMapper.insert(process);
         //当前为合同最后阶段
         PaymentConfirm newConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId,contract.getId())
               .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
         if (ObjUtil.isNotNull(newConfirm)&&newConfirm.getTotalAmount().compareTo(new BigDecimal("0"))>=0){
            //有预付且超过应收 关闭合同状态
            contract.setContractStatus(3);
            baseMapper.updateById(contract);
         }
//         //当前为合同最后阶段
//         PaymentConfirm newConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId,contract.getId())
//               .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
//         if (ObjUtil.isNotNull(newConfirm)&&newConfirm.getTotalAmount().compareTo(new BigDecimal("0"))>=0){
//            //有预付且超过应收 关闭合同状态
//            contract.setContractStatus(3);
//            baseMapper.updateById(contract);
//         }
      }else if (fitstSchedule.getStageName().equals("发货前")) {
         //更新合同下个阶段
         contract.setNextScheduleName("无");
@@ -261,29 +256,7 @@
         baseMapper.updateById(contract);
      }else {
         //新增应收
         PaymentConfirm paymentConfirm = new PaymentConfirm();
         paymentConfirm.setBusinessType("质保金");
         paymentConfirm.setBusGuestId(contract.getPartyAId());
         paymentConfirm.setBusGuestName(contract.getPartyA());
         paymentConfirm.setContractId(contract.getId());
         paymentConfirm.setContractName(contract.getContractName());
         paymentConfirm.setContractNo(contract.getContractNo());
         paymentConfirm.setScheduleId(fitstSchedule.getId());
         paymentConfirm.setScheduleName(fitstSchedule.getStageName());
         paymentConfirm.setConfirmTime(new Date());
         paymentConfirm.setTransationAmount(fitstSchedule.getPlannedAmount());
         paymentConfirm.setReceivableAmount(fitstSchedule.getPlannedAmount());
         paymentConfirm.setTotalAmount(fitstSchedule.getPlannedAmount().multiply(new BigDecimal("-1")));
         PaymentConfirm lastNewConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId,contract.getId())
               .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
         BigDecimal lastNewTotal = new BigDecimal("0");
         if(ObjUtil.isNotNull(lastNewConfirm)){
            lastNewTotal = lastNewConfirm.getTotalAmount();
         }
         paymentConfirm.setTotalAmount(lastNewTotal.subtract(paymentConfirm.getReceivableAmount()));
         if (fitstSchedule.getPaymentRatio().compareTo(new BigDecimal("0"))>0) {
            paymentConfirmMapper.insert(paymentConfirm);
         }
         savePaymentConfirm(contract,fitstSchedule);
      }
      //查询是否有后续阶段
      List<ContractPaymentSchedule> afterSchedule = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
@@ -343,6 +316,76 @@
         fitstSchedule.setEffectiveDate(DateUtil.offsetDay(contract.getSignDate(),fitstSchedule.getAgreedDays()));
         fitstSchedule.setEffectiveEndDate(contract.getExpirationDate());
         contractPaymentScheduleMapper.updateById(fitstSchedule);
      }
   }
   private void savePaymentConfirm(Contract contract, ContractPaymentSchedule schedule) {
      PaymentConfirm paymentConfirm = new PaymentConfirm();
      paymentConfirm.setBusinessType(schedule.getStageName()+"应收");
      paymentConfirm.setBusGuestId(contract.getPartyAId());
      paymentConfirm.setBusGuestName(contract.getPartyA());
      paymentConfirm.setContractId(contract.getId());
      paymentConfirm.setContractName(contract.getContractName());
      paymentConfirm.setContractNo(contract.getContractNo());
      paymentConfirm.setScheduleId(schedule.getId());
      paymentConfirm.setScheduleName(schedule.getStageName());
      paymentConfirm.setConfirmTime(new Date());
      paymentConfirm.setTransationAmount(schedule.getPlannedAmount());
      paymentConfirm.setReceivableAmount(schedule.getPlannedAmount());
      paymentConfirm.setTotalAmount(schedule.getPlannedAmount().multiply(new BigDecimal("-1")));
      PaymentConfirm lastNewConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId,contract.getId())
            .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
      BigDecimal lastNewTotal = new BigDecimal("0");
      if(ObjUtil.isNotNull(lastNewConfirm)){
         lastNewTotal = lastNewConfirm.getTotalAmount();
      }
      paymentConfirm.setTotalAmount(lastNewTotal.subtract(paymentConfirm.getReceivableAmount()));
      if (schedule.getPaymentRatio().compareTo(new BigDecimal("0"))>0){
         paymentConfirmMapper.insert(paymentConfirm);
      }
      //客户付款完成合同
      List<Contract> customerCompleteContractList = baseMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getPartyAId, contract.getPartyAId())
            .eq(Contract::getPartyBId,SecurityUtils.getUser().getCompId()).apply(" amount = paid_amount"));
      if (ArrayUtil.isNotEmpty(customerCompleteContractList)){
         List<BigDecimal> outAmountList = new ArrayList<>();
         for (Contract completeContract:customerCompleteContractList
             ) {
            PaymentConfirm contractLastConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getContractId,completeContract.getId())
                  .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1"));
            //查询客户付款完成合同中最后明细金额 有预收转入当前合同
            if (contractLastConfirm.getTotalAmount().compareTo(new BigDecimal("0"))>0){
               //新增完成付款合同 资金转出
               PaymentConfirm outConfirm = BeanUtil.copyProperties(contractLastConfirm,PaymentConfirm.class,"id","transationAmount",
                     "advanceAmount","receivableAmount","overdueAmount","totalAmount");
               outConfirm.setBusinessType("资金转出");
               outConfirm.setConfirmTime(new Date());
               outConfirm.setTransationAmount(contractLastConfirm.getTotalAmount());
               outConfirm.setTotalAmount(new BigDecimal("0"));
               paymentConfirmMapper.insert(outConfirm);
               outAmountList.add(outConfirm.getTransationAmount());
            }
         }
         if (ArrayUtil.isNotEmpty(outAmountList.toArray())){
            BigDecimal currentSum = new BigDecimal("0");
            for (BigDecimal currentAmount:outAmountList
                ) {
               currentSum=currentSum.add(currentAmount);
               //新增当前合同 资金转入
               PaymentConfirm inConfirm = BeanUtil.copyProperties(paymentConfirm,PaymentConfirm.class,"id","transationAmount",
                     "advanceAmount","receivableAmount","overdueAmount","totalAmount");
               inConfirm.setBusinessType("资金转入");
               inConfirm.setConfirmTime(new Date());
               inConfirm.setTransationAmount(currentSum.subtract(currentAmount));
               inConfirm.setTotalAmount(paymentConfirm.getTotalAmount().add(currentSum));
               if (inConfirm.getTotalAmount().compareTo(new BigDecimal("0"))>0){
                  inConfirm.setAdvanceAmount(inConfirm.getTotalAmount());
               }else if (inConfirm.getTotalAmount().compareTo(new BigDecimal("0"))<0){
                  inConfirm.setReceivableAmount(inConfirm.getTotalAmount().multiply(new BigDecimal("-1")));
               }
               paymentConfirmMapper.insert(inConfirm);
            }
         }
      }
   }
@@ -426,6 +469,7 @@
      newContract.setPaidAmount(new BigDecimal("0"));
      newContract.setBillingStatus("0");
      newContract.setErpPushFlag("0");
      newContract.setContractStatus(0);
      List<Contract> contracts;
      contracts = baseMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo,newContract.getContractNo()));
      while (ArrayUtil.isNotEmpty(contracts.toArray())){
@@ -433,6 +477,111 @@
         contracts = baseMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo,newContract.getContractNo()));
      }
      baseMapper.insert(newContract);
      List<ContractSubjectMatter> subjectMatterList = contractSubjectMatterMapper.selectList(Wrappers.<ContractSubjectMatter>lambdaQuery().eq(ContractSubjectMatter::getContractId,id));
      subjectMatterList.stream().forEach(contractSubjectMatter -> {
         ContractSubjectMatter subjectMatter = BeanUtil.copyProperties(contractSubjectMatter,ContractSubjectMatter.class,"id","createTime");
         subjectMatter.setContractId(newContract.getId());
         subjectMatter.setDeliveryStatus(0);
         subjectMatter.setDeliveredQuantity(new BigDecimal("0"));
         subjectMatter.setRemainingQuantity(new BigDecimal("0"));
         contractSubjectMatterMapper.insert(subjectMatter);
      });
      List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId,id));
      scheduleList.stream().forEach(contractPaymentSchedule -> {
         ContractPaymentSchedule schedule = BeanUtil.copyProperties(contractPaymentSchedule,ContractPaymentSchedule.class,"id","createTime");
         schedule.setContractId(newContract.getId());
         schedule.setPaymentStatus(0);
         schedule.setActualAmount(new BigDecimal("0"));
         schedule.setEffectiveDate(null);
         schedule.setEffectiveEndDate(null);
         contractPaymentScheduleMapper.insert(schedule);
      });
      return R.ok();
   }
   @Override
   public void exportContractYMJJGCLWord(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","");
         item.put("unit","");
         item.put("quantity",contractSubjectMatter.getQuantity());
         item.put("danzhong","");
         item.put("zongzhong","");
         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("companyName",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/ymjjgclht.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();
      }
   }
}