admin
2025-05-09 6159dc58f50d3e4680779b7989bbd4d49a76bad5
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
package com.taoke.autopay.task;
 
import com.taoke.autopay.dao.KeyOrderMapper;
import com.taoke.autopay.dto.DYOrderDto;
import com.taoke.autopay.entity.ClientInfo;
import com.taoke.autopay.entity.KeyOrder;
import com.taoke.autopay.exception.KeyOrderException;
import com.taoke.autopay.manager.OrderPayFailProcessor;
import com.taoke.autopay.service.ClientInfoService;
import com.taoke.autopay.service.KeyOrderService;
import com.taoke.autopay.utils.Constant;
import com.taoke.autopay.utils.StringUtil;
import com.taoke.autopay.utils.order.DYOrderApi;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
 
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
 
@Configuration
@EnableScheduling
public class KeyOrderDistributeTask {
    @Resource
    private KeyOrderService keyOrderService;
 
    @Scheduled(cron = "0/5 * * * * ? ")
    private void distribute() {
        try {
            List<KeyOrder> results = keyOrderService.listNotDistributed(1, 20);
            if (results != null) {
                for (KeyOrder order : results) {
                    if (order.getDistributeClientUid() != null) {
                        continue;
                    }
                    Long uid = keyOrderService.getCanDistributeUid(Constant.MAX_PAY_ACCOUNT_QUEUE_SIZE);
                    if (uid != null) {
                        KeyOrder orderUpdate = new KeyOrder();
                        orderUpdate.setId(order.getId());
                        orderUpdate.setDistributeClientUid(uid);
                        orderUpdate.setDistributeTime(new Date());
                        keyOrderService.update(orderUpdate);
                    }
                }
            }
        } catch (Exception e) {
 
        }
    }
 
    /**
     * @return void
     * @author hxh
     * @description 修正已经处理的订单
     * @date 17:53 2024/6/20
     **/
    @Scheduled(cron = "0/5 * * * * ? ")
    private void repaireProcessedOrders() {
        // TODO 待完成
        KeyOrderMapper.DaoQuery query = new KeyOrderMapper.DaoQuery();
        // 修正1分钟到1小时的数据之前执行的数据
        query.maxUpdateTime = new Date(System.currentTimeMillis() - 1000 * 60 * 1L);
        query.minUpdateTime = new Date(System.currentTimeMillis() - 1000 * 60 * 60L);
        query.sortList = Arrays.asList(new String[]{"update_time asc"});
        query.stateList = Arrays.asList(new Integer[]{KeyOrder.STATE_NOT_PAY, KeyOrder.STATE_PAY});
        query.orderState = DYOrderDto.ORDER_STATUS_NOT_PAY;
        query.start = 0;
        query.count = 20;
        List<KeyOrder> list = keyOrderService.list(query);
        if (list != null && list.size() > 0) {
            for (KeyOrder order : list) {
                // 如果没有拿到订单就不做处理
                if (StringUtil.isNullOrEmpty(order.getOrderNo())) {
                    continue;
                }
                // 查询结果
                DYOrderDto dto = null;
                try {
                    dto = DYOrderApi.getOrderDetail(order.getOrderNo());
                    if (dto.getOrder_status() != DYOrderDto.ORDER_STATUS_NOT_PAY) {
                        // 订单不处于尚未付款状态
                        KeyOrder orderUpdate = new KeyOrder();
                        orderUpdate.setId(order.getId());
                        orderUpdate.setOrderState(dto.getOrder_status());
                        orderUpdate.setState(KeyOrder.STATE_PAY);
                        orderUpdate.setStateDesc(dto.getOrder_status_desc());
                        keyOrderService.update(orderUpdate);
                    } else {
                        KeyOrder update = new KeyOrder();
                        update.setId(order.getId());
                        update.setState(KeyOrder.STATE_NOT_PROCESS);
                        update.setStateDesc("重置未分配");
                        update.setOrderState(dto.getOrder_status());
                        keyOrderService.update(update);
                    }
                } catch (KeyOrderException e) {
                    e.printStackTrace();
                    KeyOrder orderUpdate = new KeyOrder();
                    orderUpdate.setId(order.getId());
                    orderUpdate.setState(KeyOrder.STATE_PAY);
                    orderUpdate.setStateDesc(e.getMessage());
                    keyOrderService.update(orderUpdate);
                }
            }
        }
    }
 
 
    @Resource
    private OrderPayFailProcessor orderPayFailProcessor;
 
    @Scheduled(cron = "0/5 * * * * ? ")
    private void processPayFail() {
        for (int i = 0; i < 10; i++) {
            // 一次最多处理10条数据
            orderPayFailProcessor.processFromQueue();
        }
    }
 
    @Scheduled(cron = "0 0 3 * * ? ")
    private void clearProcessPayFailCache() {
        orderPayFailProcessor.clearCacheData();
    }
 
    @Resource
    private ClientInfoService clientInfoService;
 
    // 处理设备下线
    @Scheduled(cron = "0 0/1 * * * ? ")
    private void processPayClientOffLine() {
        KeyOrderMapper.DaoQuery daoQuery = new KeyOrderMapper.DaoQuery();
        // 分配时间在最近5分钟到最近30分钟的,状态为未处理的需要重新分配
        daoQuery.stateList = Arrays.asList(new Integer[]{KeyOrder.STATE_NOT_PAY, KeyOrder.STATE_NOT_PROCESS});
        daoQuery.minDistributeTime = new Date(System.currentTimeMillis() - 1000 * 60 * 30L);
        daoQuery.maxDistributeTime = new Date(System.currentTimeMillis() - 1000 * 60 * 5L);
        daoQuery.sortList = Arrays.asList(new String[]{"create_time desc"});
        daoQuery.count = 10;
        List<KeyOrder> orderList = keyOrderService.list(daoQuery);
        for (KeyOrder order : orderList) {
            // 查询设备活跃时间是否已经有5分钟未活跃
            if (order.getDistributeClientUid() == null) {
                continue;
            }
 
            ClientInfo clientInfo = clientInfoService.selectByPrimaryKey(order.getDistributeClientUid());
            if (clientInfo.getActiveTime()==null||System.currentTimeMillis() - clientInfo.getActiveTime().getTime() < 1000 * 60 * 5L) {
                continue;
            }
            // 重新分配
            keyOrderService.removeDistributedClient(order.getId());
        }
 
 
    }
 
}