chenrui
6 天以前 a303e40b2d843700f2b9045ca105c81dea97d964
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
package com.ruoyi.sales.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.sales.dto.CustomerInteractionDto;
import com.ruoyi.sales.dto.InvoiceLedgerDto;
import com.ruoyi.sales.dto.ReceiptPaymentDto;
import com.ruoyi.sales.mapper.ReceiptPaymentMapper;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.pojo.ReceiptPayment;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.service.ReceiptPaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
 
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.*;
import java.util.stream.Collectors;
 
@Service
public class ReceiptPaymentServiceImpl extends ServiceImpl<ReceiptPaymentMapper,ReceiptPayment> implements ReceiptPaymentService {
 
    @Autowired
    private ReceiptPaymentMapper receiptPaymentMapper;
 
    @Autowired
    private SalesLedgerMapper salesLedgerMapper;
 
    /**
     * 回款登记新增
     * @param receiptPayment
     * @return
     */
    @Override
    public int receiptPaymentSaveOrUpdate(ReceiptPayment receiptPayment) {
        if(null==receiptPayment.getId()){
            return receiptPaymentMapper.insert(receiptPayment);
        }else {
            return receiptPaymentMapper.updateById(receiptPayment);
        }
    }
 
    /**
     * 回款登记修改
     * @param receiptPayment
     * @return
     */
    @Override
    public int receiptPaymentUpdate(ReceiptPayment receiptPayment) {
        return receiptPaymentMapper.updateById(receiptPayment);
    }
 
    /**
     * 回款登记删除
     * @param ids
     * @return
     */
    @Override
    public int receiptPaymentDel(List<Integer> ids) {
        LambdaQueryWrapper<ReceiptPayment> delQuery = new LambdaQueryWrapper<>();
        delQuery.in(ReceiptPayment::getId, ids);
        return receiptPaymentMapper.delete(delQuery);
    }
 
    /**
     * 回款登记分页查询
     * @param page
     * @param receiptPaymentDto
     * @return
     */
    @Override
    public IPage<ReceiptPaymentDto> receiptPaymentListPage(Page page, ReceiptPaymentDto receiptPaymentDto) {
        // 计算分页前page.current-1 * limit条数的综合计算已经收回的回款金额
        // 计算已经分页的条数
        long total = (page.getCurrent() - 1) * page.getSize();
        BigDecimal receiptAmount = receiptPaymentMapper.getReceiptAmount(receiptPaymentDto.getCustomerId(), total);
        if(ObjectUtils.isEmpty(receiptAmount)){
            receiptAmount = BigDecimal.ZERO;
        }
        IPage<ReceiptPaymentDto> iPage = receiptPaymentMapper.receiptPaymentListPage(page, receiptPaymentDto);
        // 开票总金额
        BigDecimal invoiceTotal = CollectionUtils.isEmpty(iPage.getRecords()) ? BigDecimal.ZERO : iPage.getRecords().get(0).getInvoiceTotal();
        // 当前应收金额
        BigDecimal currentUnReceiptAmount = invoiceTotal.subtract(receiptAmount);
        for (ReceiptPaymentDto record : iPage.getRecords()) {
            currentUnReceiptAmount = currentUnReceiptAmount.subtract(record.getReceiptPaymentAmount());
            record.setNoReceiptAmount(currentUnReceiptAmount);
        }
        return iPage;
    }
 
    /**
     * 回款登记详情
     * @param id
     * @return
     */
    @Override
    public ReceiptPaymentDto receiptPaymentInfo(Integer id) {
        return receiptPaymentMapper.receiptPaymentInfo(id);
    }
 
    @Override
    public BigDecimal getReceiptAmount() {
        LocalDate now = LocalDate.now();
        YearMonth currentMonth = YearMonth.from(now);
 
        // 创建LambdaQueryWrapper
        LambdaQueryWrapper<ReceiptPayment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(ReceiptPayment::getReceiptPaymentDate, currentMonth.atDay(1).atStartOfDay())  // 大于等于本月第一天
                .lt(ReceiptPayment::getReceiptPaymentDate, currentMonth.plusMonths(1).atDay(1).atStartOfDay()); // 小于下月第一天
 
        // 执行查询并计算总和
        List<ReceiptPayment> receiptPayments = receiptPaymentMapper.selectList(queryWrapper);
 
        BigDecimal totalContractAmount = receiptPayments.stream()
                .map(ReceiptPayment::getReceiptPaymentAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
 
        return totalContractAmount;
    }
 
    /**
     * 查询已经绑定发票的开票台账
     * @param page
     * @param receiptPaymentDto
     * @return
     */
    @Override
    public IPage<ReceiptPaymentDto> bindInvoiceNoRegPage(Page page, ReceiptPaymentDto receiptPaymentDto) {
        return receiptPaymentMapper.bindInvoiceNoRegPage(page, receiptPaymentDto);
    }
 
    /**
     * 开票台账详情
     * @param id
     * @return
     */
    @Override
    public InvoiceLedgerDto invoiceInfo(Integer id) {
        return receiptPaymentMapper.invoiceInfo(id);
    }
 
    @Override
    public Map<String,BigDecimal> getAmountMouth() {
        List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(null);
        BigDecimal contractAmount = salesLedgers.stream().map(SalesLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
 
        LocalDate now = LocalDate.now();
        YearMonth currentMonth = YearMonth.from(now);
 
        // 创建LambdaQueryWrapper
        LambdaQueryWrapper<ReceiptPayment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(ReceiptPayment::getReceiptPaymentDate, currentMonth.atDay(1).atStartOfDay())  // 大于等于本月第一天
                .lt(ReceiptPayment::getReceiptPaymentDate, currentMonth.plusMonths(1).atDay(1).atStartOfDay()); // 小于下月第一天
 
        // 执行查询并计算总和
        List<ReceiptPayment> receiptPayments = receiptPaymentMapper.selectList(queryWrapper);
 
        BigDecimal receiveAmount = receiptPayments.stream()
                .map(ReceiptPayment::getReceiptPaymentAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
 
        // 构建结果
        Map<String, BigDecimal> result = new HashMap<>();
        result.put("receiveAmount", receiveAmount);
        result.put("contractAmount", contractAmount);
        return result;
    }
 
    /**
     * 查询回款记录
     */
    @Override
    public List<ReceiptPaymentDto> receiptPaymentHistoryList(ReceiptPaymentDto receiptPaymentDto) {
        return receiptPaymentMapper.receiptPaymentHistoryList(receiptPaymentDto);
    }
 
    /**
     * 查询回款记录分页
     */
    @Override
    public IPage<ReceiptPaymentDto> receiptPaymentHistoryListPage(Page page, ReceiptPaymentDto receiptPaymentDto) {
        return receiptPaymentMapper.receiptPaymentHistoryListPage(page, receiptPaymentDto);
    }
 
    /**
     * 客户往来记录查询
     * @param receiptPaymentDto
     * @return
     */
    @Override
    public List<CustomerInteractionDto> customerInteractions(ReceiptPaymentDto receiptPaymentDto) {
        ArrayList<CustomerInteractionDto> result = new ArrayList<>();
        List<CustomerInteractionDto> customerInteractionDtos = receiptPaymentMapper.customerInteractions(receiptPaymentDto);
        if(CollectionUtils.isEmpty(customerInteractionDtos)){
            return result;
        }
        // 应收总金额金额计算
        BigDecimal amountTotal = BigDecimal.ZERO;
        Map<LocalDate, List<CustomerInteractionDto>> dateListMap = customerInteractionDtos.stream().collect(
                Collectors.groupingBy(
                        CustomerInteractionDto::getHappenTime,
                        LinkedHashMap::new,
                        Collectors.toList()
                )
        );
        for (LocalDate localDate : dateListMap.keySet()) {
            BigDecimal currentReceiptAmount = BigDecimal.ZERO;
            BigDecimal invoiceAmount = BigDecimal.ZERO;
            BigDecimal currentDateTotal = BigDecimal.ZERO;
            List<CustomerInteractionDto> customerInteractionDtoList = dateListMap.get(localDate);
            // 计算当天收款数
            currentReceiptAmount = customerInteractionDtoList.stream()
                    .filter(item ->item.getType() == 0)
                    .map(CustomerInteractionDto::getReceiptAmount)
                    .reduce(BigDecimal.ZERO,BigDecimal::add);
            // 计算当天开票数
            invoiceAmount = customerInteractionDtoList.stream()
                    .filter(item ->item.getType() == 1)
                    .map(CustomerInteractionDto::getInvoiceAmount)
                    .reduce(BigDecimal.ZERO,BigDecimal::add);
            // 计算当日汇总
            currentDateTotal = currentDateTotal.add(invoiceAmount).subtract(currentReceiptAmount);
            CustomerInteractionDto customerInteractionDto = new CustomerInteractionDto();
            customerInteractionDto.setHappenTime(localDate);
            customerInteractionDto.setInvoiceAmount(invoiceAmount);
            customerInteractionDto.setReceiptAmount(currentReceiptAmount);
            amountTotal = amountTotal.add(currentDateTotal);
            customerInteractionDto.setUnReceiptAmount(amountTotal);
            result.add(customerInteractionDto);
        }
        return result;
    }
 
    /**
     * 查询回款记录分页
     */
    @Override
    public List<ReceiptPaymentDto> receiptPaymentHistoryListNoPage(ReceiptPaymentDto receiptPaymentDto) {
        return receiptPaymentMapper.receiptPaymentHistoryListNoPage( receiptPaymentDto);
    }
}