package com.by4cloud.platformx.business.service.impl; import cn.hutool.core.bean.BeanUtil; 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.business.dto.ContractPaymentScheduleProcessAddDTO; import com.by4cloud.platformx.business.entity.*; import com.by4cloud.platformx.business.mapper.*; import com.by4cloud.platformx.business.service.ContractPaymentScheduleProcessService; import com.by4cloud.platformx.business.vo.ContractPaymentScheduleVo; import com.by4cloud.platformx.business.vo.ScheduleProcessVo; import com.by4cloud.platformx.common.core.util.R; import com.github.yulichang.wrapper.MPJLambdaWrapper; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.util.Date; import java.util.List; /** * 履约节点 * * @author syt * @date 2026-05-11 11:10:26 */ @Service @RequiredArgsConstructor public class ContractPaymentScheduleProcessServiceImpl extends ServiceImpl implements ContractPaymentScheduleProcessService { private final ContractPaymentScheduleMapper contractPaymentScheduleMapper; private final ContractPaymentScheduleProcessMapper processMapper; private final ContractMapper contractMapper; private final ContractSubjectMatterMapper subjectMatterMapper; private final PaymentConfirmMapper paymentConfirmMapper; @Override public R add(ContractPaymentScheduleProcessAddDTO addDTO) { ContractPaymentSchedule schedule = contractPaymentScheduleMapper.selectById(addDTO.getScheduleId()); if (ObjUtil.isNull(schedule)){ return R.failed("履约阶段与合同不一致,请联系技术人员"); } //合同 Contract contract = contractMapper.selectById(schedule.getContractId()); List subjectMatterList = subjectMatterMapper.selectList(Wrappers.lambdaQuery().eq(ContractSubjectMatter::getContractId,contract.getId())); if (ArrayUtil.isEmpty(subjectMatterList.toArray())){ return R.failed("该合同标的物异常,请联系技术人员"); } if (!subjectMatterList.stream().allMatch(item->item.getDeliveryStatus()==2)){ return R.failed("出库数量小于当前合同标的物数量,无法进行当前操作"); } //新增当前阶段应收 PaymentConfirm currentConfim = new PaymentConfirm(); currentConfim.setBusinessType(schedule.getStageName()+"应收"); currentConfim.setBusGuestId(contract.getPartyAId()); currentConfim.setBusGuestName(contract.getPartyA()); currentConfim.setContractId(contract.getId()); currentConfim.setContractName(contract.getContractName()); currentConfim.setContractNo(contract.getContractNo()); currentConfim.setScheduleId(schedule.getId()); currentConfim.setScheduleName(schedule.getStageName()); currentConfim.setConfirmTime(addDTO.getProcessDate()); currentConfim.setTransationAmount(schedule.getPlannedAmount()); currentConfim.setReceivableAmount(schedule.getPlannedAmount()); PaymentConfirm lastConfirm = paymentConfirmMapper.selectOne(Wrappers.lambdaQuery().eq(PaymentConfirm::getContractId,contract.getId()) .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1")); BigDecimal lastTotal = new BigDecimal("0"); if(ObjUtil.isNotNull(lastConfirm)){ lastTotal = lastConfirm.getTotalAmount(); } currentConfim.setTotalAmount(lastTotal.subtract(currentConfim.getReceivableAmount())); paymentConfirmMapper.insert(currentConfim); //履约 ContractPaymentScheduleProcess contractPaymentScheduleProcess = BeanUtil.copyProperties(addDTO,ContractPaymentScheduleProcess.class); contractPaymentScheduleProcess.setContractId(schedule.getContractId()); contractPaymentScheduleProcess.setContractName(schedule.getContractName()); contractPaymentScheduleProcess.setScheduleId(schedule.getId()); contractPaymentScheduleProcess.setScheduleName(schedule.getStageName()); baseMapper.insert(contractPaymentScheduleProcess); //更新当前阶段 if (ObjUtil.isNull(schedule.getEffectiveDate())) { schedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getProcessDate(),schedule.getAgreedDays())); contractPaymentScheduleMapper.updateById(schedule); } //查询是否有之前阶段 ContractPaymentSchedule beforeSchedule = contractPaymentScheduleMapper.selectOne(Wrappers.lambdaQuery().eq(ContractPaymentSchedule::getContractId, schedule.getContractId()) .lt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder()).orderByDesc(ContractPaymentSchedule::getCreateTime).last("limit 1")); if (ObjUtil.isNotNull(beforeSchedule)){ beforeSchedule.setEffectiveEndDate(schedule.getEffectiveDate()); contractPaymentScheduleMapper.updateById(beforeSchedule); //之前阶段是否收款完成 // if (beforeSchedule.getPaymentStatus()!=2){ // //新增之前阶段超期 // PaymentConfirm beforeConfim = new PaymentConfirm(); // beforeConfim.setBusinessType("应收超期"); // beforeConfim.setBusGuestId(contract.getPartyAId()); // beforeConfim.setBusGuestName(contract.getPartyA()); // beforeConfim.setContractId(contract.getId()); // beforeConfim.setContractName(contract.getContractName()); // beforeConfim.setContractNo(contract.getContractNo()); // beforeConfim.setScheduleId(schedule.getId()); // beforeConfim.setScheduleName(schedule.getStageName()); // beforeConfim.setConfirmTime(addDTO.getProcessDate()); // beforeConfim.setTransationAmount(schedule.getPlannedAmount()); // PaymentConfirm newLastConfirm = paymentConfirmMapper.selectOne(Wrappers.lambdaQuery().eq(PaymentConfirm::getContractId,contract.getId()) // .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1")); // beforeConfim.setOverdueAmount(StrUtil.equals(schedule.getPaymentStatus()+"","0")? // schedule.getPlannedAmount(): // schedule.getPlannedAmount().subtract(schedule.getActualAmount())); // beforeConfim.setTotalAmount(newLastConfirm.getTotalAmount()); // paymentConfirmMapper.insert(beforeConfim); // } } //查询是否有后续阶段 List afterSchedule = contractPaymentScheduleMapper.selectList(Wrappers.lambdaQuery() .eq(ContractPaymentSchedule::getContractId, schedule.getContractId()) .gt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder())); if (ArrayUtil.isNotEmpty(afterSchedule.toArray())&&afterSchedule.size()==1){ //最后阶段生效时间 ContractPaymentSchedule endSchedule = afterSchedule.get(0); if (StrUtil.equals(endSchedule.getStageName(),"质保金")) { endSchedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getProcessDate(), endSchedule.getAgreedDays())); endSchedule.setEffectiveEndDate(contract.getExpirationDate()); contractPaymentScheduleMapper.updateById(endSchedule); //当前阶段生效时间 schedule.setEffectiveDate(DateUtil.offsetDay(addDTO.getProcessDate(), schedule.getAgreedDays())); schedule.setEffectiveEndDate(endSchedule.getEffectiveDate()); contractPaymentScheduleMapper.updateById(schedule); //最后阶段应收 PaymentConfirm newConfim = new PaymentConfirm(); newConfim.setBusinessType(endSchedule.getStageName() + "应收"); newConfim.setBusGuestId(contract.getPartyAId()); newConfim.setBusGuestName(contract.getPartyA()); newConfim.setContractId(contract.getId()); newConfim.setContractName(contract.getContractName()); newConfim.setContractNo(contract.getContractNo()); newConfim.setScheduleId(endSchedule.getId()); newConfim.setScheduleName(schedule.getStageName()); newConfim.setConfirmTime(addDTO.getProcessDate()); newConfim.setTransationAmount(endSchedule.getPlannedAmount()); newConfim.setReceivableAmount(endSchedule.getPlannedAmount()); PaymentConfirm lastNewConfirm = paymentConfirmMapper.selectOne(Wrappers.lambdaQuery().eq(PaymentConfirm::getContractId, contract.getId()) .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1")); BigDecimal lastNewTotal = new BigDecimal("0"); if (ObjUtil.isNotNull(lastNewConfirm)) { lastNewTotal = lastNewConfirm.getTotalAmount(); } newConfim.setTotalAmount(lastNewTotal.subtract(newConfim.getReceivableAmount())); paymentConfirmMapper.insert(newConfim); } //更新合同下个阶段 contract.setNextScheduleName(endSchedule.getStageName()); contractMapper.updateById(contract); } if(ArrayUtil.isNotEmpty(afterSchedule.toArray())&&afterSchedule.size()>1){ //更新合同下个阶段 contract.setNextScheduleName(afterSchedule.get(0).getStageName()); contractMapper.updateById(contract); } if(ArrayUtil.isEmpty(afterSchedule.toArray())){ schedule.setEffectiveEndDate(contract.getExpirationDate()); contractPaymentScheduleMapper.updateById(schedule); //更新合同下个阶段 contract.setNextScheduleName("无"); contractMapper.updateById(contract); //当前为合同最后阶段 PaymentConfirm lastNewConfirm = paymentConfirmMapper.selectOne(Wrappers.lambdaQuery().eq(PaymentConfirm::getContractId,contract.getId()) .orderByDesc(PaymentConfirm::getCreateTime).last("limit 1")); if (lastNewConfirm.getTotalAmount().compareTo(new BigDecimal("0"))>=0){ //有预付且超过应收 关闭合同状态 contract.setContractStatus(3); contractMapper.updateById(contract); } } return R.ok(); } @Override public R selectScheduleProcess(Long id) { MPJLambdaWrapper wrapper = new MPJLambdaWrapper() .select(ContractPaymentSchedule::getId,ContractPaymentSchedule::getStageName,ContractPaymentSchedule::getEffectiveDate, ContractPaymentSchedule::getPlannedAmount,ContractPaymentSchedule::getPlannedAmount, ContractPaymentSchedule::getEffectiveEndDate,ContractPaymentSchedule::getPaymentDate, ContractPaymentSchedule::getPaymentStatus,ContractPaymentSchedule::getActualAmount, ContractPaymentSchedule::getAgreedDays) .select(ContractPaymentScheduleProcess::getProcessDate) .leftJoin(ContractPaymentScheduleProcess.class,ContractPaymentScheduleProcess::getScheduleId,ContractPaymentSchedule::getId) .eq(ContractPaymentSchedule::getContractId,id) .orderByAsc(ContractPaymentSchedule::getCreateTime); List scheduleVoList = contractPaymentScheduleMapper.selectJoinList(ContractPaymentScheduleVo.class,wrapper); return R.ok(scheduleVoList); } }