FedexDataInterface.java 16 KB
package com.erry.iclear.dateInterface.api;


import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.arronlong.httpclientutil.common.HttpHeader;
import com.arronlong.httpclientutil.exception.HttpProcessException;
import com.erry.iclear.dateInterface.ErrorCodeEnum;
import com.erry.iclear.dateInterface.api.request.*;
import com.erry.iclear.dateInterface.api.response.FedexResultChmRS;
import com.erry.iclear.dateInterface.api.response.FedexResultRS;
import com.erry.iclear.dateInterface.api.response.ResultRS;
import com.erry.iclear.dateInterface.common.Constant;
import com.erry.iclear.dateInterface.entity.*;
import com.erry.iclear.dateInterface.repository.ChmConsignmentRepository;
import com.erry.iclear.dateInterface.repository.IClearApiCheckLogRepository;
import com.erry.iclear.dateInterface.service.*;
import com.erry.iclear.dateInterface.util.HttpUtils;
import com.erry.iclear.dateInterface.util.JsonToJava;
import com.erry.iclear.dateInterface.util.StringUtil;
import com.google.gson.Gson;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.Header;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StopWatch;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/fedex")
@Slf4j
public class FedexDataInterface {
    private Gson gson = new Gson();

    @Autowired
    private FedexService fedexService;

    @Autowired
    private ChmConsignmentService chmConsignmentService;

    @Autowired
    private IClearApiLogService iClearApiLogService;

    @Autowired
    private DeclaredInfoService declaredInfoService;

    @Autowired
    private FedexConsignmentService fedexConsignmentService;

    @Autowired
    private DeclearTaskService declearTaskService;

    @Autowired
    private ChmConsignmentRepository chmConsignmentRepository;

    @Autowired
    private IClearApiCheckLogRepository iClearApiCheckLogRepository;

    @Value("${api.address.iclear-submit-url}")
    String iclearCSubmitUrl;

    @ApiOperation(value = "C类运单数据校验接口", httpMethod = "POST", notes = "ex:  http://192.168.1.75:8080/fedex/checkBASFMawbFedex")
    @PostMapping("/checkMawbFedexC")
    @ResponseBody
    public ResultRS checkBASFMawbFedex(HttpServletRequest request
            ,@RequestBody Fedexc fedexc) throws HttpProcessException {
        String appId=(String) request.getHeader("Appid");
        String appKey=(String) request.getHeader("Appkey");
        String timeStamp=(String) request.getHeader("TimeStamp");
        String token=(String) request.getHeader("Token");
        String addr = request.getRemoteAddr();

        //获取APPINFO信息
        AppInfo appInfo = Constant.appInfoCache.get(appId + appKey);

        TIClearApiCheckLog tIclearApiCheckLog = new TIClearApiCheckLog();
        tIclearApiCheckLog.setRequestAppid(appId);
        tIclearApiCheckLog.setRequestAppkey(appKey);
        tIclearApiCheckLog.setRequestIp(addr);
        tIclearApiCheckLog.setRequestToken(token);
        tIclearApiCheckLog.setConsignmentCode(fedexc.getConsignmentCode());
        tIclearApiCheckLog.setCreateTime(new Date());
        tIclearApiCheckLog.setRequestText(
                new String("requestHeader[appId:"+appId+"," +
                        "appKey:"+appKey+"," +
                        "timeStamp:"+timeStamp+"," +
                        "token:"+token) + "],"
                + fedexc.toString()
        );
        TIClearApiCheckLog tiClearApiCheckLogSave = iClearApiCheckLogRepository.save(tIclearApiCheckLog);
        log.info("checkBASFMawbFedex:"+fedexc.toString());
        log.info("appId:{},appKey:{},timeStamp:{},token:{}",appId,appKey,timeStamp,token);

        ResultRS result = fedexService.checkBasfInterfaceData(fedexc,appInfo);

        StopWatch sw = new StopWatch("checkBASFMawbFedex");
        sw.start("generate FedexMawb");
        fedexc.setAppId(appId);
        FedexMawb saveMawbFedexResult = fedexService.generateFedexMawb(fedexc);
        sw.stop();

        sw.start("checkFedexMawb");
        String msg = result.getMsg();
        String iclearCheckMsg = fedexService.checkFedexMawb(saveMawbFedexResult,appInfo).getMsg();
        if(StringUtil.isEmpty(msg) && StringUtil.isEmpty(iclearCheckMsg)){
            result.setSuccess(Boolean.TRUE);
        }else{
            result.setMsg(msg+";"+iclearCheckMsg);
        }
        sw.stop();

        log.info(sw.prettyPrint());
        if(result.getSuccess()){
            result.setSuccess(Boolean.TRUE);
            result.setCode(ErrorCodeEnum.SUCCESS.getCode());
        }
        tiClearApiCheckLogSave.setResponseText(result.getMsg());
        tiClearApiCheckLogSave.setCode(result.getCode());
        tiClearApiCheckLogSave.setSuccess(result.getSuccess());

        iClearApiCheckLogRepository.save(tiClearApiCheckLogSave);
        log.info(result.toString());
        return result;
    }

    @ApiOperation(value = "Fedex推送运单数据", httpMethod = "POST", notes = "ex:  http://192.168.1.75:8080/fedex/sendMawbFedex")
    @PostMapping("/sendMawbFedex")
    @ResponseBody
    public ResultRS sendMawbFedex(HttpServletRequest request
            , @RequestBody Fedexc fedexc) throws HttpProcessException {
        log.info("接收到请求数据consignmentCode:"+fedexc.getConsignmentCode()+" data:" + gson.toJson(fedexc));
        ResultRS result = new ResultRS(Boolean.FALSE,"","");

        if(Objects.equals(fedexc,null)){
            result.setCode(ErrorCodeEnum.ERROR_7.getCode());
            result.setMsg(ErrorCodeEnum.ERROR_7.getName());
            return result;
        }
        //1、保存请求信息,保存到iClear的  SQLServer
        StopWatch sw = new StopWatch("receice mawbFedex");
        sw.start("save log");

        String appId=(String) request.getHeader("Appid");
        String appKey=(String) request.getHeader("Appkey");
        String timeStamp=(String) request.getHeader("TimeStamp");
        IClearApiLog saveIClearApiLogResult = iClearApiLogService.saveIClearApiLog(iClearApiLogService.convertToIClearApiLogC(fedexc,appId,appKey,new Date(Long.valueOf(timeStamp))));

        fedexc.setAppId(appId);
        FedexMawb saveMawbFedexResult = null;
        if(!Objects.equals(saveIClearApiLogResult,null)){
            log.info("请求保存成功,msgId:"+saveIClearApiLogResult.getMsgId());
            fedexc.setMsgId(saveIClearApiLogResult.getMsgId());
            fedexc.getConsignmentList().forEach(d->{
                d.setMsgId(saveIClearApiLogResult.getMsgId());
            });
            saveMawbFedexResult = fedexService.saveMawbFedex(fedexc);
            if(!Objects.equals(saveMawbFedexResult,null)){
                log.info("请求数据运单号"+fedexc.getConsignmentCode()+"保存成功,msgId:"+saveMawbFedexResult.getMsgId());
            }else{
                log.info("请求数据运单号"+fedexc.getConsignmentCode()+"保存失败,msgId:"+saveMawbFedexResult.getMsgId());
            }
        }else{
            log.info("请求日志保存失败,consignmentCode:"+fedexc.getConsignmentCode());
        }
        sw.stop();


        //2、TODO 校验主单
        sw.start("checkFedexMawb");
        result=fedexService.checkFedexMawb(saveMawbFedexResult,null);
        if(!result.getSuccess()){
            return result;
        }
        sw.stop();

//        //3、TODO 校验子单
//        sw.start("checkMawbDetail");
//        result=mawbDetailServicel.checkMawbDetail(mawb);
//        if(!result.getSuccess()){
//            return result;
//        }
//        sw.stop();

        //4、保存运单到iclear中
        sw.start("processChmConsignment");
        result = chmConsignmentService.processChmConsignmentFedex(saveMawbFedexResult, timeStamp,appId);
        if(!result.getSuccess()){
            return result;
        }
        sw.stop();
        ChmConsignment chmGetId = chmConsignmentRepository.findChmConsignmentByConsignmentCode(saveIClearApiLogResult.getBillNo(), saveIClearApiLogResult.getCreateTime());
        //调用IClear接口进行运单推送EXP002
        String objJson = JsonToJava.toJson(chmGetId.getId());
//        Header[] headers = HttpHeader.custom()
//                .other("Content-Type", "application/json")
//                .build();
//        log.info("******************* : " + this.iclearCSubmitUrl);
//        //推送海关回执数据
//        HttpConfig config = HttpConfig.custom()
//                .headers(headers)
//                .url(this.iclearCSubmitUrl)
//                .encoding("utf-8")
//                .json(objJson);
//        String json = HttpClientUtil.post(config);

        Map<String,String> headerMap = new HashMap<>();
        headerMap.put("Content-Type", "application/json");
        String json = HttpUtils.doPost(this.iclearCSubmitUrl,objJson,headerMap);

        log.info("<<<<<<<< checkfedexMawb start >>>>>>>>>> result :" +result.getSuccess() +" iclear check response : " + json + " status : " + result.getSuccess());

        //5、更新日志
        sw.start("updateIClearApiLog");
        if(!Objects.equals(saveIClearApiLogResult,null)){
            saveIClearApiLogResult.setReceiveResult(result.getSuccess().toString());
            saveIClearApiLogResult.setErrorCode(result.getCode());
            saveIClearApiLogResult.setResponseTime(new Date());
            saveIClearApiLogResult.setSpendTime(sw.getTotalTimeMillis());

            iClearApiLogService.updateIClearApiLog(saveIClearApiLogResult);
        }
        log.info("请求数据consignmentCode:"+fedexc.getConsignmentCode() +" 接收结束");
        sw.stop();
        log.info(sw.prettyPrint());

        //6、return result
        result.setSuccess(Boolean.TRUE);
        result.setCode(ErrorCodeEnum.SUCCESS.getCode());
        return result;
    }

    @ApiOperation(value = "Fedex获取申报信息", httpMethod = "POST", notes = "ex:  http://192.168.1.75:8080/fedex/selectDecListFedex")
    @PostMapping("/selectDecListFedex")
    @ResponseBody
    public FedexResultChmRS selectDecListFedex(HttpServletRequest request
            ,  @RequestBody ConsignmentCode consignmentCode) throws Exception {
        log.info("接收到请求数据consignmentCode:"+consignmentCode.getConsignmentCode()+" data:" + gson.toJson(consignmentCode));
        FedexResultChmRS result = new FedexResultChmRS(Boolean.FALSE,"","",null);
        //获取请求的运单号
        String[] splitConsignment = StringUtil.split(consignmentCode.getConsignmentCode(),",");
        if(splitConsignment.length > 20){
            result.setCode(ErrorCodeEnum.ERROR_30001.getCode());
            result.setMsg("请求信息不能大于20条运单");
            return result;
        }
        if(splitConsignment.length<1){
            result.setCode(ErrorCodeEnum.ERROR_7.getCode());
            result.setMsg(ErrorCodeEnum.ERROR_7.getName());
            return result;
        }
        StopWatch sw = new StopWatch("receice mawbFedex");
        sw.start("save log");

        String appId=(String) request.getHeader("Appid");
        String appKey=(String) request.getHeader("Appkey");
        String timeStamp=(String) request.getHeader("TimeStamp");
        //记录请求的运单
        IClearApiLog saveIClearApiLogResult = iClearApiLogService.saveIClearApiLog(iClearApiLogService.convertToIClearApiLogCselect(consignmentCode.getConsignmentCode(),appId,appKey,new Date(Long.valueOf(timeStamp))));
        //查询请求需要得运单申报信息
        List<DeclaredInfo> declaredInfos = declaredInfoService.selectDeclaredInfo(splitConsignment,appId);
        //构造返回数据
        List<FedexConsignment> fedexConsignments = fedexConsignmentService.fedexConsignmentList(declaredInfos);
        //返回申报数据
        if(fedexConsignments.size() > 0){
            result.setSuccess(Boolean.TRUE);
            result.setConsignmentList(fedexConsignments);
            result.setMsg("操作成功!");
        }else{
            result.setSuccess(Boolean.TRUE);
            result.setMsg("操作成功,无申报数据!");
        }
        //更新记录的请求信息
        if(!Objects.equals(saveIClearApiLogResult,null)){
            saveIClearApiLogResult.setReceiveResult(result.getSuccess().toString());
            saveIClearApiLogResult.setErrorCode(result.getCode());
            saveIClearApiLogResult.setResponseTime(new Date());
            saveIClearApiLogResult.setSpendTime(sw.getTotalTimeMillis());
            iClearApiLogService.updateIClearApiLog(saveIClearApiLogResult);
        }
        //记录返回的申报数据记录
        if(result.getSuccess()){
            declearTaskService.insertDeclareTask(fedexConsignments, saveIClearApiLogResult.getMsgId());
        }
        log.info("Fedex申报数据请求成功!");
        //6、return result
        result.setSuccess(Boolean.TRUE);
        result.setCode(ErrorCodeEnum.SUCCESS.getCode());
        return result;
    }

    @ApiOperation(value = "Fedex获取回执信息", httpMethod = "POST", notes = "ex:  http://192.168.1.75:8080/fedex/selectChmListFedex")
    @PostMapping("/selectChmListFedex")
    @ResponseBody
    public FedexResultRS selectChmListFedex(HttpServletRequest request
            , @RequestBody ConsignmentCode consignmentCode) throws Exception {
        log.info("接收到请求数据consignmentCode:"+consignmentCode.getConsignmentCode()+" data:" + gson.toJson(consignmentCode));
        FedexResultRS result = new FedexResultRS(Boolean.FALSE,"","",null);
        //获取请求的运单号
        String[] splitConsignment = StringUtil.split(consignmentCode.getConsignmentCode(),",");
        if(splitConsignment.length > 20){
            result.setCode(ErrorCodeEnum.ERROR_30001.getCode());
            result.setMsg("请求信息不能大于20条运单");
            return result;
        }
        if(splitConsignment.length<1){
            result.setCode(ErrorCodeEnum.ERROR_7.getCode());
            result.setMsg(ErrorCodeEnum.ERROR_7.getName());
            return result;
        }
        StopWatch sw = new StopWatch("receice mawbFedex");
        sw.start("save log");

        String appId=(String) request.getHeader("Appid");
        String appKey=(String) request.getHeader("Appkey");
        String timeStamp=(String) request.getHeader("TimeStamp");
        //记录请求的运单
        IClearApiLog saveIClearApiLogResult = iClearApiLogService.saveIClearApiLog(iClearApiLogService.convertToIClearApiLogCselect(consignmentCode.getConsignmentCode(),appId,appKey,new Date(Long.valueOf(timeStamp))));
        //查询请求需要得运单申报信息
        //List<DeclaredInfo> declaredInfos = declaredInfoService.selectDeclaredInfo(splitConsignment);
        List<FedexReceipt> chmConsignments = chmConsignmentService.getConsignmentCode(splitConsignment,appId);
        //构造返回数据
        //返回申报数据
        if(chmConsignments.size() > 0){
            result.setSuccess(Boolean.TRUE);
            result.setConsignmentList(chmConsignments);
            result.setMsg("操作成功!");
        }else{
            result.setSuccess(Boolean.TRUE);
            result.setMsg("操作成功,无回执数据!");
        }
        //更新记录的请求信息
        if(!Objects.equals(saveIClearApiLogResult,null)){
            saveIClearApiLogResult.setReceiveResult(result.getSuccess().toString());
            saveIClearApiLogResult.setErrorCode(result.getCode());
            saveIClearApiLogResult.setResponseTime(new Date());
            saveIClearApiLogResult.setSpendTime(sw.getTotalTimeMillis());
            iClearApiLogService.updateIClearApiLog(saveIClearApiLogResult);
        }
        //记录返回的申报数据记录
        if(result.getSuccess()){
            declearTaskService.insertChmTask(chmConsignments, saveIClearApiLogResult.getMsgId());
        }
        log.info("Fedex回执数据请求成功!");
        //6、return result
        result.setSuccess(Boolean.TRUE);
        result.setCode(ErrorCodeEnum.SUCCESS.getCode());
        return result;
    }
}