context: namespace Maticsoft.BLL.CMS
  {
      using Maticsoft.BLL.SysManage;
      using Maticsoft.Common;
      using Maticsoft.IDAL.CMS;
      using Maticsoft.Model.CMS;
      using System;
      using System.Collections.Generic;
      using System.Data;
      using System.Linq;
      using System.Runtime.InteropServices;
      using System.Text;
  
      public class Content
      {
          private readonly IContent dal = DataAccess<IContent>.Create("CMS.Content");
  
          public int Add(Maticsoft.Model.CMS.Content model)
          {
              return this.dal.Add(model);
          }
  
          public List<Maticsoft.Model.CMS.Content> ContentToList(DataTable dt)
          {
              List<Maticsoft.Model.CMS.Content> list = new List<Maticsoft.Model.CMS.Content>();
              int count = dt.Rows.Count;
              if (count > 0)
              {
                  for (int i = 0; i < count; i++)
                  {
                      DataRow row = dt.Rows[i];
                      if (row != null)
                      {
                          Maticsoft.Model.CMS.Content item = new Maticsoft.Model.CMS.Content();
                          if ((row["ContentID"] != null) && (row["ContentID"].ToString() != ""))
                          {
                              item.ContentID = int.Parse(row["ContentID"].ToString());
                          }
                          if (row["Title"] != null)
                          {
                              item.Title = row["Title"].ToString();
                          }
                          if (row["SubTitle"] != null)
                          {
                              item.SubTitle = row["SubTitle"].ToString();
                          }
                          if (row["Summary"] != null)
                          {
                              item.Summary = row["Summary"].ToString();
                          }
                          if (row["Description"] != null)
                          {
                              item.Description = row["Description"].ToString();
                          }
                          if (row["ImageUrl"] != null)
                          {
                              item.ImageUrl = row["ImageUrl"].ToString();
                          }
                          if (row["ThumbImageUrl"] != null)
                          {
                              item.ThumbImageUrl = row["ThumbImageUrl"].ToString();
                          }
                          if (row["NormalImageUrl"] != null)
                          {
                              item.NormalImageUrl = row["NormalImageUrl"].ToString();
                          }
                          if ((row["CreatedDate"] != null) && (row["CreatedDate"].ToString() != ""))
                          {
                              item.CreatedDate = DateTime.Parse(row["CreatedDate"].ToString());
                          }
                          if ((row["CreatedUserID"] != null) && (row["CreatedUserID"].ToString() != ""))
                          {
                              item.CreatedUserID = int.Parse(row["CreatedUserID"].ToString());
                          }
                          if ((row["LastEditUserID"] != null) && (row["LastEditUserID"].ToString() != ""))
                          {
                              item.LastEditUserID = new int?(int.Parse(row["LastEditUserID"].ToString()));
                          }
                          if ((row["LastEditDate"] != null) && (row["LastEditDate"].ToString() != ""))
                          {
                              item.LastEditDate = new DateTime?(DateTime.Parse(row["LastEditDate"].ToString()));
                          }
                          if (row["LinkUrl"] != null)
                          {
                              item.LinkUrl = row["LinkUrl"].ToString();
                          }
                          if ((row["PvCount"] != null) && (row["PvCount"].ToString() != ""))
                          {
                              item.PvCount = int.Parse(row["PvCount"].ToString());
                          }
                          if ((row["State"] != null) && (row["State"].ToString() != ""))
                          {
                              item.State = int.Parse(row["State"].ToString());
                          }
                          if ((row["ClassID"] != null) && (row["ClassID"].ToString() != ""))
                          {
                              item.ClassID = int.Parse(row["ClassID"].ToString());
                          }
                          if (row["Keywords"] != null)
                          {
                              item.Keywords = row["Keywords"].ToString();
                          }
                          if ((row["Sequence"] != null) && (row["Sequence"].ToString() != ""))
                          {
                              item.Sequence = int.Parse(row["Sequence"].ToString());
                          }
                          if ((row["IsRecomend"] != null) && (row["IsRecomend"].ToString() != ""))
                          {
                              if ((row["IsRecomend"].ToString() == "1") || (row["IsRecomend"].ToString().ToLower() == "true"))
                              {
                                  item.IsRecomend = true;
                              }
                              else
                              {
                                  item.IsRecomend = false;
                              }
                          }
                          if ((row["IsHot"] != null) && (row["IsHot"].ToString() != ""))
                          {
                              if ((row["IsHot"].ToString() == "1") || (row["IsHot"].ToString().ToLower() == "true"))
                              {
                                  item.IsHot = true;
                              }
                              else
                              {
                                  item.IsHot = false;
                              }
                          }
                          if ((row["IsColor"] != null) && (row["IsColor"].ToString() != ""))
                          {
                              if ((row["IsColor"].ToString() == "1") || (row["IsColor"].ToString().ToLower() == "true"))
                              {
                                  item.IsColor = true;
                              }
                              else
                              {
                                  item.IsColor = false;
                              }
                          }
                          if ((row["IsTop"] != null) && (row["IsTop"].ToString() != ""))
                          {
                              if ((row["IsTop"].ToString() == "1") || (row["IsTop"].ToString().ToLower() == "true"))
                              {
                                  item.IsTop = true;
                              }
                              else
                              {
                                  item.IsTop = false;
                              }
                          }
                          if (row["Attachment"] != null)
                          {
                              item.Attachment = row["Attachment"].ToString();
                          }
                          if (row["Remary"] != null)
                          {
                              item.Remary = row["Remary"].ToString();
                          }
                          if ((row["TotalComment"] != null) && (row["TotalComment"].ToString() != ""))
                          {
                              item.TotalComment = int.Parse(row["TotalComment"].ToString());
                          }
                          if ((row["TotalSupport"] != null) && (row["TotalSupport"].ToString() != ""))
                          {
                              item.TotalSupport = int.Parse(row["TotalSupport"].ToString());
                          }
                          if ((row["TotalFav"] != null) && (row["TotalFav"].ToString() != ""))
                          {
                              item.TotalFav = int.Parse(row["TotalFav"].ToString());
                          }
                          if ((row["TotalShare"] != null) && (row["TotalShare"].ToString() != ""))
                          {
                              item.TotalShare = int.Parse(row["TotalShare"].ToString());
                          }
                          if (row["BeFrom"] != null)
                          {
                              item.BeFrom = row["BeFrom"].ToString();
                          }
                          if (row["FileName"] != null)
                          {
                              item.FileName = row["FileName"].ToString();
                          }
                          if (row["Meta_Title"] != null)
                          {
                              item.Meta_Title = row["Meta_Title"].ToString();
                          }
                          if (row["Meta_Description"] != null)
                          {
                              item.Meta_Description = row["Meta_Description"].ToString();
                          }
                          if (row["Meta_Keywords"] != null)
                          {
                              item.Meta_Keywords = row["Meta_Keywords"].ToString();
                          }
                          if (row["SeoUrl"] != null)
                          {
                              item.SeoUrl = row["SeoUrl"].ToString();
                          }
                          if (row["SeoImageAlt"] != null)
                          {
                              item.SeoImageAlt = row["SeoImageAlt"].ToString();
                          }
                          if (row["SeoImageTitle"] != null)
                          {
                              item.SeoImageTitle = row["SeoImageTitle"].ToString();
                          }
                          if (row["StaticUrl"] != null)
                          {
                              item.StaticUrl = row["StaticUrl"].ToString();
                          }
                          if (row["ComCount"] != null)
                          {
                              item.ComCount = Globals.SafeInt(row["ComCount"].ToString(), 0);
                          }
                          list.Add(item);
                      }
                  }
              }
              return list;
          }
  
          public List<Maticsoft.Model.CMS.Content> DataTableToList(DataTable dt)
          {
              List<Maticsoft.Model.CMS.Content> list = new List<Maticsoft.Model.CMS.Content>();
              int count = dt.Rows.Count;
              if (count > 0)
              {
                  for (int i = 0; i < count; i++)
                  {
                      Maticsoft.Model.CMS.Content item = this.dal.DataRowToModel(dt.Rows[i]);
                      if (item != null)
                      {
                          list.Add(item);
                      }
                  }
              }
              return list;
          }
  
          public bool Delete(int ContentID)
          {
              return this.dal.Delete(ContentID);
          }
  
          public bool DeleteList(string ContentIDlist)
          {
              return this.dal.DeleteList(ContentIDlist);
          }
  
          public bool Exists(int ContentID)
          {
              return this.dal.Exists(ContentID);
          }
  
          public bool ExistsByClassID(int ClassID)
          {
              return this.dal.ExistsByClassID(ClassID);
          }
  
          public bool ExistTitle(string Title)
          {
              return this.dal.ExistTitle(Title);
          }
  
          public DataSet GetAllList()
          {
              return this.GetList("");
          }
  
          public string GetContentUrl(Maticsoft.Model.CMS.Content model)
          {
              if (model == null)
              {
                  return "";
              }
              int intValueByCache = ConfigSystem.GetIntValueByCache("CMS_Static_ContentRule");
              switch (intValueByCache)
              {
                  case 0:
                      return model.ContentID.ToString();
  
                  case 1:
                      return (PinyinHelper.GetPinyin(model.Title) + "_" + model.ContentID);
              }
              if ((intValueByCache == 2) && !string.IsNullOrWhiteSpace(model.SeoUrl))
              {
                  return (model.SeoUrl + "_" + model.ContentID);
              }
              return model.ContentID.ToString();
          }
  
          public List<Maticsoft.Model.CMS.Content> GetHotCom(string comType, int Top)
          {
              int diffDate = 1;
              string str = comType;
              if (str != null)
              {
                  if (!(str == "day"))
                  {
                      if (str == "week")
                      {
                          diffDate = 7;
                      }
                  }
                  else
                  {
                      diffDate = 1;
                  }
              }
              DataSet hotCom = this.dal.GetHotCom(diffDate, Top);
              return this.ContentToList(hotCom.Tables[0]);
          }
  
          public List<Maticsoft.Model.CMS.Content> GetHotComList(int ClassId, int Top = -1)
          {
              StringBuilder builder = new StringBuilder();
              builder.Append(" State=0 ");
              if (ClassId > 0)
              {
                  builder.AppendFormat(" and ClassID={0}", ClassId);
              }
              DataSet set = this.GetList(Top, builder.ToString(), "  TotalComment Desc");
              return this.DataTableToList(set.Tables[0]);
          }
  
          public DataSet GetList(string strWhere)
          {
              return this.dal.GetList(strWhere);
          }
  
          public DataSet GetList(int Top, string strWhere, string filedOrder)
          {
              return this.dal.GetList(Top, strWhere, filedOrder);
          }
  
          public List<Maticsoft.Model.CMS.Content> GetList(int? ClassID, int startIndex, int endIndex, string Keywords)
          {
              DataSet ds = this.GetListByPage(ClassID, startIndex, endIndex, Keywords);
              if (DataSetTools.DataSetIsNull(ds))
              {
                  return null;
              }
              return this.dal.DataTableToListEx(ds.Tables[0]);
          }
  
          public DataSet GetListByItem(int Top, string strWhere, string filedOrder)
          {
              return this.dal.GetListByItem(Top, strWhere, filedOrder);
          }
  
          public List<Maticsoft.Model.CMS.Content> GetListByPage(int classID, int startIndex, int endIndex)
          {
              DataSet set = this.dal.GetListByPage(" State=0  and ClassID= " + classID, "  CreatedDate desc   ", startIndex, endIndex);
              return this.DataTableToList(set.Tables[0]);
          }
  
          public DataSet GetListByPage(int? ClassID, int startIndex, int endIndex, string Keywords)
          {
              StringBuilder builder = new StringBuilder();
              builder.AppendFormat("  T.State={0} ", Globals.SafeInt(EnumHelper.ContentStateType.Approve.ToString(), 0));
              if (ClassID.HasValue)
              {
                  if (builder.Length != 0)
                  {
                      builder.AppendFormat(" AND T.ClassID={0} ", ClassID.Value);
                  }
                  else
                  {
                      builder.AppendFormat(" T.ClassID={0} ", ClassID.Value);
                  }
              }
              if (!string.IsNullOrWhiteSpace(Keywords))
              {
                  Keywords = Globals.HtmlEncode(InjectionFilter.SqlFilter(Keywords));
                  if (builder.Length != 0)
                  {
                      builder.AppendFormat(" AND T.Title like '%{0}%' ", Keywords);
                  }
                  else
                  {
                      builder.AppendFormat(" T.Title like '%{0}%' ", Keywords);
                  }
              }
              return this.GetListByPageEx(builder.ToString(), "", startIndex, endIndex);
          }
  
          public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
          {
              return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
          }
  
          public List<Maticsoft.Model.CMS.Content> GetListByPage(int classID, int startIndex, int endIndex, string ImageUrl, int topclass, out int totalCount)
          {
              StringBuilder builder = new StringBuilder();
              if (new Maticsoft.BLL.CMS.ContentClass().GetRecordCount(string.Format(" State=0  and  ParentId={0}  ", classID)) > 0)
              {
                  builder.AppendFormat(" EXISTS ( SELECT TOP {0} ClassID   FROM   CMS_ContentClass AS contclas WHERE  State=0  and  ParentId={1} AND contclas.ClassID=T.ClassID  ORDER BY   Sequence  )", topclass, classID);
                  if (!string.IsNullOrWhiteSpace(ImageUrl))
                  {
                      builder.Append(" and  ImageUrl is not null ");
                  }
                  totalCount = this.dal.GetRecordCountEx(builder.ToString());
                  DataSet set = this.dal.GetListByPage(builder.ToString(), "  CreatedDate desc   ", startIndex, endIndex);
                  return this.DataTableToList(set.Tables[0]);
              }
              builder.AppendFormat("  State=0  and  ClassID={0} ", classID);
              if (!string.IsNullOrWhiteSpace(ImageUrl))
              {
                  builder.Append(" and  ImageUrl is not null ");
              }
              totalCount = this.dal.GetRecordCountEx(builder.ToString());
              DataSet set2 = this.dal.GetListByPage(builder.ToString(), "  CreatedDate desc   ", startIndex, endIndex);
              return this.DataTableToList(set2.Tables[0]);
          }
  
          public List<Maticsoft.Model.CMS.Content> GetListByPage4Menu(int? ClassID, int startIndex, int endIndex, string Keywords)
          {
  
gt:             StringBuilder builder = new StringBuilder();