|
|
package com.tianbo.messagebus.service;
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.tianbo.messagebus.controller.response.ResultJson;
|
|
|
import com.tianbo.messagebus.model.CUSTOM_RESPONSE;
|
|
|
import com.tianbo.messagebus.model.CustomReception;
|
|
|
import com.tianbo.messagebus.model.HEADER;
|
|
|
import com.tianbo.messagebus.myinterface.KafkaReciveApi;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.scheduling.annotation.EnableAsync;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.util.StringUtils;
|
|
|
import java.util.List;
|
|
|
|
|
|
@Service
|
|
|
@Slf4j
|
|
|
@EnableAsync
|
|
|
public class Custom_Response_Processor {
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 账号名
|
|
|
*/
|
|
|
@Value("${message-bus.auth.username}")
|
|
|
private String USER_NAME;
|
|
|
|
|
|
@Autowired
|
|
|
KafkaReciveApi kafkaReciveApi;
|
|
|
|
|
|
@Autowired
|
|
|
Custom_Response_Service custom_response_service;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* feigin从服务直接获取消息
|
|
|
*/
|
|
|
@Async
|
|
|
@Scheduled(fixedRate = 5000)
|
|
|
public void getDataFromFeigin(){
|
|
|
try{
|
|
|
//初始化数据库
|
|
|
CUSTOM_RESPONSE test = custom_response_service.selectByPrimaryKey("111");
|
|
|
log.info("1-开始执行获取任务,获取账号为:{}",USER_NAME);
|
|
|
if(!StringUtils.isEmpty(USER_NAME)){
|
|
|
analysis();
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
log.error("[MAIN-ERR]!!!处理消息出错:{}!!!",e.toString());
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
public void analysis(){
|
|
|
ResultJson<List<String>> listResultJson = kafkaReciveApi.recive(USER_NAME);
|
|
|
log.info("2-获取结果为:{},数量为:{}",listResultJson.toString(),listResultJson.getData().size());
|
|
|
if ("200".equals(listResultJson.getCode()) && listResultJson.getData()!=null && listResultJson.getData().size()>0){
|
|
|
log.info("3-开始处理获取数据");
|
|
|
List<String> dataList = listResultJson.getData();
|
|
|
for (int i = 0; i <dataList.size() ; i++) {
|
|
|
String msg = dataList.get(i);
|
|
|
try{
|
|
|
analysisMessage(msg,i);
|
|
|
}catch (Exception e){
|
|
|
log.error("[Analysis-ERR]!!!{}报文内容解析异常:{},开始处理下一条消息!!!",msg,e.toString());
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
}
|
|
|
}else {
|
|
|
log.info("[CONSUMER-RESULT] - 未获取到消息");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void analysisMessage(String msg,int i){
|
|
|
if (!StringUtils.isEmpty(msg)){
|
|
|
log.info("4-循环处理消息[{}]--->{}<---",i,msg);
|
|
|
JSONObject rootJson = JSON.parseObject(msg);
|
|
|
JSONObject msgJson = rootJson.getJSONObject("MSG");
|
|
|
|
|
|
//回执实体
|
|
|
JSONObject body = msgJson.getJSONObject("BODY");
|
|
|
|
|
|
//报头
|
|
|
HEADER msgHeader = msgJson.getObject("HEADER",HEADER.class);
|
|
|
|
|
|
//判断类型
|
|
|
if ("CDHZ".equals(msgHeader.getSTYPE())){
|
|
|
analysisHZ(body);
|
|
|
}
|
|
|
}else {
|
|
|
log.error("[MSG-ERR]消息为空");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public void analysisHZ(JSONObject body){
|
|
|
log.info("@[一]@消息为舱单回执");
|
|
|
//判断回执类型
|
|
|
JSONObject manifest = body.getJSONObject("Manifest");
|
|
|
JSONObject head = manifest.getJSONObject("Head");
|
|
|
|
|
|
if(head != null){
|
|
|
log.info("@[.]开始回执报头解析");
|
|
|
String messageType = head.getString("MessageType");
|
|
|
String messageID = head.getString("MessageID");
|
|
|
String sendTime = head.getString("SendTime");
|
|
|
String senderID = head.getString("SenderID");
|
|
|
String receiverID = head.getString("ReceiverID");
|
|
|
Integer version = head.getInteger("Version");
|
|
|
Integer functionCode = head.getInteger("FunctionCode");
|
|
|
log.info("@[MessageType:{}]回执报头解析完毕",messageType);
|
|
|
|
|
|
if("MT2201".equals(messageType) || "MT9999".equals(messageType) || "MT3201".equals(messageType)){
|
|
|
|
|
|
analysisBody(messageType,manifest,messageID,sendTime,senderID,receiverID,version,functionCode);
|
|
|
}
|
|
|
}else {
|
|
|
log.info("@[四零一]@缺少Manifest或Head节点");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void analysisBody(String messageType,
|
|
|
JSONObject manifest,
|
|
|
String messageID,
|
|
|
String sendTime,
|
|
|
String senderID,
|
|
|
String receiverID,
|
|
|
Integer version,
|
|
|
Integer functionCode
|
|
|
){
|
|
|
log.info("@[二]@开始解析:{}",messageType);
|
|
|
CUSTOM_RESPONSE custom_response_nmms2 = new CUSTOM_RESPONSE();
|
|
|
messageType = "MT2201";
|
|
|
// 航班信息
|
|
|
JSONObject response = manifest.getJSONObject("Response");
|
|
|
if (response!=null){
|
|
|
JSONObject borderTransportMeans = response.getJSONObject("BorderTransportMeans");
|
|
|
if (borderTransportMeans!=null ){
|
|
|
String flightNo = "UNKONW";
|
|
|
String flightDate = "20101010";
|
|
|
|
|
|
String journeyid = borderTransportMeans.getString("JourneyID");
|
|
|
|
|
|
//运单信息
|
|
|
JSONObject consignment = response.getJSONObject("Consignment");
|
|
|
if (consignment!=null){
|
|
|
JSONObject responseType = consignment.getJSONObject("ResponseType");
|
|
|
JSONObject transportContractDocument = consignment.getJSONObject("TransportContractDocument");
|
|
|
JSONObject associatedTransportDocument = consignment.getJSONObject("AssociatedTransportDocument");
|
|
|
|
|
|
Integer responseCode = 3;
|
|
|
String responseText = "回执报文未有信息";
|
|
|
|
|
|
if (responseType!=null){
|
|
|
//回执代码
|
|
|
responseCode = responseType.getIntValue("Code");
|
|
|
//回执内容
|
|
|
responseText = responseType.getString("Text");
|
|
|
|
|
|
}
|
|
|
String waybillMaster = "00000000000";
|
|
|
if (transportContractDocument!=null){
|
|
|
waybillMaster = transportContractDocument.getString("ID");
|
|
|
}
|
|
|
|
|
|
|
|
|
String waybillSecond="";
|
|
|
|
|
|
if (associatedTransportDocument!=null){
|
|
|
waybillSecond = associatedTransportDocument.getString("ID");
|
|
|
}
|
|
|
|
|
|
|
|
|
CustomReception customReception = new CustomReception( messageType,
|
|
|
flightNo,
|
|
|
flightDate,
|
|
|
waybillMaster,
|
|
|
waybillSecond,
|
|
|
String.valueOf(responseCode),
|
|
|
responseText,
|
|
|
messageID,
|
|
|
sendTime,
|
|
|
senderID,
|
|
|
receiverID,
|
|
|
String.valueOf(version),
|
|
|
String.valueOf(functionCode));
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 如果回执中没有携带航班信息节点,说明是出错报文
|
|
|
* 到发送日志表根据messageid 找到相应的发送日志报文的航班及运单信息,再进行解析
|
|
|
*/
|
|
|
if(!StringUtils.isEmpty(journeyid)){
|
|
|
|
|
|
String[] flightList = journeyid.split("/");
|
|
|
if(flightList.length > 0){
|
|
|
flightNo = flightList[0];
|
|
|
flightDate = flightList[1];
|
|
|
|
|
|
log.info("@[三]@航班信息为:{}",journeyid);
|
|
|
customReception.setFlightNo(flightNo);
|
|
|
customReception.setFlightDate(flightDate);
|
|
|
}
|
|
|
custom_response_nmms2 = new CUSTOM_RESPONSE(customReception);
|
|
|
}else {
|
|
|
custom_response_nmms2 = new CUSTOM_RESPONSE(customReception);
|
|
|
custom_response_nmms2 = custom_response_service.getWaybillInfoByCutomResponse(custom_response_nmms2);
|
|
|
}
|
|
|
log.info("[(三.一)]{{}",custom_response_nmms2);
|
|
|
|
|
|
int ii = custom_response_service.secondAnalysisReception(custom_response_nmms2);
|
|
|
|
|
|
log.info("@[四]@回执解析完毕[{}]\n@@^PARSE SUCCESS^@@",ii);
|
|
|
}else {
|
|
|
log.info("@[四零零]@回执报文没有运单节点,解析失败.");
|
|
|
}
|
|
|
}else {
|
|
|
log.info("@[四零三]缺少航班信息节点,解析失败");
|
|
|
}
|
|
|
|
|
|
|
|
|
}else {
|
|
|
log.info("@[四零二]缺少回执内容节点,解析失败");
|
|
|
}
|
|
|
}
|
|
|
} |
...
|
...
|
|