| | |
| | | package com.taoke.autopay.manager; |
| | | |
| | | import com.taoke.autopay.entity.KeyOrder; |
| | | import com.taoke.autopay.entity.SystemConfigKeyEnum; |
| | | 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; |
| | |
| | | |
| | | @Data |
| | | @Builder |
| | | public static class OrderQueue implements Comparable{ |
| | | public static class OrderQueue implements Comparable { |
| | | private String id; |
| | | private long expireTime; |
| | | |
| | |
| | | @Resource |
| | | private KeyOrderService keyOrderService; |
| | | |
| | | private Map<String,Integer> reProcessCountMap=new HashMap<>(); |
| | | private Map<String, Integer> reProcessCountMap = new HashMap<>(); |
| | | |
| | | private Queue<OrderQueue> orderQueues=new PriorityQueue<>(); |
| | | private Queue<OrderQueue> orderQueues = new PriorityQueue<>(); |
| | | |
| | | public void clearCacheData(){ |
| | | @Resource |
| | | private SystemConfigService systemConfigService; |
| | | |
| | | public void clearCacheData() { |
| | | reProcessCountMap.clear(); |
| | | orderQueues.clear(); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void processFromQueue(){ |
| | | if(orderQueues.isEmpty()){ |
| | | public void processFromQueue() { |
| | | if (orderQueues.isEmpty()) { |
| | | 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){ |
| | | 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; |
| | | } |
| | | |
| | | if(!reProcessCountMap.containsKey(queue.getId())){ |
| | | reProcessCountMap.put(queue.getId(), 0); |
| | | } |
| | | reProcessCountMap.put(queue.getId(), reProcessCountMap.get(queue.getId())+1); |
| | | if (!reProcessCountMap.containsKey(queue.getId())) { |
| | | reProcessCountMap.put(queue.getId(), 0); |
| | | } |
| | | reProcessCountMap.put(queue.getId(), reProcessCountMap.get(queue.getId()) + 1); |
| | | keyOrderService.removeDistributedClient(queue.getId()); |
| | | |
| | | String clientIds = systemConfigService.getValueCache(SystemConfigKeyEnum.RE_EXCUTE_PAY_CLIENTS); |
| | | |
| | | |
| | | // 移除已经分配的设备,改变状态为未分配 |
| | | KeyOrder update=new KeyOrder(); |
| | | KeyOrder update = new KeyOrder(); |
| | | update.setId(queue.getId()); |
| | | update.setState(KeyOrder.STATE_NOT_PROCESS); |
| | | update.setStateDesc("重新分配"); |
| | | if (!StringUtil.isNullOrEmpty(clientIds)) { |
| | | String[] cids = clientIds.split(","); |
| | | int index = (int) Math.round(Math.random() * cids.length); |
| | | if (index + 1 > cids.length) { |
| | | index = cids.length - 1; |
| | | } |
| | | update.setDistributeClientUid(Long.parseLong(cids[index])); |
| | | } |
| | | keyOrderService.update(update); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * @return void |
| | | * @author hxh |
| | | * @description 处理支付失败 |
| | | * @date 21:28 2024/7/26 |
| | | * @param: id |
| | | * @param: msg |
| | | * @return void |
| | | **/ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void processPayFail(String id,String msg){ |
| | | if(msg==null||!msg.contains("超时")){ |
| | | public void processPayFail(String id, String msg) { |
| | | if (msg == null || !msg.contains("超时")) { |
| | | return; |
| | | } |
| | | //加入重试队列 |
| | | orderQueues.add(OrderQueue.builder().id(id).expireTime(System.currentTimeMillis()+2*60*1000).build()); |
| | | KeyOrder update=new KeyOrder(); |
| | | orderQueues.add(OrderQueue.builder().id(id).expireTime(System.currentTimeMillis() + 2 * 60 * 1000).build()); |
| | | KeyOrder update = new KeyOrder(); |
| | | update.setId(id); |
| | | update.setState(KeyOrder.STATE_REJECT_PAY); |
| | | update.setStateDesc(msg); |
| | | keyOrderService.update(update); |
| | | } |
| | | |
| | | public static void main(String[] args){ |
| | | Queue<OrderQueue> orderQueues=new PriorityQueue<>(); |
| | | 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(); |
| | | OrderQueue queue = orderQueues.peek(); |
| | | queue = orderQueues.poll(); |
| | | orderQueues.isEmpty(); |
| | | |
| | | |