jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
package com.ld.igds.three.manager;
 
import com.alibaba.fastjson.JSONObject;
import com.ld.igds.three.ThreeCodeEnum;
import com.ld.igds.three.data.ThreeResponse;
import com.ld.igds.three.param.ThreeRequest;
import com.ld.igds.three.service.ThreeService;
import com.ld.igds.three.util.ThreeRespUtil;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 接口业务分发
 * @author chen
 */
@Component
public class ThreeManager implements ApplicationContextAware {
 
    private static Map<String, ThreeService> serviceMap;
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, ThreeService> map = applicationContext
                .getBeansOfType(ThreeService.class);
        serviceMap = new HashMap<>();
        for (String key : map.keySet()) {
            serviceMap.put(map.get(key).getInterfaceId(), map.get(key));
        }
    }
 
    /**
     * 业务执行入口
     * @param req
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public ThreeResponse<Object> execute(ThreeRequest<JSONObject> req) throws Exception{
 
        ThreeService service = serviceMap.get(req.getInterfaceId());
        if(null == service){
            return ThreeRespUtil.error(ThreeCodeEnum.CODE_1111, "当前接口没有找到实现类,无法执行处理!", req);
        }
 
        return service.execute(req);
    }
}