package com.cpxt.biz.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cpxt.biz.domain.BizCarState;
import com.cpxt.biz.domain.BizOrder;
import com.cpxt.biz.mapper.BizCarStateMapper;
import com.cpxt.biz.mapper.BizOrderMapper;
import com.cpxt.biz.service.IBizCarStateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 车辆实时状态Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-18
 */
@Service
public class BizCarStateServiceImpl implements IBizCarStateService
{
    @Autowired
    private BizCarStateMapper bizCarStateMapper;

    @Autowired
    private BizOrderMapper bizOrderMapper;

    /**
     * 查询车辆实时状态列表（分页）
     *
     * @param bizCarState 车辆实时状态
     * @return 车辆实时状态
     */
    @Override
    public Page<BizCarState> selectBizCarStatePage(BizCarState bizCarState,int pageNum, int pageSize)
    {
        LambdaQueryWrapper<BizCarState> queryWrapper = buildQueryWrapper(bizCarState);
        return bizCarStateMapper.selectPage(new Page<>(pageNum,pageSize),queryWrapper);
    }

    @Override
    public BizCarState selectBizCarStateByCarId(Long carId) {
        return bizCarStateMapper.selectBizCarStateByCarId(carId);
    }

    private LambdaQueryWrapper<BizCarState> buildQueryWrapper(BizCarState bizCarState) {
        LambdaQueryWrapper<BizCarState> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(bizCarState.getCarNo())){
            queryWrapper.eq(BizCarState::getCarNo, bizCarState.getCarNo());
        }
        if (ObjectUtil.isNotEmpty(bizCarState.getCarId()) && bizCarState.getCarId() != 0){
            queryWrapper.eq(BizCarState::getCarId, bizCarState.getCarId());
        }
        return queryWrapper;
    }

    /**
     * 查询车辆实时状态
     * 
     * @param id 车辆实时状态主键
     * @return 车辆实时状态
     */
    @Override
    public BizCarState selectBizCarStateById(Long id)
    {
        return bizCarStateMapper.selectById(id);
    }

    /**
     * 查询车辆实时状态列表
     * 
     * @param bizCarState 车辆实时状态
     * @return 车辆实时状态
     */
    @Override
    public List<BizCarState> selectBizCarStateList(BizCarState bizCarState)
    {
        LambdaQueryWrapper<BizCarState> queryWrapper = buildQueryWrapper(bizCarState);
        List<BizCarState> bizCarStateList = bizCarStateMapper.selectList(queryWrapper);
        for (BizCarState carState : bizCarStateList) {
//            Date gpsTime = carState.getGpsTime();
            String carNo = carState.getCarNo();
            LambdaQueryWrapper<BizOrder> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(BizOrder::getCarNo,carNo);
            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){
                carState.setOrderSn(bizOrder.getOrderSn());
                carState.setDriverName(bizOrder.getDriverName());
                carState.setCopilotName(bizOrder.getCopilotName());
            }
        }
        return bizCarStateList;
    }

    /**
     * 新增车辆实时状态
     * 
     * @param bizCarState 车辆实时状态
     * @return 结果
     */
    @Override
    public int insertBizCarState(BizCarState bizCarState)
    {
        return bizCarStateMapper.insert(bizCarState);
    }

    /**
     * 修改车辆实时状态
     * 
     * @param bizCarState 车辆实时状态
     * @return 结果
     */
    @Override
    public int updateBizCarState(BizCarState bizCarState)
    {
        return bizCarStateMapper.updateById(bizCarState);
    }

    /**
     * 批量删除车辆实时状态
     * 
     * @param ids 需要删除的车辆实时状态主键
     * @return 结果
     */
    @Override
    public int deleteBizCarStateByIds(Long[] ids)
    {
        return bizCarStateMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除车辆实时状态信息
     * 
     * @param id 车辆实时状态主键
     * @return 结果
     */
    @Override
    public int deleteBizCarStateById(Long id)
    {
        return bizCarStateMapper.deleteById(id);
    }
}
