package com.tofly.gpsboot.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

@Component
public class CurrentCarUtil {
  private static ValueOperations<String,String> valueOperations;
    private static String CAR_INFO_CARNO="CAR:INFO:CARNO";

    private static String  CAR_INFO_CARID="CAR:INFO:CARID";
    private static String  CAR_INFO_GPSID="CAR:INFO:GPSID";
    @Autowired
    public CurrentCarUtil(ValueOperations valueOperations) {
        this.valueOperations = valueOperations;
    }

    /**
     *
     * @param userId  用户id
     * @param carNo  车牌号
     * @param carId 车辆id
     */
    public  static  void setCarInfo(Long  userId,String carNo,String carId,String gpsId){
        valueOperations.getOperations().opsForHash().put(CAR_INFO_CARNO,String.valueOf(userId) ,carNo);
        valueOperations.getOperations().opsForHash().put(CAR_INFO_CARID,String.valueOf(userId),carId);
        valueOperations.getOperations().opsForHash().put(CAR_INFO_GPSID,String.valueOf(userId),gpsId);
    }

    public static  void  clearCarInfo(Long  userId){
        valueOperations.getOperations().opsForHash().delete(CAR_INFO_CARNO,String.valueOf(userId) );
        valueOperations.getOperations().opsForHash().delete(CAR_INFO_CARID,String.valueOf(userId));
        valueOperations.getOperations().opsForHash().delete(CAR_INFO_GPSID,String.valueOf(userId));
    }
    /**
     * 通过用户获取当前车牌号
     * @param userId
     * @return
     */
    public  static  String  getCarNo(Long userId){
        return (String) valueOperations.getOperations().opsForHash().get(CAR_INFO_CARNO,String.valueOf(userId));
    }

    public static  Long getGpsId(Long userId){
        return (Long) valueOperations.getOperations().opsForHash().get(CAR_INFO_GPSID,String.valueOf(userId));
    }
}
