Exemple #1
0
        public async Task <IResultModel> Tree(string group, string code)
        {
            if (group.IsNull() || code.IsNull())
            {
                return(ResultModel.Failed("请指定分组和编码"));
            }

            TreeResultModel <string, DictItemTreeResultModel> tree;
            var key = string.Format(CacheKeys.DictTree, group.ToUpper(), code.ToUpper());

            if (_options.DictCacheEnabled)
            {
                tree = await _cacheHandler.GetAsync <TreeResultModel <string, DictItemTreeResultModel> >(key);

                if (tree != null)
                {
                    return(ResultModel.Success(tree));
                }
            }

            var dict = await _repository.GetByCode(group, code);

            if (dict == null)
            {
                return(ResultModel.Failed("字典不存在"));
            }

            tree = new TreeResultModel <string, DictItemTreeResultModel>
            {
                Id    = "",
                Label = dict.Name,
                Path  = { dict.Name },
                Item  = new DictItemTreeResultModel()
            };
            var list = await _itemRepository.QueryAll(group, code);

            tree.Children = ResolveTree(list, tree);

            if (_options.DictCacheEnabled)
            {
                await _cacheHandler.SetAsync(key, tree);
            }

            return(ResultModel.Success(tree));
        }
        public async Task <IResultModel> Tree(string group, string code)
        {
            if (group.IsNull() || code.IsNull())
            {
                return(ResultModel.Failed("请指定分组和编码"));
            }

            var config = _configProvider.Get <CommonConfig>();
            var key    = $"{CacheKeys.DICT_TREE}:{group.ToUpper()}_{code.ToUpper()}";

            if (config.DictCacheEnabled && _cacheHandler.TryGetValue(key, out TreeResultModel <int, DictItemTreeResultModel> root))
            {
                return(ResultModel.Success(root));
            }

            var dict = await _repository.GetByCode(group, code);

            if (dict == null)
            {
                return(ResultModel.Failed("字典不存在"));
            }

            root = new TreeResultModel <int, DictItemTreeResultModel>
            {
                Id    = 0,
                Label = dict.Name,
                Item  = new DictItemTreeResultModel
                {
                    Name = dict.Name
                }
            };

            var all = await _itemRepository.QueryAll(group, code);

            root.Children = ResolveTree(all);

            if (config.DictCacheEnabled)
            {
                await _cacheHandler.SetAsync(key, root);
            }

            return(ResultModel.Success(root));
        }