李白
昨天 e7e6d39b095b6a763e54ab34631171c3de32ec9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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.Contract;
import com.by4cloud.platformx.business.entity.ContractPaymentSchedule;
import com.by4cloud.platformx.business.entity.ContractPaymentScheduleProcess;
import com.by4cloud.platformx.business.entity.PaymentConfirm;
import com.by4cloud.platformx.business.mapper.ContractMapper;
import com.by4cloud.platformx.business.mapper.ContractPaymentScheduleMapper;
import com.by4cloud.platformx.business.mapper.ContractPaymentScheduleProcessMapper;
import com.by4cloud.platformx.business.mapper.PaymentConfirmMapper;
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<ContractPaymentScheduleProcessMapper, ContractPaymentScheduleProcess> implements ContractPaymentScheduleProcessService {
 
    private final ContractPaymentScheduleMapper contractPaymentScheduleMapper;
    private final ContractPaymentScheduleProcessMapper processMapper;
    private final ContractMapper contractMapper;
    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());
        //新增当前阶段应收
        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.<PaymentConfirm>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.<ContractPaymentSchedule>lambdaQuery().eq(ContractPaymentSchedule::getContractId, schedule.getContractId())
                .lt(ContractPaymentSchedule::getStageOrder, schedule.getStageOrder()).orderByDesc(ContractPaymentSchedule::getCreateTime).last("limit 1"));
        if (ObjUtil.isNotNull(beforeSchedule)){
            beforeSchedule.setEffectiveEndDate(DateUtil.offsetDay(addDTO.getProcessDate(),beforeSchedule.getAgreedDays()));
            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.<PaymentConfirm>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<ContractPaymentSchedule> afterSchedule = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>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.<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();
                }
                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);
        }
        return R.ok();
    }
 
    @Override
    public R selectScheduleProcess(Long id) {
        MPJLambdaWrapper<ContractPaymentSchedule> wrapper = new MPJLambdaWrapper<ContractPaymentSchedule>()
                .select(ContractPaymentSchedule::getId,ContractPaymentSchedule::getStageName,ContractPaymentSchedule::getEffectiveDate,
                        ContractPaymentSchedule::getPlannedAmount,ContractPaymentSchedule::getPlannedAmount,
                        ContractPaymentSchedule::getEffectiveEndDate,ContractPaymentSchedule::getPaymentDate,
                        ContractPaymentSchedule::getPaymentStatus,ContractPaymentSchedule::getActualAmount)
                .eq(ContractPaymentSchedule::getContractId,id)
                .orderByAsc(ContractPaymentSchedule::getCreateTime);
        List<ContractPaymentScheduleVo> scheduleVoList = contractPaymentScheduleMapper.selectJoinList(ContractPaymentScheduleVo.class,wrapper);
        if (ArrayUtil.isEmpty(scheduleVoList.toArray())){
            scheduleVoList.stream().forEach(contractPaymentScheduleVo -> {
                List<ContractPaymentScheduleProcess> paymentScheduleProcessList = processMapper.selectList(Wrappers.<ContractPaymentScheduleProcess>lambdaQuery()
                        .eq(ContractPaymentScheduleProcess::getContractId,contractPaymentScheduleVo.getId())
                        .orderByAsc(ContractPaymentScheduleProcess::getCreateTime));
                if (ArrayUtil.isNotEmpty(paymentScheduleProcessList.toArray())){
                    contractPaymentScheduleVo.setProcessVoList(BeanUtil.copyToList(paymentScheduleProcessList, ScheduleProcessVo.class));
                }
            });
        }
        return R.ok(scheduleVoList);
    }
}