using System;
using System.Data;
using System.Collections.Generic;


namespace CallCenterApi.BLL
{
    /// <summary>
    /// 文章内容
    /// </summary>
    public partial class article
    {
        //private readonly Model.sysconfig sysConfig = new BLL.sysconfig().loadConfig();//获得系统配置信息
        private readonly CallCenterApi.DAL.article dal;

        public article()
        {
            dal = new CallCenterApi.DAL.article();
        }

        #region 基本方法================================
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int channel_id, int article_id)
        {
            //string channelName = new BLL.site_channel().GetChannelName(channel_id);//查询频道名称
            //if (string.IsNullOrEmpty(channelName))
            //{
            //    return false;
            //}
            return dal.Exists(article_id);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string call_index)
        {
            if (string.IsNullOrEmpty(call_index))
            {
                return false;
            }
            //string channelName = new BLL.site_channel().GetChannelName(channel_id);//查询频道名称
            //if (string.IsNullOrEmpty(channelName))
            //{
            //    return false;
            //}
            return dal.Exists(call_index);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.article model)
        {
            return dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.article model)
        {
            return dal.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int article_id)
        {
            //string channelName = new BLL.site_channel().GetChannelName(channel_id);//查询频道名称
            //if (string.IsNullOrEmpty(channelName))
            //{
            //    return false;
            //}
            string content = dal.GetContent(article_id);//获取信息内容
            bool result = dal.Delete(article_id);//删除内容
            //if (result && !string.IsNullOrEmpty(content))
            //{
            //    FileHelper.DeleteContentPic(content, sysConfig.webpath + sysConfig.filepath); //删除内容图片
            //}
            return result;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteBatch(string where)
        {
            bool result = dal.DeleteBatch(where);//删除内容
            return result;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.article GetModel(int article_id)
        {
            //string channelName = new BLL.site_channel().GetChannelName(channel_id);//查询频道名称
            //if (string.IsNullOrEmpty(channelName))
            //{
            //    return null;
            //}
            return dal.GetModel(article_id);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.article GetModel(string call_index)
        {
            //string channelName = new BLL.site_channel().GetChannelName(channel_id);//查询频道名称
            //if (string.IsNullOrEmpty(channelName))
            //{
            //    return null;
            //}
            return dal.GetModel(call_index);
        }

        public List<Model.article> DataTableToList(DataTable dt)
        {
            List<Model.article> list = new List<Model.article>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                CallCenterApi.Model.article model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new CallCenterApi.Model.article();
                    if (dt.Rows[n]["id"] != null && dt.Rows[n]["id"].ToString() != "")
                    {
                        model.id = int.Parse(dt.Rows[n]["id"].ToString());
                    }
                    if (dt.Rows[n]["category_id"] != null && dt.Rows[n]["category_id"].ToString() != "")
                    {
                        model.category_id = Convert.ToInt32(dt.Rows[n]["category_id"]);
                    }
                    //if (dt.Rows[n]["trade_no"] != null && dt.Rows[n]["trade_no"].ToString() != "")
                    //{
                    //    model.trade_no = dt.Rows[n]["trade_no"].ToString();
                    //}
                    if (dt.Rows[n]["title"] != DBNull.Value && dt.Rows[n]["title"].ToString() != "")
                    {
                        model.title = dt.Rows[n]["title"].ToString();
                    }

                    if (dt.Rows[n]["img_url"] != null && dt.Rows[n]["img_url"].ToString() != "")
                    {
                        model.img_url = dt.Rows[n]["img_url"].ToString();
                    }
                    if (dt.Rows[n]["zhaiyao"] != null && dt.Rows[n]["zhaiyao"].ToString() != "")
                    {
                        model.zhaiyao = dt.Rows[n]["zhaiyao"].ToString();
                    }
                    if (dt.Rows[n]["content"] != null && dt.Rows[n]["content"].ToString() != "")
                    {
                        model.content = dt.Rows[n]["content"].ToString();
                    }
                    if (dt.Rows[n]["sort_id"] != null && dt.Rows[n]["sort_id"].ToString() != "")
                    {
                        model.sort_id = Convert.ToInt32(dt.Rows[n]["sort_id"]);
                    }
                    if (dt.Rows[n]["status"] != null && dt.Rows[n]["status"].ToString() != "")
                    {
                        model.status = Convert.ToInt32(dt.Rows[n]["status"]);
                    }
                    if (dt.Rows[n]["add_time"] != null && dt.Rows[n]["add_time"].ToString() != "")
                    {
                        model.add_time = Convert.ToDateTime(dt.Rows[n]["add_time"]);
                    }
                    if (dt.Rows[n]["update_time"] != DBNull.Value && dt.Rows[n]["update_time"].ToString() != "")
                    {
                        model.update_time = Convert.ToDateTime(dt.Rows[n]["update_time"]);
                    }
                    else
                    {
                        model.update_time = null;
                    }
                    if (dt.Rows[n]["sell_price"] != DBNull.Value && dt.Rows[n]["sell_price"].ToString() != "")
                    {
                        model.sell_price = decimal.Parse(dt.Rows[n]["sell_price"].ToString());
                    }
                    else
                    {
                        model.sell_price = null;
                    }
                    if (dt.Rows[n]["goods_no"] != null && dt.Rows[n]["goods_no"].ToString() != "")
                    {
                        model.goods_no = dt.Rows[n]["goods_no"].ToString();
                    }
                    if (dt.Rows[n]["department"] != null && dt.Rows[n]["department"].ToString() != "")
                    {
                        model.department = Convert.ToInt32(dt.Rows[n]["department"]);
                    }
                    if (dt.Rows[n]["categorytitle"] != null && dt.Rows[n]["categorytitle"].ToString() != "")
                    {
                        model.categorytitle = dt.Rows[n]["categorytitle"].ToString();
                    }

                    list.Add(model);
                }
            }
            return list;
        }
        #endregion

        #region 扩展方法================================
        /// <summary>
        /// 是否存在标题
        /// </summary>
        public bool ExistsTitle(int channel_id, string title)
        {
            return dal.ExistsTitle(title);
        }
        /// <summary>
        /// 是否存在标题
        /// </summary>
        public bool ExistsTitle(int channel_id, int category_id, string title)
        {
            return dal.ExistsTitle(category_id, title);
        }

        /// <summary>
        /// 返回信息标题
        /// </summary>
        public string GetTitle(int channel_id, int article_id)
        {
            return dal.GetTitle(article_id);
        }

        /// <summary>
        /// 返回信息内容
        /// </summary>
        public string GetContent(string channel_name, int article_id)
        {
            return dal.GetContent(article_id);
        }

        /// <summary>
        /// 返回信息内容
        /// </summary>
        public string GetContent(string channel_name, string call_index)
        {
            return dal.GetContent(call_index);
        }

        /// <summary>
        /// 返回信息封面图
        /// </summary>
        public string GetImgUrl(int channel_id, int article_id)
        {
            return dal.GetImgUrl(article_id);
        }

        /// <summary>
        /// 获取阅读次数
        /// </summary>
        public int GetClick(int channel_id, int article_id)
        {
            return dal.GetClick(article_id);
        }

        /// <summary>
        /// 返回数据总数
        /// </summary>
        public int GetCount(int channel_id, string strWhere)
        {
            return dal.GetCount(strWhere);
        }

        /// <summary>
        /// 返回商品库存数量
        /// </summary>
        public int GetStockQuantity(int channel_id, int article_id)
        {
            return dal.GetStockQuantity(article_id);
        }

        /// <summary>
        /// 修改一列数据
        /// </summary>
        public bool UpdateField(int channel_id, int article_id, string strValue)
        {
            return dal.UpdateField(article_id, strValue);
        }

        /// <summary>
        /// 获取微信推送实体
        /// </summary>
        public Model.article GetWXModel(int channel_id, int article_id)
        {
            DataTable dt = dal.GetList(1, "id=" + article_id, "id desc").Tables[0];
            if (dt.Rows.Count == 0)
            {
                return null;
            }
            Model.article model = new Model.article();
            model.id = int.Parse(dt.Rows[0]["id"].ToString());
            model.title = dt.Rows[0]["title"].ToString();
            model.img_url = dt.Rows[0]["img_url"].ToString();
            model.zhaiyao = dt.Rows[0]["zhaiyao"].ToString();
            model.content = dt.Rows[0]["content"].ToString();
            return model;
        }
        #endregion

    }
}