MawbService.java 10.7 KB
package com.erry.iclear.dateInterface.service;

import com.arronlong.httpclientutil.HttpClientUtil;
import com.arronlong.httpclientutil.common.HttpConfig;
import com.arronlong.httpclientutil.common.HttpHeader;
import com.erry.iclear.dateInterface.api.request.Mawb;
import com.erry.iclear.dateInterface.api.request.MawbDetail;
import com.erry.iclear.dateInterface.api.response.IClearCheckResponseResult;
import com.erry.iclear.dateInterface.api.response.ResultRS;
import com.erry.iclear.dateInterface.entity.ChmConsignment;
import com.erry.iclear.dateInterface.entity.ChmConsignmentDetail;
import com.erry.iclear.dateInterface.repository.MawbRepository;
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 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.stereotype.Service;

import java.util.*;

/**
 * @author Administrator
 */
@Service
@Slf4j
public class MawbService {

    private Gson gson = new Gson();

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

    @Autowired
    private MawbRepository mawbRepository;

    @Autowired
    private ChmConsignmentService consignmentService;

    @Autowired
    private ChmConsignmentDetailService consignmentDetailService;

    /**
     * 保存主单Request
     * @param mawb
     */
    public Map<String,String> saveMawb(Mawb mawb){
        Map<String,String> map = new HashMap<>();
        try{
            log.info("saveMawb start>>>>>>>>>>>");
            Mawb mawb1 = mawbRepository.save(mawb);
            log.info("saveMawb end>>>>>>>>>>>");
            map.put("success","true");
            map.put("msg",mawb1.getMsgId().toString());
        }catch (Exception e){
            log.error("saveMawb() error:"+e.getMessage());
            map.put("success","false");
            map.put("msg",e.getMessage());
        }
        return map;
    }


    public String checkMawbBaseInfo(Mawb mawb){

        if(StringUtil.isEmpty(mawb.getOverseasConsigneeEname())){
            return "参数overseasConsigneeEname不能为空";
        }
        if(StringUtil.isEmpty(mawb.getEntyPortCode())){
            return "参数entyPortCode不能为空";
        }
        if(StringUtil.isEmpty(mawb.getOwnerCodeScc())){
            return "参数ownerCodeScc不能为空";
        }
        if(StringUtil.isEmpty(mawb.getTradeCodeScc())){
            return "参数tradeCodeScc不能为空";
        }
        if(StringUtil.isEmpty(mawb.getTransMode())){
            return "参数transMode不能为空";
        }
        if(StringUtil.isEmpty(mawb.getWrapType())){
            return "参数wrapType不能为空";
        }
        if(StringUtil.isEmpty(mawb.getTradeName())){
            return "参数tradeName不能为空";
        }
        if(StringUtil.isEmpty(mawb.getInsurCurr())){
            return "参数insurCurr不能为空";
        }
        if(StringUtil.isEmpty(mawb.getIePort())){
            return "参数iePort不能为空";
        }
        if(StringUtil.isEmpty(mawb.getFeeMark())){
            return "参数feeMark不能为空";
        }
        if(StringUtil.isEmpty(mawb.getFeeCurr())){
            return "参数feeCurr不能为空";
        }
        if(StringUtil.isEmpty(mawb.getEdiId())){
            return "参数ediId不能为空";
        }
        if(StringUtil.isEmpty(mawb.getDistinatePort())){
            return "参数distinatePort不能为空";
        }
        if(StringUtil.isEmpty(mawb.getDeclTrnRel())){
            return "参数declTrnRel不能为空";
        }
        if(StringUtil.isEmpty(mawb.getCutMode())){
            return "参数cutMode不能为空";
        }
        if(StringUtil.isEmpty(mawb.getCustomMaster())){
            return "参数customMaster不能为空";
        }
        if(StringUtil.isEmpty(mawb.getBillNo())){
            return "参数billNo不能为空";
        }
        if(StringUtil.isEmpty(mawb.getAgentName())){
            return "参数agentName不能为空";
        }
        if(StringUtil.isEmpty(mawb.getAgentCode())){
            return "参数agentCode不能为空";
        }

        List<MawbDetail> mawbDetailList = mawb.getMawbDetailList();
        if(Objects.nonNull(mawbDetailList)){
            for (int i = 0, maxi = mawbDetailList.size(); i < maxi; i++) {
                MawbDetail mawbDetail = mawbDetailList.get(i);
                if(StringUtil.isEmpty(mawbDetail.getDetailId())){
                    return "参数detailId不能为空";
                }
                if(StringUtil.isEmpty(mawbDetail.getCodeTS())){
                    return "参数codeTS不能为空";
                }
                if(StringUtil.isEmpty(mawbDetail.getDutyMode())){
                    return "参数dutyMode不能为空";
                }
                if(StringUtil.isEmpty(mawbDetail.getFirstUnit())){
                    return "参数firstUnit不能为空";
                }
                if(StringUtil.isEmpty(mawbDetail.getGunit())){
                    return "参数gunit不能为空";
                }
                if(StringUtil.isEmpty(mawbDetail.getGmodel())){
                    return "参数gmodel不能为空";
                }
                if(StringUtil.isEmpty(mawbDetail.getGname())){
                    return "参数gname不能为空";
                }
                if(StringUtil.isEmpty(mawbDetail.getGno())){
                    return "参数gno不能为空";
                }
                if(StringUtil.isEmpty(mawbDetail.getOriginCountry())){
                    return "参数originCountry不能为空";
                }
                if(StringUtil.isEmpty(mawbDetail.getTradeCurr())){
                    return "参数tradeCurr不能为空";
                }
                if(StringUtil.isEmpty(mawbDetail.getDestinationCountry())){
                    return "参数destinationCountry不能为空";
                }
                if(StringUtil.isEmpty(mawbDetail.getDistrictCode())){
                    return "参数districtCode不能为空";
                }
            }
        }

        return "";
    }


    /**
     * 校验主单
     * @param mawb
     * @return
     */
    public ResultRS checkMawb(Mawb mawb){
        ResultRS result = new ResultRS(Boolean.FALSE,null,null);
        try{
            log.info("checkMawb start>>>>>>>>>>  mawb:"+gson.toJson(mawb));

            //1、将请求转换为运单
            ChmConsignment chmConsignment=consignmentService.convertToChmConsignment(mawb,null,"IDE-ICLEAR-API");
            if(Objects.equals(chmConsignment,null)){
                result.setCode("30001");
                result.setMsg("数据格式不正确");
                return result;
            }
            //校验成交方式 成交方式为CIF时  '参数feeCurr不能为空','参数feeMark不能为空'',参数feeRate不能为空','参数insurCurr不能为空', '参数insurRate不能为空'
            result = this.notBlankCheck(mawb,chmConsignment.getDealMode());
            if(!result.getSuccess()){
                return result;
            }

            List<ChmConsignmentDetail> chmConsignmentDetailList = new ArrayList<>();
            mawb.getMawbDetailList().forEach(detail ->{
                chmConsignmentDetailList.add(consignmentDetailService.convertToChmConsignmentDetail(detail,chmConsignment));
            });
            chmConsignment.setChmConsignmentDetailList(chmConsignmentDetailList);

            //调用IClear接口进行运单数据校验
            String objJson = JsonToJava.toJson(chmConsignment);
//            Header[] headers = HttpHeader.custom()
//                    .other("Content-Type", "application/json")
//                    .build();
//            log.info("******************* : " + this.iclearCheckUrl);
//            //推送海关回执数据
//            HttpConfig config = HttpConfig.custom()
//                    .headers(headers)
//                    .url(this.iclearCheckUrl)
//                    .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.iclearCheckUrl,objJson,headerMap);



            IClearCheckResponseResult iclearCheckResponseResult = JsonToJava.parseJson(json, IClearCheckResponseResult.class);
            result.setSuccess(iclearCheckResponseResult.getSuccess());
            result.setCode(iclearCheckResponseResult.getCode());
            result.setMsg(iclearCheckResponseResult.getMessage());
            log.info("<<<<<<<< checkMawb start >>>>>>>>>>  mawb:"+gson.toJson(mawb) +" result :" +result.getSuccess() +" iclear check response : " + json + " status : " + result.getSuccess());
        }catch (Exception e){
            log.error("checkMawb() error:" + e.getMessage());
        }
        return result;
    }

    /**
     * 校验成交方式 成交方式为CIF时  '参数feeCurr不能为空','参数feeMark不能为空'',参数feeRate不能为空','参数insurCurr不能为空', '参数insurRate不能为空'
     * @param mawb
     * @param dealMode 转换过的成交方式
     * @return
     */
    private ResultRS notBlankCheck(Mawb mawb,String dealMode) {
        ResultRS result = new ResultRS(Boolean.TRUE, null, null);
        //成交方式为CIF时  '参数feeCurr不能为空','参数feeMark不能为空'',参数feeRate不能为空','参数insurCurr不能为空', '参数insurRate不能为空'
        if(dealMode.equals("CIF、CIP、DDP、DDU、DAP")){
            if(StringUtil.isEmpty(mawb.getFeeCurr())){
                result.setSuccess(false);
                result.setCode("30002");
                result.setMsg("运费币制不能为空");
            }else if(StringUtil.isEmpty(mawb.getFeeMark())){
                result.setSuccess(false);
                result.setCode("30002");
                result.setMsg("运费标记不能为空");
            }else if(mawb.getFeeRate() == null){
                result.setSuccess(false);
                result.setCode("30002");
                result.setMsg("运费/率不能为空");
            }else if(StringUtil.isEmpty(mawb.getInsurCurr())){
                result.setSuccess(false);
                result.setCode("30002");
                result.setMsg("保险费币制不能为空");
            }else if(mawb.getInsurRate() == null){
                result.setSuccess(false);
                result.setCode("30002");
                result.setMsg("保险费/率不能为空");
            }
        }
        return result;
    }
}