package com.prolog.rdc.tms.service.impl.intransit;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.exception.ParameterException;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Order;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.dao.util.PageUtils;
import com.prolog.rdc.tms.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.constant.StatusConstant;
import com.prolog.rdc.tms.dao.mapper.TmsLoadingBillDetailMapper;
import com.prolog.rdc.tms.dao.mapper.TmsLoadingBillHeaderMapper;
import com.prolog.rdc.tms.dao.mapper.TmsShipbillHeaderMapper;
import com.prolog.rdc.tms.dao.mapper.TmsVehicleAlarmMapper;
import com.prolog.rdc.tms.model.dto.ShipbillHeaderPageDTO;
import com.prolog.rdc.tms.model.dto.intransit.InTransitTrackDTO;
import com.prolog.rdc.tms.model.dto.intransit.LoadingAndShipBillHeaderDTO;
import com.prolog.rdc.tms.model.dto.intransit.LoadingBillHeaderPageDTO;
import com.prolog.rdc.tms.model.dto.intransit.VehicleAlarmDTO;
import com.prolog.rdc.tms.model.entity.SysRefdatadModel;
import com.prolog.rdc.tms.model.entity.TmsLoadingBillDetailModel;
import com.prolog.rdc.tms.model.entity.TmsLoadingBillHeaderModel;
import com.prolog.rdc.tms.model.entity.TmsShipbillHeaderModel;
import com.prolog.rdc.tms.model.entity.TmsVehicleAlarm;
import com.prolog.rdc.tms.model.vo.intransit.InTransitQueryVO;
import com.prolog.rdc.tms.util.ConvertorUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author hongbo.pan
 * @date 2022/9/20
 */
public abstract class AbstractInTransit {

    @Autowired
    protected TmsShipbillHeaderMapper tmsShipbillHeaderMapper;

    @Autowired
    protected TmsLoadingBillHeaderMapper tmsLoadingBillHeaderMapper;

    @Autowired
    protected TmsLoadingBillDetailMapper tmsLoadingBillDetailMapper;

    @Autowired
    protected TmsVehicleAlarmMapper tmsVehicleAlarmMapper;

    @Autowired
    protected ConvertorUtil convertorUtil;

    /**
     * 车辆定位
     * @param vo
     * @return
     */
    public abstract <T> T getPosition(InTransitQueryVO vo);

    /**
     * 车辆定位-运单查询
     * @param vo
     * @return
     */
    public Page<ShipbillHeaderPageDTO> getPositionShipBill(InTransitQueryVO vo) {
        checkCarAndPhoneNumber(vo);
        return pageShipBill(vo);
    }

    /**
     * 车辆定位-派车单查询
     * @param vo
     * @return
     */
    public Page<LoadingBillHeaderPageDTO> getPositionLoadingBill(InTransitQueryVO vo) {
        checkCarAndPhoneNumber(vo);
        return pageLoadingBill(vo);
    }

    /**
     * 根据派车单查询运单
     * @param vo
     * @return
     */
    public Page<ShipbillHeaderPageDTO> getShipBillByLoadingBillNo(InTransitQueryVO vo){
        ToolKits.isParamNull(vo.getLoadingBillNo(), "派车单号不能为空");
        checkPageParam(vo);
        PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
        List<ShipbillHeaderPageDTO> shipBillHeaderPageDTOList = tmsShipbillHeaderMapper.getInTransitShipBillByLoadingBillNo(vo.getLoadingBillNo());
        shipBillHeaderPageDTOList = fillShipBillHeaderPageDTO(shipBillHeaderPageDTOList);
        return PageUtils.getPage(shipBillHeaderPageDTOList);
    }

    /**
     * 车辆轨迹
     * @param vo
     * @return
     */
    public abstract <T> List<T> getTrack(InTransitQueryVO vo);

    /**
     * 车辆轨迹-运单查询
     * @param vo
     * @return
     */
    public Page<ShipbillHeaderPageDTO> getTrackShipBill(InTransitQueryVO vo) {
        checkCarAndPhoneNumber(vo);
        checkDateParam(vo);
        return pageShipBill(vo);
    }

    /**
     * 车辆轨迹-派车单查询
     * @param vo
     * @return
     */
    public Page<LoadingBillHeaderPageDTO> getTrackLoadingBill(InTransitQueryVO vo) {
        checkCarAndPhoneNumber(vo);
        checkDateParam(vo);
        return pageLoadingBill(vo);
    }

    /**
     * getLoadingAndShipBillByGpsId
     * @param gpsId
     * @return
     */
    @Deprecated
    public abstract LoadingAndShipBillHeaderDTO getLoadingAndShipBillByGpsId(String gpsId);

    /**
     * 运单定位
     * @param shipBillId
     * @return
     */
    public abstract <T> T getPositionByShipBillId(String shipBillId);

    /**
     * 运单轨迹
     * @param shipBillId
     * @return
     */
    public abstract InTransitTrackDTO getTrackByShipBillId(String shipBillId);

    /**
     * 派车单定位
     * @param loadingBillId
     * @return
     */
    public abstract <T> T getPositionByLoadingBillId(String loadingBillId);

    /**
     * 派车单轨迹
     * @param loadingBillId
     * @return
     */
    public abstract InTransitTrackDTO getTrackByLoadingBillId(String loadingBillId);

    /**
     * 运单定位/轨迹-运单查询
     * @param vo
     * @return
     */
    public Page<ShipbillHeaderPageDTO> getShipBill(InTransitQueryVO vo) {
        ToolKits.isParamNull(vo.getShipBillNo(), "运单号不能为空");
        checkPageParam(vo);
        PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
        Criteria crt = Criteria.forClass(TmsShipbillHeaderModel.class);
        TmsShipbillHeaderModel tmsShipbillHeaderModel = new TmsShipbillHeaderModel();
        crt.restrictionBuilder().like(tmsShipbillHeaderModel::getShipbillNo,"%" + vo.getShipBillNo() + "%").build();
        crt.orderDescBy(tmsShipbillHeaderModel::getUpdatetime, tmsShipbillHeaderModel::getCreatetime);
        List<TmsShipbillHeaderModel> tmsShipBillHeaderModelList = tmsShipbillHeaderMapper.findByCriteria(crt);
        Page<TmsShipbillHeaderModel> tmsShipBillHeaderModelPage = PageUtils.getPage(tmsShipBillHeaderModelList);
        List<ShipbillHeaderPageDTO> pageDTOList = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(tmsShipBillHeaderModelList)) {
            pageDTOList = tmsShipBillHeaderModelList.stream().map(model ->
                    BeanUtil.toBean(model, ShipbillHeaderPageDTO.class)).collect(Collectors.toList());
            pageDTOList = fillShipBillHeaderPageDTO(pageDTOList);
        }
        Page<ShipbillHeaderPageDTO> page = PageUtils.getPage(pageDTOList);
        page.setStartRow(tmsShipBillHeaderModelPage.getStartRow());
        page.setEndRow(tmsShipBillHeaderModelPage.getEndRow());
        page.setTotalCount(tmsShipBillHeaderModelPage.getTotalCount());
        page.setPageCount(tmsShipBillHeaderModelPage.getPageCount());
        page.setPageNum(tmsShipBillHeaderModelPage.getPageNum());
        page.setPageSize(tmsShipBillHeaderModelPage.getPageSize());
        return page;
    }

    /**
     * 派车单定位/轨迹-派车单查询
     * @param vo
     * @return
     */
    public Page<LoadingBillHeaderPageDTO> getLoadingBill(InTransitQueryVO vo) {
        ToolKits.isParamNull(vo.getLoadingBillNo(), "派车单号不能为空");
        checkPageParam(vo);
        PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
        Criteria crt = Criteria.forClass(TmsLoadingBillHeaderModel.class);
        TmsLoadingBillHeaderModel tmsLoadingBillHeaderModel = new TmsLoadingBillHeaderModel();
        crt.restrictionBuilder().like(tmsLoadingBillHeaderModel::getLoadingBillNo,"%" + vo.getLoadingBillNo() + "%").build();
        crt.orderDescBy(tmsLoadingBillHeaderModel::getUpdatetime, tmsLoadingBillHeaderModel::getCreatetime);
        List<TmsLoadingBillHeaderModel> tmsLoadingBillHeaderModelList = tmsLoadingBillHeaderMapper.findByCriteria(crt);
        Page<TmsLoadingBillHeaderModel> tmsLoadingBillHeaderModelPage = PageUtils.getPage(tmsLoadingBillHeaderModelList);
        List<LoadingBillHeaderPageDTO> pageDTOList = Lists.newArrayList();
        if (CollectionUtil.isNotEmpty(tmsLoadingBillHeaderModelList)) {
            pageDTOList = tmsLoadingBillHeaderModelList.stream().map(model ->
                    BeanUtil.toBean(model, LoadingBillHeaderPageDTO.class)).collect(Collectors.toList());
            pageDTOList = fillLoadingBillHeaderPageDTO(pageDTOList);
        }
        Page<LoadingBillHeaderPageDTO> page = PageUtils.getPage(pageDTOList);
        page.setStartRow(tmsLoadingBillHeaderModelPage.getStartRow());
        page.setEndRow(tmsLoadingBillHeaderModelPage.getEndRow());
        page.setTotalCount(tmsLoadingBillHeaderModelPage.getTotalCount());
        page.setPageCount(tmsLoadingBillHeaderModelPage.getPageCount());
        page.setPageNum(tmsLoadingBillHeaderModelPage.getPageNum());
        page.setPageSize(tmsLoadingBillHeaderModelPage.getPageSize());
        return page;
    }

    /**
     * 派车单分页
     * @param vo
     * @return
     */
    private Page<LoadingBillHeaderPageDTO> pageLoadingBill(InTransitQueryVO vo) {
        checkPageParam(vo);
        PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
        List<LoadingBillHeaderPageDTO> loadingBillHeaderPageDTOList = pageLoadingBillByPositionType(vo);
        Page<LoadingBillHeaderPageDTO> loadingBillHeaderPageDTOPage = PageUtils.getPage(loadingBillHeaderPageDTOList);
        loadingBillHeaderPageDTOPage.setList(fillLoadingBillHeaderPageDTO(loadingBillHeaderPageDTOList));
        return loadingBillHeaderPageDTOPage;
    }

    /**
     * 派车单分页
     * @param vo
     * @return
     */
    abstract List<LoadingBillHeaderPageDTO> pageLoadingBillByPositionType(InTransitQueryVO vo);

    /**
     * 运单分页
     * @param vo
     * @return
     */
    private Page<ShipbillHeaderPageDTO> pageShipBill(InTransitQueryVO vo) {
        checkPageParam(vo);
        PageUtils.startPage(vo.getPageNum(), vo.getPageSize());
        List<ShipbillHeaderPageDTO> shipBillHeaderPageDTOList = pageShipBillByPositionType(vo);
        Page<ShipbillHeaderPageDTO> tmsShipBillHeaderModelPage = PageUtils.getPage(shipBillHeaderPageDTOList);
        tmsShipBillHeaderModelPage.setList(fillShipBillHeaderPageDTO(shipBillHeaderPageDTOList));
        return tmsShipBillHeaderModelPage;
    }

    /**
     * 运单分页
     * @param vo
     * @return
     */
    abstract List<ShipbillHeaderPageDTO> pageShipBillByPositionType(InTransitQueryVO vo);

    /**
     * 派车单字典
     * @return
     */
    protected List<SysRefdatadModel> getLoadingBillDicData() {
        return convertorUtil.getDictionary(new String[] {
                DataDictionaryConstant.LOADING_BILL_STATUS// 派车单状态
        });
    }

    /**
     * 运单字典
     * @return
     */
    protected List<SysRefdatadModel> getShipBillDicData() {
        return convertorUtil.getDictionary(new String[] {
                DataDictionaryConstant.TRANSPORT_STATUS, // 运输状态
                DataDictionaryConstant.ABNORMAL_STATUS, // 异常状态
                DataDictionaryConstant.SCAN_STATUS, // 扫描状态
                DataDictionaryConstant.SHIPBILL_STATUS, // 运单状态
                DataDictionaryConstant.ORDER_TYPE, // 订单类型
                DataDictionaryConstant.ORDER_BUSINESS_TYPE, // 订单业务类型
                DataDictionaryConstant.DELIVERY_METHOD_TYPE, // 配送方式
                DataDictionaryConstant.TRANSPORT_TYPE, // 运输类型(运输模式)
                DataDictionaryConstant.TEMPERATURE_REQUIREMENT, // 温度要求
                DataDictionaryConstant.SHIPPER_TYPE, // 发货方类型
                DataDictionaryConstant.CONSIGNEE_TYPE// 收货方类型
        });
    }

    /**
     * 派车单字典转换
     * @param loadingBillHeaderPageDTOList
     * @return
     */
    private List<LoadingBillHeaderPageDTO> fillLoadingBillHeaderPageDTO(List<LoadingBillHeaderPageDTO> loadingBillHeaderPageDTOList) {
        if (CollectionUtil.isNotEmpty(loadingBillHeaderPageDTOList)) {
            List<SysRefdatadModel> dicData = getLoadingBillDicData();
            return loadingBillHeaderPageDTOList.stream().peek(dto -> translateLoadingBillHeaderPageDTO(dicData, dto)).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }

    /**
     * 运单字典转换
     * @param shipBillHeaderPageDTOList
     * @return
     */
    private List<ShipbillHeaderPageDTO> fillShipBillHeaderPageDTO(List<ShipbillHeaderPageDTO> shipBillHeaderPageDTOList) {
        if (CollectionUtil.isNotEmpty(shipBillHeaderPageDTOList)) {
            List<SysRefdatadModel> dicData = getShipBillDicData();
            return shipBillHeaderPageDTOList.stream().peek(dto -> translateShipBillHeaderPageDTO(dicData, dto)).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }

    /**
     * 派车单字典转换
     * @param dicData
     * @param dto
     * @return
     */
    protected LoadingBillHeaderPageDTO translateLoadingBillHeaderPageDTO(List<SysRefdatadModel> dicData, LoadingBillHeaderPageDTO dto) {
        dto.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.LOADING_BILL_STATUS, String.valueOf(dto.getStatus())));
        return dto;
    }

    /**
     * 运单字典转换
     * @param dicData
     * @param dto
     * @return
     */
    protected ShipbillHeaderPageDTO translateShipBillHeaderPageDTO(List<SysRefdatadModel> dicData, ShipbillHeaderPageDTO dto) {
        dto.setStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPBILL_STATUS, String.valueOf(dto.getStatus())));
        dto.setOrderTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_TYPE, String.valueOf(dto.getOrderType())));
        dto.setOrderBusinessTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ORDER_BUSINESS_TYPE, String.valueOf(dto.getOrderType())));
        dto.setDeliveryMethodTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.DELIVERY_METHOD_TYPE, String.valueOf(dto.getDeliveryMethodType())));
        dto.setTransportTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_TYPE, String.valueOf(dto.getTransportType())));
        dto.setTemperatureRequirementName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TEMPERATURE_REQUIREMENT, String.valueOf(dto.getTemperatureRequirement())));
        dto.setShipperTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SHIPPER_TYPE, String.valueOf(dto.getShipperType())));
        dto.setConsigneeTypeName(ConvertorUtil.getName(dicData, DataDictionaryConstant.CONSIGNEE_TYPE, String.valueOf(dto.getConsigneeType())));
        dto.setAbnormalStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.ABNORMAL_STATUS, String.valueOf(dto.getAbnormalStatus())));
        dto.setScanStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.SCAN_STATUS, String.valueOf(dto.getScanStatus())));
        dto.setTransportStatusName(ConvertorUtil.getName(dicData, DataDictionaryConstant.TRANSPORT_STATUS, String.valueOf(dto.getTransportStatus())));
        if (ToolKits.isNotEmpty(dto.getConsigneeLongitude()) && ToolKits.isNotEmpty(dto.getConsigneeLatitude())) {
            dto.setConsigneePositioning(StatusConstant.POSITIONING_YDW);
        } else {
            dto.setConsigneePositioning(StatusConstant.POSITIONING_WDW);
        }
        if (ToolKits.isNotEmpty(dto.getShipperLongitude()) && ToolKits.isNotEmpty(dto.getShipperLatitude())) {
            dto.setShipperPositioning(StatusConstant.POSITIONING_YDW);
        } else {
            dto.setShipperPositioning(StatusConstant.POSITIONING_WDW);
        }
        return dto;
    }

    /**
     * 车牌号手机号校验
     * @param vo
     */
    protected void checkCarAndPhoneNumber(InTransitQueryVO vo) {
        if (StrUtil.isBlank(vo.getCarNumber()) && StrUtil.isBlank(vo.getMobilePhone())) {
            throw new ParameterException("车牌号和司机手机号不能同时为空");
        }
        if (StrUtil.isNotBlank(vo.getMobilePhone()) && !Validator.isMobile(vo.getMobilePhone())) {
            throw new ParameterException("请输入正确的司机手机号");
        }
        if (StrUtil.isNotBlank(vo.getCarNumber()) && !Validator.isPlateNumber(vo.getCarNumber())) {
            throw new ParameterException("请输入正确的车牌号");
        }
    }

    /**
     * 分页校验
     * @param vo
     */
    private void checkPageParam(InTransitQueryVO vo) {
        ToolKits.isParamNull(vo.getPageNum(), "页码不能为空");
        ToolKits.isParamNull(vo.getPageSize(), "页数不能为空");
    }

    /**
     * 日期校验
     * @param vo
     */
    protected void checkDateParam(InTransitQueryVO vo) {
        ToolKits.isParamNull(vo.getStartDate(), "开始时间不能为空");
        ToolKits.isParamNull(vo.getEndDate(), "结束时间不能为空");
    }

    /**
     * 运单状态校验
     * @param shipBillId
     * @return
     */
    protected TmsShipbillHeaderModel checkShipBillAndReturn(String shipBillId) {
        ToolKits.isParamNull(shipBillId, "运单ID不能为空");
        TmsShipbillHeaderModel tmsShipBillHeaderModel = tmsShipbillHeaderMapper.findById(shipBillId, TmsShipbillHeaderModel.class);
        if (NumberUtil.compare(tmsShipBillHeaderModel.getStatus(), DataDictionaryConstant.SHIPBILL_STATUS_QY) <=0) {
            throw new BizException("当前运单["+tmsShipBillHeaderModel.getShipbillNo()+"]未启运");
        }
        return tmsShipBillHeaderModel;
    }

    /**
     * 派车单状态校验
     * @param loadingBillId
     * @return
     */
    protected TmsLoadingBillHeaderModel checkLoadingBillAndReturn(String loadingBillId) {
        ToolKits.isParamNull(loadingBillId, "派车单ID不能为空");
        TmsLoadingBillHeaderModel tmsLoadingBillHeaderModel = tmsLoadingBillHeaderMapper.findById(loadingBillId, TmsLoadingBillHeaderModel.class);
        if (tmsLoadingBillHeaderModel.getStatus() < DataDictionaryConstant.LOADING_BILL_STATUS_QY_PART) {
            throw new BizException("当前派车单["+tmsLoadingBillHeaderModel.getLoadingBillNo()+"]未启运");
        }
        return tmsLoadingBillHeaderModel;
    }

    /**
     * 告警
     * @param vo
     * @return
     */
    public List<VehicleAlarmDTO> getAlarm(InTransitQueryVO vo) {
        if (StrUtil.isNotBlank(vo.getShipBillNo())) {
            Criteria crt = Criteria.forClass(TmsLoadingBillDetailModel.class);
            Restriction r1 = Restrictions.eq("shipbillId", vo.getShipBillNo());
            crt.setRestriction(Restrictions.and(r1));
            crt.setOrder(Order.newInstance().desc(TmsLoadingBillDetailModel::getCreatetime));
            List<TmsLoadingBillDetailModel> tmsLoadingBillDetailModels = tmsLoadingBillDetailMapper.findByCriteria(crt);
            ToolKits.isParamNull(tmsLoadingBillDetailModels, "未找到对应的调度单明细");
            TmsLoadingBillDetailModel loadingBillDetailModel = tmsLoadingBillDetailModels.get(0);
            vo.setLoadingBillNo(loadingBillDetailModel.getLoadingBillId());
        }
        if (StrUtil.isNotBlank(vo.getLoadingBillNo())) {
            TmsLoadingBillHeaderModel loadingBillHeaderModel = tmsLoadingBillHeaderMapper.findById(vo.getLoadingBillNo(), TmsLoadingBillHeaderModel.class);
            String shipBillStr = getShipBillIdStr(loadingBillHeaderModel.getLoadingBillId());
            List<TmsShipbillHeaderModel> shipbillHeaderModelList = tmsShipbillHeaderMapper.findByIds(TmsShipbillHeaderModel.class, shipBillStr.split(","), null);
            Date startDate;
            Optional<Date> optionalDate = shipbillHeaderModelList.stream().filter(shipbillHeaderModel ->
                            ObjectUtil.isNotNull(shipbillHeaderModel.getDepartureTime())).map(TmsShipbillHeaderModel::getDepartureTime)
                    .sorted().findFirst();
            if (optionalDate.isPresent()) {
                startDate = optionalDate.get();
            } else {
                throw new BizException("派车单未启运");
            }
            Date endDate = DateUtil.date();
            if (shipbillHeaderModelList.stream().allMatch(shipbillHeaderModel ->
                    ObjectUtil.isNotNull(shipbillHeaderModel.getDeliveryTime()))) {
                endDate = shipbillHeaderModelList.stream().filter(shipbillHeaderModel ->
                                ObjectUtil.isNotNull(shipbillHeaderModel.getDeliveryTime())).map(TmsShipbillHeaderModel::getDeliveryTime)
                        .sorted(Comparator.reverseOrder()).findFirst().get();
            }
            vo.setCarNumber(loadingBillHeaderModel.getVehiclePlateNumber());
            vo.setStartDate(startDate);
            vo.setEndDate(endDate);
        }
        checkDateParam(vo);
        List<VehicleAlarmDTO> vehicleAlarmDTOList = Lists.newArrayList();

        Criteria crt = Criteria.forClass(TmsVehicleAlarm.class);
        Restriction r1 = Restrictions.eq(TmsVehicleAlarm::getVehiclePlateNumber, vo.getCarNumber());
        Restriction r2 = Restrictions.ge(TmsVehicleAlarm::getStartTime, vo.getStartDate());
        Restriction r3 = Restrictions.le(TmsVehicleAlarm::getEndTime, vo.getEndDate());
        crt.setRestriction(Restrictions.and(r1, r2, r3));
        crt.setOrder(Order.newInstance().desc(TmsVehicleAlarm::getCreatetime));
        List<TmsVehicleAlarm> tmsVehicleAlarmList = tmsVehicleAlarmMapper.findByCriteria(crt);
        if (CollectionUtil.isNotEmpty(tmsVehicleAlarmList)) {
            vehicleAlarmDTOList = tmsVehicleAlarmList.stream().map(tmsVehicleAlarm -> BeanUtil.copyProperties(tmsVehicleAlarm, VehicleAlarmDTO.class)).collect(Collectors.toList());
        }
        return vehicleAlarmDTOList;
    }

    /**
     * getShipBillIdList
     * @param loadingBillId
     * @return
     */
    protected String getShipBillIdStr(String loadingBillId) {
        Criteria crt = Criteria.forClass(TmsLoadingBillDetailModel.class);
        Restriction r1 = Restrictions.eq(TmsLoadingBillDetailModel::getLoadingBillId, loadingBillId);
        crt.setRestriction(r1);
        List<TmsLoadingBillDetailModel> loadingBillDetailModelList = tmsLoadingBillDetailMapper.findByCriteria(crt);
        if (CollectionUtil.isNotEmpty(loadingBillDetailModelList)) {
            return loadingBillDetailModelList.stream().map(TmsLoadingBillDetailModel::getShipbillId).distinct().collect(Collectors.joining(","));
        }
        return StrUtil.EMPTY;
    }
}
