package cn.iocoder.yudao.module.member.service.amount;

import java.util.concurrent.ConcurrentHashMap;

public class LockManager {

    private static final ConcurrentHashMap<Long, Object> userLocks = new ConcurrentHashMap<>();

    private static final ConcurrentHashMap<Long, Object> storeLocks = new ConcurrentHashMap<>();

    private static final ConcurrentHashMap<Long, Object> supermarketLocks = new ConcurrentHashMap<>();

    private static final ConcurrentHashMap<String, Object> storeOrderLock = new ConcurrentHashMap<>();


    public static final String CANCEL_KEY = "cancel";

    public static final String INVENTORY_KEY = "inventory";

    public static Object getUserLock(Long userId) {
        // 从 Map 中获取 WeakReference 对象
        return userLocks.computeIfAbsent(userId, k -> new Object());
    }

    public static Object getStoreLock(Long storeId) {
        // 对每个 userId 生成独立的锁对象，并发地存储在 ConcurrentHashMap 中
        return storeLocks.computeIfAbsent(storeId, k -> new Object());
    }

    public static Object getSupermarketLock(Long storeId) {
        // 对每个 userId 生成独立的锁对象，并发地存储在 ConcurrentHashMap 中
        return supermarketLocks.computeIfAbsent(storeId, k -> new Object());
    }

    public static Object getStoreOrderLock(String key) {
        // 对每个 userId 生成独立的锁对象，并发地存储在 ConcurrentHashMap 中
        return storeOrderLock.computeIfAbsent(key, k -> new Object());
    }
}

