package com.cpxt.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cpxt.biz.domain.BizDriverState;
import com.cpxt.biz.domain.BizOrder;
import com.cpxt.biz.mapper.BizDriverStateMapper;
import com.cpxt.biz.mapper.BizOrderMapper;
import com.cpxt.biz.service.BizDriverStateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @author LuoJian
* @description 针对表【biz_driver_state(司机定位状态)】的数据库操作Service实现
* @createDate 2025-01-03 09:28:09
*/
@Service
public class BizDriverStateServiceImpl extends ServiceImpl<BizDriverStateMapper, BizDriverState>
    implements BizDriverStateService {

    @Autowired
    private BizDriverStateMapper bizDriverStateMapper;

    @Autowired
    private BizOrderMapper bizOrderMapper;

    @Override
    public int insertDriveDriveState(BizDriverState bizDriverState) {
        return bizDriverStateMapper.insertBizDriverState(bizDriverState);
    }

    @Override
    public List<Map> selectDriverTrack(String startTime, String endTime, String tableName) {
        return bizDriverStateMapper.selectDriverTrack(startTime,endTime,tableName);
    }

    @Override
    public List<BizDriverState> selectDriverLocation() {
        List<BizDriverState> bizDriverStateList = bizDriverStateMapper.selectDriverLocation();
        for (BizDriverState bizDriverState : bizDriverStateList) {
            Integer driverId = bizDriverState.getDriverId();
            Date gpsTime = bizDriverState.getGpsTime();
            LambdaQueryWrapper<BizOrder> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(BizOrder::getDriverId,driverId);
            queryWrapper1.in(BizOrder::getOrderStatus,"1","2");
            queryWrapper1.le(BizOrder::getUpdateTime,gpsTime);
            queryWrapper1.orderByDesc(BizOrder::getUpdateTime);
            queryWrapper1.last("limit 1");
            BizOrder bizOrder = bizOrderMapper.selectOne(queryWrapper1);
            if (bizOrder != null){
                bizDriverState.setOrderSn(bizOrder.getOrderSn());
                bizDriverState.setCarNo(bizOrder.getCarNo());
            }
        }
        return bizDriverStateList;
    }
}




