package com.mortals.xhx.modules.implementlist.service.impl;

import com.github.benmanes.caffeine.cache.Cache;
import com.mortals.xhx.modules.implementlist.dao.RybMatterDao;
import com.mortals.xhx.modules.implementlist.model.RybMatterListEntry;
import com.mortals.xhx.modules.implementlist.service.RybMatterListService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RybMatterListServiceImpl implements RybMatterListService {

    @Resource
    private RybMatterDao rybMatterDao;

    private final Cache<String, Object> caffeineCache;

    @Override
    public RybMatterListEntry getThImplement(String event_code) {
        caffeineCache.getIfPresent(event_code);
        RybMatterListEntry thImplementList = (RybMatterListEntry) caffeineCache.asMap().get(event_code);
        if (thImplementList != null) {
            return thImplementList;
        }
        //根据事项编码查
        List<RybMatterListEntry> rybMatterListEntries = rybMatterDao.find(event_code);
        if (rybMatterListEntries.size() > 0) {
            for (RybMatterListEntry entity : rybMatterListEntries) {
                thImplementList=entity;
                caffeineCache.put(event_code, entity);
            }
        }
        return thImplementList;
    }
}