shikeyin
2024-01-11 65da8373531677b1c37a98f53eaa30c892f35e5a
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package com.iplatform.base.cache;
 
import com.iplatform.base.Constants;
import com.iplatform.base.service.CodeServiceImpl;
import com.iplatform.model.po.S_dict_data;
import com.iplatform.model.po.S_dict_type;
import com.walker.cache.tree.AbstractCacheTreeProvider;
import com.walker.cache.tree.CacheTreeNode;
import com.walker.cache.tree.DefaultCacheTreeNode;
import com.walker.infrastructure.utils.StringUtils;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
 
/**
 * 数据字典缓存定义,该缓存因为树结构复杂,并没有提供'Redis'方式实现。<br>
 * 不过一般字典数据变动很小,因此在集群环境中也是相对安全。<p></p>
 * 1) 注意:如果生产环境字典变动较大,而且是集群环境则必须实现'Redis'集中存储缓存对象。
 * @author 时克英
 * @date 2023-03-10
 */
public class DictCacheProvider extends AbstractCacheTreeProvider<S_dict_data> {
 
    private final SortComparator sort = new SortComparator();
    private final NodeComparator nodeSort = new NodeComparator();
 
    // 2023-04-15 添加字典类型与id映射记录,因为
    private final Map<String, String> dictTypeIdMap = new HashMap<>();
 
    /**
     * 根据字典类型,找出对应id,因为前端若依框架中,需要传类型,但系统使用id查询。
     * @param dictType
     * @return
     * @date 2023-04-15
     */
    public String getDictTypeId(String dictType){
        String id = this.dictTypeIdMap.get(dictType);
        if(id == null){
            throw new IllegalArgumentException("根据字典类型,未找到id:" + dictType);
        }
        return id;
    }
 
    /**
     * 返回代码集合对象,通常在数据库中可能会存储多个代码ID,可以通过此 方法获取代码集合。
     * @param codeIds 输入多个代码ID数组
     * @return
     */
    public List<CacheTreeNode> getCodeList(String[] codeIds){
        List<CacheTreeNode> result = new ArrayList<CacheTreeNode>(2*2);
        if(codeIds != null && codeIds.length > 0){
            for(String cid : codeIds){
                result.add(this.get(cid));
            }
        }
        return result;
    }
 
    /**
     * 返回给定代码表的子代码项,树结构
     * @param codeTableName 代码表ID
     * @return
     */
    public List<CacheTreeNode> getCodeChildrenList(String codeTableName){
        CacheTreeNode node = this.getOneRootNode(codeTableName);
        if(node == null){
            throw new IllegalArgumentException("not found node: " + codeTableName);
        }
        Collection<CacheTreeNode> list = node.getChildren();
        if(list != null){
            List<CacheTreeNode> result = new ArrayList<CacheTreeNode>();
            for(CacheTreeNode n : list){
                result.add(n);
            }
            Collections.sort(result, nodeSort);
            return result;
        }
        return null;
    }
 
    /**
     * 返回代码表的下一级子代码集合,返回的是代码对象
     * @param codeTableName 代码表ID
     * @return
     */
    public List<S_dict_data> getRootChildrenOneLevelList(String codeTableName){
        CacheTreeNode node = this.getOneRootNode(codeTableName);
//        CacheTreeNode node2 =this.getOneRootNode("101");
//        Object rootCacheNode = null;
//        for(Iterator<Cachable> it = this.getCache().getIterator(); it.hasNext();){
//            rootCacheNode = it.next().getValue();
//            logger.debug("........ {}" + rootCacheNode);
//        }
        if(node == null){
            throw new IllegalArgumentException("not found node: " + codeTableName);
        }
        Collection<CacheTreeNode> list = node.getChildren();
        if(list != null){
            List<S_dict_data> result = new ArrayList<>();
            S_dict_data temp = null;
            for(CacheTreeNode n : list){
                temp = (S_dict_data)n.getSource();
//                temp.setChildSum(n.getChildrenSize());
                result.add(temp);
            }
            Collections.sort(result, sort);
            return result;
        }
        return null;
    }
 
    /**
     * 根据某个代码ID,返回其下一级所有子代码集合
     * @param id
     * @return
     */
    public List<S_dict_data> getCodeChildrenOneLevelList(String id){
        CacheTreeNode parent = this.get(id);
        if(parent == null){
            return null;
        }
        Collection<CacheTreeNode> list = parent.getChildren();
        if(list != null){
            List<S_dict_data> result = new ArrayList<>();
            for(CacheTreeNode n : list){
                result.add((S_dict_data)n.getSource());
            }
            return result;
        }
        return null;
    }
 
    /**
     * 返回某个代码下面的所有代码树。
     * @param id 给定的代码id
     * @return
     */
    public List<CacheTreeNode> getCodeChildrenTreeList(String id){
        CacheTreeNode parent = this.get(id);
        if(parent == null){
            return null;
        }
        Collection<CacheTreeNode> list = parent.getChildren();
        if(list != null){
            List<CacheTreeNode> result = new ArrayList<CacheTreeNode>();
            for(CacheTreeNode n : list){
                result.add(n);
            }
            return result;
        }
        return null;
    }
 
//    /**
//     * 搜索代码树,支持多关键字搜索,返回集合数据展示给ztree,注意:返回结果只有一级不再分层
//     * @param codeTableId 代码表ID
//     * @param keyLike 查询关键字,如:'锅炉' 或者 '锅炉 电 9号',多关键词用空格分隔
//     * @return
//     */
//    public String searchLikeTreeNodeList(String codeTableId, String keyLike){
////        Collection<CacheTreeNode> deptList = this.getRootList();
//        Collection<CacheTreeNode> deptList = this.getCodeChildrenList(codeTableId);
//        JSONArray array = new JSONArray();
//
//        if(deptList != null){
//            // 过滤某个单位下的根数据集合,后续补充
//            // ...
//            String[] keys = StringUtils.stringToArray(keyLike, " ");
//            boolean multiKeys = false;
//            if(keys != null && keys.length > 1){
//                multiKeys = true;
//            }
//
//            List<CacheTreeNode> tempList = null;
//            for(CacheTreeNode node : deptList){
//                // 不是给定单位的数据不要
////                if(((TypeTree)node.getSource()).getDept() != dept){
////                    continue;
////                }
//                if(multiKeys){
//                    logger.debug("------------------");
//                    tempList = node.searchLike(keys);
//                } else {
//                    tempList = node.searchLike(keyLike);
//                }
//                if(!StringUtils.isEmptyList(tempList)){
//                    for(CacheTreeNode c : tempList){
//                        array.add(CodeUtils.createJsonTree(c.getKey(), c.getText()
//                                , Constants.DEFAULT_TREE_ROOT_ID, false, true, CodeUtils.DEFAULT_SEARCH_ICON, false));
//                    }
//                }
//            }
//        }
//        return array.toString();
//    }
 
    @Override
    protected Map<String, CacheTreeNode> loadRootList() {
        List<S_dict_type> rootList = this.codeService.queryRootCodeList();
        if(StringUtils.isEmptyList(rootList)){
            return null;
        }
        // 需要转成'S_dict_data'统一对象,老系统中代码表是一个表,这里是两个表。2023-03-10
        List<S_dict_data> dictList = new ArrayList<>();
        S_dict_data data = null;
        for(S_dict_type type : rootList){
            data = new S_dict_data();
            data.setDict_code(type.getDict_id());
            data.setDict_label(type.getDict_name());
            data.setParent_id(0L);  // 根节点不存在父id,默认;0
            dictList.add(data);
            //
            this.dictTypeIdMap.put(type.getDict_type(), String.valueOf(type.getDict_id()));
        }
        return obtainMap(dictList);
    }
 
    @Override
    protected Map<String, CacheTreeNode> loadChildList() {
        List<S_dict_data> list = this.codeService.queryAllCodeItemList();
        return obtainMap(list);
    }
 
    @Override
    protected CacheTreeNode toCacheTreeNode(S_dict_data entity) {
        return new DefaultCacheTreeNode(entity.getDict_code().toString()
                , entity.getDict_label(), entity, entity.getParent_id().toString());
    }
 
    @Override
    public String getProviderName() {
        return Constants.CACHE_NAME_DICT;
    }
 
    @Override
    public Class<?> getProviderType() {
        return S_dict_data.class;
    }
 
    private Map<String, CacheTreeNode> obtainMap(List<S_dict_data> list){
        if(list != null && list.size() > 0){
            Map<String, CacheTreeNode> map = new TreeMap<String, CacheTreeNode>();
            for(S_dict_data code : list){
                map.put(code.getDict_code().toString(), toCacheTreeNode(code));
            }
            return map;
        } else
            return null;
    }
 
    private class SortComparator implements Comparator<S_dict_data> {
        @Override
        public int compare(S_dict_data o1, S_dict_data o2) {
            return (int)(o1.getDict_sort() - o2.getDict_sort());
        }
    }
    private class NodeComparator implements Comparator<CacheTreeNode>{
        @Override
        public int compare(CacheTreeNode o1, CacheTreeNode o2) {
            S_dict_data n1 = (S_dict_data)o1.getSource();
            S_dict_data n2 = (S_dict_data)o2.getSource();
            return (int)(n1.getDict_sort() - n2.getDict_sort());
        }
    }
 
    public void setCodeService(CodeServiceImpl codeService) {
        this.codeService = codeService;
    }
 
    private CodeServiceImpl codeService;
}