www.pudn.com > MyShop.rar > Class.cs


using System; 
using System.Collections.Generic; 
using System.Data; 
 
using MyShop.DALFactory; 
using MyShop.IDAL; 
using MyShop.Model; 
 
namespace MyShop.BLL 
{ 
    public class Class 
    { 
        private IClass dal = DataAccess.CreateClass(); 
 
        private int _id; 
 
        public Class() { } 
        public Class(int id) 
        { 
            this._id = id; 
        } 
 
        #region  IClass 
 
        ///  
        /// 
        ///  
        ///  
        ///  
        protected int Add(ClassInfo model) 
        { 
            if (model == null) 
            { 
                return 0; 
            } 
            return dal.Add(model); 
        } 
 
        protected int Delete(string filter) 
        { 
            if (string.IsNullOrEmpty(filter)) 
                return 0; 
            return dal.Delete(filter); 
        } 
 
        public bool Exist(string filter) 
        { 
            filter = filter.Trim(); 
            if (string.IsNullOrEmpty(filter)) 
                return false; 
            return dal.Exist(filter); 
        } 
        public DataSet GetDataSet() 
        { 
            return dal.GetDataSet(); 
        } 
 
        public DataSet GetDataSet(string filter) 
        { 
            filter = filter.Trim(); 
            if (string.IsNullOrEmpty(filter)) 
                return null; 
            return dal.GetDataSet(filter); 
        } 
 
        public ClassInfo GetModel(DataRow dr) 
        { 
            if (dr == null) 
                return null; 
            return dal.GetModel(dr); 
        } 
 
        private DataSet Query(string sql) 
        { 
            sql = sql.Trim(); 
            if (string.IsNullOrEmpty(sql)) 
                return null; 
            return dal.Query(sql); 
        } 
 
        public int Update(ClassInfo model, string filter) 
        { 
            if (model == null) 
                return 0; 
            filter = filter.Trim(); 
            if (string.IsNullOrEmpty(filter)) 
                return 0; 
            return dal.Update(model, filter); 
        } 
 
        #endregion 
 
        #region common 
 
        ///  
        /// 加入栏目 
        ///  
        ///  
        ///  
        ///  
        public int Add(ClassInfo model, out string msg) 
        { 
            msg = ""; 
            if (model == null) 
            { 
                msg = msg + "
  • 数据不能为空
  • "; return 0; } bool isErr = false; if (isErr) return 0; int count = 0; count = Add(model); if (count == 0) msg = "
  • 系统发生错误,请重新添加!
  • "; if (count == 1) msg = "
  • 添加成功!
  • "; return count; } /// /// 删除Class栏目,只能用于删除无孩子的结点,否则目录树结构将被破坏 /// /// /// protected int Delete(int classId) { ClassInfo model = new ClassInfo(); model = GetModel(classId); if (model == null) return 0; string filer; filer = " ClassId =" + classId; return Delete(filer); } public int Update(ClassInfo model) { if (model == null) { return 0; } string filter; filter = " ClassId=" + model.ClassID; return Update(model, filter); } public ClassInfo GetModel(int classId) { DataSet dataset = new DataSet(); dataset = GetDataSet(" ClassId=" + classId); if (dataset != null && dataset.Tables[0].Rows.Count > 0) return GetModel(dataset.Tables[0].Rows[0]); return null; } public ClassInfo GetModel(string className) { className = Utils.ReplaceBadSQL(className.Trim()); if (string.IsNullOrEmpty(className.ToString())) return null; DataSet dataset = new DataSet(); dataset = GetDataSet(" className='" + className + "'"); if (dataset != null && dataset.Tables[0].Rows.Count > 0) return GetModel(dataset.Tables[0].Rows[0]); return null; } #endregion #region 前台 /// /// 得到以classId为父栏目的子栏目列表 /// /// /// public DataSet GetClassList(int classId) { return GetDataSet(" [parentId] = " + classId + " and [disabled] = 0 Order by orderId "); } /// /// 得到以channelId频道的一级栏目列表 /// /// /// public DataSet GetTopClassList(int channelId) { return GetDataSet(" [parentId] = 0 and [channelId] = " + channelId + " and [disabled] = 0 Order by orderId "); } #endregion #region 以下过程要用Class cls = new Class(id)实例化后才能进行调用 #region property /// /// 获取当前项目ID /// public int ID { get { return this._id; } } /// /// 获取调查项目的实体 /// public ClassInfo Model { get { return GetModel(this._id); } set { Update(value); } } /// /// 获取当前对象是否存在 /// public bool isExist { get { return Model == null ? false : true; } } #endregion #endregion /// /// 得到顶部导航栏目数据 /// /// /// public DataSet GetDataSetTopNav(int channelId) { return GetDataSet(" [channelId] = " + channelId + " and [depth] = 1 Order by parentpath,depth,orderId "); } #region 后台管理 public DataSet GetDataSetOrderByOrderId() { return GetDataSet(" [classId] > 0 order by channelId, parentpath,orderId "); } public DataSet GetDataSetOrderByOrderId(int channelId) { return GetDataSet(" [channelId] = " + channelId + " Order by parentpath,depth,orderId "); } /// /// 启用或禁用 /// /// /// public int Enable(int classId, bool action) { ClassInfo model = new ClassInfo(); model = GetModel(classId); if (model == null) return 0; model.Disabled = action ? 0 : 1; return Update(model); } /// /// 频道是否存在 /// /// /// public bool IsExist(string className) { className = className.Trim(); if (Utils.ChkBadChar(className)) return false; if (Exist(" [className] = '" + Utils.ReplaceBadSQL(className.Trim()) + "' ")) { return true; } else return false; } /// /// 排序 /// /// /// /// public int Order(int classId, int orderId) { ClassInfo model = new ClassInfo(); model = GetModel(classId); if (model != null) { model.OrderID = orderId; return Update(model); } return 0; } /// /// 将该栏目的child加上num数目 /// /// /// 可正可负 /// public int AddChildNumber(int classId, int num) { ClassInfo model = new ClassInfo(); model = GetModel(classId); if (model == null) return 0; model.Child = model.Child + num; return Update(model); } /// /// 将classId该栏目的父栏目的child加上num数目 /// /// /// 可正可负 /// public int AddChildren(int classId, int num) { ClassInfo model = new ClassInfo(); model = GetModel(classId); if (model == null) return 0; ClassInfo parModel = new ClassInfo(); parModel = GetModel(model.ParentID); if (parModel == null) return 0; parModel.Child = parModel.Child + num; return Update(parModel); } /// /// 将栏目向上或向下移动一位 /// /// /// true:向上移动;false向下移动 public void MoveClass(int classId, bool moveUp) { } /// /// 删除属于channelid频道的ClassId栏目,同时删除所有子栏目,及其信息内容,如文章等; /// /// /// /// public int Delete(int channelId, int classId) { ClassInfo model = new ClassInfo(); model = GetModel(classId); if (model == null) return 0; string filer; filer = " ClassId =" + classId; int temParentId = 0; int temClassId = 0; temParentId = temClassId = classId; while (HasChild(classId)) { temClassId = classId; //if当前结点不是叶子 while (HasChild(temClassId)) { // temParentId = temClassId; temClassId = GetLeftChild(temClassId); } DeleteClassAndItsContent(channelId, temClassId); } AddChildren(classId, -1); return DeleteClassAndItsContent(channelId, classId); } /// ///只用于删除不带孩子的叶子结点及其信息内容,如文章等,该叶子结点属于channelid频道的ClassId栏目; /// /// /// /// protected int DeleteClassAndItsContent(int channelId, int classId) { Channel channel = new Channel(); ChannelInfo chModel = new ChannelInfo(); chModel = channel.GetModel(channelId); int result = 0; switch (chModel.ModuleType) { case 1: //文章频道 Article article = new Article(); article.Delete(channelId, classId); break; case 2: //商城频道 break; case 3: //留言频道 break; case 4: //下载频道 break; case 5: //图片频道 break; default: break; } result = Delete(classId); return result; } /// /// 判断是否有孩子 /// /// /// public bool HasChild(int classId) { string filter = " [parentId] = " + classId; if (GetDataSet(filter).Tables[0].Rows.Count > 0) return true; return false; } /// /// 判断是否有文章 /// /// /// public bool HasArticle(int classId) { Article art = new Article(); if (art.GetDataSetByClassId(classId).Tables[0].Rows.Count > 0) return true; return false; } /// /// 得到左孩子 /// /// /// public int GetLeftChild(int classId) { if (HasChild(classId)) { string filter = " [parentId] = " + classId + " order by [orderId] "; return Convert.ToInt32(GetDataSet(filter).Tables[0].Rows[0]["classId"]); } return 0; } /// /// 返回孩子的个数 /// /// /// public int GetChildrenCount(int parentId) { string filter = "[parentId] = " + parentId; return GetDataSet(filter).Tables[0].Rows.Count; }   #endregion } }