shiyunteng
昨天 fca21683e1b5b906d2514082ddfbae8eb820c9ea
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
package com.by4cloud.platformx.business.service.impl;
 
import cn.hutool.core.bean.BeanUtil;
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.PaymentConfirmAddDTO;
import com.by4cloud.platformx.business.entity.*;
import com.by4cloud.platformx.business.mapper.ContractPaymentScheduleMapper;
import com.by4cloud.platformx.business.mapper.CurrentOverdueMapper;
import com.by4cloud.platformx.business.mapper.HistoryOverdueMapper;
import com.by4cloud.platformx.business.mapper.PaymentConfirmMapper;
import com.by4cloud.platformx.business.service.BusinessCustomerService;
import com.by4cloud.platformx.business.service.ContractPaymentScheduleService;
import com.by4cloud.platformx.business.service.ContractService;
import com.by4cloud.platformx.business.service.PaymentConfirmService;
import com.by4cloud.platformx.common.core.util.R;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
 
/**
 * 收款确认
 *
 * @author syt
 * @date 2026-04-29 11:33:26
 */
@Service
@RequiredArgsConstructor
public class PaymentConfirmServiceImpl extends ServiceImpl<PaymentConfirmMapper, PaymentConfirm> implements PaymentConfirmService {
 
    private final ContractService contractService;
    private final BusinessCustomerService businessCustomerService;
    private final ContractPaymentScheduleMapper contractPaymentScheduleMapper;
    private final CurrentOverdueMapper currentOverdueMapper;
    private final HistoryOverdueMapper historyOverdueMapper;
 
    @Override
    public R add(PaymentConfirmAddDTO addDTO) {
        Contract contract = contractService.getOne(Wrappers.<Contract>lambdaQuery().eq(Contract::getContractNo,addDTO.getContractNo()));
        if(ObjUtil.isNull(contract)){
            return R.failed("合同查询失败,请检查合同编号");
        }
        BusinessCustomer customer = businessCustomerService.getOne(Wrappers.<BusinessCustomer>lambdaQuery()
                .eq(StrUtil.isNotBlank(addDTO.getBusGuestName()),BusinessCustomer::getCompanyName,addDTO.getBusGuestName())
                .eq(StrUtil.isNotBlank(addDTO.getBusGuestId()),BusinessCustomer::getId,addDTO.getBusGuestId()));
        if(ObjUtil.isNull(customer)){
            return R.failed("客商查询失败,请检查客商");
        }
 
        PaymentConfirm lastConfirm = baseMapper.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();
        }
 
        //转入
        if (StrUtil.equals(addDTO.getInOrOut(),"1")){
            BigDecimal newtotal = addDTO.getTransationAmount().add(lastTotal);
            PaymentConfirm entity = new PaymentConfirm();
            entity.setContractId(contract.getId());
            entity.setContractName(contract.getContractName());
            entity.setContractNo(addDTO.getContractNo());
            if(ObjUtil.isNotNull(lastConfirm)){
                entity.setScheduleId(lastConfirm.getScheduleId());
                entity.setScheduleName(lastConfirm.getScheduleName());
            }
            entity.setBusGuestId(customer.getId());
            entity.setBusGuestName(customer.getCompanyName());
            entity.setBusinessType("客户付款");
            if (newtotal.compareTo(new BigDecimal("0"))>0){
                entity.setTransationAmount(addDTO.getTransationAmount());
                entity.setAdvanceAmount(newtotal);
                entity.setTotalAmount(newtotal);
                entity.setConfirmTime(new Date());
                baseMapper.insert(entity);
            }else if (newtotal.compareTo(new BigDecimal("0"))==0){
                entity.setTransationAmount(addDTO.getTransationAmount());
                entity.setTotalAmount(newtotal);
                entity.setConfirmTime(new Date());
                baseMapper.insert(entity);
            }else {
                entity.setTransationAmount(addDTO.getTransationAmount());
                entity.setReceivableAmount(newtotal.multiply(new BigDecimal("-1")));
                entity.setTotalAmount(newtotal);
                entity.setConfirmTime(new Date());
                baseMapper.insert(entity);
            }
            List<ContractPaymentSchedule> scheduleList = contractPaymentScheduleMapper.selectList(Wrappers.<ContractPaymentSchedule>lambdaQuery()
                    .eq(ContractPaymentSchedule::getContractId,contract.getId()).ne(ContractPaymentSchedule::getPaymentStatus,2)
                    .orderByAsc(ContractPaymentSchedule::getCreateTime));
            if (ArrayUtil.isNotEmpty(scheduleList.toArray())){
                BigDecimal remain = addDTO.getTransationAmount();
                for (ContractPaymentSchedule schedule:scheduleList) {
                    BigDecimal lastRemain = remain;
                    remain = remain.subtract(StrUtil.equals(schedule.getPaymentStatus()+"","0")?schedule.getPlannedAmount():
                            schedule.getPlannedAmount().subtract(schedule.getActualAmount()));
                    if (remain.compareTo(new BigDecimal("0"))>=0){
                        schedule.setActualAmount(schedule.getPlannedAmount());
                        schedule.setPaymentDate(addDTO.getConfirmTime());
                        schedule.setPaymentStatus(2);
                        contractPaymentScheduleMapper.updateById(schedule);
                        //查询是否有当前逾期
                        CurrentOverdue currentOverdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getScheduleId,schedule.getId())
                                .eq(CurrentOverdue::getContractId,schedule.getContractId()).last("limit 1"));
                        if (ObjUtil.isNotNull(currentOverdue)){
                            //新增逾期历史
                            HistoryOverdue historyOverdue = BeanUtil.copyProperties(currentOverdue,HistoryOverdue.class,"id");
                            historyOverdue.setPaymentTime(addDTO.getConfirmTime());
                            historyOverdue.setCompId(schedule.getCompId());
                            historyOverdueMapper.insert(historyOverdue);
                            //删除当前逾期
                            currentOverdueMapper.deleteById(currentOverdue);
                        }
                    }else {
                        schedule.setActualAmount(StrUtil.equals(schedule.getPaymentStatus()+"","0")?schedule.getPlannedAmount().add(remain):
                                schedule.getActualAmount().add(lastRemain));
                        schedule.setPaymentDate(addDTO.getConfirmTime());
                        schedule.setPaymentStatus(1);
                        contractPaymentScheduleMapper.updateById(schedule);
                        //查询是否有当前逾期
                        CurrentOverdue currentOverdue = currentOverdueMapper.selectOne(Wrappers.<CurrentOverdue>lambdaQuery().eq(CurrentOverdue::getScheduleId,schedule.getId())
                                .eq(CurrentOverdue::getContractId,schedule.getContractId()).last("limit 1"));
                        if (ObjUtil.isNotNull(currentOverdue)){
                            //新增逾期历史
                            HistoryOverdue historyOverdue = BeanUtil.copyProperties(currentOverdue,HistoryOverdue.class,"id");
                            historyOverdue.setReceivableAmount(lastRemain);
                            historyOverdue.setPaymentTime(addDTO.getConfirmTime());
                            historyOverdue.setCompId(schedule.getCompId());
                            historyOverdueMapper.insert(historyOverdue);
                            //查询历史已付逾期金额
                            List<HistoryOverdue> historyOverdueList = historyOverdueMapper.selectList(Wrappers.<HistoryOverdue>lambdaQuery().eq(HistoryOverdue::getScheduleId,currentOverdue.getScheduleId()));
                            BigDecimal hisTotal = historyOverdueList.stream().map(item->item.getReceivableAmount()).reduce(BigDecimal.ZERO,BigDecimal::add);
                            //更新当前逾期
                            currentOverdue.setReceivableAmount(schedule.getPlannedAmount().subtract(hisTotal));
                            currentOverdueMapper.updateById(currentOverdue);
                        }
                        break;
                    }
                }
            }
        }
        //转出
        if (StrUtil.equals(addDTO.getInOrOut(),"2")){
            PaymentConfirm entity = new PaymentConfirm();
            entity.setContractId(contract.getId());
            entity.setContractName(contract.getContractName());
            entity.setContractNo(addDTO.getContractNo());
 
            entity.setBusGuestId(customer.getId());
            entity.setBusGuestName(customer.getCompanyName());
            entity.setBusinessType("");
            entity.setTransationAmount(addDTO.getTransationAmount());
            BigDecimal newtotal = addDTO.getTransationAmount().multiply(new BigDecimal("-1")).add(lastConfirm.getTotalAmount());
            if (newtotal.compareTo(new BigDecimal("0"))>0){
                entity.setAdvanceAmount(newtotal);
                entity.setTotalAmount(newtotal);
            }else if (newtotal.compareTo(new BigDecimal("0"))==0){
                entity.setTotalAmount(newtotal);
            }else {
                return R.failed("当前客户余额为"+lastConfirm.getTotalAmount()+"不足以满足当前发生金额,无法操作");
            }
            entity.setConfirmTime(new Date());
            baseMapper.insert(entity);
        }
        return R.ok();
    }
}