using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;
namespace BLL.Models
{
    public class MArticleModels
    {
        string[] str_files = { "ID","Name","Classid","Content","flag","istj"};
        /// <summary>
        /// 添加文章
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddArticle(MODEL.MArticle model) {

            bool ret = false;
            string str_sql = "insert into [tb_mc_article] ( Name,Classid,Content,flag,istj)values( '" +model.Name + "'," +model.Classid.ToString() + " ,'" + model.Content + "' ," +model.Flag.ToString() + "," +model.Istj.ToString() + " )";
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        }
        /// <summary>
        /// 修改文章
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool UpdateArticle(MODEL.MArticle model) {

            bool ret = false;
            string str_sql = "update [tb_mc_article] set Name='"+model.Name+"',Classid="+model.Classid.ToString()+",Content='"+model.Content+"',flag="+model.Flag.ToString()+",istj="+model.Istj.ToString()+" where id="+model.ID.ToString();
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        }
        /// <summary>
        /// 删除文章
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DelArticle(string id) {

            bool ret = false;
            string str_sql = " delete from [tb_mc_article] where id in ("+id+")";
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {
                db.ExecuteNonQuery(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql);
                ret = true;
            }
            catch (Exception ex) { }
            return ret;
        }
        /// <summary>
        /// 获得一条文章信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetArticleById(string id) {

            string ret = string.Empty;
            string str_sql = "select * from [tb_mc_article] where id=" + id;
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            DataTable dt = new DataTable();
            try
            {
                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
            }
            catch (Exception ex) { }
            BLL.FormJSONHelper jsons = new BLL.FormJSONHelper();
            jsons.success = true;


            foreach (DataRow dr in dt.Rows)
            {

                foreach (string s in str_files)
                {

                    jsons.AddItem(s, dr[s].ToString());
                }
                jsons.ItemOk();

            }



            ret = jsons.ToString();
            return ret;
        }
        /// <summary>
        /// 取得文章信息，分页
        /// </summary>
        /// <param name="start"></param>
        /// <param name="limit"></param>
        /// <param name="fiter"></param>
        /// <returns></returns>
        public string AllArticle(string start,string limit,string fiter) {

            string ret = string.Empty;
            string str_sql = "select top " + limit + " * from [tb_mc_article] where id not in( select top " + start + " id from [tb_mc_article] order by id desc ) order by id desc";
            if (start == "0")
            {

                str_sql = " select top " + limit + " * from [tb_mc_article] order by id desc ";
            }
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            DataTable dt = new DataTable();
            string tcount = "0";
            try
            {
                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
                tcount = db.ExecuteScalar(COMM.PucConfig.ConnectionStringA, CommandType.Text, "select count(*) from [tb_mc_article] ").ToString();
            }
            catch (Exception ex) { }

            BLL.JSONHelper jsons = new BLL.JSONHelper();



            foreach (DataRow dr in dt.Rows)
            {

                foreach (string s in str_files)
                {

                    if (s == "Content") {

                        continue;
                    }
                    jsons.AddItem(s, dr[s].ToString());
                }
                jsons.ItemOk();

            }


            jsons.totlalCount = Convert.ToInt32(tcount);
            ret = jsons.ToString();
            return ret;
        }
        /// <summary>
        /// 通过条件获取文章信息
        /// </summary>
        /// <param name="tops"></param>
        /// <param name="fiters"></param>
        /// <returns></returns>
        public DataTable GetArticleByFiter(string tops,string fiters) {

            DataTable dt = new DataTable();
            string str_sql = " select "+tops+" * from [tb_mc_article] where "+fiters;
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            try
            {
                dt = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
                
            }
            catch (Exception ex) { }
            return dt;
        }
        public DataTable GetArticleAll( string start,ref string tcout,string fiter,string pagesize ) {
        
                        DataTable ret = new DataTable();
                        string str_sql = "select top  " + pagesize + " * from tb_mc_article where id not in(select top " + start + " id from tb_mc_article where 1=1" + fiter + ") and 1=1 " + fiter;
            if(start=="0"){

                str_sql = "select top  " + pagesize + " * from tb_mc_article where  1=1 " + fiter;
            }
            Library.SqlProvider.OleDb db = new Library.SqlProvider.OleDb();
            string str_count = " select count(id) from tb_mc_article where 1=1 " + fiter;
            try {

                ret = db.ExecuteDataset(COMM.PucConfig.ConnectionStringA, CommandType.Text, str_sql).Tables[0];
                tcout = db.ExecuteScalar(COMM.PucConfig.ConnectionStringA,CommandType.Text,str_count).ToString();
            }
            catch (Exception ex) { }
            
            return ret;
        }
        /// <summary>
        /// 获得首页公告信息
        /// </summary>
        /// <returns></returns>
        public string GetIndexGonggao() {

            string ret = string.Empty;
            StringBuilder str_html = new StringBuilder();
            DataTable dt=new DataTable();
            dt=GetArticleByFiter(" top 9 "," classid=87 ");
            foreach(DataRow dr in dt.Rows){

                str_html.Append("<LI  ><A   target=_blank  href='/NewsMcViews.aspx?Id=");
                str_html.Append(dr["id"].ToString());
                str_html.Append("' title='");
                str_html.Append(dr["name"].ToString());
                str_html.Append("'>·");
                str_html.Append(dr["name"].ToString());
                str_html.Append("</a>");


            }
            ret = str_html.ToString();

            return ret;
        }
        /// <summary>
        /// 获得首页新闻信息
        /// </summary>
        /// <returns></returns>
        public string GetIndexNews()
        {

            string ret = string.Empty;
            StringBuilder str_html = new StringBuilder();
            DataTable dt = new DataTable();
            dt = GetArticleByFiter(" top 10 ", " classid=88 ");
            foreach (DataRow dr in dt.Rows)
            {

                str_html.Append("<LI  ><A   target=_blank  href='/NewsMcViews.aspx?Id=");
                str_html.Append(dr["id"].ToString());
                str_html.Append("' title='");
                str_html.Append(dr["name"].ToString());
                str_html.Append("'>·");
                str_html.Append(dr["name"].ToString());
                str_html.Append("</a>");


            }
            ret = str_html.ToString();

            return ret;
        }
        /// <summary>
        /// 获得首页特惠信息
        /// </summary>
        /// <returns></returns>
        public string GetIndexTehui()
        {

            string ret = string.Empty;
            StringBuilder str_html = new StringBuilder();
            DataTable dt = new DataTable();
            dt = GetArticleByFiter(" top 10 ", " classid=130 ");
            foreach (DataRow dr in dt.Rows)
            {

                str_html.Append("<LI  ><A   target=_blank  href='/NewsMcViews.aspx?Id=");
                str_html.Append(dr["id"].ToString());
                str_html.Append("' title='");
                str_html.Append(dr["name"].ToString());
                str_html.Append("'>·");
                str_html.Append(dr["name"].ToString());
                str_html.Append("</a>");


            }
            ret = str_html.ToString();

            return ret;
        }
    }
}
