OrderJob.java
9.6 KB
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
package com.order.erp.job;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.order.erp.common.constant.Constant;
import com.order.erp.common.utils.DateUtils;
import com.order.erp.common.utils.ProfitUtils;
import com.order.erp.common.utils.TransactionHelper;
import com.order.erp.domain.OrderStatusEnum;
import com.order.erp.domain.dto.BaseDO;
import com.order.erp.domain.dto.order.*;
import com.order.erp.domain.vo.order.OrderInfoResultVO;
import com.order.erp.domain.vo.order.ProfitCalculateVO;
import com.order.erp.service.order.*;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author: xms
* @description: TODO
* @date: 2023/6/25 10:35
* @version: 1.0
*/
@Slf4j
@Component
public class OrderJob {
@Resource
private OrderBaseInfoService orderBaseInfoService;
@Resource
private OrderProfitAnalysisService profitAnalysisService;
@Resource
private OrderCompletionReportService reportService;
@Resource
private OrderTrackStageService trackStageService;
@Resource
private OrderInspectionStageService inspectionStageService;
@Resource
private TransactionHelper transactionHelper;
/**
* 每隔5分执行一次
*/
@Scheduled(cron = "0 */5 * * * ?")
// @Scheduled(cron = "*/5 * * * * ?")
public void checkCompleteExecute() {
log.info("执行开始时间:{}", DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
LambdaQueryWrapper<OrderBaseInfoDO> queryWrapper = new LambdaQueryWrapper<OrderBaseInfoDO>()
.eq(BaseDO::getEnableFlag, Constant.ENABLE_TEN)
.ge(OrderBaseInfoDO::getCreateTime, DateUtils.format(DateTime.now().minusMonths(Constant.ONE).toDate(), DateUtils.DATE_TIME))
.le(OrderBaseInfoDO::getCreateTime, DateUtils.format(DateTime.now().toDate(), DateUtils.DATE_TIME))
.ne(OrderBaseInfoDO::getOrderStatus, OrderStatusEnum.ORDER_FINISH.getStatus());
List<OrderBaseInfoDO> ordersDOS = orderBaseInfoService.list(queryWrapper);
List<OrderInfoResultVO> orderInfoResultVOS = orderBaseInfoService.wrapperOrderResultList(false, ordersDOS);
if (CollectionUtils.isNotEmpty(orderInfoResultVOS)) {
Set<Long> orderIds = orderInfoResultVOS.stream().filter(x -> Objects.nonNull(x.getSchedule()) && Constant.ONE == x.getSchedule()).map(OrderInfoResultVO::getId).collect(Collectors.toSet());
if (CollectionUtils.isNotEmpty(orderIds)) {
LambdaUpdateWrapper<OrderBaseInfoDO> orderBaseUpdateWrapper = new LambdaUpdateWrapper<OrderBaseInfoDO>()
.in(OrderBaseInfoDO::getId, orderIds).set(OrderBaseInfoDO::getOrderStatus, OrderStatusEnum.ORDER_FINISH.getStatus());
LambdaUpdateWrapper<OrderCompletionReportDO> reportUpdateWrapper = new LambdaUpdateWrapper<OrderCompletionReportDO>()
.in(OrderCompletionReportDO::getOrderId, orderIds).set(OrderCompletionReportDO::getOrderStatus, OrderStatusEnum.ORDER_FINISH.getStatus());
LambdaUpdateWrapper<OrderProfitAnalysisDO> profitUpdateWrapper = new LambdaUpdateWrapper<OrderProfitAnalysisDO>()
.in(OrderProfitAnalysisDO::getOrderId, orderIds).set(OrderProfitAnalysisDO::getOrderStatus, OrderStatusEnum.ORDER_FINISH.getStatus());
LambdaUpdateWrapper<OrderTrackStageDO> trackUpdateWrapper = new LambdaUpdateWrapper<OrderTrackStageDO>()
.in(OrderTrackStageDO::getOrderId, orderIds).set(OrderTrackStageDO::getOrderStatus, OrderStatusEnum.ORDER_FINISH.getStatus());
LambdaUpdateWrapper<OrderInspectionStageDO> inspectUpdateWrapper = new LambdaUpdateWrapper<OrderInspectionStageDO>()
.in(OrderInspectionStageDO::getOrderId, orderIds).set(OrderInspectionStageDO::getOrderStatus, OrderStatusEnum.ORDER_FINISH.getStatus());
transactionHelper.run(() -> {
orderBaseInfoService.update(orderBaseUpdateWrapper);
reportService.update(reportUpdateWrapper);
profitAnalysisService.update(profitUpdateWrapper);
trackStageService.update(trackUpdateWrapper);
inspectionStageService.update(inspectUpdateWrapper);
});
}
}
log.info("执行结束时间:{}", DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
}
/**
* 每隔5分执行一次
*/
@Scheduled(cron = "0 */5 * * * ?")
// @Scheduled(cron = "*/5 * * * * ?")
public void checkChargeOrderCount() {
log.info("执行开始时间:{}", DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
LambdaQueryWrapper<OrderBaseInfoDO> queryWrapper = new LambdaQueryWrapper<OrderBaseInfoDO>()
.eq(BaseDO::getEnableFlag, Constant.ENABLE_TEN)
.ge(OrderBaseInfoDO::getCreateTime, DateUtils.format(DateTime.now().minusMonths(Constant.TWO).toDate(), DateUtils.DATE_TIME))
.le(OrderBaseInfoDO::getCreateTime, DateUtils.format(DateTime.now().toDate(), DateUtils.DATE_TIME))
.ne(OrderBaseInfoDO::getOrderStatus, OrderStatusEnum.ORDER_FINISH.getStatus());
List<OrderBaseInfoDO> ordersDOS = orderBaseInfoService.list(queryWrapper);
List<OrderInfoResultVO> orderInfoResultVOS = orderBaseInfoService.wrapperOrderResultList(false, ordersDOS);
if (CollectionUtils.isNotEmpty(orderInfoResultVOS)) {
Set<Long> orderIds = orderInfoResultVOS.stream().map(OrderInfoResultVO::getId).collect(Collectors.toSet());
if (CollectionUtils.isNotEmpty(orderIds)) {
List<OrderProfitAnalysisDO> profitAnalysisDOS = profitAnalysisService.list(new LambdaQueryWrapper<OrderProfitAnalysisDO>()
.eq(BaseDO::getEnableFlag, Constant.ENABLE_TEN)
.in(OrderProfitAnalysisDO::getOrderId, orderIds));
Map<Long, OrderProfitAnalysisDO> profitAnalysisDOMap = new HashMap<>();
if (CollectionUtils.isNotEmpty(profitAnalysisDOS)) {
profitAnalysisDOMap = profitAnalysisDOS.stream().collect(Collectors.toMap(OrderProfitAnalysisDO::getOrderId, Function.identity(), (x, y) -> y));
}
for (OrderInfoResultVO resultVO : orderInfoResultVOS) {
if (profitAnalysisDOMap.containsKey(resultVO.getId())) {
OrderProfitAnalysisDO profitAnalysisDO = profitAnalysisDOMap.get(resultVO.getId());
Integer orderCount = resultVO.getOrderCount();
BigDecimal customerTotalPrice = new BigDecimal(profitAnalysisDO.getCustomerTotalPrice()).setScale(Constant.TWO, RoundingMode.HALF_UP);
BigDecimal customerPrice = new BigDecimal(profitAnalysisDO.getCustomerPrice()).setScale(Constant.TWO, RoundingMode.HALF_UP);
BigDecimal calculateTotalPrice = new BigDecimal(orderCount).multiply(customerPrice).setScale(Constant.TWO, RoundingMode.HALF_UP);
if (customerTotalPrice.compareTo(calculateTotalPrice) != Constant.ZERO) {
BigDecimal customerRmbPrice = new BigDecimal(profitAnalysisDO.getCustomerRmbPrice());
BigDecimal customerRmbTotalPrice = new BigDecimal(orderCount).multiply(customerRmbPrice).setScale(Constant.TWO, RoundingMode.HALF_UP);
BigDecimal productionDepartmentPrice = new BigDecimal(profitAnalysisDO.getProductionDepartmentPrice());
BigDecimal productionDepartmentTotalPrice = new BigDecimal(orderCount).multiply(productionDepartmentPrice).setScale(Constant.TWO, RoundingMode.HALF_UP);
BigDecimal packetPrice = new BigDecimal(profitAnalysisDO.getPacketPrice());
BigDecimal packetTotalPrice = new BigDecimal(orderCount).multiply(packetPrice).setScale(Constant.TWO, RoundingMode.HALF_UP);
Double profitRate = ProfitUtils.calculateProfitRate(ProfitCalculateVO.builder()
.exchangeRate(profitAnalysisDO.getExchangeRate())
.profitType(profitAnalysisDO.getProfitType())
.customerTotalPrice(calculateTotalPrice.doubleValue())
.productionDepartmentTotalPrice(productionDepartmentTotalPrice.doubleValue())
.packetTotalPrice(packetTotalPrice.doubleValue()).build());
profitAnalysisDO.setCustomerTotalPrice(calculateTotalPrice.doubleValue());
profitAnalysisDO.setCustomerRmbTotalPrice(customerRmbTotalPrice.doubleValue());
profitAnalysisDO.setProductionDepartmentTotalPrice(productionDepartmentTotalPrice.doubleValue());
profitAnalysisDO.setPacketTotalPrice(packetTotalPrice.doubleValue());
profitAnalysisDO.setProfitRate(profitRate);
profitAnalysisService.updateById(profitAnalysisDO);
}
}
}
}
}
log.info("执行结束时间:{}", DateTime.now().toString("yyyy-MM-dd HH:mm:ss"));
}
}