王佳豪
2021-06-26 718f604deb342b0bee6c588bb44e22ced3371fb8
src/main/java/com/slcf/filter/UpdateThread.java
@@ -5,11 +5,16 @@
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import javax.annotation.Resource;
import com.slcf.pojo.SapRequestLogBean;
import com.slcf.service.SapRequestLogService;
import net.sf.json.JSONString;
import org.activiti.engine.impl.transformer.IntegerToString;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -40,6 +45,8 @@
//   WaitPakOutService waitPakOutService;
//   @Resource
//   private static WaitPakInServiceImpl waitPakInService;
   @Autowired
   SapRequestLogService sapRequestLogService;
   
   Logger logger=Logger.getLogger(UpdateThread.class);
   
@@ -66,9 +73,11 @@
      waitPakOutService = getWaitPakOutService();
      while(true) {
         try {
            updateWaitPakInData();
            sendPickPrintCmd();
            updateWaitPakOutData();
            updateWaitPakInData(); // 入库
            sendPickPrintCmd(); // 打印
            updateWaitPakOutData(); // 出库
            // 清空2天前的日志
            sapRequestLogService.clearSapLog();
//            updateWaitCheckData();
            Thread.sleep(5000);
         } catch (InterruptedException e) {
@@ -105,19 +114,33 @@
        String encodedPassword;
        String token="";
        String session_value="";
        HttpURLConnection connection1 = null;
      try {
         encodedPassword = base.encode(input.getBytes("UTF-8"));
         URL url1 = new URL(httpUrl);
           HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection();
           connection1 = (HttpURLConnection) url1.openConnection();
           connection1.setDoOutput(true);
           connection1.setDoInput(true);
           connection1.setConnectTimeout(30000);
           connection1.setReadTimeout(30000);
           connection1.setRequestMethod("GET");
           connection1.setRequestProperty("Authorization", "Basic " + encodedPassword);
           connection1.setRequestProperty("x-csrf-token","Fetch");
           String sessionCookie = connection1.getHeaderField("Set-Cookie");
           if(sessionCookie==null || sessionCookie.length()<1)
           {
              logger.error("SAP ERROR-----调用上架完成接口,get请求获取cookie失败");
            // SAP INFO-----调用上架完成接口,日志记录
            SapRequestLogBean logInfo = new SapRequestLogBean();
            logInfo.setMatnr(waitPakIn.getMatnr());
            logInfo.setRequest("获取cookie失败,cookie值:" + sessionCookie);
            logInfo.setResponse("SAP ERROR-----调用上架完成接口,get请求获取cookie失败");
            logInfo.setCreate_time(new Date());
            logInfo.setType(2);
            sapRequestLogService.addSapLog(logInfo);
              return result;
           }
           String[] sessionId = sessionCookie.split(";");
@@ -126,7 +149,20 @@
//           Map<String, List<String>> map = connection1.getHeaderFields();
//           token = map.get("x-csrf-token").toString();
      } catch (Exception e1) {
         e1.printStackTrace();
         logger.error("SAP ERROR-----调用上架完成接口1,get请求获取cookie失败。" + e1.getMessage());
         // SAP INFO-----调用上架完成接口,日志记录
         SapRequestLogBean logInfo = new SapRequestLogBean();
         logInfo.setMatnr(waitPakIn.getMatnr());
         logInfo.setRequest("获取cookie失败");
         logInfo.setResponse("SAP ERROR-----调用上架完成接口1,get请求获取cookie失败。" + e1.getMessage());
         logInfo.setCreate_time(new Date());
         logInfo.setType(2);
         sapRequestLogService.addSapLog(logInfo);
//         e1.printStackTrace();
      }finally {
         if(connection1!=null){
            connection1.disconnect();
         }
      }
        
      try {
@@ -150,16 +186,19 @@
         byte[] buff = jsObj.toString().getBytes();
         int buffLen = buff.length;
         //创建连接
         HttpURLConnection connection = null;
           try {
//              String input = "ZHANGX:654321";
//               BASE64Encoder base = new BASE64Encoder();
//               String encodedPassword = base.encode(input.getBytes("UTF-8"));
               
              URL url = new URL(httpUrl);
              HttpURLConnection connection = (HttpURLConnection) url.openConnection();
              connection = (HttpURLConnection) url.openConnection();
              connection.setDoOutput(true);
              connection.setDoInput(true);
              connection.setRequestMethod("POST");
              connection.setConnectTimeout(30000);
              connection.setReadTimeout(30000);
//              connection.setUseCaches(false);            
//              connection.setInstanceFollowRedirects(true);
//              connection.setRequestProperty("Content-Type","text/json;charset=gb2312");
@@ -207,11 +246,29 @@
                  }catch(Exception em) {
                  }
                  if(zstatus.equals("s") || message.contains("已经完全提供")) {//上报SAP成功,转历史档
                     // SAP INFO-----调用上架完成接口,日志记录
                     SapRequestLogBean logInfo = new SapRequestLogBean();
                     logInfo.setMatnr(waitPakIn.getMatnr());
                     logInfo.setRequest(jsObj.toString());
                     logInfo.setResponse(sb.toString());
                     logInfo.setCreate_time(new Date());
                     logInfo.setRemark("上架完成接口:" + message);
                     logInfo.setType(1);
                     sapRequestLogService.addSapLog(logInfo);
//                  if(zstatus.equals("s")) {
                     waitPakIn.setIo_status("Y");
                     waitPakInService.upWaitPakIn(waitPakIn);
                     waitPakInService.moveToLog(waitPakIn);
                  }else if(zstatus.equals("e")) {//上报SAP失败,更新io_status=X
                     // SAP INFO-----调用上架完成接口,日志记录
                     SapRequestLogBean logInfo = new SapRequestLogBean();
                     logInfo.setMatnr(waitPakIn.getMatnr());
                     logInfo.setRequest(jsObj.toString());
                     logInfo.setResponse(sb.toString());
                     logInfo.setCreate_time(new Date());
                     logInfo.setRemark("上架完成接口:" + message);
                     logInfo.setType(2);
                     sapRequestLogService.addSapLog(logInfo);
                     waitPakIn.setIo_status("X");
                     waitPakIn.setMemo(message);
                     waitPakInService.upWaitPakIn(waitPakIn);
@@ -227,6 +284,10 @@
            waitPakInService.upWaitPakIn(waitPakIn);
              logger.error("SAP Error sendPakInStatus--"+waitPakIn.getMatnr()+"---" + e.getMessage());
//            System.out.println(e.getMessage());
         }finally {
            if(connection!=null){
               connection.disconnect();
            }
         }
      }catch (Exception e) {
         waitPakIn.setIo_status("X");
@@ -265,12 +326,16 @@
        String encodedPassword;
        String token="";
        String session_value="";
        HttpURLConnection connection1 = null;
      try {
         encodedPassword = base.encode(input.getBytes("UTF-8"));
         URL url1 = new URL(httpUrl);
           HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection();
           connection1 = (HttpURLConnection) url1.openConnection();
           connection1.setDoOutput(true);
           connection1.setDoInput(true);
           connection1.setConnectTimeout(30000);
           connection1.setReadTimeout(30000);
           connection1.setRequestMethod("GET");
           connection1.setRequestProperty("Authorization", "Basic " + encodedPassword);
           connection1.setRequestProperty("x-csrf-token","Fetch");
@@ -278,15 +343,37 @@
           if(sessionCookie==null || sessionCookie.length()<1)
           {
              logger.error("调用下架完成接口,get请求获取cookie失败");
            // SAP INFO-----调用上架完成接口,日志记录
            SapRequestLogBean logInfo = new SapRequestLogBean();
            logInfo.setMatnr(waitPakOut.getMatnr());
            logInfo.setRequest("获取cookie值失败,cookie:" + sessionCookie);
            logInfo.setResponse("调用出库完成接口,get请求获取cookie失败");
            logInfo.setCreate_time(new Date());
            logInfo.setType(2);
            sapRequestLogService.addSapLog(logInfo);
              return result;
           }
           String[] sessionId = sessionCookie.split(";");
           session_value = sessionId[0];
           token = connection1.getHeaderField("x-csrf-token");
//           Map<String, List<String>> map = connection1.getHeaderFields();
//           token = map.get("x-csrf-token").toString();
      } catch (Exception e1) {
         e1.printStackTrace();
         logger.error("调用下架完成接口,get请求获取cookie失败" + e1.getMessage());
         // SAP INFO-----调用上架完成接口,日志记录
         SapRequestLogBean logInfo = new SapRequestLogBean();
         logInfo.setMatnr(waitPakOut.getMatnr());
         logInfo.setRequest("获取cookie失败");
         logInfo.setResponse("调用出库完成接口,get请求获取cookie失败");
         logInfo.setCreate_time(new Date());
         logInfo.setType(2);
         sapRequestLogService.addSapLog(logInfo);
//         e1.printStackTrace();
      }finally{
         if(connection1!=null){
            connection1.disconnect();
         }
      }
        
      try {
@@ -325,15 +412,18 @@
         byte[] buff = jsObj.toString().getBytes();
         int buffLen = buff.length;
         //创建连接
         HttpURLConnection connection = null;
           try {
//              String input = "ZHANGX:654321";
//               BASE64Encoder base = new BASE64Encoder();
//               String encodedPassword = base.encode(input.getBytes("UTF-8"));
               
              URL url = new URL(httpUrl);
              HttpURLConnection connection = (HttpURLConnection) url.openConnection();
              connection = (HttpURLConnection) url.openConnection();
              connection.setDoOutput(true);
              connection.setDoInput(true);
              connection.setConnectTimeout(30000);
              connection.setReadTimeout(30000);
              connection.setRequestMethod("POST");
      //        connection.setUseCaches(false);            
      //        connection.setInstanceFollowRedirects(true);
@@ -380,11 +470,29 @@
                  }catch(Exception em) {
                  }
                  if(zstatus.equals("s") || message.contains("已经完全提供") || message.contains("已确认")) {
                     // SAP INFO-----调用下架完成接口,日志记录
                     SapRequestLogBean logInfo = new SapRequestLogBean();
                     logInfo.setMatnr(waitPakOut.getMatnr());
                     logInfo.setRequest(jsObj.toString());
                     logInfo.setResponse(sb.toString());
                     logInfo.setCreate_time(new Date());
                     logInfo.setType(1);
                     logInfo.setRemark("出库完成接口:" + message);
                     sapRequestLogService.addSapLog(logInfo);
//                  if(zstatus.equals("s")) {//上报SAP成功,转历史档
                     waitPakOut.setIo_status("Y");
                     waitPakOutService.upWaitPakOut(waitPakOut,0);
                     waitPakOutService.moveToLog(waitPakOut);
                  }else if(zstatus.equals("e")) {//上报SAP失败,更新io_status=X
                     // SAP INFO-----调用下架完成接口,日志记录
                     SapRequestLogBean logInfo = new SapRequestLogBean();
                     logInfo.setMatnr(waitPakOut.getMatnr());
                     logInfo.setRequest(jsObj.toString());
                     logInfo.setResponse(sb.toString());
                     logInfo.setCreate_time(new Date());
                     logInfo.setType(2);
                     logInfo.setRemark("出库完成接口:" + message);
                     sapRequestLogService.addSapLog(logInfo);
                     waitPakOut.setIo_status("X");
                     waitPakOut.setMemo(message);
                     waitPakOutService.upWaitPakOut(waitPakOut,0);
@@ -401,6 +509,10 @@
            waitPakOutService.upWaitPakOut(waitPakOut,0);
              logger.error("出库通知档回报异常--"+waitPakOut.getMatnr() + "---" + e.getMessage());
//            System.out.println(e.getMessage());
         }finally{
            if(connection!=null){
               connection.disconnect();
            }
         }
      }catch (Exception e) {
         waitPakOut.setIo_status("X");
@@ -438,13 +550,17 @@
        String encodedPassword;
        String token="";
        String session_value="";
        HttpURLConnection connection1 = null;
      try {
//         logger.error("TQS-----调用打印接口" + waitPakOut.getMaktx());
//         logger.error("sendPrinCommand-----调用打印接口" + waitPakOut.getMaktx());
         encodedPassword = base.encode(input.getBytes("UTF-8"));
         URL url1 = new URL(httpUrl);
           HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection();
           connection1 = (HttpURLConnection) url1.openConnection();
           connection1.setDoOutput(true);
           connection1.setDoInput(true);
           connection1.setConnectTimeout(30000);
           connection1.setReadTimeout(30000);
           connection1.setRequestMethod("GET");
           connection1.setRequestProperty("Authorization", "Basic " + encodedPassword);
           connection1.setRequestProperty("x-csrf-token","Fetch");
@@ -452,6 +568,14 @@
           if(sessionCookie==null || sessionCookie.length()<1)
           {
              logger.error("调用打印接口,get请求获取cookie失败");
              // 调用sap打印接口,记录日志
            SapRequestLogBean logInfo = new SapRequestLogBean();
            logInfo.setMatnr(waitPakOut.getMatnr());
            logInfo.setRequest("调用打印接口,get请求获取cookie失败");
            logInfo.setResponse(sessionCookie);
            logInfo.setCreate_time(new Date());
            logInfo.setType(2);
            sapRequestLogService.addSapLog(logInfo);
              return result;
           }
           String[] sessionId = sessionCookie.split(";");
@@ -460,12 +584,24 @@
//           Map<String, List<String>> map = connection1.getHeaderFields();
//           token = map.get("x-csrf-token").toString();
      } catch (Exception e1) {
         logger.error("TQS1-----调用打印接口" + waitPakOut.getMaktx());
         logger.error("sendPrinCommand1-----调用打印接口请求cookie失败" + waitPakOut.getMaktx() + e1.getMessage());
         // 调用sap打印接口,记录日志
         SapRequestLogBean logInfo = new SapRequestLogBean();
         logInfo.setMatnr(waitPakOut.getMatnr());
         logInfo.setRequest("sendPrinCommand1-----调用打印接口请求cookie失败"  + waitPakOut.getMaktx());
         logInfo.setResponse(e1.getMessage());
         logInfo.setCreate_time(new Date());
         logInfo.setType(2);
         sapRequestLogService.addSapLog(logInfo);
         e1.printStackTrace();
      }finally{
         if(connection1!=null){
            connection1.disconnect();
         }
      }
        
      try {
         logger.error("TQS2-----调用打印接口" + waitPakOut.getMaktx());
//         logger.error("sendPrinCommand2-----开始调用打印接口" + waitPakOut.getMaktx());
         JSONObject jsObj = new JSONObject();
         JSONArray arr = new JSONArray();
         for(int i=0;i<1;i++) {
@@ -493,15 +629,18 @@
         byte[] buff = jsObj.toString().getBytes();
         int buffLen = buff.length;
         //创建连接
         HttpURLConnection connection = null;
           try {
//              String input = "ZHANGX:654321";
//               BASE64Encoder base = new BASE64Encoder();
//               String encodedPassword = base.encode(input.getBytes("UTF-8"));
               
              URL url = new URL(httpUrl);
              HttpURLConnection connection = (HttpURLConnection) url.openConnection();
              connection = (HttpURLConnection) url.openConnection();
              connection.setDoOutput(true);
              connection.setDoInput(true);
              connection.setConnectTimeout(30000);
              connection.setReadTimeout(30000);
              connection.setRequestMethod("POST");
      //        connection.setUseCaches(false);            
      //        connection.setInstanceFollowRedirects(true);
@@ -518,7 +657,7 @@
              OutputStream out = connection.getOutputStream();
              out.write(buff);
            out.close();
            logger.error("TQS3-----调用打印接口" + waitPakOut.getMaktx());
//            logger.error("sendPrinCommand3-----调用打印接口" + waitPakOut.getMaktx());
              //读取响应
              BufferedReader reader = new BufferedReader(new InputStreamReader(
                      connection.getInputStream()));
@@ -536,7 +675,7 @@
            JSONObject jsonItem = JSONObject.fromObject(Item);
            String results = jsonItem.get("results").toString();
            JSONArray array = JSONArray.fromObject(results);
            logger.error("TQS4-----调用打印接口" + waitPakOut.getMaktx());
            logger.error("sendPrinCommand4-----调用打印接口" + waitPakOut.getMaktx());
            if(array.size()>0) {
               for(int i=0;i<array.size();i++){
                  JSONObject job = array.getJSONObject(i);
@@ -552,11 +691,31 @@
                     waitPakOut.setAction(1);     //模式变为1单独确认
                     waitPakOut.setPrnstatus(2);  //打印完成
                     waitPakOutService.upWaitPakOut(waitPakOut,0);
//                     logger.error("调用打印接口成功,请求json数据--" + jsObj.toString());
                     logger.error("sendPrinCommand5---调用打印接口成功,请求json数据--" + jsObj.toString());
                     logger.info("sendPrinCommand5---调用打印接口成功,返回json数据--" + sb.toString());
                     // 调用sap打印接口,记录日志
                     SapRequestLogBean logInfo = new SapRequestLogBean();
                     logInfo.setMatnr(waitPakOut.getMatnr());
                     logInfo.setRequest(jsObj.toString());
                     logInfo.setResponse(sb.toString());
                     logInfo.setCreate_time(new Date());
                     logInfo.setRemark(message);
                     logInfo.setType(1);
                     sapRequestLogService.addSapLog(logInfo);
                  }else if(zstatus.equals("e")) {//上报SAP失败,更新io_status=X
                     waitPakOut.setPrnstatus(3);    //打印失败
                     waitPakOut.setMemo(message);
                     waitPakOutService.upWaitPakOut(waitPakOut,0);
                     logger.error("sendPrinCommand6---调用打印接口失败,请求json数据--" + message);
                     // 调用sap打印接口,记录日志
                     SapRequestLogBean logInfo = new SapRequestLogBean();
                     logInfo.setMatnr(waitPakOut.getMatnr());
                     logInfo.setRequest(jsObj.toString());
                     logInfo.setResponse(sb.toString());
                     logInfo.setCreate_time(new Date());
                     logInfo.setRemark(message);
                     logInfo.setType(2);
                     sapRequestLogService.addSapLog(logInfo);
                  }
               }
            }
@@ -565,14 +724,36 @@
              waitPakOut.setPrnstatus(3);    //打印失败
            waitPakOut.setMemo(e.getMessage());
            waitPakOutService.upWaitPakOut(waitPakOut,0);
              logger.error("调用打印接口回报异常--"+waitPakOut.getMatnr() + "---" + e.getMessage());
              logger.error("sendPrinCommand7---调用打印接口回报异常--"+waitPakOut.getMatnr() + "---" + e.getMessage());
            // 调用sap打印接口,记录日志
            SapRequestLogBean logInfo = new SapRequestLogBean();
            logInfo.setMatnr(waitPakOut.getMatnr());
            logInfo.setRequest(jsObj.toString());
            logInfo.setResponse("sendPrinCommand7---调用打印接口回报异常--"+waitPakOut.getMatnr() + "---");
            logInfo.setCreate_time(new Date());
            logInfo.setRemark(e.getMessage());
            logInfo.setType(2);
            sapRequestLogService.addSapLog(logInfo);
//            System.out.println(e.getMessage());
         }finally{
            if(connection!=null){
               connection.disconnect();
            }
         }
      }catch (Exception e) {
         waitPakOut.setPrnstatus(3);    //打印失败
         waitPakOut.setMemo(e.getMessage());
         waitPakOutService.upWaitPakOut(waitPakOut,0);
         logger.error("调用打印接口回报异常--"+waitPakOut.getMatnr() + "---" + e.getMessage());
         logger.error("sendPrinCommand8---调用打印接口回报异常--"+waitPakOut.getMatnr() + "---" + e.getMessage());
         // 调用sap打印接口,记录日志
         SapRequestLogBean logInfo = new SapRequestLogBean();
         logInfo.setMatnr(waitPakOut.getMatnr());
         logInfo.setRequest("打印请求失败");
         logInfo.setResponse("sendPrinCommand8---调用打印接口回报异常--"+waitPakOut.getMatnr() + "---");
         logInfo.setCreate_time(new Date());
         logInfo.setRemark(e.getMessage());
         logInfo.setType(2);
         sapRequestLogService.addSapLog(logInfo);
//         System.out.println(e.getMessage());
      }
      return result;