liujie
2023-08-31 d6811336d546e4963a9af3bd7cd8654d14193bce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.stylefeng.guns.modular.system.utils.PortUtil;
 
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
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 java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
public class APMStrategy implements WharfStrategy{
 
    //Sandbox Interface
    private static final String url  = "https://api-sandbox.apmterminals.com/import-availability";
 
    //Production Interface
//    private static final String url  = "https://api.apmterminals.com/import-availability";
 
    private static final String auth_url  = "https://api.apmterminals.com/oauth/client_credential/accesstoken";
 
    private static final String KEY  = "di3RgCcM9zlgb5BG1UiYSIxwYdmYUxTo";
    private static final String SECRET  = "AIJczPxBeocsEjtI";
 
    private static String bearerToken  = "5dDHq3LwPW3EYZ6rtPGK0zqWsRA5";
 
 
    public static void main(String[] args) {
        getAuth();
        String us = auth_url+ "?assetId="+"EITU1509974"+"&facilityCode="+"USLAX";
        HttpResponse execute = HttpRequest.get(us).header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36")
                .header("cookie", "_ga=GA1.2.1226281326.1675309614; _gid=GA1.2.1467106222.1675309614; _ga=GA1.4.1226281326.1675309614; _gid=GA1.4.1467106222.1675309614; AWSALB=1bWqINVI+LJP87FTEXfEw1Ob1nkbr+I4baSbUGUmu5+/LdiqL9ic04Nj7F0Vz3rvharAG7a8dVe3MX6YMNEbUINVr++CCv/UBw6JeCRS0PcbRLxK7wVHb1lPT8Jl; AWSALBCORS=1bWqINVI+LJP87FTEXfEw1Ob1nkbr+I4baSbUGUmu5+/LdiqL9ic04Nj7F0Vz3rvharAG7a8dVe3MX6YMNEbUINVr++CCv/UBw6JeCRS0PcbRLxK7wVHb1lPT8Jl")
                .header("path", "?assetId=" + "EITU1509974" + "&facilityCode=" + "USLAX")
                .header("Authorization", "Bearer " + bearerToken)
                .execute();
        int status = execute.getStatus();
        System.out.println(status);
        System.out.println(execute.body());
    }
 
    @Override
    public TerminaleDataWarpper executeLogic(String... params) {
        String containerId = "";
        String enums = "";
        if (params.length == 0) {
            throw new GunsException(BizExceptionEnum.REQUEST_EMPTY_ERROR);
        } else {
            containerId = params[0];
            enums = params[1];
        }
 
        String us = url+ "?assetId="+containerId+"&facilityCode="+enums;
        HttpResponse execute = HttpRequest.get(us).header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36")
                .header("cookie", "_ga=GA1.2.1226281326.1675309614; _gid=GA1.2.1467106222.1675309614; _ga=GA1.4.1226281326.1675309614; _gid=GA1.4.1467106222.1675309614; AWSALB=1bWqINVI+LJP87FTEXfEw1Ob1nkbr+I4baSbUGUmu5+/LdiqL9ic04Nj7F0Vz3rvharAG7a8dVe3MX6YMNEbUINVr++CCv/UBw6JeCRS0PcbRLxK7wVHb1lPT8Jl; AWSALBCORS=1bWqINVI+LJP87FTEXfEw1Ob1nkbr+I4baSbUGUmu5+/LdiqL9ic04Nj7F0Vz3rvharAG7a8dVe3MX6YMNEbUINVr++CCv/UBw6JeCRS0PcbRLxK7wVHb1lPT8Jl")
                .header("path", "?assetId=" + containerId + "&facilityCode=" + enums)
                .header("Authorization", "Bearer " + bearerToken)
                .execute();
 
        String body = execute.body();
        int status = execute.getStatus();
        if (status != 200){
            getAuth();
            return executeLogic();
        }
        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode jsonArray = null;
        TerminaleDataWarpper warpper = new TerminaleDataWarpper();
        try {
            jsonArray = objectMapper.readTree(body);
            for (JsonNode jsonNode : jsonArray) {
                String appointmentDateTimeLocal = jsonNode.get("appointmentDateTimeLocal").asText();
                warpper.setLfd(appointmentDateTimeLocal);
                String appointmentNumber = jsonNode.get("containerId").asText();
                warpper.setAppointmentNumber(appointmentNumber);
 
                String readyForDelivery = jsonNode.get("readyForDelivery").asText();
                if (ToolUtil.isNotEmpty(readyForDelivery)) {
                    warpper.setAvailability(readyForDelivery);
                }
 
                String containerHolds = jsonNode.get("containerHolds").asText();
                String location = jsonNode.get("location").asText();
                if (ToolUtil.isNotEmpty(location)){
                    warpper.setClosedArea(location);
                }
                List<String> collect = new ArrayList<>();
                if (ToolUtil.isEmpty(containerHolds)){
                    return warpper;
                }
                if (containerHolds.contains(",")){
                    String[] split = containerHolds.split(",");
                    collect =  Arrays.stream(split).collect(Collectors.toList());
                    if (collect.contains("LINE")){
                        warpper.setLineHold("1");
                    }
                    if (collect.contains("TMF")){
                        warpper.setCustomHold("1");
                    }
                    if (collect.contains("PIER")){
                        warpper.setPierpass("1");
                    }
                    if (collect.contains("CTF")){
                        warpper.setCtf("1");
                    }
 
                }else {
                    if (containerHolds.equals("LINE")){
                        warpper.setLineHold("1");
                    }
                    if (containerHolds.equals("TMF")){
                        warpper.setCustomHold("1");
                    }
                    if (containerHolds.equals("PIER")){
                        warpper.setPierpass("1");
                    }
                    if (containerHolds.equals("CTF")){
                        warpper.setCtf("1");
                    }
                    if (containerHolds.equals("AREA")){
                        warpper.setClosedArea("1");
                    }
                }
                break;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
 
        return warpper;
    }
 
 
    public static void getAuth(){
        String us = auth_url+ "?grant_type=client_credentials";
        String body = HttpRequest.post(us)
                .form("client_id", KEY)
                .form("client_id", SECRET)
                .execute().body();
        JSONObject object = JSONObject.parseObject(body);
        Object o = object.get("access_token");
        if (ToolUtil.isNotEmpty(o)){
            bearerToken = (String) o;
        }
    }
}