CZT
2023-09-17 8cbee71a1145555fabb0ebcdd9a8654da4a485ae
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package com.fzzy.push.gb2022;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fzzy.api.Constant;
import com.fzzy.api.data.ApiParam;
import com.fzzy.api.data.AuthToken;
import com.fzzy.api.data.PushProtocol;
import com.fzzy.api.dto.ResponseDto;
import com.fzzy.api.entity.ApiConfs;
import com.fzzy.api.entity.ApiLog;
import com.fzzy.api.entity.ApiParent;
import com.fzzy.api.service.ApiCommonService;
import com.fzzy.api.service.ApiRemoteService;
import com.fzzy.api.utils.*;
import com.fzzy.api.view.repository.ApiLogRep;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
/**
 * @author vince.xu
 * @Title: ApiService
 * @ProjectName igds-api
 * @Description: 上传数据服务类
 * @date 2022-9-216:27
 */
@Slf4j
@Data
@Service
public class GBApiRemoteService implements ApiRemoteService {
 
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private ApiCommonService apiCommonService;
 
    @Autowired
    private ApiLogRep apiLogRep;
 
 
    @Override
    public String getProtocol() {
        return PushProtocol.GB_2022.getCode();
    }
 
    @Override
    public ResponseDto pushData(ApiParam param, ApiConfs conf, Object data) {
        String inteId = param.getInteId();
        String kqdm = param.getKqdm();
        String bizId = param.getBizId();
        //添加LOGO
        ApiLog apiLog = new ApiLog();
        apiLog.setId(ContextUtil.getUUID());
        apiLog.setKqdm(kqdm);
        try {
 
            if (null == conf) {
                conf = apiCommonService.getConf(kqdm);
            }
            //获取TOKEN
            AuthToken token = getAuthToken(conf);
 
            if (!Constant.API_CODE_1109.equals(inteId)) {
                apiLog.setData(JSON.toJSONString(data));
            }
            apiLog.setInteId(inteId);
 
            if (conf == null) {
                ResponseDto responseDto = new ResponseDto(99, "没有获取到上传配置", bizId);
                apiLog.setStatus(99);
                apiLog.setResult("没有获取到上传配置");
                apiLogRep.save(apiLog);
                return responseDto;
            }
            if (null == token) {
                ResponseDto responseDto = new ResponseDto(99, "没有获取到TOKEN信息", bizId);
                apiLog.setStatus(99);
                apiLog.setResult("没有获取到TOKEN信息");
                apiLogRep.save(apiLog);
                return responseDto;
            }
 
 
            Map<String, Object> map = new HashMap<>();
            map.put("token", token.getToken());
            String jsondata = JSON.toJSONString(data);
            log.debug("-----------------数据报文----------------{}", jsondata);
            String d = SM2Utils.encrypt(conf.getPublicKey(), jsondata);
            log.info("json:" + jsondata);
            map.put("data", d);
            map.put("table", inteId);
            map.put("JSESSIONID", token.getJSESSIONID());
            ResponseDto responseDto = HttpClientUtil.postFormData(conf.getApiUrl() + inteId, map);
            responseDto.setBizId(bizId);
            apiLog.setStatus(responseDto.getSuccess());
            apiLog.setResult(responseDto.getMsg());
            apiLogRep.save(apiLog);
 
            updateAuthToken(responseDto, token);
            return responseDto;
        } catch (Exception e) {
            apiLog.setStatus(99);
            apiLog.setResult("失败:" + e.getMessage());
            apiLogRep.save(apiLog);
            log.error(e.getMessage(), e);
            return new ResponseDto(99, e.getMessage());
        }
    }
 
    @Override
    public ResponseDto pushData(ApiParam param, Object data) {
        return pushData(param, null, data);
    }
 
 
    /**
     * @Desc: 国标自定义方法
     * @author: Andy
     * @update-time: 2022/10/10
     */
    public ResponseDto pushData(String inteId, String kqdm, String bizId, Object data) {
        ApiConfs conf = apiCommonService.getConf(kqdm);
 
        ApiParam param = new ApiParam(conf);
 
        param.setInteId(inteId);
        param.setInteCategory(inteId.substring(0, 2));
        param.setBizId(bizId);
 
        return pushData(param, conf, data);
    }
 
 
    /**
     * 单条上传数据
     *
     * @param inteId
     * @param data
     * @return
     */
    public ResponseDto pushData(String inteId, ApiParent data) {
        try {
            //获取配置
            ApiConfs conf = apiCommonService.getConf(data.getKqdm());
            //获取TOKEN
            AuthToken token = getAuthToken(conf);
 
            //添加LOGO
            ApiLog apiLog = new ApiLog();
            apiLog.setId(ContextUtil.getUUID());
            if (!Constant.API_CODE_1109.equals(inteId)) {
                apiLog.setData(JSON.toJSONString(data));
            }
            apiLog.setInteId(inteId);
            apiLog.setKqdm(data.getKqdm());
            apiLog.setUploadTime(new Date());
 
            if (conf == null) {
                ResponseDto responseDto = new ResponseDto(99, "没有获取到上传配置", data.getBizId());
                apiLog.setStatus(99);
                apiLog.setResult("没有获取到上传配置");
                apiLogRep.save(apiLog);
                return responseDto;
            }
            if (null == token) {
                ResponseDto responseDto = new ResponseDto(99, "没有获取到TOKEN信息", data.getBizId());
                apiLog.setStatus(99);
                apiLog.setResult("没有获取到TOKEN信息");
                apiLogRep.save(apiLog);
                return responseDto;
            }
 
            Map<String, Object> map = new HashMap<>();
            map.put("token", token.getToken());
            String jsondata = JSON.toJSONString(data);
            String d = SM2Utils.encrypt(conf.getPublicKey(), jsondata);
            log.info("json:" + jsondata);
            map.put("data", d);
            map.put("table", inteId);
            map.put("JSESSIONID", token.getJSESSIONID());
 
            ResponseDto responseDto = HttpClientUtil.postFormData(conf.getApiUrl() + inteId, map);
            responseDto.setBizId(data.getBizId());
            apiLog.setStatus(responseDto.getSuccess());
            apiLog.setResult(responseDto.getMsg());
 
            apiLogRep.save(apiLog);
 
            updateAuthToken(responseDto, token);
 
            return responseDto;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new ResponseDto(99, e.getMessage());
        }
    }
 
    /**
     * 代码调整
     *
     * @param conf
     * @return
     */
    public AuthToken getAuthToken(ApiConfs conf) {
        try {
            String key = RedisConst.buildKey(RedisConst.KYE_TOKEN, conf.getKqdm());
 
            AuthToken token = (AuthToken) redisUtil.get(key);
            if (null != token) return token;
 
            //从新获取TOKEN
            Map<String, Object> map = new HashMap<>();
            map.put("UserName", conf.getUserName());
            String password = SM2Utils.encrypt(conf.getPublicKey(), conf.getPassword());
            map.put("Password", password);
            ResponseDto responseDto = HttpClientUtil.postFormData(conf.getApiUrl() + "login", map);
            token = new AuthToken(conf.getKqdm(), responseDto.getToken(), responseDto.getJSESSIONID());
 
            updateAuthToken(null, token);
 
            return token;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }
 
    private void updateAuthToken(ResponseDto dto, AuthToken token) {
        String key = RedisConst.buildKey(RedisConst.KYE_TOKEN, token.getKqdm());
 
        if (null == dto) {
            redisUtil.set(key, token, 170);
            return;
        }
 
        if (dto.getSuccess() > 1) {
            return;
        } else if (dto.getSuccess() == 1) {
            redisUtil.del(key);
        } else {
            token.setToken(dto.getToken());
            token.setJSESSIONID(dto.getJSESSIONID());
            redisUtil.set(key, token, 170);
        }
    }
 
 
}