package com.muyu.openbusiness.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.openbusiness.config.SnowflakeIdWorker;
import com.muyu.openbusiness.domain.SysCarMessage;
import com.muyu.openbusiness.mapper.SysCarMessageMapper;
import com.muyu.openbusiness.service.ISysCarMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * 车辆报文记录Service业务层处理
 *
 * @author muyu
 * @date 2024-09-18
 */




@Service
public class SysCarMessageServiceImpl
        extends ServiceImpl<SysCarMessageMapper, SysCarMessage>
        implements ISysCarMessageService {


    /**
     * 精确查询车辆报文记录
     *
     * @param id 车辆报文记录主键
     * @return 车辆报文记录
     */

    @Autowired
    RedisTemplate
    redisTemplate;
    @Override
    public SysCarMessage selectSysCarMessageById(Long id)
    {
        LambdaQueryWrapper<SysCarMessage> queryWrapper = new LambdaQueryWrapper<>();
        Assert.notNull(id, "id不可为空");
        queryWrapper.eq(SysCarMessage::getId, id);

        return this.getOne(queryWrapper);
    }


    /**
     * 查询车辆报文记录列表
     *
     * @param sysCarMessage 车辆报文记录
     * @return 车辆报文记录
     */
    @Override
    public List<SysCarMessage> selectSysCarMessageList(SysCarMessage sysCarMessage)
    {
        LambdaQueryWrapper<SysCarMessage> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(sysCarMessage.getModelCode())){
            queryWrapper.eq(SysCarMessage::getModelCode, sysCarMessage.getModelCode());
        }
        if (StringUtils.isNotEmpty(sysCarMessage.getMessageType())){
            queryWrapper.eq(SysCarMessage::getMessageType, sysCarMessage.getMessageType());
        }

        //生成一个时间唯一戳
        SnowflakeIdWorker snowMaker = new SnowflakeIdWorker();
        for (int i = 0; i < 100; i++) {
            long id = snowMaker.nextId();
            System.out.println(id);

        }
        redisTemplate.opsForValue().set("id",list(queryWrapper));
        return this.list(queryWrapper);
    }

    /**
    * 唯一 判断
    * @param sysCarMessage 车辆报文记录
     * @return 车辆报文记录
    */
    @Override
    public Boolean checkIdUnique(SysCarMessage sysCarMessage) {
        LambdaQueryWrapper<SysCarMessage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysCarMessage::getId, sysCarMessage.getId());
        return this.count(queryWrapper) > 0;
    }

}
