CoustomAnalysisServiceImp.java 7.1 KB
package com.tianbo.analysis.service.imp;

import com.tianbo.analysis.dao.CUSTOM_RESPONSEMapper;
import com.tianbo.analysis.model.CUSTOM_RESPONSE;
import com.tianbo.analysis.model.CustomReception;
import com.tianbo.analysis.service.CoustomAnalysisService;
import com.tianbo.util.Date.DateUtil;
import com.tianbo.util.regular.RegularUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.UUID;

@Service
@Slf4j
public class CoustomAnalysisServiceImp implements CoustomAnalysisService {

    @Autowired
    CUSTOM_RESPONSEMapper custom_responseMapper;



    @Override
    public int insertRecept(CustomReception cus, String errorCodes, String errorTexts) throws ParseException {

        RegularUtil regularUtil = new RegularUtil();

        CUSTOM_RESPONSE custom_response = new CUSTOM_RESPONSE();
        custom_response.setId(UUID.randomUUID().toString());

        String awb = "";
        String awbh = "";
        if (cus.getWayBillMaster()!=null && cus.getWayBillMaster().length()>0){
            StringBuffer stringBuffer = new StringBuffer(cus.getWayBillMaster());
            StringBuffer insert = stringBuffer.insert(3, "-");
            String s = insert.toString();
            //设置 主单号
            awb = s;
        }


        if (cus.getWayBillSecond()!=null && cus.getWayBillSecond().length()>0){
            //取分单号
            String[] awbhArr = cus.getWayBillSecond().split("_");
            awbh = awbhArr[1];
        }


        // 主单号
        custom_response.setAwbano(awb);
        // 分单号
        custom_response.setAwbhno(awbh);
        // 判断日期是否为空
        String flightDate = cus.getFlightDate();
        if (!StringUtils.isEmpty(flightDate)){

            Date date = DateUtil.formatByyyyyMMdd(flightDate);
            // 航班日期
            custom_response.setFlightdate(date);
        }

        String flightNo = cus.getFlightNo();
        if (!StringUtils.isEmpty(flightNo)){

            // 承运人代码
            custom_response.setCarrier(flightNo.substring(0, 2));

            // 航班号
            custom_response.setFlightno(flightNo.substring(2));
        }

        // 报文发送序号
        custom_response.setCusmsgid(cus.getMessageID());
        // 回执发送者id
        custom_response.setCussenderid(cus.getSenderID());
        // 回执接受这id
        custom_response.setCusreciverid(cus.getReceiverID());
        // 报文版本
        custom_response.setCusversion(cus.getVersion());
        // 对应海关的functioncode业务代码
        custom_response.setCusfunctioncode(cus.getFunctionCode());
        // 报文类型
        custom_response.setBustype(cus.getMessageType());


        // 设置时区
//        SimpleDateFormat bjSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        bjSdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
//        String format = bjSdf.format(new Date());
        // 回执接受时间
        custom_response.setCusresrcvtime(new Date());
        // 业务发生时间
        custom_response.setBusdate(new Date());
        // 判断日期是否为空
        String sendTime = cus.getSendTime();
        if (!StringUtils.isEmpty(sendTime)){

            Date date = DateUtil.fromatByyyMMdd_hhmmssSSS(sendTime);
            // 回执报文  海关发送时间
            custom_response.setCusressendtime(date);
        }

        // 回执Code代码
        String responseCode = cus.getResponseCode();
        // 回执内容
        String responseText = cus.getResponseText();
        // 判断是否为空
        if (responseText != null && responseCode != null){
            // 回执状态
            String cusText = regularUtil.cusText(responseText);
            custom_response.setCusresstatus(cusText);
            // 回执Code代码
            custom_response.setCusrescode(responseCode);
            // 回执内容
            custom_response.setCusrestext(responseText);

        }else {
            // 回执状态
            String substring = errorTexts.substring(0, 5);
            custom_response.setCusresstatus(substring);
            // 回执Code代码
            custom_response.setCusrescode(errorCodes);
            // 回执内容
            custom_response.setCusrestext(errorTexts);
        }

        // 添加
        int insert = custom_responseMapper.insert(custom_response);
        if (insert > 0){

            return 1;
        }else {

            return 0;
        }
    }


//    @Override
//    public int insertRecept(CustomReception customReception){
//
//        String awbA = customReception.getWayBillMaster();
//        String awbH = customReception.getWayBillSecond();
//        // 回执内容
//        String reception = customReception.getResponseText();
//        String flightDateStr = customReception.getFlightDate();
//
//        // 实体
//        CUSTOM_RESPONSE custom_response = new CUSTOM_RESPONSE();
//        // 判断日期是否为空
//        if (!StringUtils.isEmpty(flightDateStr)){
//
//            Date  flightDate = DateUtil.formatByyyyyMMdd(flightDateStr);
//
//            //处理主单格式,将海关回执的主单号58019316861,变为580-19316861
//            if (awbA!=null && awbA.length()>0){
//                StringBuffer stringBuffer = new StringBuffer(awbA);
//                stringBuffer.insert(3,"-");
//                awbA = stringBuffer.toString();
//            }
//
//
//            // 查询是否存在  先用messageId查询
//
//            // 单号 航班号 航班日期
//
//
//            //有分单号 更新分单
//            if(awbA!=null && awbA.length()>0 && awbH!=null && awbH.length()> 0){
//
//                // 取分单号
//                String[] awbhArr = awbH.split("_");
//                String awbh = awbhArr[1];
//                // 分单号 赋值
//                custom_response.setAwbhno(awbh);
//                // 回执内容
//                custom_response.setCusrestext(reception);
//
//                //更新分单回执
//                int i = custom_responseMapper.updateAwbhReceiption(custom_response);
//
//                if (i > 0){
//                    return 1;
//                }
//
//                return 0;
//            }
//
//
//            if (awbA!=null && awbA.length()>0){
//
//                // 设置主单回执、航班号、航班日期
//                custom_response.setCusrestext(reception);
//                custom_response.setFlightno(customReception.getFlightNo());
//                custom_response.setFlightdate(flightDate);
//
//                //更新主单回执
//                int i = custom_responseMapper.updateAwbReceiption(custom_response);
//
//                if (i > 0){
//                    return 1;
//                }
//                return 0;
//            }
//
//        }
//        //未进入主分单解析 返回0 失败
//        return 0;
//    }


}