CZT
2023-12-14 757ee5dc2ca4185d3c60db1e8c35d2e96e15c50c
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
package com.fzzy.order;
 
import com.alibaba.fastjson.JSONObject;
import com.fzzy.api.entity.ApiConfs;
import com.fzzy.api.view.pr.ApiConfsPR;
import com.fzzy.order.data.OrderData;
import com.fzzy.order.data.OrderReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 下发指令接收入口
 *
 * @author czt
 * @date 2023/8/17
 */
@Slf4j
@Component(OrderManager.BEAN_ID)
public class OrderManager implements ApplicationContextAware {
    public static final String BEAN_ID = "order.orderManager";
 
    private static Map<String, OrderApiService> serviceMap;
 
    @Autowired
    private ApiConfsPR apiConfsPR;
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, OrderApiService> map = applicationContext.getBeansOfType(OrderApiService.class);
        serviceMap = new HashMap<>();
        for (String key : map.keySet()) {
            serviceMap.put(map.get(key).getProtocol(), map.get(key));
        }
    }
 
 
    /**
     * 指令解析入口
     * @param req
     */
    @Async
    public void execute(OrderReq<JSONObject> req){
 
        try{
            //获取配置信息
            List<ApiConfs> apiConfList = apiConfsPR.listAll();
            if(null == apiConfList || apiConfList.isEmpty()){
                return;
            }
            OrderData data = JSONObject.parseObject(req.getData().toString(), OrderData.class);
            data.setOrderid(req.getOrderid());
            //根据配置信息执行
            OrderApiService service;
            for (ApiConfs conf : apiConfList) {
 
                service = serviceMap.get(conf.getPushProtocol());
                if(null == service){
                    continue;
                }
 
                service.execute(data, req.getType(), conf, req.getOrderid());
            }
        } catch (Exception e) {
            log.error("-----指令解析异常-----指令={}", req);
            e.printStackTrace();
        }
 
    }
}