OrderJob.java 9.6 KB
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 */1 * * * ?")
//    @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"));
    }


}