package thyyxxk.webserver.service.hutoolcache;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Component;
import thyyxxk.webserver.dao.his.hutoolcache.HutoolCacheDao;
import thyyxxk.webserver.entity.medicine.YpZdGroupName;
import thyyxxk.webserver.entity.zhuyuanyisheng.yizhuluru.YzZdOrderItemConfirm;

import java.util.*;
import java.util.function.Supplier;

@Component
@RequiredArgsConstructor
public class ExtraCache implements HutoolCacheInterface<Object> {

    private final HutoolCacheDao dao;

    Cache<String, Object> cache = CacheUtil.newLFUCache(200);

    /**
     * 注意
     *
     * @param key
     * @param function
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public <T> T getValue(String key, Supplier<T> function) {
        Object value = get(key);
        if (value == null) {
            value = function.get();
            put(key, value);
            return (T) value;
        }
        return (T) value;
    }

    public <T> T getValue(CacheEnums key, Supplier<T> function) {
        return getValue(key.getName(), function);
    }

    @Override
    public void put(String key, Object value) {
        if (value == null) {
            return;
        }
        cache.put(key, value);
    }

    @Override
    public Object get(String key) {
        return cache.get(key);
    }

    @Override
    public void del(String key) {
        cache.remove(key);
    }

    public void del(@NotNull CacheEnums key) {
        cache.remove(key.getName());
    }

    @Override
    public void clear() {
        cache.clear();
    }

    @Override
    public List<CacheAll> getAll() {
        return this.getAllUtils(cache.cacheObjIterator());
    }

    /**
     * 获取一些特殊的医嘱的
     *
     * @return 返回数据
     */
    public List<YzZdOrderItemConfirm> getYzZdOrderItemConfirm() {
        return getValue(CacheEnums.YI_ZHU_SPECIAL_MEDICAL_ADVICE, dao::getYzZdOrderItemConfirm);
    }


    public Map<String, List<String>> getYzZdOrderItemMap() {
        List<YzZdOrderItemConfirm> yzZdOrderItemConfirm = getYzZdOrderItemConfirm();

        if (yzZdOrderItemConfirm == null) {
            return Collections.emptyMap();
        }

        Map<String, List<String>> map = new HashMap<>();

        yzZdOrderItemConfirm.forEach(item -> {
            map.computeIfAbsent(item.getItemName(), k -> new ArrayList<>()).add(item.getOrderCode());
        });
        return map;
    }


    public Map<String, String> getMessage() {
        Map<String, String> map = new HashMap<>();
        map.put(CacheEnums.SYSTEM_UPDATES_MESSAGE.getName(),
                (String) get(CacheEnums.SYSTEM_UPDATES_MESSAGE.getName()));
        map.put(CacheEnums.SCROLLING_MESSAGES.getName(),
                (String) get(CacheEnums.SCROLLING_MESSAGES.getName()));
        return map;
    }


    public Map<String, List<YpZdGroupName>> getYf() {
        return this.getValue("住院药房", () -> {
            Map<String, List<YpZdGroupName>> rst = new HashMap<>();
            rst.put("xy", dao.selectYpZdGroup());
            rst.put("cy", dao.selectYpCyGroup());
            return rst;
        });
    }

    @Override
    public String getLabel() {
        return "其他缓存";
    }
}
