BillAllocateCheck.groovy 5.8 KB
package com.sy.groovy

import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.sy.model.Feign_Allocate_Search
import com.sy.model.GatherInfo
import com.sy.model.LAND_BUSINEESTYPE_LIST_INFO
import com.sy.model.LandBusinessTypeList
import com.sy.service.NmmsService
import com.sy.service.impl.GatherInfoHandle
import org.basis.enhance.groovy.entity.ExecuteParams
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.context.ApplicationContext

import javax.validation.constraints.NotNull

/**
 * 单证验放
 * 舱单分拨申请验放
 */
class BillAllocateCheck extends Script implements ChannelCheckScript {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    private final String FENBO =               "装载运单的分拨申请舱单未通过校验";

    /**过磅重量判定
    * 传入gatherInfo,三大属性验证,二维码/车牌/过卡重量
     * 适用于分拨业务或者调拨业务
    */
    Boolean check(ExecuteParams executeParams) {
        try{
            GatherInfo gatherInfo = (GatherInfo) executeParams.get("GatherInfo");
            LandBusinessTypeList landBusinessTypeList = (LandBusinessTypeList) executeParams.get("ChanelFormInfo");
            List<LAND_BUSINEESTYPE_LIST_INFO> listinfos = (List<LAND_BUSINEESTYPE_LIST_INFO>) executeParams.get("ChanelFormBillLists");


            // 调用方法
            ApplicationContext context = getContext();
            // 获取容器中的bean
            GatherInfoHandle gatherInfoHandle = context.getBean(GatherInfoHandle.class);

//            boolean allocatCheck = checkNmmsAllocate(landBusinessTypeList.getMasterList());
            boolean allocatCheckByList = checkNMMSAllocateByList(listinfos);

            if(!allocatCheckByList){
                logger.error("[分拨业务]-分拨申请舱单未通过校验:"+FENBO);
                gatherInfoHandle.sendBw(gatherInfo,false,FENBO,landBusinessTypeList,listinfos);
                return false;
            }
            return true;

        }catch (Exception e){
            e.printStackTrace();
            logger.error("[BillAllocateCheck-ERROR]:",e);
            return false;
        }
    }

    @Override
    Object run() {
        return null
    }

    // 获取spring容器
    ApplicationContext getContext() {
        // 获取spring IOC容器
        ApplicationContext context = applicationContext;
        return context;
    }

    /**
     * 新舱单分拨申请数据查询
     * @param waybill 查询的运单号
     * @return
     */
    private Map nmmsAllocate(@NotNull String waybill){
        logger.info("新舱单查询分拨申请数据开始");
        Feign_Allocate_Search feignAllocateSearch = new Feign_Allocate_Search(waybill,0,10);
        ApplicationContext context = getContext();
        NmmsService nmmsService = context.getBean(NmmsService.class)
        Map map = nmmsService.getAllocate(feignAllocateSearch);
        logger.info(""+map);
        return map;
    }


    /**
     * 分拨申请查询
     * @param waybill
     * @return
     */
    private boolean checkNmmsAllocate(String waybill){
        if (waybill.length() < 1) {
            return false;
        }
        waybill = waybill.replace("-", "");
        //中文逗号替换
        waybill = waybill.replace(",", ",");
        String[] maifest = waybill.split(",");
        logger.info("运单列表:" + waybill);
        for (String awb : maifest) {
            Map map= nmmsAllocate(awb);
            if (map.containsKey("reslut")){
                String result = map.get("reslut").toString();
                JSONObject jsonObject = JSON.parseObject(result);
                if (jsonObject.containsKey("ds")) {
                    JSONArray ds = jsonObject.getJSONArray("ds");
                    JSONObject awbinfo = ds.getJSONObject(0);
                    if (awbinfo.containsKey("RECEIPTINFORMATION")){
                        if (awbinfo.getString("RECEIPTINFORMATION").contains("39301") || awbinfo.getString("RECEIPTINFORMATION").contains("39103")){
                            logger.info("运单:{},分拨回执查询结果:{}",awb,awbinfo.getString("RECEIPTINFORMATION"));
                        }else {
                            logger.info("运单{}分拨申请回执不正常:{}",awb,awbinfo.getString("RECEIPTINFORMATION"));
                            return false;
                        }
                    }
                }else {
                    logger.info("运单:{}分拨申请回执未查询到,或未进行分拨申请",awb);
                    return false;
                }
            }else{
                logger.info("运单:{}分拨申请回执未查询到,或未进行分拨申请",awb);
                return false;
            }

        }
        return true;
    }

    boolean checkNMMSAllocateByList(List<LAND_BUSINEESTYPE_LIST_INFO> listinfos){
        //单证类型- 提运单
        String BILL_TYPE_WAYBILL = "B"
        //单证类型-集装器
        String BILL_TYPE_ULD = "C"
        if (listinfos!=null && !listinfos.isEmpty() ){
            for(LAND_BUSINEESTYPE_LIST_INFO item : listinfos){
                if (BILL_TYPE_WAYBILL.equals(item.getExt4())){
                    logger.info("分拨申请验放-单证[{}]:提运单",item.getAwba());
                    if (!checkNmmsAllocate(item.getAwba())){
                        return false;
                    }
                }
                if (BILL_TYPE_ULD.equals(item.getExt4())){
                    logger.info("分拨申请验放-单证[{}]:集装器",item.getAwba());
                }else {
                    logger.info("分拨申请验放-单证[{}]:其他类型[{}]",item.getAwba(),item.getExt4());
                }
            }
            return true;
        }

        return false;
    }
}