2026-01-26 16:11:34 +08:00

250 lines
9.5 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.czlis.interfaceCommon.utils;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.czlis.common.annotation.Async;
import com.czlis.common.annotation.Log;
import com.czlis.common.asyncmanager.AsyncManager;
import com.czlis.common.config.RuoYiConfig;
import com.czlis.common.core.domain.entity.SysOperLog;
import com.czlis.common.core.domain.entity.SysUser;
import com.czlis.common.core.domain.model.LoginUser;
import com.czlis.common.core.text.Convert;
import com.czlis.common.enums.BusinessStatus;
import com.czlis.common.enums.BusinessType;
import com.czlis.common.utils.ExceptionUtil;
import com.czlis.common.utils.SecurityUtils;
import com.czlis.common.utils.ServletUtils;
import com.czlis.common.utils.StringUtils;
import com.czlis.common.utils.ip.AddressUtils;
import com.czlis.common.utils.ip.IpUtils;
import com.czlis.common.utils.spring.SpringUtils;
import com.czlis.interfaceCommon.constants.LisinterfaceNameConstants;
import com.czlis.common.core.domain.Result;
import com.czlis.interfaceCommon.mapper.LisInterfaceMapper;
import com.czlis.interfaceCommon.service.ISysOperLogService;
import com.czlis.interfaceCommon.service.LisInterfaceType;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimerTask;
@Slf4j
@Component
public class LisInterfaceUtil {
@Resource
LisInterfaceMapper lisInterfaceMapper;
@Resource
CommonUtil commonUtil;
/**
* 接口调用
* @param type
* @param
* @return
*/
@Log(title = "接口调用",businessType = BusinessType.INTERFACE_INVOKE)
public Result invokeLisInterface(String type, LisInterfaceType lisInterfaceType) {
String interfaceType = commonUtil.getComOptValue("HISOPT","SP_INTER");
log.info("开始调用{}接口,入参:{}",type,interfaceType);
String inValue = lisInterfaceType.serializationJson();
log.info("接口入参:{}",inValue);
switch (interfaceType){
case "2": //存储过程
return invokeSP(type, inValue);
case "3": //http方式
return invokeHttp(type, inValue);
}
return new Result("-1","无效的接口方式,请检查参数设置!!");
}
/**
* 内部写日志的接口调用
* @param type
* @param lisInterfaceType
* @param userId
* @return
*/
public Result invokeLisInterfaceLog(String type, LisInterfaceType lisInterfaceType,String userId) {
try{
String interfaceType = commonUtil.getComOptValue("HISOPT","SP_INTER");
log.info("开始调用{}接口,入参:{}",type,interfaceType);
String inValue = lisInterfaceType.serializationJson();
log.info("接口入参:{}",inValue);
switch (interfaceType){
case "2": //存储过程
Result result = invokeSP(type, inValue);
insertDBLog(userId,"0","",JSONUtil.toJsonStr(lisInterfaceType));
return result;
case "3": //http方式
Result result1 = invokeHttp(type, inValue);
insertDBLog(userId,"0","",JSONUtil.toJsonStr(lisInterfaceType));
return result1;
}
}catch (Exception e){
e.printStackTrace();
log.error("调用lis接口报错:",e);
insertDBLog(userId,"-1",e.getMessage(),JSONUtil.toJsonStr(lisInterfaceType));
}
return new Result("-1","无效的接口方式,请检查参数设置!!");
}
/**
* 存储过程方式调用接口
* @param type
* @param inValue
* @return
*/
@Transactional(rollbackFor = Exception.class)
public Result invokeSP(String type,String inValue){
//为了兼容老接口,存储过程方式使用的是xml,这里需要把入参转成xml格式
String xmlStr = changeXml(inValue);
Map<String,Object> map = new HashMap<>();
map.put("invalue",xmlStr);
map.put("retcode","");
map.put("retmsg","");
switch (type){
case LisinterfaceNameConstants.GETREQINTERFACE:
lisInterfaceMapper.sp_lis_getreqinterface(map);
break;
case LisinterfaceNameConstants.SETDICT:
lisInterfaceMapper.sp_lis_setdict(map);
break;
case LisinterfaceNameConstants.SETREPORT:
lisInterfaceMapper.sp_lis_setreport(map);
break;
case LisinterfaceNameConstants.SETREQSTATUS:
lisInterfaceMapper.sp_lis_setreqstatus(map);
break;
case LisinterfaceNameConstants.DAYMESSAGE:
lisInterfaceMapper.sp_lis_daymessage(map);
break;
}
String retCode = String.valueOf(map.get("retcode"));
String retMsg = String.valueOf(map.get("retmsg"));
log.info("调用存储过程{}返回值:{},{}",type,retCode,retMsg);
if(retCode.equals("0")){
return new Result("0","调用接口成功!"+retMsg);
}else{
return new Result(retCode,"调用接口失败!"+retMsg);
}
}
/**
* http方式调用接口
* @param transCode
* @param jsonStr
* @return
*/
private Result invokeHttp(String transCode,String jsonStr){
String interfaceUrl = RuoYiConfig.getInterfaceUrl();
if(interfaceUrl == null || "".equals(interfaceUrl)) return new Result("-1","没有配置地址,请检查interfaceUrl配置项!!!");
interfaceUrl = interfaceUrl+"/"+transCode;
String retMsg = "";
try {
HttpRequest httpRequest = HttpRequest.post(interfaceUrl);
httpRequest.header("Content-Type","application/json");
HttpResponse httpResponse = httpRequest.body(jsonStr).execute();
int status = httpResponse.getStatus();
retMsg = httpResponse.body();
if (status != 200) {
log.info("调用http接口失败:"+retMsg);
return new Result("-1","调用http接口失败:"+retMsg);
}
}catch (Exception e){
e.printStackTrace();
log.error("调用lis的http接口出错:",e);
return new Result("-1","调用http接口失败:"+e.getMessage());
}
log.info("调用http接口成功类型:{},入参:{}/r/n,出参:{}",transCode,jsonStr,retMsg);
String message="";
String resultCode="";
// 检查输入是否为空
if (JSONUtil.isJson(retMsg)) {
JSONObject json = JSONUtil.parseObj(retMsg);
// 使用安全的方式获取值
message = json.getStr("message");
resultCode= json.getStr("resultCode");
} else {
// 处理非JSON格式的情况
}
return new Result(resultCode,"调用接口成功!",message);
}
/**
* 把json格式转换成xml格式
* @param inValue
* @return
*/
private String changeXml(String inValue){
JSONObject jsonObj = JSONUtil.parseObj(inValue);
Map<String,Object> map = jsonObj.toBean(Map.class);
return XmlUtil.mapToXmlStr(map, "Request");
}
public void insertDBLog(String userId,String status,String errorMsg,String requestParam){
try {
// *========数据库日志=========*//
SysOperLog operLog = new SysOperLog();
operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
// 请求的地址
String ip = IpUtils.getIpAddr();
operLog.setOperIp(ip);
operLog.setOperName(userId);
operLog.setOperUrl(StringUtils.substring(ServletUtils.getRequest().getRequestURI(), 0, 255));
// 设置action动作
operLog.setBusinessType(10);
// 设置标题
operLog.setTitle("接口调用-住院采血");
// 设置操作人类别
operLog.setOperatorType(1);
// 获取参数的信息,传入到数据库中。
operLog.setOperParam(requestParam);
//返回值
operLog.setJsonResult("返回值:"+status+";返回信息:"+errorMsg);
operLog.setStatus(Integer.parseInt(status));
operLog.setErrorMsg(errorMsg);
// 设置方法名称
operLog.setMethod(this.getClass().getName() + ".invokeLisInterfaceLog()");
// 设置请求方式
operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
// 设置消耗时间
//operLog.setCostTime(System.currentTimeMillis() - TIME_THREADLOCAL.get());
// 保存数据库
AsyncManager.me().execute(recordOper(operLog));
} catch (Exception exp) {
// 记录本地异常日志
log.error("异常信息:{}", exp.getMessage());
exp.printStackTrace();
}
}
public static TimerTask recordOper(final SysOperLog operLog) {
return new TimerTask() {
@Override
public void run() {
// 远程查询操作地点
operLog.setOperLocation(AddressUtils.getRealAddressByIP(operLog.getOperIp()));
SpringUtils.getBean(ISysOperLogService.class).insertOperlog(operLog);
}
};
}
}