All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xyz.erupt.upms.handler.DictChoiceFetchHandler Maven / Gradle / Ivy

The newest version!
package xyz.erupt.upms.handler;

import org.springframework.stereotype.Component;
import xyz.erupt.annotation.fun.ChoiceFetchHandler;
import xyz.erupt.annotation.fun.VLModel;
import xyz.erupt.core.cache.EruptCache;
import xyz.erupt.core.cache.EruptCacheLRU;
import xyz.erupt.core.util.EruptAssert;
import xyz.erupt.jpa.dao.EruptDao;
import xyz.erupt.upms.constant.FetchConst;
import xyz.erupt.upms.model.EruptDictItem;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author YuePeng
 * date 2020/12/16 18:00
 */
@Component
public class DictChoiceFetchHandler implements ChoiceFetchHandler {

    @Resource
    private EruptDao eruptDao;

    private final EruptCache> dictCache = new EruptCacheLRU<>(500);

    @Override
    public List fetch(String[] params) {
        EruptAssert.notNull(params, DictChoiceFetchHandler.class.getSimpleName() + " → params[0] must dict → code");
        return dictCache.getAndSet(DictChoiceFetchHandler.class.getName() + ":" + params[0],
                params.length == 2 ? Long.parseLong(params[1]) : FetchConst.DEFAULT_CACHE_TIME, ()
                        -> eruptDao.lambdaQuery(EruptDictItem.class).addCondition("eruptDict.code = :code",
                                new HashMap() {{
                                    this.put("code", params[0]);
                                }}).orderBy(EruptDictItem::getSort).list()
                        .stream().map((item) -> new VLModel(item.getId(), item.getName())).collect(Collectors.toList()));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy