package com.stylefeng.guns.modular.system.utils.PortUtil; import cn.hutool.core.util.XmlUtil; import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import cn.hutool.json.JSONUtil; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.stylefeng.guns.core.common.exception.BizExceptionEnum; import com.stylefeng.guns.core.exception.GunsException; import com.stylefeng.guns.core.util.ToolUtil; import com.stylefeng.guns.modular.system.warpper.TerminaleDataWarpper; import org.w3c.dom.Document; import org.w3c.dom.Element; import java.io.IOException; public class ITSStrategy implements WharfStrategy { private static final String authUrl = "https://api.itslb.com/tms2/Account/Login"; private static final String url = "https://api.itslb.com/tms2/Import/ContainerAvailability"; private static final String username = "Bcllc1441"; private static final String password = "Bcllc1441@"; private static String bearerToken = ""; @Override public TerminaleDataWarpper executeLogic(String... params) { String enums = ""; if (params.length == 0) { throw new GunsException(BizExceptionEnum.REQUEST_EMPTY_ERROR); } else { enums = params[0]; } String jsonBody = JSONUtil.createObj() .put("refType", "CN") .put("refNums", enums) .toString(); HttpResponse execute = HttpRequest.post(url) .header("Authorization", "Bearer " + bearerToken) .body(jsonBody) .execute(); int statusCode = execute.getStatus(); String body = execute.body(); if ("401".equals(statusCode)){ getAuth(); executeLogic(params); } TerminaleDataWarpper terminaleDataWarpper = new TerminaleDataWarpper(); if ("404".equals(statusCode)){ return terminaleDataWarpper; } if ("200".equals(statusCode)){ JSONObject jsonObject = JSONObject.parseObject(body); Object o = jsonObject.get("containerAvailabilities"); if (ToolUtil.isNotEmpty(o)){ try { ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(o.toString()); for (JsonNode node : jsonNode) { String lineFeeStatusNote = node.get("lineFeeStatusNote").asText(); if ("OK".equals(lineFeeStatusNote)){ terminaleDataWarpper.setLineHold(""); } String customsStatusNote = node.get("customsStatusNote").asText(); if (ToolUtil.isNotEmpty(customsStatusNote) && "OK".equals(customsStatusNote)){ terminaleDataWarpper.setCustomHold(""); } String pierPassStatusNote = node.get("pierPassStatusNote").asText(); if (ToolUtil.isNotEmpty(pierPassStatusNote) && "OK".equals(pierPassStatusNote)){ terminaleDataWarpper.setPierpass(""); } String estDischargeDateShift = node.get("estDischargeDateShift").asText(); if (ToolUtil.isNotEmpty(estDischargeDateShift)){ terminaleDataWarpper.setLfd(estDischargeDateShift); } String spotStatusNote = node.get("spotStatusNote").asText(); if (ToolUtil.isNotEmpty(estDischargeDateShift) && "AREA CLOSED".equals(spotStatusNote)){ terminaleDataWarpper.setClosedArea(""); } String containerNumber = node.get("containerNumber").asText(); if (ToolUtil.isNotEmpty(containerNumber) ){ terminaleDataWarpper.setAppointmentNumber(containerNumber); } String isContainerAvailable = node.get("isContainerAvailable").asText(); if (ToolUtil.isNotEmpty(isContainerAvailable) && "true".equals(isContainerAvailable)){ terminaleDataWarpper.setAvailability(isContainerAvailable); }else { terminaleDataWarpper.setAvailability("false"); } break; } } catch (IOException e) { throw new RuntimeException(e); } } } return terminaleDataWarpper; } public static void getAuth(){ String jsonInputString = "{\"userName\": \"Bcllc1441\", \"password\": \"Bcllc1441@\"}"; String body = HttpRequest.post(authUrl) .body(jsonInputString) .execute().body(); Document document = XmlUtil.parseXml(body); Element rootElement = document.getDocumentElement(); String access_token = rootElement.getElementsByTagName("access_token").item(0).getTextContent(); if (ToolUtil.isNotEmpty(access_token)){ bearerToken = access_token; } } }