package com.bawei.mall.search.utils;

import com.alibaba.ttl.TransmittableThreadLocal;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author DongZl
 * @description: 线程变量工具类
 * @Date 2022-10-24 下午 04:48
 */
public class SearchResultUtils {

    private static final String LIST_NAME = "list";

    private static final String TOTAL_NAME = "total";

    private static final TransmittableThreadLocal<Map<String, Object>> THREAD_LOCAL = new TransmittableThreadLocal<>();

    private static Map<String, Object> get(){
        Map<String, Object> map = THREAD_LOCAL.get();
        if (map != null){
            return map;
        }
        THREAD_LOCAL.set(new HashMap<>());
        return THREAD_LOCAL.get();
    }

    /**
     * 获取 集合
     * @return
     * @param <T>
     */
    public static <T> List<T> getList(){
        return (List<T>) get().get(LIST_NAME);
    }

    /**
     * 存储List
     * @param list
     */
    public static void setList(List list){
        get().put(LIST_NAME, list);
    }

    /**
     * 总条数
     * @return
     * @param <T>
     */
    public static <T> T getTotal(){
        return (T) get().get(TOTAL_NAME);
    }

    /**
     * 存储
     * @param total
     */
    public static void setTotal(Object total){
        get().put(TOTAL_NAME, total);
    }
}
