public AjaxResult zhihuiya(@RequestBody ZlRecord zlRecord) {
SysUser user = SecurityUtils.getLoginUser().getUser();
ZlCount zlCount = newLabelMapper.selectUserCount(user.getUserId());
if (ObjectUtils.isEmpty(zlCount)){
ZlCount zlCount2 = newLabelMapper.selectDeptCount(user.getDeptId());
if (ObjectUtils.isEmpty(zlCount2))
throw new RuntimeException("该部门或用户没有下载次数");
}
String token="";
String jobid="";
String filePath="";
String patentNumber="CN106561027B";
String clientId="w6nf8ZI4AKA0oYgjJb";
String clientSecret="Gy2geNN56TX1h79";
String cacheObject = redisCache.getCacheObject("zha");
if (StringUtils.isNotEmpty(cacheObject)){
token = cacheObject;
}else {
String tokenhost = "https://"+clientId+":"+clientSecret+"@connect.ziya.com";
String tokenpath = "/oauth/token";
Map headers = new HashMap();
headers.put("Content-Type", "application/x-www-form-urlencoded");
Map requestbody = new HashMap();
requestbody.put("grant_type", "client_credentials");
try {
HttpResponse response = HttpUtils.doPost(tokenhost, tokenpath, null, headers, null, requestbody);
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
org.json.JSONObject jsonResponse = new org.json.JSONObject(result);
org.json.JSONObject data = jsonResponse.getJSONObject("data");
if (data.has("token")&&StringUtils.isNotEmpty(data.getString("token"))){
token = "Bearer "+data.getString("token");
redisCache.setCacheObject("zhihuiya" , token, 25, TimeUnit.MINUTES);
}
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException("获取token失败", HttpStatus.ERROR);
}
}
String host = "https://connect.zya.com";
String path = "/patent-valuation-data/patent-report/task/create";
Map headers2 = new HashMap();
headers2.put("Content-Type", "application/json");
headers2.put("authorization", token);
Map querys = new HashMap();
querys.put("apikey", clientId);
JSONObject dataBody = new JSONObject();
dataBody.put("report_type", "pdf");
dataBody.put("patent_number", zlRecord.getPatentNumber());
try {
HttpResponse response = HttpUtils.doPost(host, path, null, headers2, querys, dataBody.toJSONString());
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
org.json.JSONObject jsonResponse = new org.json.JSONObject(result);
org.json.JSONObject data = jsonResponse.getJSONObject("data");
jobid = data.getString("job_id");
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException("获取报告导出任务失败", HttpStatus.ERROR);
}
String host3 = "https://connect.zhya.com";
String path3 = "/patent-valuation-data/patent-report/job_id/status";
Map headers3 = new HashMap();
headers3.put("Content-Type", "application/json");
headers3.put("authorization", token);
Map querys3 = new HashMap();
querys3.put("job_id", jobid);
querys3.put("apikey", clientId);
try {
HttpResponse response = HttpUtils.doGet(host3, path3, null, headers3, querys3);
String result = EntityUtils.toString(response.getEntity());
System.out.println(result);
org.json.JSONObject jsonResponse = new org.json.JSONObject(result);
org.json.JSONObject data = jsonResponse.getJSONObject("data");
if (jsonResponse.has("error_code")&&jsonResponse.getInt("error_code")!=0){
throw new ServiceException("下载报告失败"+jsonResponse.getInt("error_code"), HttpStatus.ERROR);
}
if (data.has("job_status")&&jsonResponse.getInt("error_code")==0){
while (data.getInt("job_status")==1){
System.out.println("导出中");
Thread.sleep(5000);
response = HttpUtils.doGet(host3, path3, null, headers3, querys3);
result = EntityUtils.toString(response.getEntity());
jsonResponse = new org.json.JSONObject(result);
data = jsonResponse.getJSONObject("data");
}
if (data.getInt("job_status")==2){
System.out.println("导出完成");
filePath = data.getString("download_url");
}else {
throw new ServiceException("导出失败"+data.getInt("job_status")+"errorcode"+jsonResponse.getString("error_code"), HttpStatus.ERROR);
}
}
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException("导出报告失败", HttpStatus.ERROR);
}
String fileName1="";
try {
URL url = new URL(filePath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileNameWithQuery = filePath.substring(filePath.lastIndexOf("/") + 1);
int queryIndex = fileNameWithQuery.indexOf("?");
String fileName = (queryIndex != -1) ? fileNameWithQuery.substring(0, queryIndex) : fileNameWithQuery;
long currentTimeMillis = System.currentTimeMillis();
fileName1 = user.getUserId()+"_"+currentTimeMillis+"_"+fileName;
Path saveFilePath = Paths.get("/home/www/zlbg/", fileName1);
Files.copy(connection.getInputStream(), saveFilePath, StandardCopyOption.REPLACE_EXISTING);
System.out.println("文件下载成功:" + saveFilePath.toAbsolutePath());
System.out.println("/home/www/zlbg/"+ fileName1);
zlRecord.setId(UUID.randomUUID().toString().replaceAll("-", ""));
zlRecord.setFilePath("/home/www/zlbg/"+ fileName1);
zlRecord.setUserId(user.getUserId());
zlRecord.setDeptId(user.getDeptId());
newLabelMapper.insertZlRecord(zlRecord);
try {
if (ObjectUtils.isEmpty(zlCount)){
newLabelMapper.updateDeptCount(user.getDeptId());
}else {
newLabelMapper.updateUserCount(user.getUserId());
}
} catch (Exception e) {
throw new ServiceException("文件下载次数超限", HttpStatus.ERROR);
}
} else {
System.out.println("文件下载失败,响应码:" + responseCode);
throw new ServiceException("文件下载失败", HttpStatus.ERROR);
}
connection.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new ServiceException("文件下载失败", HttpStatus.ERROR);
}
return AjaxResult.success("/home/www/zlbg/"+ fileName1);
}