#
Junjie
2024-09-07 8eba3203511d8edbc5ee04aa82eceeaa3e38f889
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
package com.zy.asrs.wms.asrs.timer;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy;
import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wms.asrs.entity.*;
import com.zy.asrs.wms.asrs.entity.enums.OrderSettleType;
import com.zy.asrs.wms.asrs.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
 
import java.util.List;
 
@Component
public class OrderTimer {
 
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetlService orderDetlService;
    @Autowired
    private OrderDetlFieldService orderDetlFieldService;
    @Autowired
    private OrderLogService orderLogService;
    @Autowired
    private OrderDetlLogService orderDetlLogService;
    @Autowired
    private OrderDetlFieldLogService orderDetlFieldLogService;
 
    @Scheduled(cron = "0/3 * * * * ? ")
    @Transactional
    public void orderToHistory() {
        InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build());
        try {
            List<Order> list = orderService.list(new LambdaQueryWrapper<Order>().eq(Order::getOrderSettle, OrderSettleType.COMPLETE.val()));
            if (list.isEmpty()) {
                return;
            }
 
            for (Order order : list) {
                //转历史档
                OrderLog orderLog = new OrderLog();
                orderLog.sync(order);
                if (!orderLogService.save(orderLog)) {
                    throw new CoolException("订单转历史档失败");
                }
 
                //订单明细转历史档
                List<OrderDetl> orderDetls = orderDetlService.list(new LambdaQueryWrapper<OrderDetl>().eq(OrderDetl::getOrderId, order.getId()));
                for (OrderDetl orderDetl : orderDetls) {
                    OrderDetlLog orderDetlLog = new OrderDetlLog();
                    orderDetlLog.sync(orderDetl);
                    if(!orderDetlLogService.save(orderDetlLog)) {
                        throw new CoolException("订单明细转历史档失败");
                    }
 
                    //明细扩展字段转历史档
                    List<OrderDetlField> orderDetlFields = orderDetlFieldService.list(new LambdaQueryWrapper<OrderDetlField>().eq(OrderDetlField::getDetlId, orderDetl.getId()));
                    for (OrderDetlField orderDetlField : orderDetlFields) {
                        OrderDetlFieldLog orderDetlFieldLog = new OrderDetlFieldLog();
                        orderDetlFieldLog.sync(orderDetlField);
                        if(!orderDetlFieldLogService.save(orderDetlFieldLog)) {
                            throw new CoolException("明细扩展字段转历史档失败");
                        }
 
                        //删除明细扩展字段
                        if (!orderDetlFieldService.removeById(orderDetlField.getId())) {
                            throw new CoolException("删除明细扩展字段失败");
                        }
                    }
 
                    //删除订单明细
                    if (!orderDetlService.removeById(orderDetl.getId())) {
                        throw new CoolException("删除订单明细失败");
                    }
                }
 
                //删除订单
                if (!orderService.removeById(order.getId())) {
                    throw new CoolException("删除订单失败");
                }
            }
 
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }finally {
            InterceptorIgnoreHelper.clearIgnoreStrategy();
        }
    }
 
}