P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
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
package foundation.icall.connector;
 
import java.util.List;
 
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.profile.Language;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
 
import foundation.dao.DataPackage;
import foundation.dao.DataSource;
import foundation.dao.Settings;
import foundation.dao.bizlogic.DocDescription;
import foundation.dao.bizlogic.IRequest;
import foundation.data.entity.Entity;
import foundation.icall.ContentType;
import foundation.icall.ICall;
import foundation.icall.callout.ICallRequest;
import foundation.icall.callout.JSONResponse;
import foundation.icall.callout.ai.InvoiceVerificationDao;
import foundation.icall.callout.ai.OCRResult;
import foundation.icall.callout.ai.OCRResultKingDee;
import foundation.io.template.Template;
import foundation.util.MapList;
import foundation.util.Util;
import foundation.workflow.WorkStep;
 
public class TencentAIConn extends HttpServerConn {
    
    private static TencentAIConn instance;
    private static String monitorId = "腾讯AI";
    public static int TimeOut_Second_Write = 10;
    public static int TimeOut_Second_Read = 10;
    public static boolean Debug = true;
    public OcrClient client;
    
    private TencentAIConn() {
        
    }
    
    public static synchronized TencentAIConn getInstance() {
        if (instance == null) {
            instance = new TencentAIConn();
        }
        
        return instance;
    }
 
    @Override
    public void login(WorkStep step, ICall iCall) throws Exception {
        createClient();
    }
 
    @Override
    public void logout(WorkStep step, ICall iCall) throws Exception {
        
    }
 
    @Override
    public ICallRequest createRequest(String url) {
        String host = meta.getString("host");
        ICallRequest request = new ICallRequest(host + url);
        
        
        return request;
    }
 
    @Override
    public IRequest buildBody(IRequest request, WorkStep step, ICall iCall) throws Exception {
        DataPackage dataPackage = step.getDataPackage();
        
        String jsonBody = null;
        ContentType contentType = iCall.getContentType();
        
        dataPackage.loadDataFromRequest();
        Entity master = dataPackage.getMasterEntity(DataSource.Request);
    
        MapList<String, Template> templateList = iCall.getRequestTemplateList();
        
        if (templateList != null  && templateList.size() > 0) {
            Template template = templateList.get(0);
            jsonBody = template.fillVariants(dataPackage);
        }
        
        if (master != null) {
            request.setDocDescription(new DocDescription(master.getId(), master.getString("code")));
        }
    
        request.setContentType(contentType.getCode());
        request.setJSONBody(jsonBody);
        
        return request;
    }
    
    public OCRResult format(WorkStep step, JSONResponse response) throws Exception {
        boolean isReal = false, isBill = false;
        OCRResultKingDee resultKingDee = new OCRResultKingDee(step.getInstanceId(), response);
        List<InvoiceVerificationDao> invoiceVerificationList = resultKingDee.getInvoiceVerificationList();
        
        // 1. 发票唯一性校验
        boolean checkDistinct = Settings.getBoolean("InvoiceDistinctCheck", false);
        
        if (checkDistinct) {    
            for (InvoiceVerificationDao invoice : invoiceVerificationList) {
                boolean distincCheck = invoice.distinctCheck();
                invoice.setDistinct(distincCheck);
            }
        }
        
        if (Util.isEmpty(invoiceVerificationList) || !resultKingDee.isSuccess()) {
            isReal = false;
            isBill = false;
        }
        
        String value = Util.ocrJson(response.toString());
        OCRResult result = new OCRResult(value);
        result.setErrcode(resultKingDee.getErrcode());
        result.setDescription(resultKingDee.getDescription());
        result.setTraceId(resultKingDee.getTraceId());
        result.setIs_real(isReal);
        result.setIs_bill(isBill);
        result.setInvoiceList(invoiceVerificationList);
        
        return result;
    }
    
    private void createClient() throws Exception {
        String secretId = meta.getString("secret_id");
        String secretKey = meta.getString("secret_key");
        String region = meta.getString("region");
        String host = meta.getString("host");
        
        //1. credential
        Credential credential = new Credential(secretId, secretKey);
        
        //2. profile
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setWriteTimeout(TimeOut_Second_Write);  
        httpProfile.setReadTimeout(TimeOut_Second_Read);  
        httpProfile.setEndpoint(host);
        
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        clientProfile.setDebug(Debug);
        clientProfile.setLanguage(Language.ZH_CN);
        
        //3. client
        this.client = new OcrClient(credential, region, clientProfile);
    }
}