using System;
using System.Collections.Generic;
using System.Data;
using Maticsoft.Model;
using System.Linq;
using System.Data.Linq;
using Maticsoft.Common;
namespace Maticsoft.BLL.Products
{
    public class Product
    {
        private readonly Maticsoft.DAL.Products.Product dal = new Maticsoft.DAL.Products.Product();
        public Product()
        { }
        
        #region  成员方法
        
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string ProductId)
        {
            return dal.Exists(ProductId);
        }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Maticsoft.Model.Product model)
        {
            dal.Add(model);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Maticsoft.Model.Product model)
        {
            dal.Update(model);
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(string ProductId)
        {
            dal.Delete(ProductId);
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.Product GetModel(string ProductId)
        {
            return dal.GetModel(ProductId);
        }
        /// <summary>
        /// 得到一个对象实体,从缓存中
        /// </summary>
        public Maticsoft.Model.Product GetModelByCache(string ProductId)
        {
            string CacheKey = "ProductModel-" + ProductId;
            object objModel = DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(ProductId);
                    if (objModel != null)
                    {
                        int AdContentCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(AdContentCache), TimeSpan.Zero);
                    }
                }
                catch
                { }
            }
            return (Maticsoft.Model.Product)objModel;
        }
                                       
        #endregion  成员方法

        #region 查询

        /// <summary>
        /// 获得记录总数
        /// </summary>
        public int GetCount(string strWhere)
        {
            return dal.GetCount(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return dal.GetList("");
        }
         
        public DataSet GetListByCategoryID(int top, string CategoryID)
        {
            string strWhere = "(1=1) ";
            if (CategoryID != "")
            {
                strWhere += " and CategoryId='" + CategoryID + "' ";
            }
            return dal.GetList(top, strWhere, " ID desc");
        }
                
                
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize, int PageIndex, string strWhere)
        {
            return dal.GetList(PageSize, PageIndex, strWhere);
        }

        public DataSet GetListByCategoryID(int PageSize, int PageIndex, string CategoryID)
        {
            string strWhere = "";
            if (CategoryID != "")
            {
                strWhere += " CategoryId='" + CategoryID + "' ";
            }
            return dal.GetList(PageSize, PageIndex, strWhere);
        }

        #endregion


        #region   查询对象列表GetModelList

        /// <summary>
        /// 数据转换
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        private List<Maticsoft.Model.Product> DataTableToList(DataTable dt)
        {
            List<Maticsoft.Model.Product> modelList = new List<Maticsoft.Model.Product>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Maticsoft.Model.Product model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Maticsoft.Model.Product();
                    model.ProductId = dt.Rows[n]["ProductId"].ToString();
                    model.CategoryId = dt.Rows[n]["CategoryId"].ToString();
                    model.BrandId = dt.Rows[n]["BrandId"].ToString();
                    model.Name = dt.Rows[n]["Name"].ToString();
                    model.Descn = dt.Rows[n]["Descn"].ToString();
                    model.Image = dt.Rows[n]["Image"].ToString();
                    model.ImageSmall = dt.Rows[n]["ImageSmall"].ToString();
                    if (dt.Rows[n]["Price"].ToString() != "")
                    {
                        model.Price = decimal.Parse(dt.Rows[n]["Price"].ToString());
                    }
                    if (dt.Rows[n]["VipPrice"].ToString() != "")
                    {
                        model.VipPrice = decimal.Parse(dt.Rows[n]["VipPrice"].ToString());
                    }
                    if (dt.Rows[n]["Cheapness"].ToString() != "")
                    {
                        model.Cheapness = int.Parse(dt.Rows[n]["Cheapness"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;

        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<Maticsoft.Model.Product> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            return DataTableToList(ds.Tables[0]);
        }
        public IEnumerable<Maticsoft.Model.Product> GetModelList(int page, int size)
        {
            DataSet ds = dal.GetList(size, page, "");
            List<Maticsoft.Model.Product> prolist = DataTableToList(ds.Tables[0]);
            return prolist;
        }
        public List<Maticsoft.Model.Product> GetModelListByCategoryID(int top, string CategoryID)
        {
            string strWhere = "(1=1) ";
            if (CategoryID != "")
            {
                strWhere += " and CategoryId='" + CategoryID + "' ";
            }
            DataSet ds = dal.GetList(top, strWhere, " ID desc");
            return DataTableToList(ds.Tables[0]);
        }

        #endregion


        #region Linq查询

        /// <summary>
        /// 查询表对象的集合
        /// </summary>
        /// <returns></returns>
        public Table<Maticsoft.Model.Product> GetTableModelList()
        {
            return dal.GetTableModelList();
        }
        /// <summary>
        /// 查询条件对象的集合
        /// </summary>
        /// <returns></returns>
        public IQueryable<Maticsoft.Model.Product> GetQueryModelList()
        {
            return dal.GetQueryModelList();
        }

        /// <summary>
        /// 直接执行 SQL 查询 (LINQ to SQL)
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public IEnumerable<Maticsoft.Model.Product> GetQueryModelList(string strWhere)
        {
            return dal.GetQueryModelList(strWhere);
        }

        #endregion
    }
}
