153 lines
5.4 KiB
Java
Raw Normal View History

2025-06-24 18:01:25 +08:00
package com.czlis.interfaceCommon.utils;
2025-05-21 16:19:52 +08:00
2025-06-27 13:05:23 +08:00
import cn.hutool.core.util.XmlUtil;
2025-05-21 16:19:52 +08:00
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
2025-06-27 13:05:23 +08:00
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
2025-08-01 17:19:23 +08:00
import com.czlis.common.annotation.Async;
import com.czlis.common.annotation.Log;
2025-05-21 16:19:52 +08:00
import com.czlis.common.config.RuoYiConfig;
2025-08-01 17:19:23 +08:00
import com.czlis.common.enums.BusinessType;
2025-07-01 11:52:54 +08:00
import com.czlis.interfaceCommon.constants.LisinterfaceNameConstants;
2025-05-21 16:19:52 +08:00
import com.czlis.common.core.domain.Result;
import com.czlis.interfaceCommon.mapper.LisInterfaceMapper;
2025-08-15 17:39:40 +08:00
import com.czlis.interfaceCommon.service.LisInterfaceType;
2025-05-21 16:19:52 +08:00
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.HashMap;
2025-07-24 17:55:06 +08:00
import java.util.List;
2025-05-21 16:19:52 +08:00
import java.util.Map;
2025-06-27 13:05:23 +08:00
2025-05-21 16:19:52 +08:00
@Slf4j
@Component
public class LisInterfaceUtil {
@Resource
LisInterfaceMapper lisInterfaceMapper;
2025-06-27 13:05:23 +08:00
@Resource
CommonUtil commonUtil;
2025-05-21 16:19:52 +08:00
2025-06-27 13:05:23 +08:00
/**
2025-08-01 17:19:23 +08:00
* 接口调用
2025-06-27 13:05:23 +08:00
* @param type
2025-08-15 17:39:40 +08:00
* @param
2025-06-27 13:05:23 +08:00
* @return
*/
2025-08-01 17:19:23 +08:00
@Log(title = "接口调用",businessType = BusinessType.INTERFACE_INVOKE)
2025-08-15 17:39:40 +08:00
public Result invokeLisInterface(String type, LisInterfaceType lisInterfaceType) {
2025-06-27 13:05:23 +08:00
String interfaceType = commonUtil.getComOptValue("HISOPT","SP_INTER");
2025-08-01 17:19:23 +08:00
log.info("开始调用{}接口,入参:{}",type,interfaceType);
2025-08-15 17:39:40 +08:00
String inValue = lisInterfaceType.serializationJson();
log.info("接口入参:{}",inValue);
2025-05-21 16:19:52 +08:00
switch (interfaceType){
2025-06-27 13:05:23 +08:00
case "2": //存储过程
2025-05-21 16:19:52 +08:00
return invokeSP(type, inValue);
2025-06-27 13:05:23 +08:00
case "3": //http方式
2025-05-21 16:19:52 +08:00
return invokeHttp(type, inValue);
}
2025-06-27 13:05:23 +08:00
return new Result("-1","无效的接口方式,请检查参数设置!!");
2025-05-21 16:19:52 +08:00
}
/**
* 存储过程方式调用接口
* @param type
* @param inValue
* @return
*/
2025-06-27 13:05:23 +08:00
//@Transactional(rollbackFor = Exception.class)
private Result invokeSP(String type,String inValue){
2025-08-15 12:15:19 +08:00
2025-06-27 13:05:23 +08:00
//为了兼容老接口,存储过程方式使用的是xml,这里需要把入参转成xml格式
String xmlStr = changeXml(inValue);
2025-08-15 12:15:19 +08:00
Map<String,Object> map = new HashMap<>();
2025-06-27 13:05:23 +08:00
map.put("invalue",xmlStr);
2025-05-21 16:19:52 +08:00
map.put("retcode","");
map.put("retmsg","");
switch (type){
2025-06-27 13:05:23 +08:00
case LisinterfaceNameConstants.GETREQINTERFACE:
2025-05-21 16:19:52 +08:00
lisInterfaceMapper.sp_lis_getreqinterface(map);
break;
2025-06-27 13:05:23 +08:00
case LisinterfaceNameConstants.SETDICT:
2025-05-21 16:19:52 +08:00
lisInterfaceMapper.sp_lis_setdict(map);
break;
2025-06-27 13:05:23 +08:00
case LisinterfaceNameConstants.SETREPORT:
2025-05-21 16:19:52 +08:00
lisInterfaceMapper.sp_lis_setreport(map);
break;
2025-06-27 13:05:23 +08:00
case LisinterfaceNameConstants.SETREQSTATUS:
2025-05-21 16:19:52 +08:00
lisInterfaceMapper.sp_lis_setreqstatus(map);
break;
2025-06-27 13:05:23 +08:00
case LisinterfaceNameConstants.DAYMESSAGE:
2025-05-21 16:19:52 +08:00
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")){
2025-07-24 17:55:06 +08:00
return new Result("0","调用接口成功!",retMsg);
2025-05-21 16:19:52 +08:00
}else{
2025-07-24 17:55:06 +08:00
return new Result(retCode,"调用接口失败!",retMsg);
2025-05-21 16:19:52 +08:00
}
}
/**
* http方式调用接口
* @param transCode
* @param jsonStr
* @return
*/
2025-06-27 13:05:23 +08:00
private Result invokeHttp(String transCode,String jsonStr){
2025-05-21 16:19:52 +08:00
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);
2025-08-01 17:19:23 +08:00
return new Result("-1","调用http接口失败:"+e.getMessage());
2025-05-21 16:19:52 +08:00
}
2025-08-01 17:19:23 +08:00
log.info("调用http接口成功类型:{},入参:{}/r/n,出参:{}",transCode,jsonStr,retMsg);
2025-07-24 17:55:06 +08:00
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);
2025-05-21 16:19:52 +08:00
}
2025-06-27 13:05:23 +08:00
2025-08-01 17:19:23 +08:00
/**
* 把json格式转换成xml格式
* @param inValue
* @return
*/
2025-08-15 12:15:19 +08:00
private String changeXml(String inValue){
2025-06-27 13:05:23 +08:00
JSONObject jsonObj = JSONUtil.parseObj(inValue);
Map<String,Object> map = jsonObj.toBean(Map.class);
return XmlUtil.mapToXmlStr(map, "Request");
}
2025-05-21 16:19:52 +08:00
}