Ejemplo n.º 1
0
        public void DeleteCategories(DeleteCategoriesParameter param)
        {
            using (var scope = DbScope.Create())
                using (var context = base.CreateContext())
                {
                    scope.BeginTransaction();
                    var q = context.NewsCategories.Where(t => param.RowIDSet.Contains(t.RowID));
                    foreach (var item in q)
                    {
                        if (context.News.Any(t => t.CategoryID == item.RowID))
                        {
                            throw new InvalidInvokeException("不能删除有新闻的分类");
                        }

                        //更新子类的父类为被删除类别的父类。
                        context.NewsCategories.Update(t => t.ParentID == item.RowID, t => new NewsCategory {
                            ParentID = item.ParentID
                        });
                    }
                    q.Delete();
                    scope.Complete();
                }

            CacheInterceptorAttribute.ClearCache(_QueryCategories);
        }
Ejemplo n.º 2
0
        public void SaveCategory(SaveCategoryParameter param)
        {
            using (var scope = DbScope.Create())
                using (var context = base.CreateContext())
                {
                    scope.BeginTransaction();
                    if (param.ParentID > 0 && context.News.Any(t => t.CategoryID == param.ParentID))
                    {
                        throw new InvalidInvokeException("不能更新有新闻的父分类");
                    }

                    NewsCategory pObj;
                    if (param.RowID > 0)
                    {
                        pObj = context.NewsCategories.Single(t => t.RowID == param.RowID);
                        EntityMapper.Map <SaveCategoryParameter, NewsCategory>(param, pObj);
                    }
                    else
                    {
                        context.NewsCategories.Add(pObj = new NewsCategory());
                        EntityMapper.Map <SaveCategoryParameter, NewsCategory>(param, pObj);
                        bool isTop = pObj.ParentID == 0;
                        if (isTop)
                        {
                            pObj.Path = string.Empty;
                        }
                        else
                        {
                            var paramSet = GetDeepParentID(pObj.ParentID, true);
                            pObj.Path = string.Join(",", paramSet);
                        }
                        if (!isTop)
                        {
                            pObj.Path += ",";
                        }
                        pObj.Path  += pObj.RowID.ToString();
                        param.RowID = pObj.RowID;
                    }
                    context.SaveChanges();

                    //开始执行Path迁移
                    string sPath = string.Format(",{0},", param.RowID);
                    var    q     = from t in context.NewsCategories
                                   where t.AppID == param.AppID && t.Path.Contains(sPath)
                                   select t;
                    int level;
                    var path = new StringBuilder();
                    foreach (var item in q)
                    {
                        if (item.ParentID == 0)
                        {
                            item.Path = item.RowID.ToString();
                        }
                        else
                        {
                            level       = 0;
                            path.Length = 0;
                            var current = item;
                            do
                            {
                                level++;
                                path.Insert(0, "," + current.RowID.ToString());
                            }while ((current = context.NewsCategories.Where(t => t.RowID == param.ParentID).Single()).ParentID != 0);
                            path.Insert(0, current.RowID);

                            item.Level = level;
                            item.Path  = path.ToString();
                        }
                    }
                    context.SaveChanges();
                    scope.Complete();
                }

            CacheInterceptorAttribute.ClearCache(_QueryCategories);
        }