项目总结:复杂树状菜单-结点增改删(ZTree)



        [HttpPost]
        public JsonResult SaveTree(ComponentIndexModel model, string TreeNodes)
        {


            try
            {
                string brandname = model.TXTbrand;
                int productId;


                ComponentManageProxy component = new ComponentManageProxy();
                IBrandRepository _BrandRepository = new BrandRepository();
                BrandRow row = _BrandRepository.GetBrandByName(brandname);


                if (row == null)
                {


                    row = new BrandRow();


                    row.BrandName = brandname;


                    row.UniqueID = Guid.NewGuid();
                    row.BrandType = model.ProductType ? 1 : 0;


                    _BrandRepository.AddBrand(row);


                }




                ProductRepository _ProductRepository = new ProductRepository();


                if (model.hidden_product != null)
                {
                    ProductRow _row = _ProductRepository.GetProductByName(model.hidden_product);
                    if (_row == null)
                    {
                        _row = new ProductRow();
                        _row.UniqueID = Guid.NewGuid();
                        _row.ProductName = model.hidden_product;
                        _row.ComponentTreeID = Guid.NewGuid();
                        _row.BrandName = brandname;
                        _ProductRepository.AddProduct(_row);
                        _row = _ProductRepository.GetProductByUniqueID(_row.UniqueID);


                        productId = _row.ID;
                    }
                    else
                    {
                        //productId = int.Parse(model.Product);
                        productId = _row.ID;
                        if (productId.ToString() != model.Product)
                        {
                            return Json(new
                            {
                                Msg = "The product is already exsit.",
                                Result = "2",
                            });
                        }
                    }
                }
                else
                {
                    productId = int.Parse(model.Product);
                }


                //component.DeleteTree(productId);


                ProductManage Producttarget = new ProductManage(_ProductRepository);
                ProductRow _Product = _ProductRepository.GetProductByID(productId);
                //component.GetAllComponentNodes(_Product.ComponentTreeID);


                IComponentRepository _ComponentBackupRepo = new ComponentRepository();
                IComponentTreeNodeRepository _NodeBackupRepo = new ComponentTreeNodeRepository();
                IComponentTreeNodeCompoundRepository _TreeBackupRepo = new ComponentTreeCompoundRepository();




                IList _TreeBackupList = _TreeBackupRepo.GetAllComponentNodes(_Product.ComponentTreeID);
                JavaScriptSerializer jss = new JavaScriptSerializer();
                List nodelist = new List();
                IList _MarketSpecificNodeList;
                if (model.Market == "General") _MarketSpecificNodeList = _TreeBackupList.Where(x => (x.Level == 3 && x.MarketName != "General" && (x.Name != null && x.Name != String.Empty))).ToList();
                else _MarketSpecificNodeList = _TreeBackupList.Where(x => (x.Level == 3 && (x.MarketName != "General" && x.MarketName != model.Market && (x.Name != null && x.Name != String.Empty)))).ToList();


                //nodelist = jss.Deserialize(TreeNodes, typeof(List)) as List;
                nodelist = jss.Deserialize(model.TreeInfo_Json, typeof(List)) as List;


                foreach (var node in nodelist)
                {


                    if (_TreeBackupList.Count == 0)
                    {
                        //if (node.level == 2)
                        //{
                        //inserttodb()
                        ComponentRowwithMultipleLCA componentRow = new ComponentRowwithMultipleLCA();


                        componentRow.UniqueID = Guid.NewGuid();
                        componentRow.AssignedID = node.id;
                        componentRow.ComponentName = GetNameWithoutMarket(node.name);
                        componentRow.MarketName = (node.marketName == null || node.marketName == "") ? "General" : node.marketName;
                        if (node.level == 0) { componentRow.Description = "root"; }


                        //component.AddComponentTreeNode(componentRow, _ParentUniqueID, _RootID, level);
                        component.AddCompoment(componentRow, productId, node.pId, node.level + 1);
                    }
                    else
                    {
                        if (node.level != 2)
                        {
                            if (node.level == 1)
                            {


                                IList _nodeEnum = _MarketSpecificNodeList.Where(i => (i.PId == node.id && i.RootID == _Product.ComponentTreeID)).ToList();
                                foreach (ComponentTreeCompound o in _nodeEnum)
                                {
                                    ComponentTreeCompound obj1 = o;
                                    //UpdateModel;
                                    ComponentRowwithMultipleLCA __row = _ComponentBackupRepo.FindComponentByUniqueID(obj1.UniqueID);
                                    __row.ComponentName = node.name;
                                    //_ComponentBackupRepo.UpdateComponent(__row);
                                    _MarketSpecificNodeList.Remove(obj1);
                                    ComponentTreeCompound _MarketSpecificnode = _TreeBackupList.First(i => (i.Sid == obj1.Sid && i.RootID == _Product.ComponentTreeID));
                                    _TreeBackupList.Remove(_MarketSpecificnode);


                                }
                            }
                            ComponentTreeCompound _node = _TreeBackupList.First(i => (i.Sid == node.id && i.RootID == _Product.ComponentTreeID));
                            _TreeBackupList.Remove(_node);
                        }
                        if (node.level == 2)
                        {
                            IList _nodeEnum = _TreeBackupList.Where(i => (i.Sid == node.id && i.RootID == _Product.ComponentTreeID)).ToList();
                            if (_nodeEnum.Count >= 1)
                            {
                                ComponentTreeCompound obj1 = _nodeEnum.First();
                                //UpdateModel;
                                ComponentRowwithMultipleLCA __row = _ComponentBackupRepo.FindComponentByUniqueID(obj1.UniqueID);
                                __row.ComponentName = node.name;
                                //_ComponentBackupRepo.UpdateComponent(__row);
                                _TreeBackupList.Remove(obj1);
                            }


                            else
                            {
                                ComponentRowwithMultipleLCA componentRow = new ComponentRowwithMultipleLCA();


                                componentRow.UniqueID = Guid.NewGuid();
                                componentRow.AssignedID = node.id;
                                componentRow.ComponentName = GetNameWithoutMarket(node.name);
                                componentRow.MarketName = (node.marketName == null || node.marketName == "") ? "General" : node.marketName;
                                if (node.level == 0) { componentRow.Description = "root"; }


                                component.AddCompoment(componentRow, productId, node.pId, node.level + 1);
                            }
                        }
                    }




                }








                foreach (ComponentTreeCompound ob in _TreeBackupList)
                {
                    //delete
                    _NodeBackupRepo.DeleteComponentTreeNodeByComponentUniqueID(ob.TreeNodeUniqueID);
                    _ComponentBackupRepo.DeleteComponent(ob.UniqueID);


                }


                return Json(new
                {
                    Msg = "The hierachy definition was saved.",
                    Result = "1",
                });
            }
            catch (Exception e)
            {
                return Json(new
                {
                    Msg = "The hierachy definition was saved unsucessful. " + e.Message,
                    Result = "0",
                });
            }


        }



你可能感兴趣的:(Asp.net,MVC)