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


using System; 
using System.Collections.Generic; 
using System.Data; 
using MyShop.Model; 
using MyShop.IDAL; 
using MyShop.DALFactory; 
 
namespace MyShop.BLL 
{ 
    public class Product  
    { 
        private IProduct dal = DataAccess.CreateProduct(); 
        private ConfigInfo configInfo = new ConfigInfo(); 
        private string tableName = "Ljh_Products"; 
        private int _id; 
 
        public Product() 
        { 
            if (!string.IsNullOrEmpty(configInfo.TablePrefix.Trim())) 
                tableName = configInfo.TablePrefix + "Products"; 
        } 
 
        public Product(int id) 
        { 
            if (!string.IsNullOrEmpty(configInfo.TablePrefix.Trim())) 
                tableName = configInfo.TablePrefix + "Products"; 
            this._id = id; 
 
        } 
 
        #region IProduct member 
 
        public int Add(ProductInfo model) 
        { 
            if (model == null) 
            { 
                return 0; 
            } 
            return dal.Add(model); 
        } 
 
        public 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 DataSet GetDataSet(int productId) 
        { 
            if (productId <= 0) 
                return null; 
            string filter = " productId=" + productId; 
            return GetDataSet(filter); 
        } 
 
        public ProductInfo 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(ProductInfo 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(ProductInfo model, out string msg) 
        { 
            msg = ""; 
            if (model == null) 
            { 
                msg = msg + "
  • 数据不能为空
  • "; return 0; } bool isErr = false; if (string.IsNullOrEmpty(model.ProductName.Trim())) { msg = msg + "请输入商品名称"; isErr = true; } if (isErr) return 0; int count = 0; count = Add(model); if (count == 0) msg = "
  • 系统发生错误,请重新添加!
  • "; if (count == 1) msg = "
  • 添加成功!
  • "; return count; } /// /// 真正从数据库中删除,注意使用时要分清是不是只放到回收站 /// /// /// public int Delete(int productId) { if ( string.IsNullOrEmpty( productId.ToString()) ) return 0; string filer; filer = " productId =" + productId; return Delete(filer); } public int Update(ProductInfo model) { if (model == null) { return 0; } string filter; filter = " productId=" + model.ProductId; return Update(model, filter); } public ProductInfo GetModel(int productId) { DataSet dataset = new DataSet(); dataset = GetDataSet(" productId=" + productId); if (dataset != null && dataset.Tables[0].Rows.Count > 0) return GetModel(dataset.Tables[0].Rows[0]); return null; } #region 获取商品列表 /// /// 获取商品列表 /// /// 是否允许销售 /// 是否已删除 /// public DataSet GetDataSet(bool enableSale, bool deleted) { return dal.GetDataSet(enableSale,deleted); } /// /// 获取商品列表 /// /// 商品大类ID /// 商品小类ID /// public DataSet GetDataSet( int productKindId,int categoryId ) { return dal.GetDataSet(productKindId,categoryId); } /// /// 获取商品列表 /// /// 商品大类ID /// 商品小类ID /// 是否允许销售 /// 是否已删除 /// public DataSet GetDataSet( int productKindId,int categoryId ,bool enableSale, bool deleted) { return dal.GetDataSet(productKindId,categoryId,enableSale,deleted); } /// /// 获取商品列表 /// /// 商品大类ID /// public DataSet GetDataSetByProductKindId(int productKindId) { return dal.GetDataSetByProductKindId(productKindId); } /// /// 获取商品列表 /// /// 商品大类ID /// 是否已删除 /// public DataSet GetDataSetByProductKindId(int productKindId,bool deleted) { return dal.GetDataSetByProductKindId(productKindId,deleted); } /// /// 获取商品列表 /// /// 商品大类ID /// 是否允许销售 /// 是否已删除 /// public DataSet GetDataSetByProductKindId(int productKindId,bool enableSale, bool deleted) { return dal.GetDataSetByProductKindId(productKindId,enableSale,deleted); } /// /// 获取商品列表 /// /// 商品小类ID /// public DataSet GetDataSetByCategoryId(int categoryId) { return dal.GetDataSetByCategoryId(categoryId); } /// /// 获取商品列表 /// /// 商品小类ID /// 是否已删除 /// public DataSet GetDataSetByCategoryId(int categoryId,bool deleted) { return dal.GetDataSetByCategoryId(categoryId,deleted); } /// /// 获取商品列表 /// /// 商品小类ID /// 是否允许销售 /// 是否已删除 /// public DataSet GetDataSetByCategoryId(int categoryId,bool enableSale, bool deleted) { return dal.GetDataSetByCategoryId(categoryId,enableSale,deleted); } public DataSet KeywordsSearch(string keywords) { keywords = Utils.ReplaceBadSQL(keywords.Trim()); if (Utils.ChkBadChar(keywords.Trim()) || string.IsNullOrEmpty(keywords.Trim())) return null; return dal.KeywordsSearch(keywords); } #endregion #endregion #region 以下过程要用Product product = new Product(id)实例化后才能进行调用 #region property /// /// 获取当前项目ID /// public int ID { get { return this._id; } } /// /// 获取调查项目的实体 /// public ProductInfo Model { get { return GetModel(this._id); } set { Update(value); } } /// /// 获取当前对象是否存在(数据库中有没有这条记录) /// public bool isExist { get { return Model == null ? false : true; } } #endregion #endregion /// /// 最新商品(50个) /// /// public DataSet GetNew() { return GetNew(50); } /// /// 获取最新商品 /// /// 返回商品个数 /// public DataSet GetNew( int count) { return dal.GetNew(count); } /// /// 获取所有热卖商品 /// /// public DataSet GetHot() { return GetHot(9999); } /// /// 获取执卖商品 /// /// 返回商品个数 /// public DataSet GetHot( int count) { return dal.GetHot(count); } /// /// 获取所有推荐商品 /// /// public DataSet GetElite() { return GetElite(9999); } /// /// 获取推荐商品 /// /// 返回商品个数 /// public DataSet GetElite( int count ) { return dal.GetElite(count); } public DataSet GetDataSetByProductName(string productName) { if (string.IsNullOrEmpty(productName)) return null; return GetDataSet(" productName like '%" + Utils.ReplaceBadSQL(productName) + "%'"); } /// /// 获取销售名次排行 /// /// /// public DataSet SalePlace(int count) { return dal.SalePlace(count); } #region 后台管理 /// /// 返回所有未放入回收站的商品 /// /// public DataSet GetDataSetUndeleted() { string filter = " Deleted=0 order by productId desc "; return GetDataSet(filter); } /// /// 返回所有放入回收站的商品 /// /// public DataSet GetDataSetDeleted() { string filter = " Deleted=1 order by productId desc "; return GetDataSet(filter); } /// /// 快速搜索 /// /// /// true:返回放到回收站的商品;false:返回未放到回收站的商品 /// public DataSet QuickSearch(int searchType,bool deleted) { return dal.QuickSearch(searchType,deleted); } /// /// 高级查询 /// /// /// /// true:返回放到回收站的商品;false:返回未放到回收站的商品 /// public DataSet KeywordsSearch(string field, string keywords,bool deleted) { field = Utils.ReplaceBadSQL(field.Trim().ToLower()); keywords = Utils.ReplaceBadSQL(keywords.ToLower().Trim()); if (string.IsNullOrEmpty(field) || string.IsNullOrEmpty(keywords)) return null; return dal.KeywordsSearch(field, keywords,deleted); } /// /// 判断该商品有没有订单 /// /// /// public bool HasOrder(int productId) { OrderItem orderItem = new OrderItem(); if (orderItem.GetDataSetByProductId(productId).Tables[0].Rows.Count > 0) return true; else return false; } /// /// 判断该商品是不是执卖 /// /// /// 无该商品时返回false public bool IsHot(int productId) { ProductInfo model = new ProductInfo(); model = GetModel(productId); if (model == null) return false; if (model.IsHot == 1) return true; else return false; } /// /// 取消或设置该商品为执卖 /// /// /// true 为设置 false为取消 /// public int IsHot(int productId, bool action) { ProductInfo model = new ProductInfo(); model = GetModel(productId); if (model == null) return 0; if (action) { model.IsHot = 1; } else { model.IsHot = 0; } return Update(model); } /// /// 判断该商品是不是固顶 /// /// /// 无该商品时返回false public bool OnTop(int productId) { ProductInfo model = new ProductInfo(); model = GetModel(productId); if (model == null) return false; if (model.OnTop == 1) return true; else return false; } /// /// 取消或设置该商品为固顶 /// /// /// true 为设置 false为取消 /// public int OnTop(int productId, bool action) { ProductInfo model = new ProductInfo(); model = GetModel(productId); if (model == null) return 0; if (action ) { model.OnTop = 1; } else { model.OnTop = 0; } return Update(model); } /// /// 判断该商品是不是推荐 /// /// /// 无该商品时返回false public bool IsElite(int productId) { ProductInfo model = new ProductInfo(); model = GetModel(productId); if (model == null) return false; if (model.IsElite == 1) return true; else return false; } /// /// 取消或设置该商品为推荐 /// /// /// true 为设置 false为取消 /// public int IsElite(int productId, bool action) { ProductInfo model = new ProductInfo(); model = GetModel(productId); if (model == null) return 0; if (action) { model.IsElite = 1; } else { model.IsElite = 0; } return Update(model); } /// /// 判断该商品是不是允许销售 /// /// /// 无该商品时返回false public bool EnableSale(int productId) { ProductInfo model = new ProductInfo(); model = GetModel(productId); if (model == null) return false; if (model.EnableSale == 1) return true; else return false; } /// /// 取消或设置该商品销售状态 /// /// /// true 为设置销售 false为取消销售 /// public int EnableSale(int productId, bool action) { ProductInfo model = new ProductInfo(); model = GetModel(productId); if (model == null) return 0; if (action) { model.EnableSale = 1; } else { model.EnableSale = 0; } return Update(model); } /// /// 判断该商品有没有缩略图 /// /// /// 无该商品时返回false public bool HasThumb(int productId) { ProductInfo model = new ProductInfo(); model = GetModel(productId); if (model == null) return false; if (model.ProductThumb.Length >= 3) return true; else return false; } /// /// 判断该商品名是不是已经存在 /// /// /// public bool ExistProductName(string productName) { productName = productName.Trim(); if(string.IsNullOrEmpty(productName)) return false; return Exist(" productName ='" + Utils.ReplaceBadSQL(productName) + "'"); } public bool ExistProductNum(string productNum) { productNum = Utils.ReplaceBadSQL(productNum.Trim()); if (string.IsNullOrEmpty(productNum)) return false; return Exist(" productNum='" + productNum + "'"); } /// /// 将商品放到回收站,有订单的商品将不执行 /// /// /// public int MoveToRecycle(int productId) { if (HasOrder(productId)) return 0; ProductInfo model = new ProductInfo(); model = GetModel(productId); if (model != null) { model.Deleted = 1; return Update(model); } return 0; } /// /// 将商品从回收站还原 /// /// /// public int RestoreFromRecycle(int productId) { ProductInfo model = new ProductInfo(); model = GetModel(productId); if (model != null) { model.Deleted = 0; return Update(model); } return 0; } /// /// 将商品从回收站清空 /// /// public int DeleteAllFromRecycle() { string filter = " Deleted = 1 "; return Delete(filter); } public int RestoreAllFromRecycle() { string filter = " Deleted = 1 "; DataSet dataset = new DataSet(); dataset = GetDataSet(filter); ProductInfo model = new ProductInfo(); int i = 0; foreach (DataRow dr in dataset.Tables[0].Rows) { model = GetModel(dr); if(model != null) RestoreFromRecycle(model.ProductId); i++; } return i; } #endregion } }