package com.crunii.micro.service.sysmgr.service.impl;

import com.crunii.micro.common.constants.CommonConstants;
import com.crunii.micro.common.constants.RedisKeys;
import com.crunii.micro.common.exception.BusinessException;
import com.crunii.micro.domain.dto.Worker;
import com.crunii.micro.service.sysmgr.service.ThemeService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author wangchen create 2023/7/28 11:17
 */
@Service
public class ThemeServiceImpl implements ThemeService ,InitializingBean{

    @Resource
    private RedisTemplate<String,Integer> redisTemplate;

    private static int SYS_THEME;

    @Override
    public void afterPropertiesSet() throws Exception {
        Integer aBoolean = redisTemplate.opsForValue().get(RedisKeys.SYS_THEME_KEY);
        // 防止第一次获取为空
        if(Objects.isNull(aBoolean)){
            SYS_THEME = 0;
        }else {
            SYS_THEME = aBoolean;
        }
    }

    @Override
    public Integer updateTheme(int sysTheme, Worker worker) {
        if(!CommonConstants.SUPER_LOGIN_IDS.contains(worker.getLoginId())){
            throw new BusinessException("无权限修改！");
        }
        SYS_THEME = sysTheme;
        redisTemplate.opsForValue().set(RedisKeys.SYS_THEME_KEY,SYS_THEME);
        return SYS_THEME;
    }

    @Override
    public Integer findThem() {
       return SYS_THEME;
    }
}
