maven
2 天以前 366b67926809852c1090f14c5a24fe00109a81b5
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package com.ruoyi.home.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.approve.mapper.ApproveProcessMapper;
import com.ruoyi.approve.pojo.ApproveProcess;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.dto.MapDto;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.home.dto.*;
import com.ruoyi.home.service.HomeService;
import com.ruoyi.procurementrecord.mapper.ProcurementRecordMapper;
import com.ruoyi.procurementrecord.mapper.ProcurementRecordOutMapper;
import com.ruoyi.procurementrecord.pojo.ProcurementRecordOut;
import com.ruoyi.procurementrecord.pojo.ProcurementRecordStorage;
import com.ruoyi.project.system.domain.SysDept;
import com.ruoyi.project.system.mapper.SysDeptMapper;
import com.ruoyi.purchase.mapper.PaymentRegistrationMapper;
import com.ruoyi.purchase.mapper.PurchaseLedgerMapper;
import com.ruoyi.purchase.pojo.PaymentRegistration;
import com.ruoyi.purchase.pojo.PurchaseLedger;
import com.ruoyi.quality.mapper.QualityInspectMapper;
import com.ruoyi.quality.pojo.QualityInspect;
import com.ruoyi.sales.mapper.ReceiptPaymentMapper;
import com.ruoyi.sales.mapper.SalesLedgerMapper;
import com.ruoyi.sales.mapper.SalesLedgerProductMapper;
import com.ruoyi.sales.pojo.ReceiptPayment;
import com.ruoyi.sales.pojo.SalesLedger;
import com.ruoyi.sales.pojo.SalesLedgerProduct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
 
/**
 * @author :yys
 * @date : 2025/7/25 9:23
 */
@Service
@Slf4j
public class HomeServiceImpl implements HomeService {
 
    @Autowired
    private SalesLedgerMapper salesLedgerMapper;
 
    @Autowired
    private PurchaseLedgerMapper purchaseLedgerMapper;
 
    @Autowired
    private SalesLedgerProductMapper salesLedgerProductMapper;
 
    @Autowired
    private ProcurementRecordOutMapper procurementRecordOutMapper;
 
    @Autowired
    private ProcurementRecordMapper procurementRecordStorageMapper;
 
    @Autowired
    private QualityInspectMapper qualityStatisticsMapper;
 
    @Autowired
    private ApproveProcessMapper approveProcessMapper;
 
    @Autowired
    private ReceiptPaymentMapper receiptPaymentMapper;
 
    @Autowired
    private PaymentRegistrationMapper paymentRegistrationMapper;
 
    @Autowired
    private SysDeptMapper sysDeptMapper;
 
    @Override
    public HomeBusinessDto business() {
        LocalDate now = LocalDate.now();
        YearMonth currentMonth = YearMonth.from(now);
        // 创建LambdaQueryWrapper
        LambdaQueryWrapper<SalesLedger> salesLedgerLambdaQueryWrapper = new LambdaQueryWrapper<>();
        salesLedgerLambdaQueryWrapper.ge(SalesLedger::getEntryDate, currentMonth.atDay(1).atStartOfDay())  // 大于等于本月第一天
                .lt(SalesLedger::getEntryDate, currentMonth.plusMonths(1).atDay(1).atStartOfDay()); // 小于下月第一天
        List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(salesLedgerLambdaQueryWrapper);
        // 合计合同金额
        BigDecimal contractAmount = salesLedgers.stream().map(SalesLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductMapperLambdaQueryWrapper = new LambdaQueryWrapper<SalesLedgerProduct>();
        salesLedgerProductMapperLambdaQueryWrapper.eq(SalesLedgerProduct::getType, 1)
                .in(SalesLedgerProduct::getSalesLedgerId, salesLedgers.stream().map(SalesLedger::getId).collect(Collectors.toList()));
        List<SalesLedgerProduct> salesLedgerProducts = salesLedgerProductMapper.selectList(salesLedgerProductMapperLambdaQueryWrapper);
        // 未开票金额
        BigDecimal noInvoiceAmountTotal = salesLedgerProducts.stream().map(SalesLedgerProduct::getNoInvoiceAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
 
        // 创建LambdaQueryWrapper
        LambdaQueryWrapper<PurchaseLedger> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(PurchaseLedger::getEntryDate, currentMonth.atDay(1).atStartOfDay())  // 大于等于本月第一天
                .lt(PurchaseLedger::getEntryDate, currentMonth.plusMonths(1).atDay(1).atStartOfDay()); // 小于下月第一天
 
        // 执行查询并计算总和
        List<PurchaseLedger> purchaseLedgers = purchaseLedgerMapper.selectList(queryWrapper);
 
        LambdaQueryWrapper<SalesLedgerProduct> salesLedgerProductMapperLambdaQueryWrapperCopy = new LambdaQueryWrapper<SalesLedgerProduct>();
        salesLedgerProductMapperLambdaQueryWrapper.eq(SalesLedgerProduct::getType, 2)
                .in(SalesLedgerProduct::getSalesLedgerId, purchaseLedgers.stream().map(PurchaseLedger::getId).collect(Collectors.toList()));
        List<SalesLedgerProduct> salesLedgerProductsCopy = salesLedgerProductMapper.selectList(salesLedgerProductMapperLambdaQueryWrapperCopy);
        // 合计合同金额
        BigDecimal receiveAmount = purchaseLedgers.stream()
                .map(PurchaseLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // 未开票金额
        BigDecimal unReceiptPaymentAmount = salesLedgerProductsCopy.stream()
                .map(SalesLedgerProduct::getNoInvoiceAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
 
        // 统计库存
        List<ProcurementRecordStorage> procurementRecordStorages = procurementRecordStorageMapper.selectList(null);
        BigDecimal stockAmount = procurementRecordStorages.stream()
                .map(ProcurementRecordStorage::getInboundNum)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        List<ProcurementRecordOut> procurementRecordOuts = procurementRecordOutMapper.selectList(null);
        BigDecimal outboundAmount = procurementRecordOuts.stream()
                .map(ProcurementRecordOut::getInboundNum)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal stock = stockAmount.subtract(outboundAmount);
        // 获取当天入库数量
        LambdaQueryWrapper<ProcurementRecordStorage> procurementRecordStorageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        procurementRecordStorageLambdaQueryWrapper.ge(ProcurementRecordStorage::getCreateTime, now)  // 大于等于当天
                .lt(ProcurementRecordStorage::getCreateTime, now.plusDays(1)); // 小于明天
        List<ProcurementRecordStorage> procurementRecordStorages1 = procurementRecordStorageMapper.selectList(procurementRecordStorageLambdaQueryWrapper);
        BigDecimal stockAmount1 = procurementRecordStorages1.stream()
                .map(ProcurementRecordStorage::getInboundNum)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // 构建结果
        HomeBusinessDto homeBusinessDto = new HomeBusinessDto();
        homeBusinessDto.setMonthSaleMoney(contractAmount);
        homeBusinessDto.setMonthSaleHaveMoney(noInvoiceAmountTotal);
        homeBusinessDto.setMonthPurchaseMoney(receiveAmount);
        homeBusinessDto.setMonthPurchaseHaveMoney(unReceiptPaymentAmount);
        homeBusinessDto.setInventoryNum(stock);
        homeBusinessDto.setTodayInventoryNum(stockAmount1);
 
        return homeBusinessDto;
    }
 
    @Override
    public AnalysisCustomerContractAmountsDto analysisCustomerContractAmounts() {
        List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(null);
        // 合计合同金额
        BigDecimal contractAmount = salesLedgers.stream().map(SalesLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // 计算周同比
        // 获取当前时间
        LocalDate today = LocalDate.now();
 
        // 获取本周周一
        LocalDate startOfWeek = today.with(DayOfWeek.MONDAY);
 
        // 获取本周周日
        LocalDate endOfWeek = today.with(DayOfWeek.SUNDAY);
        List<SalesLedger> salesLedgers1 = salesLedgerMapper.selectList(new LambdaQueryWrapper<SalesLedger>()
                .ge(SalesLedger::getEntryDate, startOfWeek)  // 大于等于本周周一
                .gt(SalesLedger::getEntryDate, endOfWeek));
        BigDecimal weekContractAmount = salesLedgers1.stream().map(SalesLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        // 获取去年本周时间
        LocalDate lastYearStartOfWeek = today.minusYears(1).with(DayOfWeek.MONDAY);
        LocalDate lastYearEndOfWeek = today.minusYears(1).with(DayOfWeek.SUNDAY);
        List<SalesLedger> salesLedgers2 = salesLedgerMapper.selectList(new LambdaQueryWrapper<SalesLedger>()
                .ge(SalesLedger::getEntryDate, lastYearStartOfWeek)  // 大于等于去年本周周一
                .gt(SalesLedger::getEntryDate, lastYearEndOfWeek));
        BigDecimal lastYearWeekContractAmount = salesLedgers2.stream().map(SalesLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal subtract = weekContractAmount.subtract(lastYearWeekContractAmount);
        String weekYny = "";
        // 周同比
        if(subtract.compareTo(BigDecimal.ZERO) == 0 || lastYearWeekContractAmount.compareTo(BigDecimal.ZERO) == 0){
            weekYny = "0.00";
        }else{
            weekYny = String.format("%.2f", subtract.divide(lastYearWeekContractAmount, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")));
 
        }
 
        // 计算日环比
        LocalDate yesterday = today.minusDays(1);
        LocalDate plusDays = today.plusDays(1);
        List<SalesLedger> salesLedgers3 = salesLedgerMapper.selectList(new LambdaQueryWrapper<SalesLedger>()
                .ge(SalesLedger::getEntryDate, today)  // 大于等于昨天
                .lt(SalesLedger::getEntryDate, plusDays));
        BigDecimal yesterdayContractAmount = salesLedgers3.stream().map(SalesLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        List<SalesLedger> salesLedgers4 = salesLedgerMapper.selectList(new LambdaQueryWrapper<SalesLedger>()
                .ge(SalesLedger::getEntryDate, yesterday)  // 大于等于去年昨天
                .lt(SalesLedger::getEntryDate, today));
        BigDecimal lastYearYesterdayContractAmount = salesLedgers4.stream().map(SalesLedger::getContractAmount)
                .filter(Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal subtract1 = yesterdayContractAmount.subtract(lastYearYesterdayContractAmount);
        // 日环比
        String chain = "";
        if(subtract1.compareTo(BigDecimal.ZERO) == 0 || lastYearYesterdayContractAmount.compareTo(BigDecimal.ZERO) == 0){
            chain = "0.00";
        }else{
            chain = String.format("%.2f", subtract1.divide(lastYearYesterdayContractAmount, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100")));
        }
 
 
        AnalysisCustomerContractAmountsDto analysisCustomerContractAmountsDto = new AnalysisCustomerContractAmountsDto();
        analysisCustomerContractAmountsDto.setSum(contractAmount);
        analysisCustomerContractAmountsDto.setYny(weekYny);
        analysisCustomerContractAmountsDto.setChain(chain);
 
        Map<String, BigDecimal> collect = salesLedgers.stream().collect(Collectors.groupingBy(SalesLedger::getCustomerName, Collectors.reducing(BigDecimal.ZERO,
                SalesLedger::getContractAmount, BigDecimal::add)));
        List<MapDto> mapDtos = new ArrayList<>();
        collect.forEach((k,v)->{
            MapDto mapDto = new MapDto();
            mapDto.setName(k);
            mapDto.setValue(v);
            if(contractAmount.compareTo(new BigDecimal(0)) == 0){
                mapDto.setRate("0");
            }else{
                mapDto.setRate(String.format("%.2f", v.divide(contractAmount, 2, RoundingMode.HALF_UP).multiply(new BigDecimal("100"))));
            }
            mapDtos.add(mapDto);
        });
        analysisCustomerContractAmountsDto.setItem(mapDtos);
        return analysisCustomerContractAmountsDto;
    }
 
    @Override
    public QualityStatisticsDto qualityStatistics() {
        // 获取一周数据
        // 获取当前时间
        LocalDate today = LocalDate.now();
        // 获取本周周一
        LocalDate startOfWeek = today.with(DayOfWeek.MONDAY);
        // 获取本周周日
        LocalDate endOfWeek = today.with(DayOfWeek.SUNDAY);
        LambdaQueryWrapper<QualityInspect> qualityInspectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        qualityInspectLambdaQueryWrapper.ge(QualityInspect::getCheckTime, startOfWeek)
                .gt(QualityInspect::getCheckTime, endOfWeek);
        List<QualityInspect> qualityInspects = qualityStatisticsMapper.selectList(qualityInspectLambdaQueryWrapper);
        QualityStatisticsDto qualityStatisticsDto = new QualityStatisticsDto();
        qualityStatisticsDto.setSupplierNum(qualityInspects.stream().filter(item -> item.getInspectType().equals(0)).map(QualityInspect::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add));
        qualityStatisticsDto.setProcessNum(qualityInspects.stream().filter(item -> item.getInspectType().equals(1)).map(QualityInspect::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add));
        qualityStatisticsDto.setFactoryNum(qualityInspects.stream().filter(item -> item.getInspectType().equals(2)).map(QualityInspect::getQuantity).reduce(BigDecimal.ZERO, BigDecimal::add));
        List<QualityStatisticsItem> qualityStatisticsItems = new ArrayList<>();
        for (int j = 1; j < 8; j++) {
            LocalDate endTime = startOfWeek.plusDays(j);
            LocalDate startTime = endTime.minusDays(1);
            QualityStatisticsItem qualityStatisticsItem = new QualityStatisticsItem();
            qualityStatisticsItem.setDate(startTime.toString());
            qualityStatisticsItem.setSupplierNum(qualityInspects.stream()
                    .filter(item -> item.getInspectType().equals(0) && "不合格".equals(item.getCheckResult())
                            && (startTime.isEqual(item.getCheckTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()) || startTime.isAfter(item.getCheckTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()))
                            && endTime.isBefore(item.getCheckTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()))
                    .map(QualityInspect::getQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add)
            );
            qualityStatisticsItem.setFactoryNum(qualityInspects.stream()
                    .filter(item -> item.getInspectType().equals(1) && "不合格".equals(item.getCheckResult())
                            && (startTime.isEqual(item.getCheckTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()) || startTime.isAfter(item.getCheckTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()))
                            && endTime.isBefore(item.getCheckTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()))
                    .map(QualityInspect::getQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add)
            );
            qualityStatisticsItem.setProcessNum(qualityInspects.stream()
                    .filter(item -> item.getInspectType().equals(2) && "不合格".equals(item.getCheckResult())
                            && (startTime.isEqual(item.getCheckTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()) || startTime.isAfter(item.getCheckTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()))
                            && endTime.isBefore(item.getCheckTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate()))
                    .map(QualityInspect::getQuantity)
                    .reduce(BigDecimal.ZERO, BigDecimal::add));
            qualityStatisticsItems.add(qualityStatisticsItem);
        }
        qualityStatisticsDto.setItem(qualityStatisticsItems);
        return qualityStatisticsDto;
    }
 
    @Override
    public List<ApproveProcess> todos() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        LambdaQueryWrapper<ApproveProcess> approveProcessLambdaQueryWrapper = new LambdaQueryWrapper<>();
        approveProcessLambdaQueryWrapper.eq(ApproveProcess::getApproveDelete, 0)
                .eq(ApproveProcess::getApproveUserCurrentId, loginUser.getUserId())
                .ne(ApproveProcess::getApproveStatus, 2)
                .eq(ApproveProcess::getTenantId, loginUser.getTenantId());
        return approveProcessMapper.selectList(approveProcessLambdaQueryWrapper);
    }
 
    /**
     *
     * @param type 1-周 2-月 3-季度
     * @return
     */
    @Override
    public StatisticsReceivablePayableDto statisticsReceivablePayable(Integer type) {
        LocalDate today = LocalDate.now();
        LocalDate startDate = null;
        LocalDate endDate = null;
        switch (type){
            case 1:
                // 获取本周周一
                startDate = today.with(DayOfWeek.MONDAY);
                // 获取本周周日
                endDate = today.with(DayOfWeek.SUNDAY);
                break;
            case 2:
                startDate = today.with(TemporalAdjusters.firstDayOfMonth());
                endDate = today.with(TemporalAdjusters.lastDayOfMonth());
                break;
            case 3:
                Month currentMonth = today.getMonth();
                Month firstMonthOfQuarter = currentMonth.firstMonthOfQuarter();
                Month lastMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
 
                startDate = today.withMonth(firstMonthOfQuarter.getValue())
                        .with(TemporalAdjusters.firstDayOfMonth());
                endDate = today.withMonth(lastMonthOfQuarter.getValue())
                        .with(TemporalAdjusters.lastDayOfMonth());
                break;
        }
        // 应收
        List<SalesLedger> salesLedgers = salesLedgerMapper.selectList(new LambdaQueryWrapper<SalesLedger>()
                .ge(SalesLedger::getEntryDate, startDate)
                .lt(SalesLedger::getEntryDate, endDate)
        );
        BigDecimal receivableMoney = salesLedgers.stream().map(SalesLedger::getContractAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        // 应付
        List<PurchaseLedger> procurementRecords = purchaseLedgerMapper.selectList(new LambdaQueryWrapper<PurchaseLedger>()
                .ge(PurchaseLedger::getEntryDate, startDate)
                .lt(PurchaseLedger::getEntryDate, endDate)
        );
        BigDecimal payableMoney = procurementRecords.stream().map(PurchaseLedger::getContractAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        // 预收
        List<ReceiptPayment> receiptPayments = receiptPaymentMapper.selectList(new LambdaQueryWrapper<ReceiptPayment>()
                .ge(ReceiptPayment::getReceiptPaymentDate, startDate)
                .lt(ReceiptPayment::getReceiptPaymentDate, endDate));
        BigDecimal advanceMoney = receiptPayments.stream().map(ReceiptPayment::getReceiptPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        // 预付
        List<PaymentRegistration> paymentRegistrations = paymentRegistrationMapper.selectList(new LambdaQueryWrapper<PaymentRegistration>()
                .ge(PaymentRegistration::getPaymentDate, startDate)
                .lt(PaymentRegistration::getPaymentDate, endDate));
        BigDecimal prepayMoney = paymentRegistrations.stream().map(PaymentRegistration::getCurrentPaymentAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        StatisticsReceivablePayableDto statisticsReceivablePayableDto = new StatisticsReceivablePayableDto();
        statisticsReceivablePayableDto.setPayableMoney(payableMoney);
        statisticsReceivablePayableDto.setReceivableMoney(receivableMoney);
        statisticsReceivablePayableDto.setAdvanceMoney(advanceMoney);
        statisticsReceivablePayableDto.setPrepayMoney(prepayMoney);
 
        return statisticsReceivablePayableDto;
    }
}