package com.czlis.interfaceCommon.utils; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.czlis.common.config.RuoYiConfig; import com.czlis.common.core.domain.Result; import com.czlis.interfaceCommon.mapper.LisInterfaceMapper; 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.Map; @Slf4j @Component public class LisInterfaceUtil { @Resource LisInterfaceMapper lisInterfaceMapper; public Result invokeLisInterface(String type,String inValue) { String interfaceType = RuoYiConfig.getInterfaceType(); switch (interfaceType){ case "1": //存储过程 return invokeSP(type, inValue); case "2": //http方式 return invokeHttp(type, inValue); } return new Result("-1","无效的接口方式,请检查配置文件的interfaceType!!"); } /** * 存储过程方式调用接口 * @param type * @param inValue * @return */ @Transactional(rollbackFor = Exception.class) public Result invokeSP(String type,String inValue){ Map map = new HashMap<>(); map.put("invalue",inValue); map.put("retcode",""); map.put("retmsg",""); switch (type){ case "getreqinterface": lisInterfaceMapper.sp_lis_getreqinterface(map); break; case "setdict": lisInterfaceMapper.sp_lis_setdict(map); break; case "setreport": lisInterfaceMapper.sp_lis_setreport(map); break; case "setreqstatus": lisInterfaceMapper.sp_lis_setreqstatus(map); break; case "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","调用接口成功!"); }else{ return new Result("-1","调用接口失败!"+retMsg); } } /** * http方式调用接口 * @param transCode * @param jsonStr * @return */ public 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("0","调用接口成功!",retMsg); } }