作者 朱兆平

转关运抵报文生成发送接口优化

... ... @@ -37,23 +37,23 @@ spring:
#eureka主机名,会在控制页面中显示
#DEV环境关闭注册。
features:
enabled: false
enabled: true
discovery:
enabled: false
enabled: true
service-registry:
auto-registration:
enabled: false
enabled: true
datasource:
type: com.alibaba.druid.pool.DruidDataSource
#oracle
driver-class-name: oracle.jdbc.OracleDriver
url: jdbc:oracle:thin:@192.168.1.199:10069:CGODB
username: CGONMS
password: 1q2w3e4r
# url: jdbc:oracle:thin:@192.168.1.253:1522:ORCLL
# url: jdbc:oracle:thin:@192.168.1.199:10069:CGODB
# username: CGONMS
# password: vmvnv1v2
# password: 1q2w3e4r
url: jdbc:oracle:thin:@192.168.1.253:1522:ORCLL
username: CGONMS
password: vmvnv1v2
#spring datasource mysql,注意编码配置,缺少数据库编码配置容易引起中文入库乱码
# url: jdbc:mysql://127.0.0.1:3307/statistics?useUnicode=true&characterEncoding=utf8
# username: root
... ... @@ -133,13 +133,25 @@ jwt:
max-alive: 300
# 舱单生成配置
customs:
# 操作员IC卡姓名
inputOpName: 翟梦一
# 操作员IC卡号
inputOpId: 8930000085548
# 报关公司
applyName: 河南航空货运发展有限公司
# 报关公司海关十位编码
applyCode: 4101888126
# 报关公司组织机构代码
copCode: 70678920X
#生成报文文件夹路径,尾部要带斜杠/
xml-path: customTemplate/
# 海关通道数据传输人识别号
transport-number: 460470678920X
# 转关运抵相关配置
transarrive:
#生成报文文件夹路径,尾部要带斜杠/
xml-save: transarriveSend/
sso:
witheIP: 10.5.14.108,10.5.14.109,10.5.14.110
info:
... ...
package com.tianbo.analysis.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author mrz
* 海关相关配置
*/
@Slf4j
@Component
@ConfigurationProperties(prefix = "customs")
public class CustomsProperties {
private String inputOpName;
private String inputOpId;
private String applyName;
private String applyCode;
private String copCode;
private Transarrive transarrive;
public String getInputOpName() {
return inputOpName;
}
public void setInputOpName(String inputOpName) {
this.inputOpName = inputOpName;
}
public String getInputOpId() {
return inputOpId;
}
public void setInputOpId(String inputOpId) {
this.inputOpId = inputOpId;
}
public String getApplyName() {
return applyName;
}
public void setApplyName(String applyName) {
this.applyName = applyName;
}
public String getApplyCode() {
return applyCode;
}
public void setApplyCode(String applyCode) {
this.applyCode = applyCode;
}
public String getCopCode() {
return copCode;
}
public void setCopCode(String copCode) {
this.copCode = copCode;
}
public Transarrive getTransarrive() {
return transarrive;
}
public void setTransarrive(Transarrive transarrive) {
this.transarrive = transarrive;
}
public static class Transarrive {
private String xmlSave;
public String getXmlSave() {
return xmlSave;
}
public void setXmlSave(String xmlSave) {
this.xmlSave = xmlSave;
}
}
}
... ...
... ... @@ -107,6 +107,10 @@ public class TRANSTOARRIVEEXPORT {
return customscode;
}
/**
*
* @param customscode 海关关区
*/
public void setCustomscode(String customscode) {
this.customscode = customscode == null ? null : customscode.trim();
}
... ... @@ -358,4 +362,9 @@ public class TRANSTOARRIVEEXPORT {
public void setUpdatetime(Date updatetime) {
this.updatetime = updatetime;
}
@Override
public String toString() {
return super.toString();
}
}
... ...
... ... @@ -2,6 +2,7 @@ package com.tianbo.analysis.service.imp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tianbo.analysis.config.CustomsProperties;
import com.tianbo.analysis.dao.TRANSTOARRIVEEXPORTMapper;
import com.tianbo.analysis.model.ResultJson;
import com.tianbo.analysis.model.TRANSTOARRIVEEXPORT;
... ... @@ -39,6 +40,9 @@ public class TransArriveExportImpl implements TransArriveExportService {
@Autowired
private TemplateEngine templateEngine;
@Autowired
private CustomsProperties customsProperties;
@Value("${customs.transarrive.xml-save}")
private String saveXmlForSendDir;
... ... @@ -67,6 +71,15 @@ public class TransArriveExportImpl implements TransArriveExportService {
@Override
public ResultJson send(TRANSTOARRIVEEXPORT record) {
//设置海关相关信息配置
record.setApplycode(customsProperties.getApplyCode());
record.setApplyname(customsProperties.getApplyName());
record.setCopcode(customsProperties.getCopCode());
record.setCustomscode("4604");
//操作员IC卡号
record.setInputopid(customsProperties.getInputOpId());
// 操作员姓名
record.setInputopname(customsProperties.getInputOpName());
ResultJson resultJson= new ResultJson();
try{
... ... @@ -85,45 +98,49 @@ public class TransArriveExportImpl implements TransArriveExportService {
//生成的文件名
String fileName = messageID + ".xml";
//生成的报文路径,带斜杠
String filePath = saveXmlForSendDir+fileName;
String filePath = customsProperties.getTransarrive().getXmlSave()+fileName;
File file = ResourceUtils.getFile(filePath);
/**
* 中文的地方在模板中要用utext,否则中文会被 escape 转义
*/
String xmlStr = templateEngine.process("transToArrive/EXPORT_DECLARE_TPL.xml",context);
boolean valied = XmlTools.validateXMLSchema("xsd/ETAImportMessage.xsd", xmlStr);
if (valied){
FileUtils.writeStringToFile(file,xmlStr, StandardCharsets.UTF_8);
boolean valied = XmlTools.validateXMLSchemaThrowError("xsd/ETAImportMessage.xsd", xmlStr);
if (valied) {
FileUtils.writeStringToFile(file, xmlStr, StandardCharsets.UTF_8);
//todo:更新发送状态,插入发送日志
resultJson.setCode("200");
resultJson.setMsg("报文发送成功");
}else {
resultJson.setCode("400");
resultJson.setMsg("报文发送失败,报文校验不通过");
resultJson.setError("报文发送失败,报文校验不通过");
log.info("[XML-BUILD]- 报文已生成到目录{}",filePath);
}
}catch (FileNotFoundException e){
e.printStackTrace();
resultJson.setCode("401");
resultJson.setMsg("报文发送失败,文件未找到,"+e.toString());
resultJson.setError("报文发送失败,文件未找到,"+e.toString());
resultJson.setMsg("报文发送失败,文件未找到");
resultJson.setError(e.toString());
log.error("报文发送失败,[{}]文件未找到",e.toString());
}catch (IOException e) {
e.printStackTrace();
resultJson.setCode("402");
resultJson.setMsg("报文发送失败,文件读取失败,"+e.toString());
resultJson.setError("报文发送失败,文件读取失败,"+e.toString());
resultJson.setMsg("报文发送失败,文件读取失败");
resultJson.setError(e.toString());
log.error("报文发送失败,[{}]文件IO读取失败",e.toString());
}catch (TemplateInputException e){
resultJson.setCode("404");
resultJson.setMsg("报文发送失败,报文模板未找到,"+e.toString());
resultJson.setError("报文发送失败,报文模板未找到,"+e.toString());
resultJson.setMsg("报文发送失败,报文模板未找到");
resultJson.setError(e.toString());
log.error("报文发送失败,[{}]报文模板未找到",e.toString());
}catch (SAXParseException e){
log.error("[XML-ERR]-xml报文校验失败:{}",e.getMessage());
log.error("[XML-ERR]-xml报文校验失败:[{}]", e.getMessage());
resultJson.setCode("400");
resultJson.setMsg("报文发送失败,报文校验不通过");
resultJson.setError(e.getMessage());
} catch (Exception ignored){
resultJson.setCode("405");
resultJson.setMsg("报文发送失败,其他错误"+ignored.toString());
resultJson.setError("报文发送失败,其他错误"+ignored.toString());
resultJson.setMsg("报文发送失败,其他错误");
resultJson.setError(ignored.toString());
log.error("[XML-ERR]-报文发送失败,其他错误:[{}]", ignored.getMessage());
}
return resultJson;
}
... ...
... ... @@ -47,4 +47,16 @@ public class XmlTools {
}
return true;
}
public static boolean validateXMLSchemaThrowError(String xsdPath, String xmlStr) throws IOException,SAXException {
ByteArrayInputStream bais = new ByteArrayInputStream(xmlStr.getBytes("UTF-8"));
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = factory.newSchema(new File(xsdPath));
Validator validator = schema.newValidator();
Source source = new StreamSource(bais);
validator.validate(source);
return true;
}
}
... ...
... ... @@ -56,16 +56,16 @@
<include refid="Base_Column_List" />
from CGONMS.TRANS_TO_ARRIVE_EXPORT
where 1=1
<if test="customscode != null" >
<if test="customscode != null and customscode != ''" >
and CustomsCode = #{customscode,jdbcType=VARCHAR}
</if>
<if test="username != null" >
<if test="username != null and username != ''">
and USERNAME = #{username,jdbcType=VARCHAR}
</if>
<if test="trafmode != null" >
<if test="trafmode != null and trafmode != ''" >
and TrafMode = #{trafmode,jdbcType=VARCHAR}
</if>
<if test="unloadcode != null" >
<if test="unloadcode != null and unloadcode != ''" >
and UnloadCode = #{unloadcode,jdbcType=VARCHAR}
</if>
<if test="creattime != null" >
... ...
import com.tianbo.analysis.NmmsAdminApplication;
import com.tianbo.analysis.config.CustomsProperties;
import com.tianbo.analysis.model.TRANSTOARRIVEEXPORT;
import com.tianbo.analysis.service.TransArriveExportService;
import lombok.extern.slf4j.Slf4j;
... ... @@ -12,19 +13,22 @@ import java.util.Date;
public class TransAriiveTest {
@Autowired
private CustomsProperties customsProperties;
@Autowired
TransArriveExportService transArriveExportService;
@org.junit.jupiter.api.Test
public void send(){
TRANSTOARRIVEEXPORT t = new TRANSTOARRIVEEXPORT();
//海关信息及IC卡相关部分
t.setApplycode("4101888126");
t.setApplyname("河南航空货运发展有限公司");
t.setCopcode("70678920X");
t.setApplycode(customsProperties.getApplyCode());
t.setApplyname(customsProperties.getApplyName());
t.setCopcode(customsProperties.getCopCode());
t.setCustomscode("4604");
//操作员IC卡号
t.setInputopid("8930000085548");
t.setInputopname("翟梦一");
t.setInputopid(customsProperties.getInputOpId());
t.setInputopname(customsProperties.getInputOpName());
t.setCertno("21212321321321");
t.setSeqno("");
... ...