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