You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
LISInterface/src/main/java/com/ann/demo/service/CollectCheckService.java

271 lines
9.7 KiB
Java

package com.ann.demo.service;
import com.alibaba.fastjson.JSON;
import com.ann.demo.utils.HttpClientUtils;
import com.ann.demo.utils.JsonUtils;
import com.ann.demo.utils.Md5Utils;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.axis.client.Call;
import javax.xml.namespace.QName;
import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.encoding.XMLType;
import org.apache.axis.client.Service;
import org.apache.axis.utils.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.io.IOException;
import java.rmi.RemoteException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @author 曾文和
* @description: 外送报告采集类
* @createTime 2023/9/5 16:48
*/
@Component
@Slf4j
public class CollectCheckService {
private Map<String,String> tokenCacheMap = new HashMap<>();
private String FILENAME = "fileName";
private String FILEBASE64 = "fileBase64";
@Value("${siteCode}")
private String siteCode;
@Value("${customerCode}")
private String customerCode;
@Value("${collectionLocation}")
private String collectionLocation;
@Value("${loginUrl}")
private String loginUrl;
@Value("${namespaceURI}")
private String namespaceURI;
@Value("${webServiceMethod}")
private String webServiceMethod;
@Value("${webServiceDownMethod}")
private String webServiceDownMethod;
@Value("${downfileUrl}")
private String downfileUrl;
public boolean downFileWithCheck(String masterId,String hospBarcode,String reportOdd,String fileUrl) {
//登录
String token = getTokenByCheckKey(siteCode, customerCode, collectionLocation);
System.out.println("获取token:"+token);
//下载
return downFileWithCheck(masterId,hospBarcode,reportOdd,fileUrl,token);
}
/**
* 下载文件
* @param masterId
* @param hospBarcode
* @param reportOdd
* @param fileUrl
* @param token
* @return
*/
private boolean downFileWithCheck(String masterId,String hospBarcode,String reportOdd,String fileUrl,String token){
//根据参数请求接口返回json数据
String result = reqInterfaceByParam(hospBarcode, reportOdd, fileUrl, token);
//解析返回json数据
Map<String,String> map = analysisData(result);
//唯一文件码
String serialnum = hospBarcode + "_" + reportOdd;
//根据解析的数据调用下载
String reqResult = downFile(masterId,map,serialnum);
if(!StringUtils.isEmpty(reqResult)) {
String code = JsonUtils.getValueByKey(reqResult, "code");
if(!StringUtils.isEmpty(code) && "0".equals(code)){
return true;
}
}
return false;
}
/**
* 根据解析的数据调用下载
* @param map
*/
private String downFile(String masterId,Map<String, String> map,String serialnum) {
if(!CollectionUtils.isEmpty(map)){
//设置下载参数
Map rootMap = setDownReqParams(masterId, map, serialnum);
//调用下载接口
String result = HttpClientUtils.doPost(downfileUrl,JSON.toJSONString(rootMap));
log.info("上传下载服务result:"+result);
System.out.println("上传下载服务result:"+result);
return result;
}
return null;
}
/**
* 设置下载请求参数
* @param masterId
* @param map
* @param serialnum
* @return
*/
private Map setDownReqParams(String masterId, Map<String, String> map, String serialnum) {
//设置文件信息参数
Map<String,Object> scanfilesMap = new HashMap<>();
scanfilesMap.put("taskid",-1);
scanfilesMap.put("filetitle", map.get(FILENAME));
scanfilesMap.put("filesource",1);
scanfilesMap.put("filestoragetype",1);
scanfilesMap.put("filetype",2);
scanfilesMap.put("downurl", map.get(FILEBASE64));
scanfilesMap.put("serialnum", serialnum +"1");
List<Map<String,Object>> scanfilesList = new ArrayList<>();
scanfilesList.add(scanfilesMap);
//设置患者信息参数
Map<String,Object> patientMap = new HashMap<>();
patientMap.put("patientid", masterId);
//设置全部参数
Map rootMap = new HashMap<>();
rootMap.put("collectorid","10");
rootMap.put("ip","127.0.0.1");
rootMap.put("assortid","10");
rootMap.put("patient",patientMap);
rootMap.put("scanfiles",scanfilesList);
rootMap.put("scanusercode","zwh");
rootMap.put("scanusername","zwh");
return rootMap;
}
/**
* 解析返回json数据
* @param result
*/
private Map<String,String> analysisData(String result) {
Map<String,String> map = new HashMap<>();
if(!StringUtils.isEmpty(result)) {
ObjectMapper objectMapper = new ObjectMapper();
//解析
try {
// 将JSON字符串转换为Map对象
JsonNode jsonNode = objectMapper.readTree(result);
String fileName = jsonNode.get("result").get("fileName").asText();
if (!StringUtils.isEmpty(fileName)) {
fileName = fileName.substring(0, fileName.length() - 4);
}
map.put(FILENAME,fileName);
String fileBase64 = jsonNode.get("result").get("file").asText();
map.put(FILEBASE64,fileBase64);
} catch (IOException e) {
e.printStackTrace();
}
}
return map;
}
/**
* 根据参数请求接口
* @param hospBarcode
* @param reportOdd
* @param fileUrl
* @param token
* @return
*/
private String reqInterfaceByParam(String hospBarcode, String reportOdd, String fileUrl, String token) {
Map<String,String> paramMap = new HashMap<>();
paramMap.put("siteCode",siteCode);
paramMap.put("customerCode",customerCode);
paramMap.put("collectionLocation",collectionLocation);
paramMap.put("hospBarcode", hospBarcode);
paramMap.put("reportOdd", reportOdd);
paramMap.put("fileUrl", fileUrl);
String paramStr = JSON.toJSONString(paramMap);
String jsonMd5 = Md5Utils.strToMd5(paramStr);
//调用webService
Object[] objects = {jsonMd5, token, paramStr};
String[] params ={"md5","token","postJson"};
String result = null;
try {
result = getWebService(loginUrl,namespaceURI,webServiceDownMethod,params,objects);
} catch (Exception e) {
e.printStackTrace();
log.info("调用接口失败:"+objects.toString());
}
return result;
}
/**
* 登录
* @param siteCode
* @param customerCode
* @param collectionLocation
* @return
*/
private String getTokenByCheckKey(String siteCode,String customerCode,String collectionLocation){
//组织协定秘钥secretKey
SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
String toDay = fmt.format(new Date());
//判断token是否存在
String token = tokenCacheMap.get(toDay);
if(!StringUtils.isEmpty(token)){
//取缓存
return token;
}
String secretKey = collectionLocation + customerCode + siteCode + toDay;
//MD5转换
String secretKeyMd5 = Md5Utils.strToMd5(secretKey);
//调用第三方webService接口登录
//实例化org.apache.axis.client.Service对象
//创建service调用对象
Object[] objects = {siteCode, customerCode, collectionLocation, secretKeyMd5};
String[] params ={"siteCode","customerCode","collectionLocation","secretKey"};
String result = getWebService(loginUrl,namespaceURI,webServiceMethod,params,objects);
ObjectMapper objectMapper = new ObjectMapper();
//解析
try {
// 将JSON字符串转换为Map对象
JsonNode jsonNode = objectMapper.readTree(result);
token = jsonNode.get("result").get("token").asText();
} catch (IOException e) {
e.printStackTrace();
}
if(!StringUtils.isEmpty(token)) {
//存缓存
tokenCacheMap.clear();
tokenCacheMap.put(toDay, token);
}
return token;
}
/**
* 调用webService
* @return
*/
private String getWebService(String webServiceUrl,String namespaceURI,String webServiceMethod,String[] params,Object[] objects ){
try {
Service service = new Service();
Call call;
call=(Call) service.createCall();
QName opAddEntry = new QName(namespaceURI, webServiceMethod); //设置命名空间和需要调用的方法名
call.setTargetEndpointAddress(webServiceUrl); //设置请求路径
call.setOperationName(webServiceMethod); //调用的方法名
call.setTimeout(Integer.valueOf(2000)); //设置请求超时
//按顺序设置请求参数、请求类型,多个需多添加参数
for(String param : params){
call.addParameter(param, XMLType.XSD_STRING, ParameterMode.IN);
}
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
return (String) call.invoke(opAddEntry,objects);
} catch (ServiceException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
}
return null;
}
}