package cn.estsh.i3plus.ext.mes.pcn.apiservice.util;

import cn.estsh.i3plus.ext.mes.pcn.pojo.constant.MesCommonConstant;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
import cn.estsh.impp.framework.boot.util.ImppRedis;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Description :
 * @Reference :
 * @Author :
 * @CreateDate : 2023-01-04 17:51
 * @Modify:
 **/
@Service
public class MesRedisLockUtil {
    @Resource(name = "redisMesPcn")
    private ImppRedis redisMes;

    public Boolean redisLock(String moduleKey) {
        //加锁
        boolean lockFlg = redisMes.setIfAbsent(moduleKey, MesCommonConstant.EXPIRE_TIME);
        //成功则返回true 失败则提示消息
        if (!lockFlg) {
            throw new ImppBusiException(MesCommonConstant.REDIS_LOCK_MESSAGE);
        }
        redisMes.expire(moduleKey, MesCommonConstant.EXPIRE_TIME);
        return true;
    }

    public void redisUnLock(boolean lockFlg, String moduleKey) {
        //解锁
        if (lockFlg) {
            redisMes.deleteKey(moduleKey);
        }
    }

}
