BasfDataInterface.java 12.7 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.api.request.*;
import com.erry.iclear.dateInterface.api.response.BasfResultChmRS;
import com.erry.iclear.dateInterface.api.response.BasfResultRS;
import com.erry.iclear.dateInterface.api.response.ResultRS;
import com.erry.iclear.dateInterface.entity.ChmConsignment;
import com.erry.iclear.dateInterface.entity.DeclaredInfo;
import com.erry.iclear.dateInterface.entity.IClearApiLog;
import com.erry.iclear.dateInterface.repository.ChmConsignmentRepository;
import com.erry.iclear.dateInterface.service.*;
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.Date;
import java.util.List;
import java.util.Objects;

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

    @Autowired
    private BasfService basfService;

    @Autowired
    private ChmConsignmentService chmConsignmentService;

    @Autowired
    private IClearApiLogService iClearApiLogService;

    @Autowired
    private DeclaredInfoService declaredInfoService;

    @Autowired
    private BasfConsignmentService basfConsignmentService;

    @Autowired
    private DeclearTaskService declearTaskService;

    @Autowired
    private ChmConsignmentRepository chmConsignmentRepository;

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


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

        if(Objects.equals(basfc,null)){
            result.setCode("-7");
            result.setMsg("请求信息不能为空");
            return result;
        }
        //1、保存请求信息,保存到iClear的  SQLServer

        StopWatch sw = new StopWatch("receice mawbBasf");
        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(basfc,appId,appKey,new Date(Long.valueOf(timeStamp))));

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


        //2、TODO 校验主单
        sw.start("checkBasfMawb");
        result=basfService.checkBasfMawb(saveMawbBasfResult);
        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.processChmConsignmentBasf(saveMawbBasfResult, 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);
        log.info("<<<<<<<< checkbasfMawb 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:"+basfc.getConsignmentCode() +" 接收结束");
        sw.stop();
        log.info(sw.prettyPrint());

        //6、return result
        result.setSuccess(Boolean.TRUE);
        result.setCode("0");
        return result;
    }

    @ApiOperation(value = "BASF获取申报信息", httpMethod = "POST", notes = "ex:  http://192.168.1.75:8080/basf/selectDecListBasf")
    @PostMapping("/selectDecListBasf")
    @ResponseBody
    public BasfResultChmRS selectDecListBasf(HttpServletRequest request
            , @Validated @RequestBody ConsignmentCode consignmentCode) throws Exception {
        log.info("接收到请求数据consignmentCode:"+consignmentCode.getConsignmentCode()+" data:" + gson.toJson(consignmentCode));
        BasfResultChmRS result = new BasfResultChmRS(Boolean.FALSE,"","",null);
        //获取请求的运单号
        String[] splitConsignment = StringUtil.split(consignmentCode.getConsignmentCode(),",");
        if(splitConsignment.length > 20){
            result.setCode("30001");
            result.setMsg("请求信息不能大于20条运单");
            return result;
        }
        if(splitConsignment.length<1){
            result.setCode("-7");
            result.setMsg("请求信息不能为空");
            return result;
        }
        StopWatch sw = new StopWatch("receice mawbBasf");
        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<BasfConsignment> basfConsignments = basfConsignmentService.basfConsignmentList(declaredInfos);
        //返回申报数据
        if(basfConsignments.size() > 0){
            result.setSuccess(Boolean.TRUE);
            result.setConsignmentList(basfConsignments);
            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(basfConsignments, saveIClearApiLogResult.getMsgId());
        }
        log.info("BASF申报数据请求成功!");
        //6、return result
        result.setSuccess(Boolean.TRUE);
        result.setCode("0");
        return result;
    }

    @ApiOperation(value = "BASF获取回执信息", httpMethod = "POST", notes = "ex:  http://192.168.1.75:8080/basf/selectChmListBasf")
    @PostMapping("/selectChmListBasf")
    @ResponseBody
    public BasfResultRS selectChmListBasf(HttpServletRequest request
            , @Validated @RequestBody ConsignmentCode consignmentCode) throws Exception {
        log.info("接收到请求数据consignmentCode:"+consignmentCode.getConsignmentCode()+" data:" + gson.toJson(consignmentCode));
        BasfResultRS result = new BasfResultRS(Boolean.FALSE,"","",null);
        //获取请求的运单号
        String[] splitConsignment = StringUtil.split(consignmentCode.getConsignmentCode(),",");
        if(splitConsignment.length > 20){
            result.setCode("30001");
            result.setMsg("请求信息不能大于20条运单");
            return result;
        }
        if(splitConsignment.length<1){
            result.setCode("-7");
            result.setMsg("请求信息不能为空");
            return result;
        }
        StopWatch sw = new StopWatch("receice mawbBasf");
        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<BasfcReceipt> chmConsignments = chmConsignmentService.getConsignmentCode(splitConsignment,appId);
        //构造返回数据
        //List<BasfConsignment> basfConsignments = basfConsignmentService.basfConsignmentList(declaredInfos);
        //返回申报数据
        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("BASF回执数据请求成功!");
        //6、return result
        result.setSuccess(Boolean.TRUE);
        result.setCode("0");
        return result;
    }
}