package foundation.icall.callout.ai;
|
|
import java.sql.PreparedStatement;
|
|
import com.google.gson.Gson;
|
import com.google.gson.JsonArray;
|
import com.google.gson.JsonElement;
|
import com.google.gson.JsonObject;
|
|
import foundation.data.entity.Entity;
|
import foundation.data.entity.EntitySet;
|
import foundation.data.object.DataObject;
|
import foundation.data.object.EntitySaver;
|
import foundation.persist.IDoubleSavable;
|
import foundation.persist.ISavable;
|
import foundation.persist.SQLRunner;
|
import foundation.util.ID;
|
import foundation.util.Util;
|
|
public class VatInvoiceVerifyLogic implements IDoubleSavable,ISavable{
|
|
public void markBill(String fileId) {
|
DataObject fileDataObject = DataObject.getInstance("file_index");
|
try {
|
Entity entity = fileDataObject.getBrowseEntity(fileId);
|
entity.set("is_bill", "1");
|
fileDataObject.updateEntity(entity);
|
} catch(Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
public void deleteBillImage(String fileId) {
|
DataObject fileDataObject = DataObject.getInstance("file_index");
|
try {
|
fileDataObject.deleteEntity(fileId);
|
} catch(Exception e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
public VatInvoiceVerifyDao parseInvoiceInfo(OCRResult ocrResult) {
|
String bill_code = null, bill_no = null, bill_date = null, check_code = null, amount = null;
|
String name_str = null, value_str = null;
|
JsonObject oneBillObject = null;
|
|
VatInvoiceVerifyDao vatInvoiceVerify = new VatInvoiceVerifyDao(bill_code, bill_no, bill_date, check_code, amount);
|
|
Gson gson = new Gson();
|
JsonObject jsonObject = gson.fromJson(ocrResult.getJson(), JsonObject.class);
|
JsonElement billElement = jsonObject.get("VatInvoiceInfos");
|
JsonArray jsonBillArray = billElement.getAsJsonArray();
|
for (int i = 0; i < jsonBillArray.size(); i++) {
|
oneBillObject = jsonBillArray.get(i).getAsJsonObject();
|
|
if (Util.isEmpty(oneBillObject)) {
|
continue;
|
}
|
|
name_str = oneBillObject.get("Name").getAsString();
|
value_str = oneBillObject.get("Value").getAsString();
|
|
if (Util.isEmpty(name_str) || Util.isEmpty(value_str)) {
|
continue;
|
}
|
|
if ("发票代码".equalsIgnoreCase(name_str)) {
|
bill_code = value_str;
|
vatInvoiceVerify.setInvoiceCode(bill_code);
|
}
|
|
if ("发票号码".equalsIgnoreCase(name_str)) {
|
bill_no = value_str.replace("No", "");
|
vatInvoiceVerify.setInvoiceNo(bill_no);
|
}
|
|
if ("开票日期".equalsIgnoreCase(name_str)) {
|
bill_date = value_str.replace("年", "-").replace("月", "-").replace("日", "");
|
vatInvoiceVerify.setInvoiceDate(bill_date);
|
}
|
|
if ("校验码".equalsIgnoreCase(name_str)) {
|
check_code = value_str;
|
check_code = check_code.substring(check_code.length() - 6, check_code.length());
|
vatInvoiceVerify.setAdditional(check_code);
|
}
|
|
if ("合计金额".equalsIgnoreCase(name_str)) {
|
amount = value_str.replace("¥", "");
|
vatInvoiceVerify.setAmount(amount);
|
}
|
}
|
|
return vatInvoiceVerify;
|
}
|
|
public void saveVatInvoiceVerify(OCRResult ocrResult) {
|
DataObject dataObject = null;
|
String id = null;
|
|
Gson gson = new Gson();
|
JsonObject jsonObject = gson.fromJson(ocrResult.getJson(), JsonObject.class);
|
InvoiceVerificationDao invoiceVerification = gson.fromJson(jsonObject.get("Invoice"), InvoiceVerificationDao.class);
|
|
try {
|
SQLRunner.beginTrans();
|
//1.保存发票主体
|
id = ID.newValue();
|
dataObject = DataObject.getInstance("so_implant_invoice");
|
|
Entity entity = invoiceVerification.getEntity();
|
EntitySaver saver = dataObject.createEntitySaver();
|
saver.set(entity);
|
saver.set("id", id);
|
saver.insert();
|
|
//2.保存发票明细
|
dataObject = DataObject.getInstance("so_implant_invoice_detail");
|
saver = dataObject.createEntitySaver();
|
|
EntitySet detailSet = invoiceVerification.getDetailEntitySet();
|
for (Entity detail : detailSet) {
|
saver.set(detail);
|
saver.insert();
|
}
|
|
SQLRunner.commit();
|
} catch(Exception e) {
|
try {
|
SQLRunner.rollback();
|
} catch(Exception ex) {
|
ex.printStackTrace();
|
}
|
}
|
}
|
|
@Override
|
public void saveData(PreparedStatement stmt, Object... agrs) throws Exception {
|
// TODO Auto-generated method stub
|
// int cellIndex = 0;
|
// if (agrs.length > 0) {
|
// cellIndex = (Integer) agrs[0];
|
// }
|
// for (int h = 0; h < excelData.size(); h++) {//批处理存入数据
|
// dataLinked = excelData.get(h);
|
// int cellCnt = cellIndex == 0 ? dataLinked.size() - 1 : dataLinked.size();
|
// for (int k = cellIndex; k <= cellCnt; k++) {//dataLinked.size() - 1
|
// if (cellIndex == 0) {
|
// if (dataLinked.get(k) == null) {
|
// stmt.setObject(k+1, null);
|
// }
|
// else if (dataLinked.get(k).toString().equals("")) {
|
// stmt.setObject(k+1, null);
|
// } else {
|
// String insertData = convertSqlData(dataLinked.get(k).toString());
|
// stmt.setString(k+1, insertData);
|
// }
|
// }
|
// else {
|
// if (dataLinked.get(k-1).toString().equals("")) {
|
// stmt.setObject(k, null);
|
// } else {
|
// String insertData = convertSqlData(dataLinked.get(k-1).toString());
|
// stmt.setString(k, insertData);
|
// }
|
// }
|
// }
|
//
|
// stmt.addBatch();
|
// }
|
// stmt.executeBatch();
|
|
}
|
|
@Override
|
public void save(PreparedStatement stmt1, PreparedStatement stmt2, Object... agrs) throws Exception {
|
// TODO Auto-generated method stub
|
|
}
|
|
}
|