wzy
2022-08-14 b141df9bf9764db8567efebed48006e12ab1f657
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
135
136
137
138
139
140
141
142
143
144
145
146
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<CoreDict> findAllByType(String type) {
        return dictMapper.findAllList(type);
    }
    
    /**
     * 级联字典查询,必须提供一个字典类型
     * @param type
     * @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 id
     * @return
     */
    //@Cacheable(value = CorePlatformService.DICT_CACHE_CHILDREN)
    public List<CoreDict> findChildByParent(Long id) {
        return dictMapper.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 dictMapper.findTypeList(DelFlagEnum.NORMAL.getValue());
    }
 
   
   public String findNameByType(String value){
       return dictMapper.findNameByType(value);
   }
 
 
    public PageInfo<CoreDict> queryByCondition(CoreDictQueryModel query){
        PageHelper.startPage(query.getPage(),query.getLimit());
        List<CoreDict> ret =  dictMapper.queryByCondition(query);
        PageInfo info = new PageInfo(ret);
        //queryListAfter(ret.getList());
        return info;
    }
}