using System;
using System.Data;
using System.Collections.Generic;
using LTP.Common;
using FOUPOS.Model;
using FOUPOS.DALFactory;
using FOUPOS.IDAL;
namespace FOUPOS.BLL
{
    /// <summary>
    /// 业务逻辑类FGoods 的摘要说明。
    /// </summary>
    public class FGoods
    {
        private readonly IFGoods dal = DataAccess.CreateFGoods();
        public FGoods()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string snum)
        {
            return dal.Exists(snum);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(FOUPOS.Model.FGoods model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(FOUPOS.Model.FGoods model)
        {
            dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int ID)
        {

            dal.Delete(ID);
        }
        public void DeleteByCID(int cid)
        {
            dal.DeleteByCID(cid);
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FOUPOS.Model.FGoods GetModel(int ID)
        {

            return dal.GetModel(ID);
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public FOUPOS.Model.FGoods GetModelByNum(string snum)
        {

            return dal.GetModelByNum(snum);
        }

        /// <summary>
        /// 得到一个对象实体，从缓存中。
        /// </summary>
        public FOUPOS.Model.FGoods GetModelByCache(int ID)
        {

            string CacheKey = "FGoodsModel-" + ID;
            object objModel = LTP.Common.DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(ID);
                    if (objModel != null)
                    {
                        int ModelCache = LTP.Common.ConfigHelper.GetConfigInt("ModelCache");
                        LTP.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
                    }
                }
                catch { }
            }
            return (FOUPOS.Model.FGoods)objModel;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<FOUPOS.Model.FGoods> GetModelList(string strWhere)
        {
            DataSet ds = dal.GetList(strWhere);
            List<FOUPOS.Model.FGoods> modelList = new List<FOUPOS.Model.FGoods>();
            int rowsCount = ds.Tables[0].Rows.Count;
            if (rowsCount > 0)
            {
                FOUPOS.Model.FGoods model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new FOUPOS.Model.FGoods();
                    if (ds.Tables[0].Rows[n]["CID"].ToString() != "")
                    {
                        model.CID = int.Parse(ds.Tables[0].Rows[n]["CID"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["ID"].ToString() != "")
                    {
                        model.ID = int.Parse(ds.Tables[0].Rows[n]["ID"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["SBuyPrice"].ToString() != "")
                    {
                        model.SBuyPrice = decimal.Parse(ds.Tables[0].Rows[n]["SBuyPrice"].ToString());
                    }
                    model.SColor = ds.Tables[0].Rows[n]["SColor"].ToString();
                    if (ds.Tables[0].Rows[n]["SCount"].ToString() != "")
                    {
                        model.SCount = int.Parse(ds.Tables[0].Rows[n]["SCount"].ToString());
                    }
                    if (ds.Tables[0].Rows[n]["SJoinTime"].ToString() != "")
                    {
                        model.SJoinTime = DateTime.Parse(ds.Tables[0].Rows[n]["SJoinTime"].ToString());
                    }
                    model.SName = ds.Tables[0].Rows[n]["SName"].ToString();
                    model.SNum = ds.Tables[0].Rows[n]["SNum"].ToString();
                    model.SRemark = ds.Tables[0].Rows[n]["SRemark"].ToString();
                    if (ds.Tables[0].Rows[n]["SSalePrice"].ToString() != "")
                    {
                        model.SSalePrice = decimal.Parse(ds.Tables[0].Rows[n]["SSalePrice"].ToString());
                    }
                    model.SType = ds.Tables[0].Rows[n]["SType"].ToString();
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetAllList()
        {
            return GetList("");
        }
        public DataSet GetStockHistory(string strWhere)
        {
            return dal.GetStockHistory(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        //public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        //{
        //return dal.GetList(PageSize,PageIndex,strWhere);
        //}

        #endregion  成员方法
    }
}

