陈战涛
2023-05-09 b3ae531ee3b12fa191dc0aa656957bc00ef8ae4a
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package com.fzzy.push.gd2022;
 
import com.alibaba.fastjson.JSON;
import com.fzzy.api.Constant;
import com.fzzy.api.data.ApiParam;
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.service.ApiCommonService;
import com.fzzy.api.service.ApiRemoteService;
import com.fzzy.api.utils.*;
import com.fzzy.api.view.repository.ApiLogRep;
import com.fzzy.push.gd2022.dto.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
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 GD2022ApiRemoteService2022 implements ApiRemoteService {
 
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private ApiCommonService apiCommonService;
 
    @Autowired
    private ApiLogRep apiLogRep;
 
    @Override
    public String getProtocol() {
        return PushProtocol.SB_GD_2022.getCode();
    }
 
    @Override
    public ResponseDto pushData(ApiParam param, ApiConfs conf, Object data) {
        String inteId = param.getInteId();
        String kqdm = param.getKqdm();
        String bizId = param.getBizId();
 
        try {
 
            if (null == conf) {
                conf = apiCommonService.getConf(kqdm);
            }
            //获取TOKEN
            GD2022AuthToken token = getGD2022AuthToken(conf);
 
            //添加LOGO
            ApiLog apiLog = new ApiLog();
            apiLog.setId(ContextUtil.getUUID());
            apiLog.setInteId(inteId);
            String jsonData = getJsonData(inteId, data, conf.getApiUrl(), token);
            inteId = getInteId(inteId);
            if(StringUtils.isEmpty(inteId)){
                ResponseDto responseDto = new ResponseDto(99, "没有获取到接口", bizId);
                apiLog.setStatus(99);
                apiLog.setResult("没有获取到接口");
                apiLogRep.save(apiLog);
                return responseDto;
            }
            apiLog.setData(jsonData);
            apiLog.setKqdm(kqdm);
            apiLog.setUploadTime(new Date());
 
            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<>();
            GD2022ResponseDto responseDto = GD2022HttpClientUtil.postPushData(conf.getApiUrl() + inteId,jsonData,conf, token);
            responseDto.setBizId(bizId);
            apiLog.setStatus(responseDto.getCode() == 200 ? 0 : responseDto.getCode());
            apiLog.setResult(responseDto.getMsg());
            apiLogRep.save(apiLog);
            ResponseDto resd= new ResponseDto();
            resd.setSuccess(responseDto.getCode() == 200 ? 0 : responseDto.getCode());
            resd.setMsg(responseDto.getMsg());
            //updateGD2022AuthToken(responseDto,conf, token);
            return resd;
        } catch (Exception e) {
            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 GD2022ResponseDto 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
//            GD2022AuthToken token = getGD2022AuthToken(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) {
//                GD2022ResponseDto responseDto = new GD2022ResponseDto(99, "没有获取到上传配置", data.getBizId());
//                apiLog.setStatus(99);
//                apiLog.setResult("没有获取到上传配置");
//                apiLogRep.save(apiLog);
//                return responseDto;
//            }
//            if (null == token) {
//                GD2022ResponseDto responseDto = new GD2022ResponseDto(99, "没有获取到TOKEN信息", data.getBizId());
//                apiLog.setStatus(99);
//                apiLog.setResult("没有获取到TOKEN信息");
//                apiLogRep.save(apiLog);
//                return responseDto;
//            }
//
//            Map<String, Object> map = new HashMap<>();
//            GD2022ResponseDto responseDto = GD2022HttpClientUtil.postPushData(conf.getApiUrl() + inteId,JSON.toJSONString(data),conf, token);
//            responseDto.setBizId(data.getBizId());
//            apiLog.setStatus(responseDto.getSuccess());
//            apiLog.setResult(responseDto.getMsg());
//
//            apiLogRep.save(apiLog);
//
//            updateGD2022AuthToken(responseDto, token);
//
//            return responseDto;
//        } catch (Exception e) {
//            log.error(e.getMessage(), e);
//            return new ResponseDto(99, e.getMessage());
//        }
//    }
 
    /**
     * 代码调整
     *
     * @param conf
     * @return
     */
    public GD2022AuthToken getGD2022AuthToken(ApiConfs conf) {
        try {
            String key = RedisConst.buildKey(RedisConst.KYE_TOKEN, conf.getKqdm());
 
            GD2022AuthToken token = (GD2022AuthToken) redisUtil.get(key);
            if (null != token) return token;
 
            //从新获取TOKEN
            Map<String, Object> map = new HashMap<>();
            map.put("appid", conf.getUserName());
//            map.put("appkey", EncryptUtil.encryptHex(conf.getPassword(),conf.getPublicKey()));
            map.put("appkey", AESUtils.encrypt(conf.getPassword(),conf.getPublicKey()));
            GD2022ResponseDto<GD2022AuthToken> responseDto = GD2022HttpClientUtil.postGetToken(conf.getApiUrl() + "sfrz",map);
            token = new GD2022AuthToken();
 
            updateGD2022AuthToken(responseDto, conf,token);
 
            return token;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }
 
    private void updateGD2022AuthToken(GD2022ResponseDto<GD2022AuthToken> dto, ApiConfs conf, GD2022AuthToken token) {
        String key = RedisConst.buildKey(RedisConst.KYE_TOKEN, conf.getKqdm());
 
        if (null == dto) {
           // redisUtil.set(key, token, 170);
            return;
        }
 
        if (dto.getCode() != 200) {
            return;
        }  else {
            GD2022AuthToken token1 = dto.getData();
            token.setAccess_token(token1.getAccess_token());
            token.setAppid(token1.getAppid());
            redisUtil.set(key, token, 60 * 119);
        }
    }
 
    private String getJsonData(String inteId , Object data, String url, GD2022AuthToken authToken) throws Exception {
        if(Constant.API_CODE_1101.equals(inteId)){
            Api1101 api1101  = new Api1101();
            BeanUtils.copyProperties(data,api1101);
            return JSON.toJSONString(api1101);
        }else if(Constant.API_CODE_1109.equals(inteId)){
            //封装数据
            Api1109 api1109 = new Api1109();
            BeanUtils.copyProperties(data,api1109);
 
            if (null == authToken) {
                return JSON.toJSONString(api1109);
            }
            //先上传文件,获取文件id
            GD2022ResponseDto responseDto = GD2022HttpClientUtil.postUploadData(url, authToken, api1109.getWjdz());
            //设置上传文件id
            api1109.setFileStorageId((String) responseDto.getData());
            return JSON.toJSONString(api1109);
        }else if(Constant.API_CODE_1208.equals(inteId)){
            Api1208 api1208  = new Api1208();
            BeanUtils.copyProperties(data,api1208);
            return JSON.toJSONString(api1208);
        }else if(Constant.API_CODE_1307.equals(inteId)){
            //封装数据
            Api1307 api1307  = new Api1307();
            BeanUtils.copyProperties(data,api1307);
 
            if (null == authToken) {
                return JSON.toJSONString(api1307);
            }
            //先上传文件,获取文件id
            GD2022ResponseDto responseDto = GD2022HttpClientUtil.postUploadData(url, authToken, api1307.getWjdz());
            //设置上传文件id
            api1307.setFileStorageId((String) responseDto.getData());
            return JSON.toJSONString(api1307);
        }else if(Constant.API_CODE_1308.equals(inteId)){
            //封装数据
            Api1308 api1308  = new Api1308();
            BeanUtils.copyProperties(data,api1308);
 
            if (null == authToken) {
                return JSON.toJSONString(api1308);
            }
            //先上传文件,获取文件id
            GD2022ResponseDto responseDto = GD2022HttpClientUtil.postUploadData(url, authToken, api1308.getWjdz());
            //设置上传文件id
            api1308.setFileStorageId((String) responseDto.getData());
            return JSON.toJSONString(api1308);
        }else{
            return JSON.toJSONString(data);
        }
    }
 
 
    private String getInteId(String inteId){
        switch (inteId){
            case "1101":
                return ApiCodeConstant.API_CODE_DWXX;
            case "1102":
                return ApiCodeConstant.API_CODE_KQXX;
            case "1103":
                return ApiCodeConstant.API_CODE_CFXX;
            case "1104":
                return ApiCodeConstant.API_CODE_AJXX;
            case "1105":
                return ApiCodeConstant.API_CODE_HWXX;
            case "1106":
                return ApiCodeConstant.API_CODE_YGXX;
            case "1107":
                return ApiCodeConstant.API_CODE_SBXX;
            case "1108":
                return ApiCodeConstant.API_CODE_YJXX;
            case "1109":
                return ApiCodeConstant.API_CODE_WJXX;
            case "1110":
                return ApiCodeConstant.API_CODE_KQTCFDWBZ;
            case "1111":
                return ApiCodeConstant.API_CODE_KQTSPJKSBDWBZ;
            case "1112":
                return ApiCodeConstant.API_CODE_RYXX;
            case "1113":
                return "";
            case "1201":
                return ApiCodeConstant.API_CODE_HTXX;
            case "1202":
                return ApiCodeConstant.API_CODE_LSRKXX;
            case "1203":
                return ApiCodeConstant.API_CODE_RKJYXX;
            case "1204":
                return ApiCodeConstant.API_CODE_RKJSXX;
            case "1205":
                return ApiCodeConstant.API_CODE_LSCKXX;
            case "1206":
                return ApiCodeConstant.API_CODE_LSCKJS;
            case "1207":
                return ApiCodeConstant.API_CODE_DCXX;
            case "1208":
                return ApiCodeConstant.API_CODE_LSKC;
            case "1209":
                return ApiCodeConstant.API_CODE_SYD;
            case "1210":
                return ApiCodeConstant.API_CODE_XZZBD;
            case "1211":
                return ApiCodeConstant.API_CODE_ZMKCXX;
            case "1212":
                return ApiCodeConstant.API_CODE_KHXX;
            case "1301":
                return ApiCodeConstant.API_CODE_AQGL;
            case "1302":
                return ApiCodeConstant.API_CODE_WSDJC;
            case "1303":
                return ApiCodeConstant.API_CODE_HCJC;
            case "1304":
                return ApiCodeConstant.API_CODE_QTJC;
            case "1305":
                return ApiCodeConstant.API_CODE_TFZY;
            case "1306":
                return ApiCodeConstant.API_CODE_XZZY;
            case "1307":
                return ApiCodeConstant.API_CODE_CNSPTX;
            case "1308":
                return ApiCodeConstant.API_CODE_SPJKYCSJGJJBXX;
            case "1309":
                return ApiCodeConstant.API_CODE_WGYJXX;
            case "1310":
                return ApiCodeConstant.API_CODE_ZZSJ;
            case "1401":
                return ApiCodeConstant.API_CODE_CBGM;
            case "1402":
                return ApiCodeConstant.API_CODE_CBJHXX;
            case "1403":
                return ApiCodeConstant.API_CODE_LHJHXX;
            case "1404":
                return ApiCodeConstant.API_CODE_LHJHMXXX;
            case "1405":
                return "";
            case "1501":
                return ApiCodeConstant.API_CODE_CWBBXX;
            case "1502":
                return ApiCodeConstant.API_CODE_CWBBXX;
            case "1503":
                return ApiCodeConstant.API_CODE_CWBBXX;
            default:
                return inteId;
        }
    }
}