package com.xzx.gc.role.service; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.xzx.gc.entity.CoreDict; import com.xzx.gc.model.query.CoreDictQuery; import com.xzx.gc.role.mapper.CoreDictMapper; import com.xzx.gc.role.model.CoreDictQueryModel; import com.xzx.gc.util.enums.DelFlagEnum; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 描述: 字典 service,包含常规字典和级联字典的操作。 * @author : xiandafu */ @Service @Transactional public class CoreDictService{ private static final Logger LOGGER = LoggerFactory.getLogger(CoreDictService.class); @Autowired private CoreDictMapper dictMapper; @Autowired CorePlatformService platformService; @Autowired CoreDictService self ; private String type; /** * 根据类型获取字典集合 * @param type 字典类型, * @return List */ //@Cacheable(value = CorePlatformService.DICT_CACHE_TYPE) public List findAllByType(String type) { return dictMapper.findAllList(type); } /** * 级联字典查询,必须提供一个字典类型 * @param type * @param value * @return */ //@Cacheable(value = CorePlatformService.DICT_CACHE_CHILDREN) public List findAllByGroup(String type,String value) { List list = self.findAllByType(type); return _search(list,value); } /** * 级联字段下一级的字段列表 * @param id * @return */ //@Cacheable(value = CorePlatformService.DICT_CACHE_CHILDREN) public List findChildByParent(Long id) { return dictMapper.findChildByParent(id); } //@Cacheable(value = CorePlatformService.DICT_CACHE_VALUE) public CoreDict findCoreDict(String type,String value) { List list = self.findAllByGroup(type, value); if(list==null) { return null; } for(CoreDict dict:list) { if(dict.getValue().equals(value)) { return dict; } } return null; } /*通过名字反向查找数据字典,通常用于excel导入*/ public Map mapDictByName(String type){ List list = self.findAllByType(type); Map map = new HashMap(); for(CoreDict dict:list) { map.put(dict.getName(), dict); } return map; } /*递归查找*/ private List _search(List list,String value) { for(CoreDict dict:list) { if(dict.getValue().equals(value)) { return list; }else { List children = findChildByParent(dict.getId()); if(children.isEmpty()) { continue; }else { List ret = _search(children,value); if(ret!=null) { return ret; } } } } return null; } /** * 查询字段类型列表 * @return */ public List> findTypeList() { return dictMapper.findTypeList(DelFlagEnum.NORMAL.getValue()); } public String findNameByType(String value){ return dictMapper.findNameByType(value); } public PageInfo queryByCondition(CoreDictQueryModel query){ PageHelper.startPage(query.getPage(),query.getLimit()); List ret = dictMapper.queryByCondition(query); PageInfo info = new PageInfo(ret); //queryListAfter(ret.getList()); return info; } }