package com.muyu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.constant.CacheConstants;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.redis.service.RedisService;
import com.muyu.system.domain.SysConfig;
import com.muyu.system.mapper.SysConfigMapper;
import com.muyu.system.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author DongZl
 * @description: 配置plus业务实现层
 * @Date 2023-11-13 上午 10:06
 */
@Service
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig>
        implements SysConfigService {

    @Autowired
    private RedisService redisService;

    @Override
    public List<SysConfig> pageQuery (SysConfig config) {
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(config.getConfigName())){
            queryWrapper.like(SysConfig::getConfigName, config.getConfigName());
        }
        if (StringUtils.isNotEmpty(config.getConfigType())){
            queryWrapper.like(SysConfig::getConfigType, config.getConfigType());
        }
        if (StringUtils.isNotEmpty(config.getConfigKey())){
            queryWrapper.like(SysConfig::getConfigKey, config.getConfigKey());
        }
        Object beginTime = config.getParams().get("beginTime");
        if (Objects.nonNull(beginTime) && beginTime instanceof Date beginDate){
            queryWrapper.gt(SysConfig::getCreateTime, beginDate);
        }
        Object endTime = config.getParams().get("endTime");
        if (Objects.nonNull(endTime) && endTime instanceof Date endDate){
            queryWrapper.lt(SysConfig::getCreateTime, endDate);
        }
        return this.list(queryWrapper);
    }

    /**
     * 通过Key进行查询值
     *
     * @param configKey
     *
     * @return
     */
    @Override
    public String selectConfigByKey (String configKey) {
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        Assert.notNull(configKey, "key不可为空");
        queryWrapper.eq(SysConfig::getConfigKey, configKey);
        SysConfig sysConfig = this.getOne(queryWrapper);
        return sysConfig.getConfigValue();
    }

    @Override
    public boolean checkConfigKeyUnique (SysConfig config) {
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysConfig::getConfigKey, config.getConfigKey());
        return this.count(queryWrapper) > 0;
    }

    @Override
    public void resetConfigCache () {
        this.clearConfigCache();
        this.loadingConfigCache();
    }

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

    /**
     * 加载参数缓存数据
     */
    @Override
    public void loadingConfigCache () {
        List<SysConfig> configsList = this.list();
        for (SysConfig config : configsList) {
            redisService.setCacheObject(getCacheKey(config.getConfigKey()), config.getConfigValue());
        }
    }

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