package com.storlead.system.util;

import com.storlead.framework.common.result.Result;
import com.storlead.framework.redis.RedisService;
import com.storlead.system.constant.ReidsCacheConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

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

/**
 * @program: sp-sales
 * @description:
 * @author: chenkq
 * @create: 2022-08-16 09:29
 */
@Slf4j
@Component
public class SystemSettingCacheCheckUtil {

    @Resource
    private RedisService redisService;

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

    @PostConstruct
    public void init() {
        staticRedisService = redisService;
    }

    /**
     * 手动维护
     * @return
     */
    public static Result checkAddDataMode(){
        Object mode = staticRedisService.getCacheObject(ReidsCacheConstant.SYS_CACHE_ORG_SYNC_MODE);
        if (Objects.isNull(mode) || Integer.valueOf(0).equals(mode)) {
            return Result.ok();
        }
        if (Integer.valueOf(1).equals(mode)) {
            return Result.ok();
        }
        return Result.error("无法进行手动维护数据,如需手动维护数据,请修进入全局设置模块设置数据维护方式");
    }

    /**
     * 同步OA
     * @return
     */
    public static Result checkSyncOaDataMode(){
        Object mode = staticRedisService.getCacheObject(ReidsCacheConstant.SYS_CACHE_ORG_SYNC_MODE);
        if (Objects.isNull(mode) || Integer.valueOf(0).equals(mode)) {
            return Result.ok();
        }
        if (Integer.valueOf(2).equals(mode)) {
            return Result.ok();
        }
        return Result.error("无法通过同步OA维护数据,如需通过同步OA维护数,请修进入全局设置模块设置数据维护方式");
    }

    /**
     * 企业微信同步
     * @return
     */
    public static Result checkSyncWeChatDataMode(){
        Object mode = staticRedisService.getCacheObject(ReidsCacheConstant.SYS_CACHE_ORG_SYNC_MODE);
        if (Objects.isNull(mode) || Integer.valueOf(0).equals(mode)) {
            return Result.ok();
        }
        if (Integer.valueOf(3).equals(mode)) {
            return Result.ok();
        }
        return Result.error("无法通过企业微信同步数据,如需通过企业微信维护数据,请修进入全局设置模块设置数据维护方式");
    }
}
