shiyunteng
1 天以前 fca21683e1b5b906d2514082ddfbae8eb820c9ea
platformx-business-finance-biz/src/main/java/com/by4cloud/platformx/business/service/impl/ContractServiceImpl.java
@@ -7,6 +7,8 @@
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.business.api.feign.RemoteFlowProcessService;
import com.by4cloud.platformx.business.constant.FlowNameEnum;
import com.by4cloud.platformx.business.dto.ContractAddDTO;
import com.by4cloud.platformx.business.dto.ContractUpdateDTO;
import com.by4cloud.platformx.business.entity.*;
@@ -16,6 +18,8 @@
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 lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@@ -23,6 +27,7 @@
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
/**
@@ -39,6 +44,7 @@
   private final ContractPaymentScheduleProcessMapper contractPaymentScheduleProcessMapper;
   private final PaymentConfirmMapper paymentConfirmMapper;
   private final CurrentOverdueMapper currentOverdueMapper;
   private final RemoteFlowProcessService remoteFlowProcessService;
   @Override
   public R add(ContractAddDTO addDTO) {
@@ -114,6 +120,14 @@
      }
      Contract contract = BeanUtil.copyProperties(updateDTO,Contract.class);
      contract.setContractNo(ContractNumberGenerator.generateContractNumber());
      contract.setBillingStatus("0");
      contract.setErpPushFlag("0");
      List<Contract> contracts;
      contracts = baseMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo,contract.getContractNo()));
      while (ArrayUtil.isNotEmpty(contracts.toArray())){
         contract.setContractNo(ContractNumberGenerator.generateContractNumber());
         contracts = baseMapper.selectList(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo,contract.getContractNo()));
      }
      baseMapper.updateById(contract);
      if (ArrayUtil.isNotEmpty(updateDTO.getContractSubjectMatter())){
@@ -135,12 +149,20 @@
         AtomicInteger index = new AtomicInteger(1);
         updateDTO.getContractPaymentSchedule().stream().forEach(contractPaymentScheduleAddDTO -> {
            int currentIndex = index.getAndIncrement();
            ContractPaymentSchedule subjectMatter = BeanUtil.copyProperties(contractPaymentScheduleAddDTO, ContractPaymentSchedule.class);
            subjectMatter.setContractId(contract.getId());
            subjectMatter.setContractName(contract.getContractName());
            subjectMatter.setPlannedAmount(contract.getAmount().multiply(subjectMatter.getPaymentRatio().divide(new BigDecimal("100"))));
            subjectMatter.setStageOrder(currentIndex);
            contractPaymentScheduleMapper.insert(subjectMatter);
            ContractPaymentSchedule schedule = BeanUtil.copyProperties(contractPaymentScheduleAddDTO, ContractPaymentSchedule.class);
            schedule.setContractId(contract.getId());
            schedule.setContractName(contract.getContractName());
            schedule.setPlannedAmount(contract.getAmount().multiply(schedule.getPaymentRatio().divide(new BigDecimal("100"))));
            schedule.setStageOrder(currentIndex);
            contractPaymentScheduleMapper.insert(schedule);
            if (contractPaymentScheduleAddDTO.getStageName().equals("货到签收")){
               contract.setArrivalScheduleId(schedule.getId());
               baseMapper.updateById(contract);
            }
            if (contractPaymentScheduleAddDTO.getStageName().equals("调试完成或验收")){
               contract.setAcceptScheduleId(schedule.getId());
               baseMapper.updateById(contract);
            }
         });
      }
@@ -160,7 +182,23 @@
   }
   @Override
   public Boolean takeEffect(Long id) {
   public R startApproval(Long id) {
      Contract contract = baseMapper.selectById(id);
      //启动流程
      ProcessInstanceParamDto dto = new ProcessInstanceParamDto();
      Map<String, Object> map = BeanUtil.beanToMap(contract);
      dto.setParamMap(map);
      dto.setFlowName(FlowNameEnum.合同审批.name());
      R r1 = remoteFlowProcessService.startProcessInstance(dto);
      if (r1.getCode() == 1) {
         return R.failed("流程启动失败");
      }
      contract.setContractStatus(2);
      baseMapper.updateById(contract);
      return R.ok();
   }
   public void takeEffect(Long id) {
      Contract contract = baseMapper.selectById(id);
      contract.setContractStatus(1);
      baseMapper.updateById(contract);
@@ -181,6 +219,13 @@
         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()));
         paymentConfirmMapper.insert(paymentConfirm);
         //新增合同履约记录
         ContractPaymentScheduleProcess process = new ContractPaymentScheduleProcess();
@@ -190,6 +235,49 @@
         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 (newConfirm.getTotalAmount().compareTo(new BigDecimal("0"))>=0){
            //有预付且超过应收 关闭合同状态
            contract.setContractStatus(3);
            baseMapper.updateById(contract);
         }
      }else if (fitstSchedule.getStageName().equals("发货前")) {
         //更新合同下个阶段
         contract.setNextScheduleName("无");
         baseMapper.updateById(contract);
      }else if (fitstSchedule.getStageName().equals("货到签收")) {
         //更新合同下个阶段
         contract.setNextScheduleName("货到签收");
         baseMapper.updateById(contract);
      }else if (fitstSchedule.getStageName().equals("调试完成或验收")) {
         //更新合同下个阶段
         contract.setNextScheduleName("调试完成或验收");
         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()));
         paymentConfirmMapper.insert(paymentConfirm);
      }
      //查询是否有后续阶段
      List<ContractPaymentSchedule> afterSchedule = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
@@ -204,7 +292,7 @@
            endSchedule.setEffectiveEndDate(contract.getExpirationDate());
            contractPaymentScheduleMapper.updateById(endSchedule);
            //第一阶段生效时间
            fitstSchedule.setEffectiveDate(DateUtil.offsetDay(new Date(),fitstSchedule.getAgreedDays()));
            fitstSchedule.setEffectiveDate(DateUtil.offsetDay(contract.getSignDate(),fitstSchedule.getAgreedDays()));
            fitstSchedule.setEffectiveEndDate(endSchedule.getEffectiveDate());
            contractPaymentScheduleMapper.updateById(fitstSchedule);
@@ -235,18 +323,19 @@
         baseMapper.updateById(contract);
      }
      if(ArrayUtil.isNotEmpty(afterSchedule.toArray())&&afterSchedule.size()>1){
         //第一阶段生效时间
         fitstSchedule.setEffectiveDate(DateUtil.offsetDay(contract.getSignDate(),fitstSchedule.getAgreedDays()));
         contractPaymentScheduleMapper.updateById(fitstSchedule);
         //更新合同下个阶段
         contract.setNextScheduleName(afterSchedule.get(0).getStageName());
         baseMapper.updateById(contract);
      }
      //只有当前合同签字阶段
      if(ArrayUtil.isEmpty(afterSchedule.toArray())){
         fitstSchedule.setEffectiveDate(DateUtil.offsetDay(contract.getSignDate(),fitstSchedule.getAgreedDays()));
         fitstSchedule.setEffectiveEndDate(contract.getExpirationDate());
         contractPaymentScheduleMapper.updateById(fitstSchedule);
         //更新合同下个阶段
         contract.setNextScheduleName("无");
         baseMapper.updateById(contract);
      }
      return Boolean.TRUE;
   }
   @Override
@@ -281,6 +370,7 @@
            PaymentConfirm oiverdueConfirm = paymentConfirmMapper.selectOne(Wrappers.<PaymentConfirm>lambdaQuery().eq(PaymentConfirm::getScheduleId, contractPaymentSchedule.getId())
                  .eq(PaymentConfirm::getBusinessType, "应收超期"));
            if (ObjUtil.isNull(oiverdueConfirm)) {
               overdueConfirm.setCompId(contractPaymentSchedule.getCompId());
               paymentConfirmMapper.insert(overdueConfirm);
            }
            //当前逾期
@@ -299,6 +389,7 @@
            CurrentOverdue overdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getContractId,currentOverdue.getContractId())
                  .eq(CurrentOverdue::getScheduleId,currentOverdue.getScheduleId()).last("limit 1"));
            if (ObjUtil.isNull(overdue)){
               currentOverdue.setCompId(contractPaymentSchedule.getCompId());
               currentOverdueMapper.insert(currentOverdue);
            }else {
               overdue.setOverdueDuration(BigDecimal.valueOf(DateUtil.betweenDay(contractPaymentSchedule.getEffectiveEndDate(),new Date(),true)));