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
159
160
161
162
163
164
165
166
167
package com.taoke.autopay.manager;
 
import com.taoke.autopay.dao.KeyOrderMapper;
import com.taoke.autopay.entity.ClientInfo;
import com.taoke.autopay.entity.KeyOrder;
import com.taoke.autopay.entity.SystemConfigKeyEnum;
import com.taoke.autopay.service.ClientInfoService;
import com.taoke.autopay.service.KeyOrderService;
import com.taoke.autopay.service.SystemConfigService;
import com.taoke.autopay.utils.StringUtil;
import lombok.Builder;
import lombok.Data;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.*;
 
/**
 * @author hxh
 * @title: OrderPayFailProcessor
 * @description: 订单支付失败处理器
 * @date 2024/7/26 21:27
 */
@Component
public class OrderPayFailProcessor {
 
    @Data
    @Builder
    public static class OrderQueue implements Comparable {
        private String id;
        private long expireTime;
 
        @Override
        public int compareTo(Object o) {
            return 0;
        }
    }
 
    @Resource
    private KeyOrderService keyOrderService;
 
    private Map<String, Integer> reProcessCountMap = new HashMap<>();
 
    private Queue<OrderQueue> orderQueues = new PriorityQueue<>();
 
    @Resource
    private SystemConfigService systemConfigService;
 
    @Resource
    private ClientInfoService clientInfoService;
 
    public void clearCacheData() {
        reProcessCountMap.clear();
        orderQueues.clear();
    }
 
    private Long getTargetClientId() {
        List<Long> clientIds = clientInfoService.getRePayClientIds();
        if (clientIds != null && clientIds.size() > 0) {
            // 查询设备未执行的数量
            List<List<Long>> clist=new ArrayList<>();
            for (Long cuid : clientIds) {
                KeyOrderMapper.DaoQuery daoQuery = new KeyOrderMapper.DaoQuery();
                daoQuery.distributeClientUid =cuid;
                daoQuery.state =  KeyOrder.STATE_NOT_PAY;
                daoQuery.minCreateTime=new Date(System.currentTimeMillis() - 1000*60*30L);
                long count =  keyOrderService.count(daoQuery);
                clist.add(Arrays.asList(new Long[]{cuid,count}));
            }
            clist.sort(new Comparator<List<Long>>() {
                @Override
                public int compare(List<Long> o1, List<Long> o2) {
                    return (int)(o1.get(1)-o2.get(1));
                }
            });
            if(clist.size()>1) {
                for (int i = 1; i < clist.size(); i++) {
                    if (clist.get(i).get(1).longValue() != clist.get(i - 1).get(1)) {
                        clist = clist.subList(0, i);
                        break;
                    }
                }
            }
            Collections.shuffle(clist);
            return clist.get(0).get(0);
        }
        return null;
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void processFromQueue() {
        if (orderQueues.isEmpty()) {
            return;
        }
        Long targetCUID = getTargetClientId();
        if (targetCUID == null) {
            return;
        }
        OrderQueue queue = orderQueues.peek();
        if (queue != null && System.currentTimeMillis() >= queue.expireTime) {
            queue = orderQueues.poll();
            KeyOrder order = keyOrderService.selectById(queue.getId());
            if (order == null || order.getState() != KeyOrder.STATE_REJECT_PAY) {
                return;
            }
            if (reProcessCountMap.containsKey(queue.getId()) && reProcessCountMap.get(queue.getId()) > 3) {
                return;
            }
 
            // 超时30分钟不执行
            if (System.currentTimeMillis() - order.getCreateTime().getTime() > 30 * 60 * 1000L) {
                return;
            }
 
            if (!reProcessCountMap.containsKey(queue.getId())) {
                reProcessCountMap.put(queue.getId(), 0);
            }
 
            reProcessCountMap.put(queue.getId(), reProcessCountMap.get(queue.getId()) + 1);
            keyOrderService.removeDistributedClient(queue.getId());
            // 移除已经分配的设备,改变状态为未分配
            KeyOrder update = new KeyOrder();
            update.setId(queue.getId());
            update.setState(KeyOrder.STATE_NOT_PROCESS);
            update.setStateDesc("重新分配");
            update.setDistributeClientUid(targetCUID);
            update.setDistributeTime(new Date());
            keyOrderService.update(update);
        }
    }
 
    /**
     * @return void
     * @author hxh
     * @description 处理支付失败
     * @date 21:28 2024/7/26
     * @param: id
     * @param: msg
     **/
    @Transactional(rollbackFor = Exception.class)
    public void processPayFail(String id, String msg) {
        if (msg == null || !msg.contains("超时")) {
            return;
        }
        //加入重试队列
        orderQueues.add(OrderQueue.builder().id(id).expireTime(System.currentTimeMillis()).build());
        KeyOrder update = new KeyOrder();
        update.setId(id);
        update.setState(KeyOrder.STATE_REJECT_PAY);
        update.setStateDesc(msg);
        keyOrderService.update(update);
        processFromQueue();
    }
 
    public static void main(String[] args) {
        Queue<OrderQueue> orderQueues = new PriorityQueue<>();
        orderQueues.add(OrderQueue.builder().id("1").build());
        orderQueues.add(OrderQueue.builder().id("2").build());
        OrderQueue queue = orderQueues.peek();
        queue = orderQueues.poll();
        orderQueues.isEmpty();
 
 
    }
 
}