CustomsLockServiceImpl.java 9.1 KB
package com.sy.service.lock;

import com.alibaba.fastjson.JSONObject;
import com.sy.mapper.LandBusinessTypeListMapper;
import com.sy.mapper.LandListDao;
import com.sy.model.GatherInfo;
import com.sy.model.LAND_BUSINEESTYPE_LIST_INFO;
import com.sy.model.LandBusinessTypeList;
import com.sy.model.LandList;
import com.sy.service.CustomsLockService;
import com.sy.service.LandBusListService;
import com.sy.service.RedisService;
import com.sy.service.impl.GatherInfoHandle;
import com.sy.utils.FileTool;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 关锁业务处理
 * @author mrz
 */
@Slf4j
@Service
public class CustomsLockServiceImpl implements CustomsLockService {

    @Resource
    private LandListDao landListDao;

    @Autowired
    private RedisService redisService;

    @Autowired
    private LandBusListService landBusListService;

    @Resource
    private LandBusinessTypeListMapper landBusinessTypeListMapper;

    /**
     * 缓存关锁相关前缀
     */
    private static String LOCK_PRE="LOCK-";

    @Override
    public void lockNotice() {

    }

    @Override
    public void unLockNotice() {

    }

    @Override
    public void cacheWrite(GatherInfo info) {
        redisService.set(LOCK_PRE+info.getBarcode(),
                JSONObject.toJSONString(info),
                60*60*6);
    }

    @Override
    public GatherInfo cacheRead(String barcode) {
        String jsonStr = redisService.get(LOCK_PRE+barcode);
        if (StringUtils.isNotEmpty(jsonStr)){
            GatherInfo gatherInfo = JSONObject.parseObject(jsonStr, GatherInfo.class);
            return gatherInfo;
        }
        return null;
    }

    @Override
    public void localGatherInfoHandle(GatherInfo gatherInfo) {
        GatherInfoHandle gatherInfoHandle = new GatherInfoHandle();
        gatherInfoHandle.handel(gatherInfo);
    }

    @Override
    public boolean lockNoticeCheck(GatherInfo info) {
        if (info!=null && StringUtils.isNotEmpty(info.getBarcode())) {
            //todo:这里后期判定可以复用场站白名单验证
//            FileTool.readProperties("station").contains(info.getAreaid());
            /**
             * 申请业务类型检查
             * 调拨业务及分拨业务装载货物为单证的,必须有关锁号
             * 就算申请信息里面没关锁号也要通知
             */
            LandBusinessTypeList landBusinessTypeList = landBusListService.getLandBusinessTypeListByGather(info);
            if (landBusinessTypeList!=null){
                if ("调拨业务".equals(landBusinessTypeList.getBusinesstype()) || "分拨业务".equals(landBusinessTypeList.getBusinesstype())) {

                    String sfAreaid = "4600541001";
                    String zbqAreaid = "4600329012";
                    String zbqN3Areaid = "4612199001";


                    String carEndstationList = redisService.get(info.getVename() + "_endstationList");
                    if (StringUtils.isNotEmpty(info.getVename()) && StringUtils.isNotEmpty(carEndstationList)){

                        //涉及综保区场站的申请不核验关锁业务.
                        if (carEndstationList.contains(zbqAreaid)) {
                            log.info("[LOCK-CHECK]-申报场站列表包含综保区,不处理关锁业务,当前场站:{}",landBusinessTypeList.getEndstation());
                            return false;
                        }

                        //综保区场站及内三不通知,顺丰场站不通知
                        if (zbqAreaid.equals(landBusinessTypeList.getEndstation())
                                || zbqN3Areaid.equals(landBusinessTypeList.getEndstation())
                                || sfAreaid.equals(landBusinessTypeList.getEndstation())
                        ) {
                            log.info("[LOCK-CHECK]-综保区及内三场站,顺丰场站,不处理关锁业务,当前场站:{}",landBusinessTypeList.getEndstation());
                            return false;
                        }else {
                            return true;
                            /**
                             * 单证验证-取消
                             */
//                        List<LAND_BUSINEESTYPE_LIST_INFO> land_busineestype_list_infos = landBusinessTypeList.getLandBusineestypeListInfoList();
//                        if (!land_busineestype_list_infos.isEmpty()){
//                            LAND_BUSINEESTYPE_LIST_INFO list_info = land_busineestype_list_infos.stream().parallel()
//                                    .filter(item -> "B".equals(item.getExt4()))
//                                    .findAny().orElse(null);
//
//                            //包含单证
//                            if (list_info!=null){
//                                log.info("[LOCK-CHECK]-包含单证,开始关锁通知");
//                                return true;
//                            }
//                        }
                        }
                    }else {
                        return false;
                    }
                }
            }else {
                log.info("[LOCK-CHECK]-无对应进出场申请");
                return false;
            }
        }
        return false;
    }

    @Override
    public boolean lockCheck(GatherInfo info) {
        int e = landBusListService.selectFirstLeave(info.getVename(), info.getBarcode(), "E");
        if (e==0){
            //首次离场,通知关锁接口 上锁
            log.info("[UNLOCK-NOTICE]-非顺丰场站业务上锁通知");

            /**
             * 涉及顺丰的场站业务涉及三个场站
             * 顺丰的通道没有读解锁设备
             * 需要在快邮卡口进行解读锁
             */
            //顺丰出通道,首次出不上锁
            String sfExportChanel = "4600010001";
            if (sfExportChanel.equals(info.getChnlno())) {
                log.warn("[UNLOCK-NOTICE]-顺丰货站,不进行上锁通知,{}",info.getBarcode());
                return false;
            }

            return true;
        }else {
            /**
             * 顺丰上锁通知判定,是在第二个离场通道进行施封
             * 当前场站是快邮货站出
             * 有一个离场信息,那么就上锁通知
             */
            //快邮出通道,上锁通知,二次出上锁
            String sfExportChanel = "4604110112";
            if (sfExportChanel.equals(info.getChnlno()) && e==1){
                log.warn("[UNLOCK-NOTICE]-快邮货站,进行上锁通知,{}",info.getBarcode());
                return true;
            }
        }

        return false;
    }

    @Override
    public boolean unLockCheck(GatherInfo info){

        //从缓存获取通道流转列表信息
        List<LandBusinessTypeList> landBusinessTypeListsByGather = landBusListService.getLandBusinessTypeListsByGather(info);
        List<LandBusinessTypeList> temp = landBusinessTypeListsByGather;
        //已经过场的记录
        List<LandBusinessTypeList> landBusinessTypeLists = landBusinessTypeListMapper.selectHistory(info.getVename(), info.getBarcode());

        for (LandBusinessTypeList formItem : landBusinessTypeLists) {
            List<LandBusinessTypeList> r = landBusinessTypeListsByGather.stream().filter(item -> {
                if (item.getEndstation().equals(formItem.getEndstation())) {
                    log.info("[Lock-Stream-loop]-缓存元素场站:[{}],已出入记录场站:[{}]-核销判定对碰成功", item.getEndstation(), formItem.getEndstation());
                    return true;
                } else {
                    return false;
                }
            }).collect(Collectors.toList());
            temp.removeAll(r);
        }


        //筛选出未入场的申请
        List<String> stationList = temp.stream().map(LandBusinessTypeList::getEndstation).distinct().collect(Collectors.toList());
        log.info("[unLockCheck]-未走的流转申请:{}",temp.size());

        //判定是否还剩最后一个场站未入场
        if (stationList.size() == 1){

            //顺丰进通道,解锁通知,末次进不解锁
            String sfImportChanel = "4600011001";
            if (sfImportChanel.equals(info.getChnlno())) {
                log.warn("[UNLOCK-NOTICE]-顺丰货站,不进行解锁通知,{}",info.getBarcode());
                return false;
            }


            //场站信息 与 当前GatherInfo信息又对的上
            if (stationList.get(0).equals(info.getAreaid())) {
                //通知解锁
                log.info("[UNLOCK-NOTICE]-解锁通知");
                return true;
            }
        }

        /**
         * 顺丰快邮特殊判定
         */
        //快邮进通道,解锁通知,二次进锁
        String kyImportChanel = "4604111111";
        if (kyImportChanel.equals(info.getChnlno()) && stationList.size() == 2 ){
            log.info("[UNLOCK-NOTICE]-快邮货站,解锁通知,{}",info.getBarcode());
            return true;
        }

        return false;
    }
}