TransArriveExportImpl.java
9.0 KB
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
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.dao.TransToArriveExportResonseDao;
import com.tianbo.analysis.exception.FFMResolveException;
import com.tianbo.analysis.exception.TransArriveException;
import com.tianbo.analysis.model.MANIFEST_AIR_CHANGE;
import com.tianbo.analysis.model.ResultJson;
import com.tianbo.analysis.model.TRANSTOARRIVEEXPORT;
import com.tianbo.analysis.model.TransToArriveExportResonse;
import com.tianbo.analysis.service.MQ;
import com.tianbo.analysis.service.TransArriveExportService;
import com.tianbo.analysis.tools.TransArriveTools;
import com.tianbo.analysis.tools.XmlTools;
import com.tianbo.util.Date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.exceptions.TemplateInputException;
import org.xml.sax.SAXParseException;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
@Service
@Slf4j
public class TransArriveExportImpl implements TransArriveExportService {
@Resource
TRANSTOARRIVEEXPORTMapper mapper;
@Autowired
private TemplateEngine templateEngine;
@Autowired
private CustomsProperties customsProperties;
@Autowired
private MQ mq;
@Resource
TransToArriveExportResonseDao toArriveExportResonseDao;
@Override
public int addTransArriveExport(TRANSTOARRIVEEXPORT transtoarriveexport) {
// transtoarriveexport.setAutoid(UUID.randomUUID().toString());
String id = TransArriveTools.getClientseqno();
transtoarriveexport.setAutoid(id);
transtoarriveexport.setClientseqno(id);
transtoarriveexport.setCreattime(new Date());
return mapper.insertSelective(transtoarriveexport);
}
@Override
public int ediTransArriveExport(TRANSTOARRIVEEXPORT transtoarriveexport) {
return mapper.updateByPrimaryKeySelective(transtoarriveexport);
}
@Override
public int delTransArriveExport(String autoid) {
return mapper.deleteByPrimaryKey(autoid);
}
@Override
public PageInfo<TRANSTOARRIVEEXPORT> selectTrans(TRANSTOARRIVEEXPORT transtoarriveexport, int pageNum, int pageSize) {
PageHelper.startPage(pageNum,pageSize);
List<TRANSTOARRIVEEXPORT> list=mapper.selectTrans(transtoarriveexport);
PageInfo<TRANSTOARRIVEEXPORT> pageInfo=new PageInfo<TRANSTOARRIVEEXPORT>(list);
return pageInfo;
}
@Override
public ResultJson send(TRANSTOARRIVEEXPORT record) {
//签名信息
String sign = "kUNHDHVG1b72ZIrN8voIGUr+2JRITIo5FlusuSQCMerhV5GEGg9Z42L5F28MM+4gFL6CQgH2JPmdRdHAYIKJa89fdxGnHE3QSVd8dDUxx7q7AUUDzOg25OVB3ULywb8ruivj6tAR0daYkU3ACzWMYZoH4BNEMh4Za6DI7lBNf04=";
//设置海关相关信息配置
record.setSign(sign);
record.setApplycode(customsProperties.getApplyCode());
record.setApplyname(customsProperties.getApplyName());
record.setCopcode(customsProperties.getCopCode());
record.setHostid(customsProperties.getTransarrive().getHostId());
record.setCertno(customsProperties.getTransarrive().getCertNo());
//操作员IC卡号
record.setInputopid(customsProperties.getInputOpId());
// 操作员姓名
record.setInputopname(customsProperties.getInputOpName());
//签名时间
record.setSigndate(new Date());
ResultJson resultJson= new ResultJson();
try{
String nowStr = DateUtil.getCurrentTime17();
//从配置文件读取
String messageID = "CN_"
+ "TRANSARRIVE_EXPORT_DECLARE"
+ "_1P0_"
+ record.getCustomscode()+
"_" + nowStr;
Context context = new Context();
context.setVariable("declare",record);
context.setLocale(Locale.SIMPLIFIED_CHINESE);
//生成的文件名
String fileName = messageID + ".xml";
//生成的报文路径,带斜杠
String filePath = customsProperties.getTransarrive().getXmlSave()+fileName;
File file = ResourceUtils.getFile(filePath);
/**
* 中文的地方在模板中要用utext,否则中文会被 escape 转义
*/
String xmlStr = templateEngine.process("transToArrive/EXPORT_DECLARE_TPL.xml",context);
log.info("[XML-INFO]\n{}",xmlStr);
boolean valied = XmlTools.validateXMLSchemaThrowError("xsd/ETAImportMessage.xsd", xmlStr);
if (valied) {
FileUtils.writeStringToFile(file, xmlStr, StandardCharsets.UTF_8);
//发送报文到MQ
mq.send(xmlStr);
//更新发送状态
record.setDstatus("001");
mapper.updateStatusByPrimaryKey(record);
// 插入发送日志
sendlog(record);
resultJson.setCode("200");
resultJson.setMsg("报文发送成功");
log.info("[XML-BUILD]- 报文已生成到目录{}",filePath);
}
}catch (FileNotFoundException e){
e.printStackTrace();
resultJson.setCode("401");
resultJson.setMsg("报文发送失败,文件未找到");
resultJson.setError(e.toString());
log.error("报文发送失败,[{}]文件未找到",e.toString());
}catch (IOException e) {
e.printStackTrace();
resultJson.setCode("402");
resultJson.setMsg("报文发送失败,文件读取失败");
resultJson.setError(e.toString());
log.error("报文发送失败,[{}]文件IO读取失败",e.toString());
}catch (TemplateInputException e){
resultJson.setCode("404");
resultJson.setMsg("报文发送失败,报文模板未找到");
resultJson.setError(e.toString());
log.error("报文发送失败,[{}]报文模板未找到",e.toString());
}catch (SAXParseException e){
log.error("[XML-ERR]-xml报文校验失败:[{}]", e.getMessage());
resultJson.setCode("400");
resultJson.setMsg("报文发送失败,报文校验不通过");
resultJson.setError(e.getMessage());
} catch (Exception ignored){
resultJson.setCode("405");
resultJson.setMsg("报文发送失败,其他错误");
resultJson.setError(ignored.toString());
log.error("[XML-ERR]-报文发送失败,其他错误:[{}]", ignored.getMessage());
}
return resultJson;
}
private void checkClientseqno(String clientSeqno) throws TransArriveException {
if (clientSeqno.isEmpty() && clientSeqno.length() != 18){
throw new TransArriveException("[ClientSeqNO-ERR] clientseqno信息不正确,应为18位的字符串");
}
}
@Override
public ResultJson batchSend(List<String> autoIDlist) {
ResultJson resultJson = new ResultJson();
List<TRANSTOARRIVEEXPORT> list = selectListByAutoIDS(autoIDlist);
for (TRANSTOARRIVEEXPORT manifestAirChange : list) {
resultJson = send(manifestAirChange);
/*if (!result){
message = message + manifestAirChange.getWaybillnomaster() +"发送失败\n";
}*/
}
return resultJson;
}
@Override
public List<TRANSTOARRIVEEXPORT> selectListByAutoIDS(List<String> autoIDlist) {
List<TRANSTOARRIVEEXPORT> list = mapper.selectListByAutoIDS(autoIDlist);
return list;
}
private void sendlog(TRANSTOARRIVEEXPORT record){
TransToArriveExportResonse toArriveExportResonse = new TransToArriveExportResonse();
toArriveExportResonse.setAutoid(UUID.randomUUID().toString());
toArriveExportResonse.setOperator(record.getUsername());
toArriveExportResonse.setPcs(record.getPackno());
toArriveExportResonse.setWgt(record.getGrosswt());
toArriveExportResonse.setResponsedate(new Date());
toArriveExportResonse.setClientseqno(record.getAutoid());
toArriveExportResonse.setType("E");
//发送记录
StringBuilder sb = new StringBuilder("申报已发送:转关方式[")
.append(record.getTrnmode())
.append("],")
.append("运单号[")
.append(record.getBillno())
.append("]");
toArriveExportResonse.setResponsemessage(sb.toString());
toArriveExportResonseDao.insertSelective(toArriveExportResonse);
}
}