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

import java.util.Comparator;
import java.util.Date;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
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.rdc.tms.constant.BussinessConstant;
import com.prolog.rdc.tms.constant.DataDictionaryConstant;
import com.prolog.rdc.tms.model.dto.DeviceByOneDTO;
import com.prolog.rdc.tms.model.dto.ShipbillHeaderPageDTO;
import com.prolog.rdc.tms.model.dto.e6.EquipInfoDTO;
import com.prolog.rdc.tms.model.dto.e6.EquipInfoHistoryDTO;
import com.prolog.rdc.tms.model.dto.g7.CurrentGpsLocationDTO;
import com.prolog.rdc.tms.model.dto.g7.HistoryLocationByGpsnoDTO;
import com.prolog.rdc.tms.model.dto.intransit.DeviceGpsDTO;
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.entity.TmsLoadingBillDetailModel;
import com.prolog.rdc.tms.model.entity.TmsLoadingBillHeaderModel;
import com.prolog.rdc.tms.model.entity.TmsShipbillHeaderModel;
import com.prolog.rdc.tms.model.vo.e6.EquipInfoHistoryVO;
import com.prolog.rdc.tms.model.vo.e6.EquipInfoVO;
import com.prolog.rdc.tms.model.vo.g7.CurrentTemperatureVO;
import com.prolog.rdc.tms.model.vo.g7.HistoryLocationByGpsnoVO;
import com.prolog.rdc.tms.model.vo.intransit.InTransitQueryVO;
import com.prolog.rdc.tms.rmi.feign.InterfaceFeign;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hongbo.pan
 * @date 2023/3/21
 */
@Component
public class InTransitDeviceLinkServiceImpl extends AbstractInTransit {

    @Autowired
    private MaindataFeign maindataFeign;

    @Autowired
    private InterfaceFeign interfaceFeign;

    @Override
    public DeviceGpsDTO getPosition(InTransitQueryVO vo) {
        checkCarAndPhoneNumber(vo);
        List<TmsLoadingBillHeaderModel> loadingBillHeaderModelList = getInTransitLoadingBill(vo);
        TmsLoadingBillHeaderModel loadingBillHeaderModel = loadingBillHeaderModelList.get(0);
        DeviceByOneDTO deviceByOneDTO = getDeviceCompanyName(loadingBillHeaderModel.getVehicleDeviceId());
        return getPositionByDeviceCompanyName(loadingBillHeaderModel, deviceByOneDTO.getCompanyName());
    }

    @Override
    public List<DeviceGpsDTO> getTrack(InTransitQueryVO vo) {
        checkCarAndPhoneNumber(vo);
        List<TmsLoadingBillHeaderModel> loadingBillHeaderModelList = getInTransitLoadingBill(vo);
        TmsLoadingBillHeaderModel loadingBillHeaderModel = loadingBillHeaderModelList.get(0);
        DeviceByOneDTO deviceByOneDTO = getDeviceCompanyName(loadingBillHeaderModel.getVehicleDeviceId());
        List<DeviceGpsDTO> deviceLinkGpsDTOList = getTrackByDeviceCompanyName(loadingBillHeaderModel,
                deviceByOneDTO.getCompanyName(), vo.getStartDate(), vo.getEndDate());
        String shipBillStr = getShipBillIdStr(loadingBillHeaderModel.getLoadingBillId());
        return deviceLinkGpsDTOList.stream().peek(deviceLinkGpsDTO -> {
            deviceLinkGpsDTO.setDriverName(loadingBillHeaderModel.getDriverName());
            deviceLinkGpsDTO.setDriverPhoneNumber(loadingBillHeaderModel.getDriverPhoneNumber());
            deviceLinkGpsDTO.setLoadingBillId(loadingBillHeaderModel.getLoadingBillId());
            deviceLinkGpsDTO.setLoadingBillNo(loadingBillHeaderModel.getLoadingBillId());
            deviceLinkGpsDTO.setShipbillId(shipBillStr);
            deviceLinkGpsDTO.setShipbillNo(shipBillStr);
            deviceLinkGpsDTO.setDeviceCode(deviceLinkGpsDTO.getDeviceCode());
        }).collect(Collectors.toList());
    }

    @Override
    public LoadingAndShipBillHeaderDTO getLoadingAndShipBillByGpsId(String gpsId) {
        return null;
    }

    @Override
    public DeviceGpsDTO getPositionByShipBillId(String shipBillId) {
        checkShipBillAndReturn(shipBillId);
        Criteria crt = Criteria.forClass(TmsLoadingBillDetailModel.class);
        Restriction r1 = Restrictions.eq("shipbillId", shipBillId);
        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);
        TmsLoadingBillHeaderModel loadingBillHeaderModel = tmsLoadingBillHeaderMapper.findById(loadingBillDetailModel.getLoadingBillId(), TmsLoadingBillHeaderModel.class);
        DeviceByOneDTO deviceByOneDTO = getDeviceCompanyName(loadingBillHeaderModel.getVehicleDeviceId());
        return getPositionByDeviceCompanyName(loadingBillHeaderModel, deviceByOneDTO.getCompanyName());
    }

    @Override
    public InTransitTrackDTO getTrackByShipBillId(String shipBillId) {
        TmsShipbillHeaderModel tmsShipbillHeaderModel = checkShipBillAndReturn(shipBillId);
        Criteria crt = Criteria.forClass(TmsLoadingBillDetailModel.class);
        Restriction r1 = Restrictions.eq("shipbillId", shipBillId);
        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);
        TmsLoadingBillHeaderModel loadingBillHeaderModel = tmsLoadingBillHeaderMapper.findById(loadingBillDetailModel.getLoadingBillId(), TmsLoadingBillHeaderModel.class);
        DeviceByOneDTO deviceByOneDTO = getDeviceCompanyName(loadingBillHeaderModel.getVehicleDeviceId());
        Date startDate = tmsShipbillHeaderModel.getDepartureTime();
        Date endDate = tmsShipbillHeaderModel.getDeliveryTime() == null ? DateUtil.date() : tmsShipbillHeaderModel.getDeliveryTime();
        List<DeviceGpsDTO> deviceLinkGpsDTOList = getTrackByDeviceCompanyName(loadingBillHeaderModel,
                deviceByOneDTO.getCompanyName(), startDate, endDate);

        if (ObjectUtil.isEmpty(deviceLinkGpsDTOList)) {
            return null;
        }
        deviceLinkGpsDTOList = deviceLinkGpsDTOList.stream().peek(deviceLinkGpsDTO -> {
            deviceLinkGpsDTO.setDriverName(loadingBillHeaderModel.getDriverName());
            deviceLinkGpsDTO.setDriverPhoneNumber(loadingBillHeaderModel.getDriverPhoneNumber());
            deviceLinkGpsDTO.setLoadingBillId(loadingBillHeaderModel.getLoadingBillId());
            deviceLinkGpsDTO.setLoadingBillNo(loadingBillHeaderModel.getLoadingBillId());
            deviceLinkGpsDTO.setShipbillId(shipBillId);
            deviceLinkGpsDTO.setShipbillNo(shipBillId);
            deviceLinkGpsDTO.setDeviceCode(deviceLinkGpsDTO.getDeviceCode());
        }).collect(Collectors.toList());
        InTransitTrackDTO inTransitTrackDTO = new InTransitTrackDTO();
        inTransitTrackDTO.setStatus(tmsShipbillHeaderModel.getStatus());
        if (tmsShipbillHeaderModel.getStatus() > DataDictionaryConstant.SHIPBILL_STATUS_SD) {
            inTransitTrackDTO.setDesc("终点");
        } else {
            inTransitTrackDTO.setDesc("在途");
        }
        inTransitTrackDTO.setDeviceGpsDTOList(deviceLinkGpsDTOList);
        return inTransitTrackDTO;
    }

    @Override
    public DeviceGpsDTO getPositionByLoadingBillId(String loadingBillId) {
        TmsLoadingBillHeaderModel loadingBillHeaderModel = checkLoadingBillAndReturn(loadingBillId);
        DeviceByOneDTO deviceByOneDTO = getDeviceCompanyName(loadingBillHeaderModel.getVehicleDeviceId());
        return getPositionByDeviceCompanyName(loadingBillHeaderModel, deviceByOneDTO.getCompanyName());
    }

    @Override
    public InTransitTrackDTO getTrackByLoadingBillId(String loadingBillId) {
        TmsLoadingBillHeaderModel loadingBillHeaderModel = checkLoadingBillAndReturn(loadingBillId);
        DeviceByOneDTO deviceByOneDTO = getDeviceCompanyName(loadingBillHeaderModel.getVehicleDeviceId());
        String shipBillStr = getShipBillIdStr(loadingBillHeaderModel.getLoadingBillId());
        List<TmsShipbillHeaderModel> shipbillHeaderModelList = tmsShipbillHeaderMapper.findByIds(TmsShipbillHeaderModel.class, shipBillStr.split(","), null);
        Date startDate = shipbillHeaderModelList.stream().filter(shipbillHeaderModel ->
                ObjectUtil.isNotNull(shipbillHeaderModel.getDepartureTime())).map(TmsShipbillHeaderModel::getDepartureTime)
                .sorted().findFirst().get();
        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();
        }
        List<DeviceGpsDTO> deviceLinkGpsDTOList = getTrackByDeviceCompanyName(loadingBillHeaderModel,
                deviceByOneDTO.getCompanyName(), startDate, endDate);
        deviceLinkGpsDTOList = deviceLinkGpsDTOList.stream().peek(deviceLinkGpsDTO -> {
            deviceLinkGpsDTO.setDriverName(loadingBillHeaderModel.getDriverName());
            deviceLinkGpsDTO.setDriverPhoneNumber(loadingBillHeaderModel.getDriverPhoneNumber());
            deviceLinkGpsDTO.setLoadingBillId(loadingBillHeaderModel.getLoadingBillId());
            deviceLinkGpsDTO.setShipbillId(shipBillStr);
            deviceLinkGpsDTO.setDeviceCode(deviceLinkGpsDTO.getDeviceCode());
        }).collect(Collectors.toList());
        InTransitTrackDTO inTransitTrackDTO = new InTransitTrackDTO();
        inTransitTrackDTO.setStatus(loadingBillHeaderModel.getStatus());
        if (loadingBillHeaderModel.getStatus() >= DataDictionaryConstant.LOADING_BILL_STATUS_SD) {
            inTransitTrackDTO.setDesc("终点");
        } else {
            inTransitTrackDTO.setDesc("在途");
        }
        inTransitTrackDTO.setDeviceGpsDTOList(deviceLinkGpsDTOList);
        return inTransitTrackDTO;
    }

    @Override
    List<LoadingBillHeaderPageDTO> pageLoadingBillByPositionType(InTransitQueryVO vo) {
        return tmsLoadingBillHeaderMapper.getInTransitDeviceLinkLoadingBill(vo);
    }

    @Override
    List<ShipbillHeaderPageDTO> pageShipBillByPositionType(InTransitQueryVO vo) {
        return tmsShipbillHeaderMapper.getInTransitDeviceLinkShipBill(vo);
    }

    /**
     * 获取设备生产厂商
     * @param deviceId
     * @return
     */
    private DeviceByOneDTO getDeviceCompanyName(String deviceId) {
        if (ObjectUtil.isEmpty(deviceId)) {
            DeviceByOneDTO deviceByOne01DTO = new DeviceByOneDTO();
            deviceByOne01DTO.setCompanyName("");
            return deviceByOne01DTO;
        }
        deviceId = deviceId.split(",")[0];
        RestMessage<DeviceByOneDTO> restMessage = maindataFeign.deviceGetOne(Long.valueOf(deviceId));
        if (!restMessage.isSuccess()) {
            throw new BizException("设备查询报错，deviceId=" + deviceId);
        }
        DeviceByOneDTO deviceByOneDTO = restMessage.getData();
        if (!BussinessConstant.E6.equals(deviceByOneDTO.getCompanyName()) && !BussinessConstant.G7.equals(deviceByOneDTO.getCompanyName())) {
            throw new BizException("设备生产厂商错误，" + deviceByOneDTO.getCompanyName());
        }
        return deviceByOneDTO;
    }

    /**
     * getPositionByDeviceCompanyName
     * @param loadingBillHeaderModel
     * @param deviceCompanyName
     * @return
     */
    private DeviceGpsDTO getPositionByDeviceCompanyName(TmsLoadingBillHeaderModel loadingBillHeaderModel, String deviceCompanyName) {
        DeviceGpsDTO deviceGpsDTO = null;
        if (BussinessConstant.E6.equals(deviceCompanyName)) {
            EquipInfoVO vo = new EquipInfoVO();
            vo.setEquipCode(loadingBillHeaderModel.getVehicleDeviceCode());
            RestMessage<List<EquipInfoDTO>> equipInfoReal = interfaceFeign.getEquipInfoReal(vo);
            if (!equipInfoReal.isSuccess()) {
                throw new BizException("易流设备定位，设备直连报错，vehicleDeviceCode：" + loadingBillHeaderModel.getVehicleDeviceCode() + "，错误信息：" + equipInfoReal.getMessage());
            }
            if (CollectionUtil.isNotEmpty(equipInfoReal.getData())) {
                EquipInfoDTO equipInfoDTO = equipInfoReal.getData().get(0);
                deviceGpsDTO = equipInfoDTOToDeviceLinkGpsDTO(equipInfoDTO);
            } else {
                throw new BizException("易流设备定位，定位数据不存在，vehicleDeviceCode=" + loadingBillHeaderModel.getVehicleDeviceCode());
            }
        } else if (BussinessConstant.G7.equals(deviceCompanyName)) {
            CurrentTemperatureVO currentTemperatureVO = new CurrentTemperatureVO();
            currentTemperatureVO.setGpsnos(loadingBillHeaderModel.getVehicleDeviceCode());
            RestMessage<List<CurrentGpsLocationDTO>> locationExecute = interfaceFeign.simpleCurrentsByGpsnos(currentTemperatureVO);
            if (!locationExecute.isSuccess()) {
                throw new BizException("G7设备定位，设备直连报错，vehicleDeviceCode：" + loadingBillHeaderModel.getVehicleDeviceCode() + "，错误信息：" + locationExecute.getMessage());
            }
            if (CollectionUtil.isNotEmpty(locationExecute.getData())) {
                CurrentGpsLocationDTO currentGpsLocationDTO = locationExecute.getData().get(0);
                deviceGpsDTO = currentGpsLocationDTOToDeviceLinkGpsDTO(currentGpsLocationDTO);
            } else {
                throw new BizException("G7设备定位，定位数据不存在，vehicleDeviceCode=" + loadingBillHeaderModel.getVehicleDeviceCode());
            }
        }
        if (ObjectUtil.isNotNull(deviceGpsDTO)) {
            deviceGpsDTO.setDriverName(loadingBillHeaderModel.getDriverName());
            deviceGpsDTO.setDriverPhoneNumber(loadingBillHeaderModel.getDriverPhoneNumber());
            deviceGpsDTO.setLoadingBillId(loadingBillHeaderModel.getLoadingBillId());
            deviceGpsDTO.setLoadingBillNo(loadingBillHeaderModel.getLoadingBillId());
            deviceGpsDTO.setShipbillId(getShipBillIdStr(loadingBillHeaderModel.getLoadingBillId()));
            deviceGpsDTO.setShipbillNo(deviceGpsDTO.getShipbillId());
            deviceGpsDTO.setDeviceCode(loadingBillHeaderModel.getVehicleDeviceCode());
            deviceGpsDTO.setVehiclePlateNumber(loadingBillHeaderModel.getVehiclePlateNumber());
        }
        return deviceGpsDTO;
    }

    /**
     * getTrackByDeviceCompanyName
     * @param loadingBillHeaderModel
     * @param deviceCompanyName
     * @param startDate
     * @param endDate
     * @return
     */
    private List<DeviceGpsDTO> getTrackByDeviceCompanyName(TmsLoadingBillHeaderModel loadingBillHeaderModel, String deviceCompanyName, Date startDate, Date endDate) {
        String shipBillIdStr = getShipBillIdStr(loadingBillHeaderModel.getLoadingBillId());
        if (BussinessConstant.E6.equals(deviceCompanyName)) {
            EquipInfoHistoryVO vo = new EquipInfoHistoryVO();
            vo.setBtime(startDate);
            vo.setEtime(endDate);
            vo.setEquipCode(loadingBillHeaderModel.getVehicleDeviceCode());
            RestMessage<List<EquipInfoHistoryDTO>> equipInfoHistory = interfaceFeign.getEquipInfoHistory(vo);
            if (!equipInfoHistory.isSuccess()) {
                throw new BizException("易流设备轨迹，设备直连报错，vehicleDeviceCode：" + loadingBillHeaderModel.getVehicleDeviceCode() + "，错误信息：" + equipInfoHistory.getMessage());
            }

            return equipInfoHistory.getData().stream().map(equipInfoHistoryDTO -> {
                DeviceGpsDTO deviceGpsDTO = new DeviceGpsDTO();
                deviceGpsDTO.setVehiclePlateNumber(loadingBillHeaderModel.getVehiclePlateNumber());
                deviceGpsDTO.setDriverName(loadingBillHeaderModel.getDriverName());
                deviceGpsDTO.setDriverPhoneNumber(loadingBillHeaderModel.getDriverPhoneNumber());
                deviceGpsDTO.setLoadingBillId(loadingBillHeaderModel.getLoadingBillId());
                deviceGpsDTO.setLoadingBillNo(loadingBillHeaderModel.getLoadingBillId());
                deviceGpsDTO.setShipbillId(shipBillIdStr);
                deviceGpsDTO.setShipbillNo(shipBillIdStr);
                deviceGpsDTO.setDeviceCode(loadingBillHeaderModel.getVehicleDeviceCode());
                deviceGpsDTO.setVehiclePlateNumber(loadingBillHeaderModel.getVehiclePlateNumber());
                deviceGpsDTO.setLongitude(equipInfoHistoryDTO.getLon());
                deviceGpsDTO.setLatitude(equipInfoHistoryDTO.getLat());
                deviceGpsDTO.setGpsDate(equipInfoHistoryDTO.getGpsTime());
                deviceGpsDTO.setCurrentVelocity(equipInfoHistoryDTO.getSpeed());
                deviceGpsDTO.setVehicleDirection(equipInfoHistoryDTO.getDirection());
                return deviceGpsDTO;
            }).collect(Collectors.toList());
        } else if (BussinessConstant.G7.equals(deviceCompanyName)) {
            HistoryLocationByGpsnoVO vo = new HistoryLocationByGpsnoVO();
            vo.setGpsno(loadingBillHeaderModel.getVehicleDeviceCode());
            vo.setFrom(startDate);
            vo.setTo(endDate);
            RestMessage<List<HistoryLocationByGpsnoDTO>> historyLocationByVehExecute = interfaceFeign.historyLocationByGpsnoCall(vo);
            if (!historyLocationByVehExecute.isSuccess()) {
                throw new BizException("g7设备轨迹，设备直连报错，vehicleDeviceCode：" + loadingBillHeaderModel.getVehicleDeviceCode() + "，错误信息：" + historyLocationByVehExecute.getMessage());
            }
            return historyLocationByVehExecute.getData().stream().map(historyLocationByGpsnoDTO -> {
                DeviceGpsDTO deviceGpsDTO = new DeviceGpsDTO();
                deviceGpsDTO.setVehiclePlateNumber(loadingBillHeaderModel.getVehiclePlateNumber());
                deviceGpsDTO.setDriverName(loadingBillHeaderModel.getDriverName());
                deviceGpsDTO.setDriverPhoneNumber(loadingBillHeaderModel.getDriverPhoneNumber());
                deviceGpsDTO.setLoadingBillId(loadingBillHeaderModel.getLoadingBillId());
                deviceGpsDTO.setLoadingBillNo(loadingBillHeaderModel.getLoadingBillId());
                deviceGpsDTO.setShipbillId(shipBillIdStr);
                deviceGpsDTO.setShipbillNo(shipBillIdStr);
                deviceGpsDTO.setDeviceCode(loadingBillHeaderModel.getVehicleDeviceCode());
                deviceGpsDTO.setVehiclePlateNumber(loadingBillHeaderModel.getVehiclePlateNumber());
                deviceGpsDTO.setLongitude(historyLocationByGpsnoDTO.getLng());
                deviceGpsDTO.setLatitude(historyLocationByGpsnoDTO.getLat());
                deviceGpsDTO.setGpsDate(DateUtil.date(Long.valueOf(historyLocationByGpsnoDTO.getTime())));
                deviceGpsDTO.setCurrentVelocity(NumberUtil.toBigDecimal(historyLocationByGpsnoDTO.getSpeed()));
                deviceGpsDTO.setVehicleDirection(NumberUtil.toBigDecimal(historyLocationByGpsnoDTO.getCourse()));
                return deviceGpsDTO;
            }).collect(Collectors.toList());
        }
        return null;
    }

    /**
     * currentGpsLocationDTOToDeviceLinkGpsDTO
     * @param currentGpsLocationDTO
     * @return
     */
    private DeviceGpsDTO currentGpsLocationDTOToDeviceLinkGpsDTO(CurrentGpsLocationDTO currentGpsLocationDTO) {
        DeviceGpsDTO deviceLinkGpsDTO = new DeviceGpsDTO();
        deviceLinkGpsDTO.setLongitude(currentGpsLocationDTO.getLng());
        deviceLinkGpsDTO.setLatitude(currentGpsLocationDTO.getLat());
        deviceLinkGpsDTO.setGpsDate(DateUtil.parseDate(currentGpsLocationDTO.getTime()));
        deviceLinkGpsDTO.setCurrentVelocity(currentGpsLocationDTO.getSpeed());
        deviceLinkGpsDTO.setVehicleDirection(currentGpsLocationDTO.getCourse());
        return deviceLinkGpsDTO;
    }

    /**
     * vehicleInfoDTOToDeviceLinkGpsDTO
     * @param equipInfoDTO
     * @return
     */
    private DeviceGpsDTO equipInfoDTOToDeviceLinkGpsDTO(EquipInfoDTO equipInfoDTO) {
        DeviceGpsDTO deviceLinkGpsDTO = new DeviceGpsDTO();
        deviceLinkGpsDTO.setLongitude(equipInfoDTO.getLon());
        deviceLinkGpsDTO.setLatitude(equipInfoDTO.getLat());
        deviceLinkGpsDTO.setGpsDate(equipInfoDTO.getGpsTime());
        deviceLinkGpsDTO.setCurrentVelocity(equipInfoDTO.getSpeed());
        deviceLinkGpsDTO.setVehicleDirection(equipInfoDTO.getDirection());
        return deviceLinkGpsDTO;
    }

    /**
     * 获取在途有设备的派车单
     * @param vo
     * @return
     */
    private List<TmsLoadingBillHeaderModel> getInTransitLoadingBill(InTransitQueryVO vo) {
        Criteria crt = Criteria.forClass(TmsLoadingBillHeaderModel.class);
        Restriction r1 = StrUtil.isBlank(vo.getCarNumber()) ? null : Restrictions.eq("vehiclePlateNumber", vo.getCarNumber());
        Restriction r2 = StrUtil.isBlank(vo.getMobilePhone()) ? null : Restrictions.eq("driverPhoneNumber", vo.getMobilePhone());
        Restriction r3 = Restrictions.isNotNull(TmsLoadingBillHeaderModel::getVehicleDeviceId);
        Restriction r4 = Restrictions.ge(TmsLoadingBillHeaderModel::getStatus, DataDictionaryConstant.LOADING_BILL_STATUS_QY_PART);
        Restriction r5 = Restrictions.eq(TmsLoadingBillHeaderModel::getCarrierType, DataDictionaryConstant.LINE_CARRIER_TYPE_SJ);
        crt.setRestriction(Restrictions.and(r1, r2, r3, r4, r5));
        crt.setOrder(Order.newInstance().desc(TmsLoadingBillHeaderModel::getCreatetime));
        List<TmsLoadingBillHeaderModel> loadingBillHeaderModelList = tmsLoadingBillHeaderMapper.findByCriteria(crt);
        if (CollectionUtil.isEmpty(loadingBillHeaderModelList)) {
            throw new BizException("车辆派车单无设备");
        }
        return loadingBillHeaderModelList;
    }
}
