陈战涛
2023-05-25 7594c1e78092208fb07acecbf4d9d57ba12af7d3
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
390
391
392
393
package com.fzzy.push.gd2020;
 
import com.alibaba.fastjson.JSON;
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.*;
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.gd2020.v2.*;
 
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.Date;
 
/**
 * @author vince.xu
 * @Title: ApiService
 * @ProjectName igds-api
 * @Description: 上传数据服务类
 * @date 2022-9-216:27
 */
@Slf4j
@Data
@Component(GDApiRemoteService.BEAN_ID)
public class GDApiRemoteService implements ApiRemoteService {
 
 
    public static final String BEAN_ID = "gd.ApiRemoteService";
 
    @Autowired
    private RedisUtil redisUtil;
 
    @Autowired
    private ApiCommonService apiCommonService;
 
    @Autowired
    private ApiLogRep apiLogRep;
 
 
    /**
     * 粮食品种 -油脂类型标记
     */
    private String LSPZ_YZ_TAG = "211";
 
 
    @Override
    public String getProtocol() {
        return PushProtocol.SB_GD_2020.getCode();
    }
 
    @Override
    public ResponseDto pushData(ApiParam param, ApiConfs conf, Object data) {
 
        String inteId = param.getInteId();
        String kqdm = param.getKqdm();
        String bizId = param.getBizId();
        ResponseDto responseDto;
        //添加LOGO
        ApiLog apiLog = new ApiLog();
 
        try {
            if (null == conf) {
                conf = apiCommonService.getConf(kqdm);
            }
            //获取TOKEN
            AuthToken token = getAuthToken(conf);
 
            apiLog.setId(ContextUtil.getUUID());
            if (!Constant.API_CODE_1109.equals(inteId)) {
                apiLog.setData(JSON.toJSONString(data));
            }
            apiLog.setInteId(inteId);
            apiLog.setKqdm(kqdm);
            apiLog.setUploadTime(new Date());
 
            if (conf == null) {
                responseDto = new ResponseDto(99, "没有获取到上传配置", bizId);
                apiLog.setStatus(99);
                apiLog.setResult("没有获取到上传配置");
                apiLogRep.save(apiLog);
                return responseDto;
            }
            if (null == token.getToken()) {
                responseDto = new ResponseDto(99, "没有获取到TOKEN信息", bizId);
                apiLog.setStatus(99);
                apiLog.setResult("没有获取到TOKEN信息");
                apiLogRep.save(apiLog);
                return responseDto;
            }
 
            InvokeResult invokeResult = null;
 
 
            //企业信息
            if (Constant.API_CODE_1101.equals(param.getInteId())) {
                Api1101 apiData = (Api1101) data;
                invokeResult = CompanyWebService.newInstance().webService(token, conf, apiData);
            }
 
            //仓储人员信息
            if (Constant.API_CODE_1112.equals(param.getInteId())) {
                Api1112 apiData = (Api1112) data;
                invokeResult = CompanyStaffWebService.newInstance().webService(token, conf, apiData);
            }
 
            //库点信息
            if (Constant.API_CODE_1102.equals(param.getInteId())) {
                Api1102 apiData = (Api1102) data;
                invokeResult = StoreLocationWebService.newInstance().webService(token, conf, apiData);
            }
 
            //仓房信息
            if (Constant.API_CODE_1103.equals(param.getInteId())) {
                Api1103 apiData = (Api1103) data;
                invokeResult = StoreHouseWebService.newInstance().webService(token, conf, apiData);
            }
 
            //廒间信息
            if (Constant.API_CODE_1104.equals(param.getInteId())) {
                Api1104 apiData = (Api1104) data;
                //获取仓房信息
                Api1103 api1103 = apiCommonService.getApi1103Cache(apiData.getCfbh());
 
                invokeResult = GranaryWebService.newInstance().webService(token, conf, apiData, api1103);
            }
 
            //货位信息
            if (Constant.API_CODE_1105.equals(param.getInteId())) {
                Api1105 apiData = (Api1105) data;
 
                //获取仓房信息,根据变化长度获取 ,需要确认广东省 货位编码: 仓房编码 + 3位廒间编码 + 2位货位编码
                String cfbh = apiData.getHwdm().substring(0, apiData.getHwdm().length() - 5);
                Api1103 api1103 = apiCommonService.getApi1103Cache(cfbh);
 
                invokeResult = GoodsAllocationWebService.newInstance().webService(token, conf, apiData, api1103);
            }
 
            //油罐信息
            if (Constant.API_CODE_1106.equals(param.getInteId())) {
                Api1106 apiData = (Api1106) data;
                invokeResult = OilTankWebService.newInstance().webService(token, conf, apiData);
            }
 
            //设备信息
            if (Constant.API_CODE_1107.equals(param.getInteId())) {
                Api1107 apiData = (Api1107) data;
                invokeResult = DeviceWebService.newInstance().webService(token, conf, apiData);
            }
 
            //粮食库存信息 包含粮仓和油脂
            if (Constant.API_CODE_1208.equals(param.getInteId())) {
                Api1208 apiData = (Api1208) data;
 
                //获取货位信息
                Api1105 api1105 = apiCommonService.getApi1105Cache(apiData.getHwdm());
                //获取库区信息
                Api1102 api1102 = apiCommonService.getApi1102Cache(apiData.getKqdm());
 
                //根据粮食品种类型判断是粮仓还是油脂
                if (apiData.getLspzdm().startsWith(LSPZ_YZ_TAG)) {//说明存的是油脂,依次判断为油罐仓,是否判断合理根据库区实际参数调整
                    invokeResult = InventoryRecordLiquidWebService.newInstance().webService(token, conf, apiData, api1102, api1105);
                } else {
                    invokeResult = InventoryInsertRecordSolidWebService.newInstance().webService(token, conf, apiData, api1102, api1105);
                }
            }
 
            //损溢单接口数据
            if (Constant.API_CODE_1209.equals(param.getInteId())) {
                Api1209 apiData = (Api1209) data;
                invokeResult = IncomeStatementWebservice.newInstance().webService(token, conf, apiData);
            }
 
            //性质转变单数据接口
            if (Constant.API_CODE_1210.equals(param.getInteId())) {
 
                Api1210 apiData = (Api1210) data;
 
                invokeResult = TransferNatureWebservice.newInstance().webService(token, conf, apiData);
            }
 
 
            //粮情监测数据接口
            if (Constant.API_CODE_1302.equals(param.getInteId())) {
                Api1302 apiData = (Api1302) data;
 
                //获取货位信息
                Api1105 api1105 = apiCommonService.getApi1105Cache(apiData.getHwdm());
 
                invokeResult = GrainConditionCheckTempHumWebService.newInstance().webService(token, conf, apiData, api1105);
            }
 
            //入库数据
            if (Constant.API_CODE_1202.equals(param.getInteId())) {
                Api1202 apiData = (Api1202) data;
                invokeResult = InStorageWebService.newInstance().webService(token, conf, apiData);
            }
 
            //入库过磅
            if (Constant.API_CODE_1202.equals(param.getInteId())) {
                Api1202 apiData = (Api1202) data;
 
                //获取仓房信息,根据变化长度获取 ,需要确认广东省 货位编码: 仓房编码 + 3位廒间编码 + 2位货位编码
                String cfbh = apiData.getHwdm().substring(0, apiData.getHwdm().length() - 5);
                Api1103 api1103 = apiCommonService.getApi1103Cache(cfbh);
 
                invokeResult = InStorageDetailWebService.newInstance().webService(token, conf, apiData, api1103);
            }
 
            //入库质检
            if (Constant.API_CODE_1203.equals(param.getInteId())) {
                Api1203 apiData = (Api1203) data;
                invokeResult = InstoreCheckWebservice.newInstance().webservice(token, conf, apiData);
            }
 
            //入库核算
            if (Constant.API_CODE_1204.equals(param.getInteId())) {
                Api1204 api1204 = (Api1204) data;
                invokeResult = InstoreAccountsWebservice.newInstance().webservice(token, conf, api1204);
            }
 
 
            //出库数据
            if (Constant.API_CODE_1205.equals(param.getInteId())) {
                Api1205 apiData = (Api1205) data;
                invokeResult = OutStorageWebService.newInstance().webservice(token, conf, apiData);
            }
 
            //出库过磅
            if (Constant.API_CODE_1205.equals(param.getInteId())) {
                Api1205 apiData = (Api1205) data;
                //获取仓房信息,根据变化长度获取 ,需要确认广东省 货位编码: 仓房编码 + 3位廒间编码 + 2位货位编码
                String cfbh = apiData.getHwdm().substring(0, apiData.getHwdm().length() - 5);
                Api1103 api1103 = apiCommonService.getApi1103Cache(cfbh);
 
                invokeResult = OutStorageDetailWebService.newInstance().webservice(token, conf, apiData, api1103);
            }
 
 
            //出库结算
            if (Constant.API_CODE_1206.equals(param.getInteId())) {
                Api1206 apiData = (Api1206) data;
                invokeResult = OutStorageAccountsWebservice.newInstance().webservice(token, conf, apiData);
            }
 
            //气体数据
            if (Constant.API_CODE_1304.equals(param.getInteId())) {
                Api1304 apiData = (Api1304) data;
 
                //获取仓房信息,根据变化长度获取 ,需要确认广东省 货位编码: 仓房编码 + 3位廒间编码 + 2位货位编码
                String cfbh = apiData.getHwdm().substring(0, apiData.getHwdm().length() - 5);
                Api1103 api1103 = apiCommonService.getApi1103Cache(cfbh);
 
                invokeResult = GrainConditionCheckGasWebservice.newInstance().webservice(token, conf, apiData, api1103);
            }
 
            //虫害数据
            if (Constant.API_CODE_1303.equals(param.getInteId())) {
                Api1303 apiData = (Api1303) data;
 
                //获取仓房信息,根据变化长度获取 ,需要确认广东省 货位编码: 仓房编码 + 3位廒间编码 + 2位货位编码
                String cfbh = apiData.getHwdm().substring(0, apiData.getHwdm().length() - 5);
                Api1103 api1103 = apiCommonService.getApi1103Cache(cfbh);
 
                invokeResult = GrainConditionCheckWormWebservice.newInstance().webservice(token, conf, apiData, api1103);
            }
 
            //质检数据
            if (Constant.API_CODE_1310.equals(param.getInteId())) {
                Api1310 apiData = (Api1310) data;
                invokeResult = ZlglInventoryWebservice.newInstance().Webservice(token, conf, apiData);
            }
 
            //轮换计划
            if (Constant.API_CODE_1403.equals(param.getInteId())) {
                Api1403 apiData = (Api1403) data;
                invokeResult = LhjhWebService.newInstance().Webservice(token, conf, apiData);
            }
 
            //轮换详细
            if (Constant.API_CODE_1404.equals(param.getInteId())) {
                Api1404 apiData = (Api1404) data;
                invokeResult = LhjhDetailWebService.newInstance().webService(token, conf, apiData);
            }
 
            //合同数据
            if (Constant.API_CODE_1201.equals(param.getInteId())) {
                Api1201 apiData = (Api1201) data;
                invokeResult = ContractWebService.newInstance().webService(token, conf, apiData);
            }
 
 
            //TODO 上传附件
            if (Constant.API_CODE_1109.equals(param.getInteId())) {
                Api1109 apiData = (Api1109) data;
//                invokeResult = FileServerWebService.getWs();
            }
 
 
            if (null == invokeResult) {
                responseDto = new ResponseDto(GDResult.CODE_500.getCode(), GDResult.CODE_500.getName(), bizId);
                apiLog.setStatus(responseDto.getSuccess());
                apiLog.setResult(responseDto.getMsg());
                apiLogRep.save(apiLog);
                return responseDto;
            }
 
            if (GDResult.CODE_200.getCode() == invokeResult.getCode()) {
                responseDto = new ResponseDto(0, GDResult.CODE_200.getName(), bizId);
            } else {
                apiLog.setStatus(invokeResult.getCode());
                responseDto = new ResponseDto(invokeResult.getCode(), invokeResult.getData(), bizId);
            }
 
            apiLog.setStatus(responseDto.getSuccess());
            apiLog.setResult(responseDto.getMsg());
 
        } catch (Exception e) {
            log.error(e.getMessage(), e);
 
            responseDto = new ResponseDto(99, e.getMessage());
            apiLog.setStatus(responseDto.getSuccess());
            apiLog.setResult(responseDto.getMsg());
        }
 
 
        apiLogRep.save(apiLog);
 
        return responseDto;
    }
 
    @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 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;
 
 
            log.info("-----------------------------重新获取TOKEN-----------------------------");
 
            token = AuthTokenWebService.newInstance().getToken(conf.getApiUrl(), conf.getUserName(), conf.getPassword());
 
 
            redisUtil.set(key, token);
 
            return token;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return null;
        }
    }
 
}