diff --git a/pom.xml b/pom.xml index 6eed85b..c3f0566 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,12 @@ 3.9 + + com.alibaba + fastjson + 1.2.83 + + org.projectlombok lombok diff --git a/src/main/java/com/win/qad/config/ShellConfig.java b/src/main/java/com/win/qad/config/ShellConfig.java index 110eff2..c137845 100644 --- a/src/main/java/com/win/qad/config/ShellConfig.java +++ b/src/main/java/com/win/qad/config/ShellConfig.java @@ -10,9 +10,8 @@ import org.springframework.stereotype.Component; @ConfigurationProperties(prefix = "shell") public class ShellConfig { + //linux配置信息 private ShellVo jlht; private ShellVo jlht2; - //linux配置信息 -// private Map linux; } diff --git a/src/main/java/com/win/qad/controller/ShellController.java b/src/main/java/com/win/qad/controller/ShellController.java index 9b63a7e..4b60cbe 100644 --- a/src/main/java/com/win/qad/controller/ShellController.java +++ b/src/main/java/com/win/qad/controller/ShellController.java @@ -1,5 +1,6 @@ package com.win.qad.controller; +import com.alibaba.fastjson.JSONObject; import com.jcraft.jsch.JSchException; import com.jcraft.jsch.Session; import com.win.qad.common.CommonResult; @@ -7,8 +8,11 @@ import com.win.qad.config.ShellConfig; import com.win.qad.exception.GlobalErrorCodeConstants; import com.win.qad.utils.ProfileUtil; import com.win.qad.utils.shell.ShellUtil; +import com.win.qad.utils.shell.ShellVo; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.DigestUtils; import org.springframework.web.bind.annotation.PostMapping; @@ -18,14 +22,19 @@ import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; +import static com.win.qad.exception.GlobalErrorCodeConstants.EXECUTE_FAIL; + @Slf4j @RestController @RequestMapping("/shell") public class ShellController { + private static final Logger logger = LoggerFactory.getLogger("sys-user"); + @Autowired private ShellUtil shellUtil; @Autowired @@ -40,7 +49,7 @@ public class ShellController { */ @PostMapping("/api") @SuppressWarnings("unchecked") - public CommonResult api(HttpServletRequest request, @RequestBody String body) { + public CommonResult api(HttpServletRequest request, @RequestBody String body) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { String interfaceName = request.getHeader("interface"); String sign = request.getHeader("sign"); String timeStr = request.getHeader("timestamp"); @@ -55,11 +64,6 @@ public class ShellController { } String tmp = interfaceName + body + timestamp; String computeSign = DigestUtils.md5DigestAsHex(tmp.getBytes()); - log.info("{}, interfaceName: {}", "interfaceName", interfaceName); - log.info("{}, sign: {}", "sign", sign); - log.info("{}, timestamp: {}", "timestamp", timestamp); - log.info("{}, tmp: {}", "tmp", tmp); - log.info("{}, computeSign: {}", "computeSign", computeSign); long tenTimestamp = timestamp + (10 * 60 * 1000); // 计算10分钟后的时间戳 long currentTimestamp = System.currentTimeMillis(); // 获取当前时间戳 //过期 @@ -71,26 +75,68 @@ public class ShellController { if(!activeProfile.contains("dev") && !StringUtils.equals(sign, computeSign.toUpperCase())) { return CommonResult.error(GlobalErrorCodeConstants.SIGN_ERROR); } - try { - Method method = this.getClass().getMethod(interfaceName, String.class); - return (CommonResult) method.invoke(this, body); - } catch (NoSuchMethodException e) { - return CommonResult.error(GlobalErrorCodeConstants.INTERFACE_ERROR); - } catch (Exception e) { - return CommonResult.error(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR); - } + Method method = this.getClass().getMethod(interfaceName, String.class); + return (CommonResult) method.invoke(this, body); } - public CommonResult jlht(String body) throws JSchException, IOException { - Session session = shellUtil.getShellSession(shellConfig.getJlht()); + public CommonResult itemmastercreate(String body) throws JSchException, IOException { + Session session = shellUtil.getShellSession(shellConfig.getJlht2()); String result = shellUtil.execute(session, "cd /opt && ls -l"); return CommonResult.success(result); } - public CommonResult jlht2(String body) throws JSchException, IOException { + public CommonResult itemdetailcreate(String body) throws JSchException, IOException { Session session = shellUtil.getShellSession(shellConfig.getJlht2()); String result = shellUtil.execute(session, "cd /opt && ls -l"); return CommonResult.success(result); } + public CommonResult discretepo(String body) throws JSchException, IOException { + JSONObject jsonObject = JSONObject.parseObject(body); + jsonObject = JSONObject.parseObject(jsonObject.getString("dsDescreteOrder")); + ShellVo shellVo; + if(jsonObject.getString("domain") != null && jsonObject.getString("domain").equals("JLHT2")) { + shellVo = shellConfig.getJlht2(); + } else { + shellVo = shellConfig.getJlht(); + } + Session session = shellUtil.getShellSession(shellVo); + //生成traceid + String traceid = jsonObject.getString("traceid"); + jsonObject.remove("traceid"); + jsonObject.remove("company"); + jsonObject.remove("domain"); + String inJson = "{\"dsDescreteOrder\":"+ jsonObject+"}"; + log.info("inJson: {}", inJson); + //写入json + String result = shellUtil.execute(session, "touch /home/mfg/work/cim/po/"+traceid+".json && echo '"+inJson+"'>/home/mfg/work/cim/po/"+traceid+".json"); + log.info("write json result : " + result); + //写入dat.json + String datJson = "\"/home/mfg/work/cim/po/"+traceid+".json\" \"xxdspoapi.p\" \"/home/mfg/work/out/po/"+traceid+".out.log\" \""+shellVo.getQadUser()+"\" \""+shellVo.getQadPassword()+"\"\r\n\""+shellVo.getQadDomain()+"\""; + log.info("datJson: {}", datJson); + result = shellUtil.execute(session, "touch /home/mfg/work/cim/po/"+traceid+".dat && echo '"+datJson+"'>/home/mfg/work/cim/po/"+traceid+".dat"); + log.info("write dat json result : " + result); + //写入执行文件 + String pJson = "output to /home/mfg/work/out/po/" + traceid + ".run.log.\r\n" + + "input from /home/mfg/work/cim/po/" + traceid + ".dat.\r\n" + + "run /home/mfg/work/scripts/xxcommon01.p.\r\n" + + "input close.\r\n" + + "output close."; + log.info("pJson: {}", pJson); + result = shellUtil.execute(session, "touch /home/mfg/work/cim/po/"+traceid+".p && echo '"+pJson+"'>/home/mfg/work/cim/po/"+traceid+".p"); + log.info("write p result : " + result); + //执行QAD脚本 + result = shellUtil.execute(session, "/home/mfg/work/scripts/client-ch-cimload /home/mfg/work/cim/po/"+traceid+".p"); + log.info("execute client-ch-loaddata : " + result); + result = shellUtil.execute(session, "cat /home/mfg/work/out/po/" + traceid + ".run.out.json"); + log.info("cat execute log : " + result); + if(result.indexOf("\"SUCCESS\"") > 0) { + //移动执行结果文件 + shellUtil.execute(session, "mv /home/mfg/work/out/po/" + traceid + ".run.out.json /home/mfg/work/success/po/" + traceid + ".run.out.json"); + return CommonResult.success(traceid); + } + shellUtil.execute(session, "mv /home/mfg/work/out/po/" + traceid + ".run.out.json /home/mfg/work/err/po/" + traceid + ".run.out.json"); + return CommonResult.error(EXECUTE_FAIL.getCode(), result); + } + } diff --git a/src/main/java/com/win/qad/exception/GlobalErrorCodeConstants.java b/src/main/java/com/win/qad/exception/GlobalErrorCodeConstants.java index 5ff5e41..2319aa8 100644 --- a/src/main/java/com/win/qad/exception/GlobalErrorCodeConstants.java +++ b/src/main/java/com/win/qad/exception/GlobalErrorCodeConstants.java @@ -34,6 +34,7 @@ public interface GlobalErrorCodeConstants { ErrorCode INTERNAL_SERVER_ERROR = new ErrorCode(500, "系统异常"); ErrorCode NOT_IMPLEMENTED = new ErrorCode(501, "功能未实现/未开启"); + ErrorCode EXECUTE_FAIL = new ErrorCode(502, "执行失败"); // ========== 自定义错误段 ========== ErrorCode REPEATED_REQUESTS = new ErrorCode(900, "重复请求,请稍后重试"); // 重复请求 diff --git a/src/main/java/com/win/qad/handler/GlobalExceptionHandler.java b/src/main/java/com/win/qad/handler/GlobalExceptionHandler.java new file mode 100644 index 0000000..7c469a5 --- /dev/null +++ b/src/main/java/com/win/qad/handler/GlobalExceptionHandler.java @@ -0,0 +1,63 @@ +package com.win.qad.handler; + +import com.win.qad.common.CommonResult; +import com.win.qad.exception.ServiceException; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +import javax.servlet.http.HttpServletRequest; + +import static com.win.qad.exception.GlobalErrorCodeConstants.INTERFACE_ERROR; +import static com.win.qad.exception.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR; + +/** + * 全局异常处理器,将 Exception 翻译成 CommonResult + 对应的异常编号 + * + * @author 闻荫源码 + */ +@Slf4j +@RestControllerAdvice +public class GlobalExceptionHandler { + + /** + * 接口未找到业务异常 NoSuchMethodException + */ + @ExceptionHandler(NoSuchMethodException.class) + public CommonResult noSuchMethodExceptionHandler(NoSuchMethodException e) { + log.error("[serviceExceptionHandler]", e); + return CommonResult.error(INTERFACE_ERROR.getCode(), INTERFACE_ERROR.getMsg()); + } + + /** + * 处理业务异常 ServiceException + * + * 例如说,商品库存不足,用户手机号已存在。 + */ + @ExceptionHandler(ServiceException.class) + public CommonResult serviceExceptionHandler(ServiceException ex, HttpServletRequest request) { + log.error("[serviceExceptionHandler]", ex); + return CommonResult.error(INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg()); + } + + /** + * 处理业务异常 ServiceException + * + * 例如说,商品库存不足,用户手机号已存在。 + */ + @ExceptionHandler(RuntimeException.class) + public CommonResult runtimeExceptionHandler(ServiceException ex, HttpServletRequest request) { + log.error("[runtimeExceptionHandler]", ex); + return CommonResult.error(INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg()); + } + + /** + * 处理系统异常,兜底处理所有的一切 + */ + @ExceptionHandler(Exception.class) + public CommonResult defaultExceptionHandler(Throwable ex, HttpServletRequest request) { + log.error("[defaultExceptionHandler]", ex); + return CommonResult.error(INTERNAL_SERVER_ERROR.getCode(), INTERNAL_SERVER_ERROR.getMsg()); + } + +} diff --git a/src/main/java/com/win/qad/utils/shell/ShellUtil.java b/src/main/java/com/win/qad/utils/shell/ShellUtil.java index b9136f6..f43077c 100644 --- a/src/main/java/com/win/qad/utils/shell/ShellUtil.java +++ b/src/main/java/com/win/qad/utils/shell/ShellUtil.java @@ -45,7 +45,9 @@ public class ShellUtil { line.trim(); result = result + line + "\r\n"; } - result = result.substring(0, result.length() - 2); + if(result.length() > 2) { + result = result.substring(0, result.length() - 2); + } // 关闭SSH Channel channel.disconnect(); return result; diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 9bd98d0..d5c3d75 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -16,28 +16,19 @@ server: # Tomcat启动初始化的线程数,默认值10 min-spare: 10 -sfms: - username: admin #用户名 - password: 123456 #密码 - purchase-order: http://localhost:12080/admin-api/wms/purchase-main/create #采购订单 - access-token: http://localhost:12080/admin-api/system/oauth2/token #登陆token - refresh-token: http://localhost:12080/admin-api/system/auth/refresh-token #刷新token - +# 日志配置 logging: file: path: logs level: com.win: debug org.springframework: warn + shell: jlht: qad-domain: JLHT company-code: CCWININ server-id: master - #ip: dev.ccwin-in.com - #port: 23022 - #linux-id: root - #linux-password: Microsoft@2022 ip: 222.169.228.163 port: 6122 linux-id: mfg diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml index c191ed4..66fd880 100644 --- a/src/main/resources/logback-spring.xml +++ b/src/main/resources/logback-spring.xml @@ -87,7 +87,7 @@ - + @@ -105,4 +105,4 @@ - \ No newline at end of file + \ No newline at end of file