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.SysMessageType;
import com.muyu.openbusiness.mapper.SysMessageTypeMapper;
import com.muyu.openbusiness.service.ISysMessageTypeService;
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 SysMessageTypeServiceImpl
        extends ServiceImpl<SysMessageTypeMapper, SysMessageType>
        implements ISysMessageTypeService {

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

    @Autowired
    RedisTemplate<Object,Object> redisTemplate;
    @Override
    public SysMessageType selectSysMessageTypeByMessageCode(String messageCode)
    {
        LambdaQueryWrapper<SysMessageType> queryWrapper = new LambdaQueryWrapper<>();
        Assert.notNull(messageCode, "messageCode不可为空");
        queryWrapper.eq(SysMessageType::getMessageCode, messageCode);
        return this.getOne(queryWrapper);
    }


    /**
     * 查询车辆报文类型列表
     *
     * @param sysMessageType 车辆报文类型
     * @return 车辆报文类型
     */
    @Override
    public List<SysMessageType> selectSysMessageTypeList(SysMessageType sysMessageType)
    {
        LambdaQueryWrapper<SysMessageType> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(sysMessageType.getMessageName())){
            queryWrapper.like(SysMessageType::getMessageName, sysMessageType.getMessageName());
        }
        if (StringUtils.isNotEmpty(sysMessageType.getMessageType())){
            queryWrapper.eq(SysMessageType::getMessageType, sysMessageType.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 sysMessageType 车辆报文类型
     * @return 车辆报文类型
    */
    @Override
    public Boolean checkIdUnique(SysMessageType sysMessageType) {
        LambdaQueryWrapper<SysMessageType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysMessageType::getId, sysMessageType.getId());
        return this.count(queryWrapper) > 0;
    }

}
