Skip to content
Snippets Groups Projects

电子行程单AI查验

Merged zhuxiang requested to merge feature/zhuxiang into develop
Compare and
5 files
+ 324
1
Compare changes
  • Side-by-side
  • Inline
Files
5
package com.seasky.seaskyocr.application.command.assembler;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
* @author zhuxiang
* @since 2024-9-30
*/
@Slf4j
public class Xml2JsonAssembler {
public static JSONObject xml2Json(String xml) {
xml = xml.replaceAll("\\n", "");
JSONObject jsonObject = convertToJSON(xml);
if (ObjectUtil.isNull(jsonObject)) {
return null;
}
JSONObject eInvoiceData = jsonObject.getJSONObject("EInvoice").getJSONObject("EInvoiceData");
// 发票代码
String fpdm = "";
// 发票号码
String fphm = getJsonValue(jsonObject, "EInvoice", "TaxSupervisionInfo", "InvoiceNumber");
// 开票日期
String kprq = getJsonValue(jsonObject, "EInvoice", "TaxSupervisionInfo", "IssueTime");
// 国内国际标志
String gngjbz = getJsonValue(eInvoiceData, "SpecificInformation", "ItineraryReceiptOfE-TicketForAirTransportation", "MarkingOfDomesticOrInternational");
// 购方名称
String gfmc = getJsonValue(eInvoiceData, "BuyerInformation", "BuyerName");
// 合计税额
String hjse = getJsonValue(eInvoiceData, "BasicInformation", "TotalTaxAm");
// 购方识别号
String gfsbh = getJsonValue(eInvoiceData, "BuyerInformation", "BuyerIdNum");
// 旅客姓名
String lkxm = getJsonValue(eInvoiceData, "SpecificInformation", "ItineraryReceiptOfE-TicketForAirTransportation", "NameOfPassenger");
// 身份证件号码
String sfzjhm = getJsonValue(eInvoiceData, "SpecificInformation", "ItineraryReceiptOfE-TicketForAirTransportation", "PassengerIdentificationNumber");
// 电子客票号码
String dzkphm = getJsonValue(eInvoiceData, "SpecificInformation", "ItineraryReceiptOfE-TicketForAirTransportation", "ETicketNumber");
// GP代号
String gpdh = getJsonValue(eInvoiceData, "SpecificInformation", "ItineraryReceiptOfE-TicketForAirTransportation", "NumberOfGpOrder", "content");
// 保险金额
String bxje = getJsonValue(eInvoiceData, "SpecificInformation", "ItineraryReceiptOfE-TicketForAirTransportation", "Insurance");
// 不含保险金额
String bhbxje = getJsonValue(eInvoiceData, "SpecificInformation", "ItineraryReceiptOfE-TicketForAirTransportation", "TotalAmount");
// 价税合计
String jshj = sumStr(bhbxje, bxje);
JSONObject data = new JSONObject();
data.put("gfsbh", gfsbh);
data.put("cysj", getNow());
data.put("fpdm", fpdm);
data.put("gngjbz", gngjbz);
data.put("hjse", hjse);
data.put("gfmc", gfmc);
data.put("fplx", "61");
data.put("lkxm", lkxm);
data.put("cycs", "0");
data.put("dzkphm", dzkphm);
data.put("dkbz", "");
data.put("kprq", kprq);
data.put("jshj", jshj);
data.put("sfzjhm", sfzjhm);
data.put("fpztDm", "0");
data.put("gpdh", gpdh);
data.put("fphm", fphm);
data.put("bxje", bxje);
data.put("bhbxje", bhbxje);
JSONArray hwxx = new JSONArray();
JSONObject details = eInvoiceData.getJSONObject("SpecificInformation").getJSONObject("ItineraryReceiptOfE-TicketForAirTransportation");
JSONArray detailItems = new JSONArray();
try {
JSONObject detailItem = details.getJSONObject("ItineraryDetailInformation");
detailItems.add(detailItem);
} catch (Exception ex) {
detailItems = details.getJSONArray("ItineraryDetailInformation");
}
List<String> lines = Arrays.asList("DepartureStation", "FirstDestinationStation", "SecondDestinationStation", "ThirdDestinationStation", "FourthDestinationStation");
for (int i = 0; i < detailItems.size(); i++) {
JSONObject detailItem = detailItems.getJSONObject(i);
if (details.containsKey(lines.get(i)) && details.containsKey(lines.get(i + 1))) {
String sfz = details.getString(lines.get(i));
String mdz = details.getString(lines.get(i + 1));
String qfsj = getJsonValue(detailItem, "DepartureTime");
String zwdj = getJsonValue(detailItem, "Class");
String cyrq = getJsonValue(detailItem, "CarrierDate");
String hdxh = String.valueOf(i + 1);
String cyr = getJsonValue(detailItem, "Carrier");
String hbh = getJsonValue(detailItem, "Flight");
String kpjb = getJsonValue(detailItem, "FareBasis");
JSONObject jsonDetail = new JSONObject();
jsonDetail.put("sfz", sfz);
jsonDetail.put("mdz", mdz);
jsonDetail.put("qfsj", qfsj);
jsonDetail.put("zwdj", zwdj);
jsonDetail.put("cyrq", cyrq);
jsonDetail.put("hdxh", hdxh);
jsonDetail.put("cyr", cyr);
jsonDetail.put("hbh", hbh);
jsonDetail.put("kpjb", kpjb);
hwxx.add(jsonDetail);
} else {
break;
}
}
data.put("hwxx", hwxx);
JSONObject resultObject = new JSONObject();
resultObject.put("times", 0);
resultObject.put("data", data);
resultObject.put("fplx", "61");
resultObject.put("kprq", kprq);
resultObject.put("success", true);
resultObject.put("customReqId", "0");
resultObject.put("jshj", jshj);
resultObject.put("time", getNow());
resultObject.put("dq", "31");
resultObject.put("fphm", fphm);
return resultObject;
}
private static String getNow() {
Date currentTime = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(currentTime);
}
private static String sumStr(String str1, String str2) {
BigDecimal bd1 = toBigDecimal(str1);
BigDecimal bd2 = toBigDecimal(str2);
BigDecimal bd3 = bd1.add(bd2);
return bd3.toString();
}
private static BigDecimal toBigDecimal(String str) {
try {
if (ObjectUtil.isEmpty(str)) {
return BigDecimal.ZERO;
}
BigDecimal bd = new BigDecimal(str);
bd = bd.setScale(2, RoundingMode.HALF_UP);
return bd;
} catch (Exception ex) {
ex.printStackTrace();
log.error(ex.getMessage(), ex);
return BigDecimal.ZERO;
}
}
private static JSONObject convertToJSON(String xml) {
try {
cn.hutool.json.JSONObject jsonObject = cn.hutool.json.XML.toJSONObject(xml, true);
String jsonStr = jsonObject.toString();
JSONObject json = JSONObject.parseObject(jsonStr);
return json;
} catch (Exception ex) {
ex.printStackTrace();
log.error(ex.getMessage(), ex);
return null;
}
}
private static String getJsonValue(JSONObject json, String... keys) {
try {
if (ObjectUtil.isEmpty(json)) {
return "";
}
JSONObject thisJson = json;
for (int i = 0; i < keys.length; i++) {
String key = keys[i];
if (i == keys.length - 1) {
if (thisJson.containsKey(key)) {
return thisJson.getString(key);
} else {
return "";
}
} else {
if (thisJson.containsKey(key)) {
thisJson = thisJson.getJSONObject(key);
} else {
return "";
}
}
}
return "";
} catch (Exception ex) {
ex.printStackTrace();
log.error(ex.getMessage(), ex);
return "";
}
}
}