package com.storlead.system.util;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.storlead.framework.redis.RedisService;
import com.storlead.framework.web.exception.WebException;
import com.storlead.system.pojo.entity.SystemConfigItemEntity;
import com.storlead.system.service.SystemConfigItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

/**
 * @program: sp-sales-gotr
 * @description:
 * @author: chenkq
 * @create: 2023-07-26 16:44
 */
@Slf4j
@Component
public class SystemConfigItemCacheUtil {

    public final static String itemCacheKey = "reids_config_item_cache_";

    @Resource
    private RedisService redisService;

    @Resource
    private SystemConfigItemService configItemService;

    /** 由 {@link #init()} 赋值，禁止在静态字段上使用注入注解 */
    private static RedisService staticRedisService;

    private static SystemConfigItemService staticConfigItemService;

    @PostConstruct
    public void init() {
        staticRedisService = redisService;
        staticConfigItemService = configItemService;
    }
    public static String getCacheConfigItemValue(String code) {
//        String value = staticRedisService.getCacheObject(itemCacheKey+code);
//        if (StrUtil.isNotBlank(value)) {
//            return value;
//        }
         SystemConfigItemEntity item = getSystemConfigItem(code);
        if (Objects.isNull(item)) {
            return "";
        }
        String value = item.getItemFormatValue();
        staticRedisService.setCacheObject(itemCacheKey+code,value,24L, TimeUnit.HOURS);
        return item.getItemFormatValue();
    }

    public static SystemConfigItemEntity getSystemConfigItem(String code) {
        LambdaQueryWrapper<SystemConfigItemEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SystemConfigItemEntity::getItemCode,code);
        SystemConfigItemEntity item = staticConfigItemService.getOne(queryWrapper);
        if (Objects.isNull(item)) {
            return null;
        }
        return item;
    }


    public static String getDefaultPassWord(){
       String defaultPassWord  = getCacheConfigItemValue("DEFAULT_PASS");
       if (StrUtil.isBlank(defaultPassWord)) {
           defaultPassWord = "storlead123456";
       }
       return defaultPassWord;
    }

    public static void checkPasswordStrength(String password) {
        SystemConfigItemEntity item  = getSystemConfigItem("DEFAULT_PASSWORD_STRENGTH");

        String format = item.getItemFormatValue();
        boolean flat = Pattern.matches(format, password);
        if (flat) {
            throw new WebException("密码强度不够，密码不满足："+item.getItemDataFormatDesc());
        }
    }

    public static String getWebSocketTgpUrl() {
        String value  = getCacheConfigItemValue("CONFIG_TGP_SOCKET_TGPURL");
        return value;
    }

    public static String getWebSocketTgpProtocol() {
        String value  = getCacheConfigItemValue("CONFIG_TGP_SOCKET_PROTOCOL");
        return value;

    }

    public static Integer getLogReserveTimeDay(){
        String reserveTimeDay  = getCacheConfigItemValue("RESERVE_TIME_DAY");
        if (StrUtil.isBlank(reserveTimeDay)) {
            reserveTimeDay = "365";
        }
        return Integer.valueOf(reserveTimeDay);
    }

    public static void updateConfigItemCacheByCode(String code) {
        SystemConfigItemEntity item = getSystemConfigItem(code);
        if (Objects.isNull(item)) {
            return ;
        }
        String value = item.getItemFormatValue();
        staticRedisService.setCacheObject(itemCacheKey+item.getItemCode(),value,24L, TimeUnit.HOURS);
    }
}
