Helius
2021-06-16 5728be2af515b2200e782aa201ca5d4d67d9ea47
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package com.ibeetl.admin.core.service;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
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 com.ibeetl.admin.core.dao.CoreDictDao;
import com.ibeetl.admin.core.entity.CoreDict;
import com.ibeetl.admin.core.util.enums.DelFlagEnum;
 
/**
 * 描述: 字典 service,包含常规字典和级联字典的操作。
 * @author : xiandafu
 */
@Service
@Transactional
public class CoreDictService extends CoreBaseService<CoreDict> {
 
    private static final Logger LOGGER = LoggerFactory.getLogger(CoreDictService.class);
 
    @Autowired
    private CoreDictDao dictDao;
 
    @Autowired
    CorePlatformService platformService;
    
    @Autowired 
    CoreDictService self ;
 
 
    /**
     * 根据类型获取字典集合
     * @param type 字典类型,
     * @return List
     */
    @Cacheable(value = CorePlatformService.DICT_CACHE_TYPE)
    public List<CoreDict> findAllByType(String type) {
        return dictDao.findAllList(type);
    }
    
    /**
     * 级联字典查询,必须提供一个字典类型
     * @param group
     * @param value
     * @return
     */
    @Cacheable(value = CorePlatformService.DICT_CACHE_CHILDREN)
    public List<CoreDict> findAllByGroup(String type,String value) {
       List<CoreDict> list = self.findAllByType(type);
       return  _search(list,value);
        
    }
    
    /**
     * 级联字段下一级的字段列表
     * @param parentValue
     * @return
     */
    @Cacheable(value = CorePlatformService.DICT_CACHE_CHILDREN)
    public List<CoreDict> findChildByParent(Long id) {
        return dictDao.findChildByParent(id);
    }
 
    @Cacheable(value = CorePlatformService.DICT_CACHE_VALUE)
    public CoreDict findCoreDict(String type,String value) {
       List<CoreDict> 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<String,CoreDict> mapDictByName(String type){
        List<CoreDict> list = self.findAllByType(type);
        Map<String,CoreDict> map = new HashMap<String,CoreDict>();
        for(CoreDict dict:list) {
            map.put(dict.getName(), dict);
        }
        return  map;
    }
    
   
    
   /*递归查找*/ 
    private List<CoreDict> _search(List<CoreDict> list,String value) {
        for(CoreDict dict:list) {
            if(dict.getValue().equals(value)) {
                return list;
            }else {
                List<CoreDict> children = findChildByParent(dict.getId());
                if(children.isEmpty()) {
                    continue;
                }else {
                    List<CoreDict> ret = _search(children,value);
                    if(ret!=null) {
                        return ret;
                    }
                }
                
            }
        }
        return null;
    }
 
   
    /**
     * 查询字段类型列表
     * @return
     */
    public List<Map<String, String>> findTypeList() {
        return dictDao.findTypeList(DelFlagEnum.NORMAL.getValue());
    }
 
   
   public String findNameByType(String value){
       return dictDao.findNameByType(value);
   }
    
  
}