package com.ruoyi.app.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.app.domain.Monitor;
import com.ruoyi.app.domain.NFIDReader;
import com.ruoyi.app.mapper.NFIDReaderMapper;
import com.ruoyi.app.service.INFIDReaderService;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.mapper.SysConfigMapper;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Collection;
import java.util.List;

/**
 * 参数配置 服务层实现
 * 
 * @author ruoyi
 */
@Service
public class NFIDReaderServiceImpl implements INFIDReaderService
{
    @Autowired
    private NFIDReaderMapper nfidReaderMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 项目启动时，初始化到缓存
     */
    @PostConstruct
    public void init()
    {
        resetConfigCache();
    }


    /**
     * 根据键名查询信息
     * 
     * @param key 设备序列号
     * @return 参数键值
     */
    @Override
    public String selectNFIDReaderSpotByKey(String key)
    {
        NFIDReader reader = redisCache.getCacheObject(getCacheKey(key));
        if (StringUtils.isNotNull(reader))
        {
            return reader.getDeviceSpot();
        }
        reader = nfidReaderMapper.selectNFIDReaderByDeviceSerial(key);
        if (StringUtils.isNotNull(reader))
        {
            redisCache.setCacheObject(getCacheKey(key), reader);

            return reader.getDeviceSpot();
        }
        return StringUtils.EMPTY;
    }


    /**
     * 查询nfid识别器列表
     * @param NFIDReader nfid识别器
     * @return 参数配置集合
     */
    @Override
    public List<NFIDReader> selectNFIDReaderList(NFIDReader NFIDReader)
    {
        return nfidReaderMapper.selectNFIDReaderList(NFIDReader);
    }

    /**
     * 修改nfid识别器
     *
     * @param NFIDReader nfid识别器
     * @return 结果
     */
    @Override
    public int updateNFIDReaderBySerial(NFIDReader NFIDReader)
    {
        return nfidReaderMapper.updateNFIDReaderBySerial(NFIDReader);
    }

    /**
     * 加载参数缓存数据
     */
    @Override
    public void loadingConfigCache()
    {
        List<NFIDReader> NFIDReaderList = nfidReaderMapper.selectNFIDReaderList(new NFIDReader());
        for (NFIDReader nfidReader : NFIDReaderList)
        {
            redisCache.setCacheObject(getCacheKey(nfidReader.getDeviceSerial()), nfidReader);
        }
    }

    /**
     * 清空参数缓存数据
     */
    @Override
    public void clearConfigCache()
    {
        Collection<String> keys = redisCache.keys(CacheConstants.APP_NFID_READER + "*");
        redisCache.deleteObject(keys);
    }

    /**
     * 重置参数缓存数据
     */
    @Override
    public void resetConfigCache()
    {
        clearConfigCache();
        loadingConfigCache();
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey)
    {
        return CacheConstants.APP_NFID_READER + configKey;
    }
}
