zxl
2025-05-12 fdcdd41fba7874c045766e3dea54d56d70df73ef
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
package com.ycl.feign;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import com.ycl.platform.domain.param.HK.BaseParam;
import com.ycl.platform.domain.param.HK.FaceDetectParam;
import com.ycl.utils.DateUtils;
import com.ycl.utils.bean.BeanUtils;
import constant.ApiConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
 
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.jar.JarEntry;
 
/**
 * @author:xp
 * @date:2024/8/12 11:47
 */
@Slf4j
public class HkApiUtil {
 
 
    /**
     * 调用海康接口 基础数据平台 Post请求
     *
     * @param apiUrl 接口地址:/api/dqd/service/rs/v2/data/faceDetect/query
     * @param params 请求参数
     * @param resultType 响应结果接收类
     * @return 调用结果
     */
    public static <T> List<T> sendAPI(String host,String appKey,String appSecret,String apiUrl, BaseParam params, Class<T> resultType) {
        // STEP1:设置平台参数,根据实际情况,设置host appkey appsecret 三个参数.
        ArtemisConfig.host = host; // 平台的ip端口
        ArtemisConfig.appKey = appKey;  // 密钥appkey
        ArtemisConfig.appSecret = appSecret;// 密钥appSecret
 
        // STEP2:设置OpenAPI接口的上下文
        final String ARTEMIS_PATH = "/artemis";
 
        // STEP3:设置接口的URI地址
        final String previewURLsApi = ARTEMIS_PATH + apiUrl;
        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewURLsApi);
            }
        };
 
        // STEP4:设置参数提交方式
        String contentType = "application/json";
 
        // STEP5:组装请求参数
        String body = JSON.toJSONString(params);
        // STEP6:调用接口
        String result = null;// post请求application/json类型参数
        List<T> dataList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            result = ArtemisHttpUtil.doPostStringArtemis(path, body, null, null, contentType , null);
            JSONObject jsonObject = JSONObject.parseObject(result);
            if(jsonObject.getString("code") == null || !ApiConstants.HKSuccessCode.equals(jsonObject.getString("code"))){
                log.error("请求失败{}",result);
                dataList = null;
                break;
            }
            List<T> resultList = HkApiUtil.getDataList(JSONObject.parseObject(result), resultType);
            if(CollectionUtils.isEmpty(resultList) || resultList.size()<ApiConstants.HKPageSize) {
                dataList.addAll(resultList);
                break;
            }else {
                dataList.addAll(resultList);
                params.setPageNo(params.getPageNo()+1);
            }
        }
 
        return dataList;
    }
 
 
 
    //解析数据
    private static <T> List<T> getDataList(JSONObject jsonObject, Class<T> resultClass) {
        if (jsonObject != null && ApiConstants.HKSuccessCode.equals(jsonObject.getString("code"))) {
            JSONObject data = jsonObject.getJSONObject("data");
            if (data == null) {
                return null;
            }
            List<T> list = data.getList("list", resultClass);
            if (CollectionUtils.isEmpty(list)) {
                return null;
            }
            return list;
        }
        return null;
    }
 
    /**
     * 实体对象转成Map
     * @param obj 实体对象
     * @return
     */
    public static Map<String, String> object2Map(Object obj) {
        Map<String, String> map = new HashMap<>();
        if (obj == null) {
            return map;
        }
        Class clazz = obj.getClass();
        Field[] fields = clazz.getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                map.put(field.getName(), field.get(obj).toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
 
    /**
     * Map转成实体对象
     * @param map map实体对象包含属性
     * @param clazz 实体对象类型
     * @return
     */
    public static Object map2Object(Map<String, Object> map, Class<?> clazz) {
        if (map == null) {
            return null;
        }
        Object obj = null;
        try {
            obj = clazz.newInstance();
 
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                int mod = field.getModifiers();
                if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
                    continue;
                }
                field.setAccessible(true);
                field.set(obj, map.get(field.getName()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return obj;
    }
}