package com.friday.custody.common;


import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.LinkedBlockingQueue;

@Slf4j
public class CacheAdminTrustNoTypeMap {
    //key admin + trustno + type
    private static final Map<String, Map<Integer, LinkedBlockingQueue<Object>>> cacheLockMap = new HashMap<>();

    public static synchronized void put(String key, Integer reqFlag, Object entity) {
        try {
            Map<Integer, LinkedBlockingQueue<Object>> integerListMap = cacheLockMap.get(key);
            if (Objects.isNull(integerListMap)) {
                integerListMap = new HashMap<>();
                LinkedBlockingQueue<Object> objects = new LinkedBlockingQueue<Object>();
                integerListMap.put(reqFlag, objects);
                objects.put(entity);
                cacheLockMap.put(key, integerListMap);
                return;
            }
            LinkedBlockingQueue<Object> invokeRecordEntities = integerListMap.get(reqFlag);
            if (CollectionUtils.isEmpty(invokeRecordEntities)) {
                invokeRecordEntities = new LinkedBlockingQueue<>();
                integerListMap.put(reqFlag, invokeRecordEntities);
            }
            invokeRecordEntities.put(entity);
        } catch (InterruptedException e) {
            log.error("数据插入失败:the key is " + key + " ,the reqFlag is " + reqFlag + ", the entity is " + JSON.toJSONString(entity), e);
            Thread.currentThread().interrupt();
        }

    }

    public static synchronized Object get(String key) {
        if (cacheLockMap.isEmpty()) {
            return null;
        }
        Map<Integer, LinkedBlockingQueue<Object>> integerLinkedListMap = cacheLockMap.get(key);
        if (integerLinkedListMap == null || integerLinkedListMap.isEmpty()) {
            return null;
        }
        LinkedBlockingQueue<Object> updateRecordEntities = integerLinkedListMap.get(CustodyContants.UPDATE_FLAG);
        if (!CollectionUtils.isEmpty(updateRecordEntities)) {
            return updateRecordEntities.poll();
        }
        LinkedBlockingQueue<Object> initRecordEntities = integerLinkedListMap.get(CustodyContants.INIT_FLAG);
        if (!CollectionUtils.isEmpty(initRecordEntities)) {
            return initRecordEntities.poll();
        }
        return null;
    }

    public static synchronized void remove(String key) {
        cacheLockMap.remove(key);
    }

    public static List<Object> get(String trustNo, String type) {
        String str = trustNo + "::" + type;
        log.info("CacheAdminTrustNoTypeMap get:the str is :{}", str);
        ArrayList<Object> values = new ArrayList<>();
        Set<String> keys = cacheLockMap.keySet();
        for (String key : keys) {
            if (key.contains(str)) {
                values.add(get(key));
            }
        }
        log.info("CacheAdminTrustNoTypeMap get::the str is :{},the values is :{}", str, JSON.toJSONString(values));
        return values;
    }

}