DictService.java 2.25 KB
package com.huaheng.framework.web.service;

import com.huaheng.pc.system.dict.domain.DictData;
import com.huaheng.pc.system.dict.service.IDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * huaheng首创 html调用 thymeleaf 实现字典读取
 * 
 * @author huaheng
 */
@Service("dict")
public class DictService
{
    @Autowired
    private IDictDataService dictDataService;

    public static List<DictData> dictDataList = null;

    /**
     * 根据字典类型查询字典数据信息
     * 
     * @param dictType 字典类型
     * @return 参数键值
     */
    public List<DictData> getType(String dictType)
    {
        if (DictService.dictDataList == null ||DictService.dictDataList.size()==0)
        {
            DictData dictData = new DictData();
            dictData.setEnable(true);
            dictData.setDeleted(false);
            DictService.dictDataList = dictDataService.selectDictDataList(dictData);
        }
        List<DictData> result = DictService.dictDataList.stream().
                                    filter(x -> x.getDictType().equals(dictType)).
                                    collect(Collectors.toList());
                                    result.sort(Comparator.comparing(DictData::getDictSort));

                                return result;
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     * 
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    public String getLabel(String dictType, String dictValue)
    {
        if (DictService.dictDataList == null)
        {
            DictData dictData = new DictData();
            dictData.setEnable(true);
            DictService.dictDataList = dictDataService.selectDictDataList(dictData);
        }
        String result = DictService.dictDataList.stream().
                        filter(x -> x.getDictType().equals(dictType) && x.getDictValue().equals(dictValue)).
                        findFirst().
                        map(DictData::getDictLabel).
                        orElse(null);
        return result;
    }
}