using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections.Generic;

/// <summary>
/// ArticleInfoManager 的摘要说明
/// </summary>
public static class ArticleInfoManager
{
    public static string add(ArticleInfo article)
    {
        string sql = string.Format("INSERT INTO ArticleInfo VALUES ('" + article.txtTitle+ "','"+article.txtContent+"','"+article.CreateTime+"')");
        int i = DBHelper.ExecuteCommand(sql);
        if (i > 0)
        {
            return  "系统提示：添加成功";
        }
        else
        {
            return "系统提示：添加失败";
        }
    }

    public static string update(ArticleInfo article)
    {
        string sql = string.Format("UPDATE ArticleInfo SET txtTitle='" + article.txtTitle + "',txtContent='" + article.txtContent + "' WHERE AId=" + article .AId+ "");
        int i = DBHelper.ExecuteCommand(sql);
        if (i > 0)
        {
            return "系统提示：修改成功";
        }
        else
        {
            return "系统提示：修改失败";
        }
    }

    public static string delete(int aid)
    {
        string sql = "DELETE ArticleInfo WHERE AId=" + aid;
        int i = DBHelper.ExecuteCommand(sql);
        if (i > 0)
        {
            return "系统提示：删除成功";
        }
        else
        {
            return "系统提示：删除失败";
        }
    }

    public static List<ArticleInfo> ShowArticleList()
    {
        string sql = "SELECT * FROM ArticleInfo ORDER BY AId DESC";
        List<ArticleInfo> list = new List<ArticleInfo>();
        using (SqlDataReader Reader = DBHelper.GetReader(sql))
        {
            if (!Reader.HasRows)
            {
                list = null;
            }
            else
            {
                while (Reader.Read())
                {
                    ArticleInfo article = new ArticleInfo();
                    article.AId = Convert.ToInt32(Reader["AId"]);
                    article.txtTitle = Reader["txtTitle"].ToString();
                    article.txtContent = Reader["txtContent"].ToString();
                    article.CreateTime = Reader["CreateTime"].ToString();
                    list.Add(article);
                }
            }
        }
        return list;
    }

    public static List<ArticleInfo> ShowArticleList(string key)
    {
        string sql = "SELECT * FROM ArticleInfo WHERE txtTitle LIKE '%" + key + "%'";
        List<ArticleInfo> list = new List<ArticleInfo>();
        using (SqlDataReader Reader = DBHelper.GetReader(sql))
        {
            if (!Reader.HasRows)
            {
                list = null;
            }
            else
            {
                while (Reader.Read())
                {
                    ArticleInfo article = new ArticleInfo();
                    article.AId = Convert.ToInt32(Reader["AId"]);
                    article.txtTitle = Reader["txtTitle"].ToString();
                    article.txtContent = Reader["txtContent"].ToString();
                    article.CreateTime = Reader["CreateTime"].ToString();
                    list.Add(article);
                }
            }
        }
        return list;
    }

    public static List<ArticleInfo> ShowArticleList(int no,int pagenum)
    {
        string sql = string.Format("SELECT TOP {0} * FROM ArticleInfo WHERE AId NOT IN (SELECT TOP ({1}*{2}) AId FROM ArticleInfo ORDER BY AId DESC)ORDER BY AId DESC",no,pagenum,no);
        List<ArticleInfo> list = new List<ArticleInfo>();
        using (SqlDataReader Reader = DBHelper.GetReader(sql))
        {
            if (!Reader.HasRows)
            {
                list = null;
            }
            else
            {
                while (Reader.Read())
                {
                    ArticleInfo article = new ArticleInfo();
                    article.AId = Convert.ToInt32(Reader["AId"]);
                    article.txtTitle = Reader["txtTitle"].ToString();
                    article.txtContent = Reader["txtContent"].ToString();
                    article.CreateTime = Reader["CreateTime"].ToString();
                    list.Add(article);
                }
            }
        }
        return list;
    }

    public static ArticleInfo SelectArticleInfoByAId(int aid)
    {
        string sql = string.Format("SELECT * FROM ArticleInfo WHERE AId={0}",aid);
        ArticleInfo article = new ArticleInfo();
        using (SqlDataReader Reader = DBHelper.GetReader(sql))
        {
            if (!Reader.HasRows)
            {
                article = null;
            }
            else
            {
                if (Reader.Read())
                {
                    article.AId = Convert.ToInt32(Reader["AId"]);
                    article.txtTitle = Reader["txtTitle"].ToString();
                    article.txtContent = Reader["txtContent"].ToString();
                    article.CreateTime = Reader["CreateTime"].ToString();
                }
            }
        }
        return article;
    }

    public static int ReadCount()
    {
        string sql = "SELECT COUNT(*) FROM ArticleInfo";
        return DBHelper.GetScalar(sql);
    }

    public static int PageCount()
    {
        int count = ReadCount();
        int page = 0;
        if (count % 10 == 0)
        {
            page = count / 10;
        }
        else
        {
            page = (count / 10) + 1;
        }
        return page;
    }




}
