package com.yunzhupaas.integrate.job;

import cn.hutool.core.util.ObjectUtil;
import com.yunzhupaas.base.UserInfo;
import com.yunzhupaas.integrate.model.nodeJson.IntegrateModel;
import com.yunzhupaas.util.JsonUtil;
import com.yunzhupaas.util.RedisUtil;
import com.yunzhupaas.util.StringUtil;
import lombok.extern.slf4j.Slf4j;


/**
 * @author 云筑产品开发平台组
 * @version V3.3.0 flowable
 * @copyright 深圳市乐程软件有限公司
 * @date 2024/6/15 17:37
 */
@Slf4j
public class IntegrateJobUtil {
    /**
     * 缓存key
     */
    public static final String WORKTIMEOUT_REDIS_KEY = "idgenerator_IntegrateList";

    /**
     * 缓存key
     */
    public static final String IDGENERATOR_REDIS_KEY = "idgenerator_IntegrateModel";

    /**
     * 缓存key
     */
    public static final String IDGENERATORID_REDIS_KEY = "idgenerator_id";

    public static IntegrateModel getModel(IntegrateModel model, RedisUtil redisUtil) {
        String id = model.getId();
        String hashValues = redisUtil.getHashValues(IDGENERATOR_REDIS_KEY, id);
        IntegrateModel integrateModel = StringUtil.isNotEmpty(hashValues) ? JsonUtil.getJsonToBean(hashValues, IntegrateModel.class) : null;
        return integrateModel;
    }

    public static void insertModel(IntegrateModel model, RedisUtil redisUtil) {
        String integrateId = model.getId();
        redisUtil.insertHash(IDGENERATOR_REDIS_KEY, integrateId, JsonUtil.getObjectToString(model));
        insertTenant(model, redisUtil);
    }

    public static void removeModel(IntegrateModel model, RedisUtil redisUtil) {
        redisUtil.removeHash(IDGENERATOR_REDIS_KEY, model.getId());
    }

    public static void insertTenant(IntegrateModel model, RedisUtil redisUtil) {
        String tenantId = StringUtil.isNotEmpty(model.getUserInfo().getTenantId()) ? model.getUserInfo().getTenantId() : "yunzhupaas";
        UserInfo userInfo = model.getUserInfo();
        redisUtil.insertHash(WORKTIMEOUT_REDIS_KEY, tenantId, JsonUtil.getObjectToString(userInfo));
    }

    public static void removeTenant(String tenantId, RedisUtil redisUtil) {
        tenantId = StringUtil.isNotEmpty(tenantId) ? tenantId : "com.yunzhupaas";
        redisUtil.removeHash(WORKTIMEOUT_REDIS_KEY, tenantId);
    }

    public static boolean getIntegrate(IntegrateModel model, RedisUtil redisUtil){
        String value = redisUtil.getHashValues(IDGENERATORID_REDIS_KEY, model.getId());
        return ObjectUtil.isNotEmpty(value);
    }

    public static void insertIntegrate(IntegrateModel model, RedisUtil redisUtil){
        redisUtil.insertHash(IDGENERATORID_REDIS_KEY, model.getId(), model.getId());
    }

    public static void removeIntegrate(IntegrateModel model, RedisUtil redisUtil){
        redisUtil.removeHash(IDGENERATORID_REDIS_KEY, model.getId());
    }

}

