chenrui
6 天以前 4b6ed9aec826d49abc239f8d768bfecb91eceabc
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package com.ruoyi.purchase.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.basic.mapper.SupplierManageMapper;
import com.ruoyi.basic.pojo.SupplierManage;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.purchase.dto.PaymentLedgerDto;
import com.ruoyi.purchase.dto.PaymentRegistrationDto;
import com.ruoyi.purchase.mapper.*;
import com.ruoyi.purchase.pojo.PaymentRegistration;
import com.ruoyi.purchase.pojo.ProductRecord;
import com.ruoyi.purchase.pojo.PurchaseLedger;
import com.ruoyi.purchase.pojo.TicketRegistration;
import com.ruoyi.purchase.service.IPaymentRegistrationService;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 付款登记Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-15
 */
@Service
@AllArgsConstructor
public class PaymentRegistrationServiceImpl extends ServiceImpl<PaymentRegistrationMapper, PaymentRegistration> implements IPaymentRegistrationService {
    private PaymentRegistrationMapper paymentRegistrationMapper;
 
    private PurchaseLedgerMapper purchaseLedgerMapper;
 
    private InvoicePurchaseMapper invoicePurchaseMapper;
 
    private SalesLedgerMapper salesLedgerMapper;
 
    private SupplierManageMapper supplierManageMapper;
 
    private SalesLedgerProductMapper salesLedgerProductMapper;
 
    private TicketRegistrationMapper ticketRegistrationMapper;
 
    private ProductRecordMapper productRecordMapper;
 
    /**
     * 查询付款登记
     *
     * @param id 付款登记主键
     * @return 付款登记
     */
    @Override
    public PaymentRegistration selectPaymentRegistrationById(Long id) {
        return paymentRegistrationMapper.selectPaymentRegistrationById(id);
    }
 
    /**
     * 查询付款登记列表
     *
     * @param paymentRegistrationDto 付款登记
     * @return 付款登记
     */
    @Override
    public List<PaymentRegistrationDto> selectPaymentRegistrationList(PaymentRegistrationDto paymentRegistrationDto) {
        List<PaymentRegistrationDto> list = paymentRegistrationMapper.selectPaymentRegistrationList(paymentRegistrationDto);
        for (PaymentRegistrationDto registrationDto : list) {
            List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new QueryWrapper<PaymentRegistration>()
                    .eq("ticket_registration_id", registrationDto.getTicketRegistrationId()));
            BigDecimal total = paymentRegistrations.stream().map(PaymentRegistration::getCurrentPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            registrationDto.setUnPaymentAmount(registrationDto.getInvoiceAmount().subtract(total));
        }
        return list;
    }
 
    /**
     * 新增付款登记
     *
     * @param paymentRegistration 付款登记
     * @return 结果
     */
    @Override
    public int insertPaymentRegistration(PaymentRegistration paymentRegistration) {
        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(paymentRegistration.getPurchaseLedgerId());
        SalesLedger salesLedger = salesLedgerMapper.selectOne(new QueryWrapper<SalesLedger>().
                eq("sales_contract_no", purchaseLedger.getSalesContractNo()));
        if (salesLedger == null) {
            throw new RuntimeException("关联销售合同号不存在");
        }
 
        paymentRegistration.setSaleLedgerId(salesLedger.getId());
        paymentRegistration.setSupplierId(purchaseLedger.getSupplierId());
 
        TicketRegistration tr = ticketRegistrationMapper.selectOne(new LambdaQueryWrapper<TicketRegistration>().eq(TicketRegistration::getId, paymentRegistration.getTicketRegistrationId()));
 
        if (tr == null) {
            throw new RuntimeException("关联发票不存在");
        }
 
        List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new QueryWrapper<PaymentRegistration>()
                .eq("ticket_registration_id", tr.getId()));
        BigDecimal total = paymentRegistrations.stream().map(PaymentRegistration::getCurrentPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
 
        if (total.add(paymentRegistration.getCurrentPaymentAmount()).compareTo(tr.getInvoiceAmount()) > 0) {
            throw new RuntimeException("付款金额超出发票金额");
        }
 
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Integer tenantId = loginUser.getTenantId();
        paymentRegistration.setTenantId(tenantId.longValue());
        paymentRegistration.setRegistrantId(loginUser.getUserId());
        paymentRegistration.setCreateTime(DateUtils.getNowDate());
        paymentRegistration.setUpdateTime(DateUtils.getNowDate());
        return paymentRegistrationMapper.insert(paymentRegistration);
    }
 
    /**
     * 修改付款登记
     *
     * @param paymentRegistration 付款登记
     * @return 结果
     */
    @Override
    public int updatePaymentRegistration(PaymentRegistration paymentRegistration) {
        TicketRegistration ticketRegistration = ticketRegistrationMapper.selectById(paymentRegistration.getTicketRegistrationId());
 
        List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new QueryWrapper<PaymentRegistration>()
                .eq("ticket_registration_id", paymentRegistration.getTicketRegistrationId()).ne("id", paymentRegistration.getId()));
        BigDecimal total = paymentRegistrations.stream().map(PaymentRegistration::getCurrentPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
 
        if (total.add(paymentRegistration.getCurrentPaymentAmount()).compareTo(ticketRegistration.getInvoiceAmount()) > 0) {
            throw new RuntimeException("付款金额超出发票金额");
        }
 
        paymentRegistration.setUpdateTime(DateUtils.getNowDate());
        return paymentRegistrationMapper.updateById(paymentRegistration);
    }
 
    /**
     * 批量删除付款登记
     *
     * @param ids 需要删除的付款登记主键
     * @return 结果
     */
    @Override
    public int deletePaymentRegistrationByIds(Long[] ids) {
        return paymentRegistrationMapper.delete(new QueryWrapper<PaymentRegistration>().in("id", ids));
    }
 
    @Override
    public PaymentRegistration selectPaymentRegistrationByPurchaseId(Long id) {
        PaymentRegistrationDto paymentRegistrationDto = new PaymentRegistrationDto();
        PurchaseLedger purchaseLedger = purchaseLedgerMapper.selectById(id);
        paymentRegistrationDto.setSalesContractNo(purchaseLedger.getSalesContractNo());
        paymentRegistrationDto.setSupplierName(purchaseLedger.getSupplierName());
        paymentRegistrationDto.setSupplierId(purchaseLedger.getSupplierId());
 
        List<TicketRegistration> ticketRegistrations = ticketRegistrationMapper.selectList(new QueryWrapper<TicketRegistration>()
                .eq("purchase_contract_number", purchaseLedger.getPurchaseContractNumber()));
        if (ticketRegistrations != null && ticketRegistrations.size() > 0) {
            paymentRegistrationDto.setInvoiceNumber(ticketRegistrations.get(0).getInvoiceNumber());
            paymentRegistrationDto.setInvoiceAmount(ticketRegistrations.get(0).getInvoiceAmount());
        }
        return paymentRegistrationDto;
    }
 
    @Override
    public List<Map<String, Object>> selectPaymentLedgerList(PaymentLedgerDto paymentLedgerDto) {
        List<Map<String, Object>> result = new ArrayList<>();
        LambdaQueryWrapper<SupplierManage> queryWrapper = new LambdaQueryWrapper<>();
        Optional.ofNullable(paymentLedgerDto)
                .ifPresent(dto -> {
                    if (StringUtils.hasText(dto.getSupplierName())) {
                        queryWrapper.like(SupplierManage::getSupplierName, dto.getSupplierName());
                    }
                });
 
        List<SupplierManage> supplierManages = supplierManageMapper.selectList(queryWrapper);
 
        for (SupplierManage supplierManage : supplierManages) {
            Map<String, Object> res = new HashMap<>();
            res.put("supplierName", supplierManage.getSupplierName());
 
            // 应付金额
            BigDecimal payableAmount = BigDecimal.ZERO;
            List<PurchaseLedger> purchaseLedgers = purchaseLedgerMapper.selectList(new QueryWrapper<PurchaseLedger>().eq("supplier_id", supplierManage.getId()));
            if (purchaseLedgers != null && purchaseLedgers.size() > 0) {
                List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(new QueryWrapper<SalesLedgerProduct>()
                        .in("sales_ledger_id", purchaseLedgers.stream().map(PurchaseLedger::getId).collect(Collectors.toList())));
                // 应付金额
                payableAmount = salesLedgerProducts.stream().map(SalesLedgerProduct::getTaxInclusiveTotalPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
 
            }
 
            BigDecimal invoiceAmount = BigDecimal.ZERO;
            List<TicketRegistration> ticketRegistrations = Collections.emptyList();
 
            // 增加空值检查,避免NullPointerException
            if (CollectionUtils.isNotEmpty(purchaseLedgers)) {
                Long[] ids = purchaseLedgers.stream()
                        .map(PurchaseLedger::getId)
                        .toArray(Long[]::new);
 
                // 检查数组是否有元素
                if (ids.length > 0) {
                    ticketRegistrations = ticketRegistrationMapper.selectList(
                            new LambdaQueryWrapper<TicketRegistration>()
                                    .in(TicketRegistration::getPurchaseLedgerId, ids)
                    );
                }
            }
            if (ticketRegistrations != null && ticketRegistrations.size() > 0) {
                // 来票金额
                invoiceAmount = ticketRegistrations.stream().map(TicketRegistration::getInvoiceAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            }
            // 付款金额
            List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new QueryWrapper<PaymentRegistration>()
                    .eq("supplier_id", supplierManage.getId()));
 
            BigDecimal paymentAmount = BigDecimal.ZERO;
            if (paymentRegistrations != null && paymentRegistrations.size() > 0) {
                paymentAmount = paymentRegistrations.stream().map(PaymentRegistration::getCurrentPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
            }
 
            res.put("invoiceAmount", invoiceAmount);
            res.put("payableAmount", payableAmount);
            res.put("paymentAmount", paymentAmount);
 
            // 详情
            List<Map<String, Object>> details = new ArrayList<>();
            for (PaymentRegistration paymentRegistration : paymentRegistrations) {
                Map<String, Object> detail = new HashMap<>();
                detail.put("paymentAmount", paymentRegistration.getCurrentPaymentAmount()); // 付款金额
                TicketRegistration ticketRegistration = ticketRegistrationMapper.selectById(paymentRegistration.getTicketRegistrationId());
                detail.put("payableAmount", ticketRegistration.getInvoiceAmount()); // 应付金额
                BigDecimal voteCount = productRecordMapper.selectList(
                                new LambdaQueryWrapper<ProductRecord>()
                                        .eq(ProductRecord::getTicketRegistrationId, ticketRegistration.getId()))
                        .stream()
                        .map(ProductRecord::getTicketsNum)
                        .map(BigDecimal::new)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                detail.put("voteCount", voteCount); // 票数
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String formattedDate = sdf.format(paymentRegistration.getPaymentDate());
                detail.put("paymentDate", formattedDate); // 发生时间
                details.add(detail);
            }
            res.put("details", details);
            result.add(res);
        }
        return result;
    }
}