package thyyxxk.webserver.service.hutoolcache;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.util.StrUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import thyyxxk.webserver.dao.his.hutoolcache.HutoolCacheDao;

import java.util.List;

@Service
public class ZyBillItemCache implements HutoolCacheInterface<ZyBillItemCache.ZyBillItem> {
    private final HutoolCacheDao dao;
    private static final int CACHE_SIZE = 32;
    Cache<String, ZyBillItemCache.ZyBillItem> cache = CacheUtil.newLFUCache(CACHE_SIZE);

    @Autowired
    public ZyBillItemCache(HutoolCacheDao dao) {
        this.dao = dao;
    }

    @Data
    public static class ZyBillItem {
        private String code;
        private String name;
        private String pyCode;
        private String chargeCode;
    }

    @Override
    public void put(String key, ZyBillItem value) {
        cache.put(key, value);
    }

    @Override
    public ZyBillItem get(String key) {

        if (StrUtil.isBlank(key)) {
            return new ZyBillItem();
        }

        ZyBillItem value = cache.get(key);
        if (null == value) {
            value = dbInfo(key);
            cache.put(key, value);
        }
        return value;
    }

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

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

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

    @Override
    public void refreshCache(String key) {
        put(key, dbInfo(key));
    }


    @Override
    public String getLabel() {
        return "住院收费类别";
    }

    private ZyBillItem dbInfo(String key) {
        return dao.selectZyBillItem(key);
    }
}
