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);
|
}
|
}
|
|
|
}
|