예제 #1
0
        public string Tree()
        {
            var cateProdList = Bak365Service.GetCateProdList();

            var cateList = cateProdList.GroupBy(a => new { a.PareNo, a.CateNo, a.CateName }, (a, b) => a);

            var nodes = new ConcurrentBag <ZTreeNode>();

            foreach (var cate in cateList)
            {
                nodes.Add(new ZTreeNode()
                {
                    id       = cate.CateNo,
                    name     = cate.CateName,
                    pId      = cate.PareNo,
                    isParent = true
                });
            }

            var preProdList = _preProdService.Get(Global.USER_ID);

            Parallel.ForEach(cateList, cate =>
            {
                var prodList = cateProdList.Where(a => a.CateNo == cate.CateNo && !string.IsNullOrWhiteSpace(a.ProdNo));

                foreach (var prod in prodList)
                {
                    var node = new ZTreeNode();

                    node.id   = prod.ProdNo;
                    node.name = prod.ProdName;
                    node.pId  = prod.CateNo;
                    if (preProdList.Exists(a => a.ProdNo == prod.ProdNo))
                    {
                        node.@checked = true;
                    }

                    nodes.Add(node);
                }
            });

            var sortNodes = nodes.OrderBy(a => a.id);

            //将获取的节点集合转换为json格式字符串,并返回
            string json = JsonConvert.SerializeObject(sortNodes);

            return(json);
        }