P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package foundation.ai;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
 
import foundation.ai.dao.InvoiceVerificationDao;
import foundation.ai.logic.OCRResultKingDee;
import foundation.dao.Filter;
import foundation.dao.Settings;
import foundation.data.entity.EntitySet;
import foundation.data.object.DataObject;
import foundation.util.ContentBuilder;
import foundation.util.MD5Utils;
import foundation.util.Util;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import sun.misc.BASE64Encoder;
 
public class KingdeeAIProvider extends AIProvider {
 
    private static Logger logger;
 
    private static String HOST = "https://api.piaozone.com";
    private static String CLIENT_ID = "Pa0umVUNzg6EiWSEpGht";
    private static String CLIENT_SECRET = "a3cbabd08e684413a30aae1803c773d5";
    private static String TOKEN_PATH = "/base/oauth/token";
    protected static String AES_KEY = "goopyi5VTKD5WIgs";
 
    public static int TimeOut_Second_Write = 10;
    public static int TimeOut_Second_Read = 10;
    public static boolean Debug = true;
    private static OkHttpClient httpClient = null;
 
    static {
        logger = LogManager.getLogger(KingdeeAIProvider.class);
    }
 
    @Override
    protected String getName() {
        return "金蝶AI";
    }
 
    @Override
    public OCRResult executeAction(Operator operator, File file) throws Exception {
        boolean isReal = false, isBill = false;
 
        // 1. 得到图片的Base64
        String base64 = imageToBase64(file);
        if (base64 == null) {
            logger.error("无法对图片{}进行Base64编码,无法进行AI识别", file);
            return null;
        }
 
        // 2. 获取access_token
        Response tokenResponse = getAccessToken();
        if (!tokenResponse.isSuccessful()) {
            return new OCRResult(operator, "error");
        }
        ResponseBody body = tokenResponse.body();
        String bodyStr = body.string();
        JSONObject tokenJson = JSONObject.parseObject(bodyStr);
        body.close();
 
        // 3. 创建请求
        OperateUrl operation = OperateUrl.parse(operator);
        Request request = new Request.Builder().url(HOST + operation.requestUrl + tokenJson.getString("access_token"))
                .post(RequestBody.create(MediaType.parse("text/plain"), base64)).build();
 
        // 4. 发送请求
        String value = "";
        try {
            value = send(operation, request);
        } catch (Exception e) {
            e.printStackTrace();
            value = "fail";
        }
 
        // 5.分析校验结果
        OCRResultKingDee resultKingDee = OCRResultKingDee.parseBatch(value);
        List<InvoiceVerificationDao> invoiceVerificationList = resultKingDee.getInvoiceVerificationList();
        
        if(invoiceVerificationList.size()> 0 || !resultKingDee.isSuccess()) { 
            isReal = false; isBill = false;
        }
        
        // 5.1 发票唯一性校验
        boolean checkDistinct = Settings.getBoolean("InvoiceDistinctCheck", false);
        
        if (checkDistinct) {    
            for (InvoiceVerificationDao invoice : invoiceVerificationList) {
                boolean distincCheck = distinctCheck(invoice.getCode(), invoice.getNumber());
                invoice.setDistinct(distincCheck);
            }
        }
         
        // 6. 返回结果
        value = Util.ocrJson(value);
        OCRResult result = new OCRResult(operator, value);
        result.setErrcode(resultKingDee.getErrcode());
        result.setDescription(resultKingDee.getDescription());
        result.setTraceId(resultKingDee.getTraceId());
        result.setIs_real(isReal);
        result.setIs_bill(isBill);
        result.setJsonArray(resultKingDee.getData());
        result.setInvoiceList(invoiceVerificationList);
 
        return result;
    }
 
 
    @Override
    public OCRResult executeAction(Operator operator, Object paramObject) throws Exception {
        boolean isReal = false, isBill = false;
 
        // 1. 获取发票信息
        JSONObject vatInvoiceVerify = (JSONObject) paramObject;
        if (vatInvoiceVerify == null || Util.isEmpty(vatInvoiceVerify)) {
            logger.error("未获取到相关发票信息,无法查询发票");
            return null;
        }
 
        // 2.获取access_token
        Response tokenResponse = getAccessToken();
        if (!tokenResponse.isSuccessful()) {
            return new OCRResult(operator, "error");
        }
        ResponseBody body = tokenResponse.body();
        String bodyStr = body.string();
        JSONObject tokenJson = JSONObject.parseObject(bodyStr);
        body.close();
 
        // 3. 创建请求
        String value = "";
        OperateUrl operation = OperateUrl.parse(operator);
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("invoiceCode", vatInvoiceVerify.getString("invoiceCode"));
        jsonBody.put("invoiceNo", vatInvoiceVerify.getString("invoiceNo"));
        jsonBody.put("invoiceDate", vatInvoiceVerify.getString("invoiceDate"));
        jsonBody.put("invoiceMoney", vatInvoiceVerify.getString("invoiceMoney"));
        jsonBody.put("totalAmount", vatInvoiceVerify.getString("totalAmount"));
        jsonBody.put("checkCode", vatInvoiceVerify.getString("checkCode"));
        jsonBody.put("isCreateUrl", vatInvoiceVerify.getString("isCreateUrl"));
        Request request = new Request.Builder().url(HOST + operation.requestUrl + tokenJson.getString("access_token"))
                .post(RequestBody.create(MediaType.parse("application/json"), jsonBody.toJSONString())).build();
 
        // 4. 发送请求
        try {
            value = send(operation, request);
        } catch (Exception e) {
            e.printStackTrace();
            value = e.getMessage();
        }
 
        // 5. 分析查询结果
        OCRResultKingDee resultKingDee = OCRResultKingDee.parseOne(value);
        List<InvoiceVerificationDao> invoiceVerificationList = resultKingDee.getInvoiceVerificationList();
        
        // 5.1 发票唯一性校验
        boolean checkDistinct = Settings.getBoolean("InvoiceDistinctCheck", false);
        
 
        if (checkDistinct) {    
            for (InvoiceVerificationDao invoice : invoiceVerificationList) {
                boolean distincCheck = distinctCheck(invoice.getCode(), invoice.getNumber());
                invoice.setDistinct(distincCheck);
            }
        }
        
        if (Util.isEmpty(invoiceVerificationList) || !resultKingDee.isSuccess()) {
            isReal = false;
            isBill = false;
        }
        
 
        // 6. 返回结果
        value = Util.ocrJson(value);
        OCRResult result = new OCRResult(operator, value);
        result.setErrcode(resultKingDee.getErrcode());
        result.setDescription(resultKingDee.getDescription());
        result.setTraceId(resultKingDee.getTraceId());
        result.setIs_real(isReal);
        result.setIs_bill(isBill);
        result.setJsonArray(resultKingDee.getData());
        result.setInvoiceList(invoiceVerificationList);
 
        return result;
 
    }
 
    private String send(OperateUrl operation, Request request) throws IOException {
        httpClient = new OkHttpClient();
        String json = null;
 
        Response response = httpClient.newCall(request).execute();
        if (response.isSuccessful()) {
            try {
                json = response.body().string();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                response.close();
            }
        }
 
        return json;
    }
    
    private static Response getAccessToken() throws Exception {
        httpClient = new OkHttpClient();
        long currentTimeStamp = System.currentTimeMillis();
        String sign = MD5Utils.MD5Encode(CLIENT_ID + CLIENT_SECRET + currentTimeStamp, "UTF-8");
 
        JSONObject body = new JSONObject();
        // 1. set parameter
        body.put("client_id", CLIENT_ID);
        body.put("client_secret", CLIENT_SECRET);
        body.put("sign", sign);
        body.put("timestamp", currentTimeStamp);
 
        MediaType jsonMediaType = MediaType.get("application/json");
        String bodyStr = JSON.toJSONString(body);
 
        Request request = new Request.Builder().url(HOST + TOKEN_PATH).post(RequestBody.create(jsonMediaType, bodyStr))
                .build();
 
        // 3. do sendRequest
        Response response = httpClient.newCall(request).execute();
 
        return response;
    }
 
    private static String imageToBase64(File file) {
        byte[] data = null;
 
        InputStream in = null;
        try {
            // 1. bytes
            in = new FileInputStream(file);
            data = new byte[in.available()];
            in.read(data);
            in.close();
 
            // 2. encode
            BASE64Encoder encoder = new BASE64Encoder();
            return encoder.encode(Objects.requireNonNull(data));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
        return null;
    }
 
    public static boolean distinctCheck(String invoiceCode , String invoiceNumber) throws Exception {
        DataObject dataObject = DataObject.getInstance("so_implant_invoice_list");
        Filter filter = new Filter();
        
        ContentBuilder builder = new ContentBuilder(", ");
        builder.append(Util.quotedStr("input"));
        builder.append(Util.quotedStr("cancel"));
        builder.append(Util.quotedStr("close"));
        
        filter.add("so_implant_invoice.code", invoiceCode);
        filter.add("so_implant_invoice.number", invoiceNumber);
        filter.add("so_implant.state_code", "not in ", "(" + builder.toString() + ")");
        EntitySet entitySet = dataObject.getBrowseEntitySet(filter);    
        
        if (entitySet.size() > 0 ) {
            return false;
        }
        return true;    
    }
}