package com.yiqi.system.service.impl;

import java.util.Collection;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.constant.CacheConstants;
import com.yiqi.common.core.redis.RedisCache;
import com.yiqi.common.core.text.Convert;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.common.utils.StringUtils;
import com.yiqi.system.domain.SysConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.system.mapper.SysConfigRecordMapper;
import com.yiqi.system.domain.SysConfigRecord;
import com.yiqi.system.service.ISysConfigRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.PostConstruct;

/**
 * 配置Service业务层处理
 *
 * @author douya
 * @date 2024-11-01
 */
@Service
public class SysConfigRecordServiceImpl extends ServiceImpl<SysConfigRecordMapper, SysConfigRecord> implements ISysConfigRecordService {

    @Autowired
    private RedisCache redisCache;

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

    /**
     * 查询配置
     *
     * @param id 配置主键
     * @return 配置
     */
    @Override
    public SysConfigRecord selectSysConfigRecordById(Integer id) {
        return baseMapper.selectSysConfigRecordById(id);
    }

    /**
     * 查询配置列表
     *
     * @param sysConfigRecord 配置
     * @return 配置
     */
    @Override
    public List<SysConfigRecord> selectSysConfigRecordList(SysConfigRecord sysConfigRecord) {
        return baseMapper.selectSysConfigRecordList(sysConfigRecord);
    }

    /**
     * 新增配置
     *
     * @param sysConfigRecord 配置
     * @return 结果
     */
    @Override
    public int insertSysConfigRecord(SysConfigRecord sysConfigRecord) {
        return baseMapper.insert(sysConfigRecord);
    }

    /**
     * 修改配置
     *
     * @param sysConfigRecord 配置
     * @return 结果
     */
    @Override
    public int updateSysConfigRecord(SysConfigRecord sysConfigRecord) {
        return baseMapper.updateById(sysConfigRecord);
    }

    /**
     * 批量删除配置
     *
     * @param ids 需要删除的配置主键
     * @return 结果
     */
    @Override
    public int deleteSysConfigRecordByIds(Integer[] ids) {
        return baseMapper.deleteSysConfigRecordByIds(ids);
    }

    /**
     * 删除配置信息
     *
     * @param id 配置主键
     * @return 结果
     */
    @Override
    public int deleteSysConfigRecordById(Integer id) {
        return baseMapper.deleteSysConfigRecordById(id);
    }

    /**
     * 修改配置状态
     *
     * @param sysConfigRecord 配置
     * @return 结果
     */
    @Override
    public int updateSysConfigRecordStatus(SysConfigRecord sysConfigRecord) {
        return baseMapper.updateSysConfigRecord(sysConfigRecord);
    }

    @Override
    public SysConfigRecord getConfigRecordByKey(String key, Integer tabId) {
        QueryWrapper<SysConfigRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_name", key).eq("config_tab_id", tabId);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<SysConfigRecord> getConfigRecordByKey(String[] keys) {
        QueryWrapper<SysConfigRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("menu_name", keys);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public String getCacheConfigRecordByKey(String key) {
        String configValue = Convert.toStr(redisCache.getCacheObject(getCacheKey(key)));
        if (StringUtils.isNotEmpty(configValue)) {
            return configValue;
        }
        QueryWrapper<SysConfigRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("menu_name", key);
        SysConfigRecord sysConfigRecord = baseMapper.selectOne(queryWrapper);

        if (StringUtils.isNotNull(sysConfigRecord)) {
            redisCache.setCacheObject(getCacheKey(key), sysConfigRecord.getValue());
            return sysConfigRecord.getValue();
        }
        return StringUtils.EMPTY;
    }

    @Override
    public void clearConfigCache(String key) {
        redisCache.deleteObject(getCacheKey(key));
    }

    /**
     * 加载参数缓存数据
     */
    @Override
    public void loadingConfigCache() {
        List<SysConfigRecord> configsList = baseMapper.selectList(new QueryWrapper<>());
        for (SysConfigRecord config : configsList) {
            redisCache.setCacheObject(getCacheKey(config.getMenuName()), config.getValue());
        }
    }

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

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

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