using System;
using System.Collections.Generic;
using System.Text;
using JuSNS.Factory;
using System.Data;
using System.Data.SqlClient;
using JuSNS.Profile;
using JuSNS.Config;
using JuSNS.Model;
using JuSNS.Common;

namespace JuSNS.SQLServer
{
    public class News : DbBase, INews
    {
        public IDataReader GetNewsClass(int ParentID)
        {
            SqlParameter param = new SqlParameter("@ParentID", ParentID);
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, "JuSNS_GetNewsClass", param);
        }

        public DataTable GetNewsClassD(int ParentID)
        {
            SqlParameter param = new SqlParameter("@ParentID", ParentID);
            return DbHelper.ExecuteTable(CommandType.StoredProcedure, "JuSNS_GetNewsClass", param);
        }

        public NewsInfo GetNewsId(int NewsID, int Num)
        {
            SqlConnection sqlConn = new SqlConnection(DBConfig.CnString);
            sqlConn.Open();
            try
            {
                NewsInfo nInfo = new NewsInfo();
                string UpdateSTR = string.Empty;
                if (Num == 1)
                {
                    UpdateSTR = "update NT_News set click=click+1 where ID=" + NewsID + "";
                    DbHelper.ExecuteNonQuery(sqlConn, CommandType.Text, UpdateSTR, null);
                }
                string sql = "select id,title,Content,UserID,ClassID,CreatTime,islock,click,isSys,ip,shareNum,Pic,(select ClassName from NT_NewsClass where NT_NewsClass.ID=NT_News.ClassID) as ClassName from NT_News where ID=" + NewsID + "";
                DataTable rd = DbHelper.ExecuteTable(sqlConn,CommandType.Text, sql, null);
                if (rd != null && rd.Rows.Count > 0)
                {
                    nInfo.ID = (int)rd.Rows[0]["ID"];
                    nInfo.Title = rd.Rows[0]["title"].ToString();
                    nInfo.Content = rd.Rows[0]["Content"].ToString();
                    nInfo.UserID = Convert.ToInt32(rd.Rows[0]["UserID"].ToString());
                    nInfo.ClassID = Convert.ToInt32(rd.Rows[0]["ClassID"].ToString());
                    nInfo.CreatTime = Convert.ToDateTime(rd.Rows[0]["CreatTime"].ToString());
                    nInfo.islock = Convert.ToInt32(rd.Rows[0]["islock"].ToString());
                    nInfo.isSys = Convert.ToInt32(rd.Rows[0]["isSys"].ToString());
                    nInfo.click = Convert.ToInt32(rd.Rows[0]["click"].ToString());
                    nInfo.shareNum = Convert.ToInt32(rd.Rows[0]["shareNum"].ToString());
                    nInfo.ClassName = rd.Rows[0]["ClassName"].ToString();
                    nInfo.IP = rd.Rows[0]["IP"].ToString();
                    nInfo.Pic = rd.Rows[0]["Pic"].ToString();
                }
                rd.Clear(); rd.Dispose();
                return nInfo;
            }
            finally
            {
                if (sqlConn.State == ConnectionState.Open)
                    sqlConn.Close();
            }

        }


        public int InsertNewsComment(int UserID, int NewsID, string Content)
        {
            SqlConnection cn = new SqlConnection(DBConfig.CnString);
            cn.Open();
            try
            {
                SqlTransaction trans = cn.BeginTransaction();
                try
                {
                    SqlParameter[] param = new SqlParameter[5];
                    param[0] = new SqlParameter("@UserID", SqlDbType.Int, 4);
                    param[0].Value = UserID;
                    param[1] = new SqlParameter("@NewsID", SqlDbType.Int, 4);
                    param[1].Value = NewsID;
                    param[2] = new SqlParameter("@Content", SqlDbType.NVarChar, 200);
                    param[2].Value = Content;
                    param[3] = new SqlParameter("@IP", SqlDbType.NVarChar, 15);
                    param[3].Value = Common.Public.GetClientIP();
                    param[4] = new SqlParameter("@CreatTime", SqlDbType.DateTime, 8);
                    param[4].Value = DateTime.Now;
                    string sql = "insert into NT_NewsComment(newsID,content,UserID,parentID,IP,CreatTime) values (@NewsID,@Content,@UserID,0,@IP,@CreatTime);Select @@IDENTITY";
                    int n = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, sql, param).ToString());
                    JUser ju = new JUser();
                    if (ju.CheckPrivacyInfo(trans, UserID, "ActLogComment"))
                    {
                         UserDyn(trans, 0, 0, "commentnews", UserID, 0, NewsID, "ÆÀÂÛÁËÐÂÎÅ", n.ToString());
                    }
                    trans.Commit();
                    return n;
                }
                catch
                {
                    trans.Rollback();
                    throw;
                }
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }

        public DataTable GetSysNews(int Num,int isSys)
        {
            string sql = "select top " + Num + " id,title,Content,CreatTime,Pic from NT_News where isSys=" + isSys + " order by id desc";
            return DbHelper.ExecuteTable(CommandType.Text, sql, null);
        }

        public int DelNews(int ID,int flg)
        {
            string sql = string.Empty;
            if (flg == 1)
            {
                sql = "delete from NT_News";
            }
            else
            {
                sql = "delete from NT_News where ID=" + ID;
            }
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        }

        public void DelNewsComment(int UserID, int ID)
        {
            string sql = "delete from NT_NewsComment where UserID=" + UserID + " and ID=" + ID + "";
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        }

        public int UpdateNews(NewsInfo ns, int NewsID)
        {
            SqlParameter[] param = new SqlParameter[11];
            param[0] = new SqlParameter("@Title", SqlDbType.NVarChar, 50);
            param[0].Value = ns.Title;
            param[1] = new SqlParameter("@Content", SqlDbType.NText);
            param[1].Value = ns.Content;
            param[2] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[2].Value = ns.UserID;
            param[3] = new SqlParameter("@ClassID", SqlDbType.Int, 4);
            param[3].Value = ns.ClassID;
            param[4] = new SqlParameter("@CreatTime", SqlDbType.DateTime, 8);
            param[4].Value = ns.CreatTime;
            param[5] = new SqlParameter("@Islock", SqlDbType.TinyInt, 1);
            param[5].Value = ns.islock;
            param[6] = new SqlParameter("@IP", SqlDbType.NVarChar, 15);
            param[6].Value = ns.IP;
            param[7] = new SqlParameter("@Click", SqlDbType.Int, 4);
            param[7].Value = ns.click;
            param[8] = new SqlParameter("@IsSys", SqlDbType.TinyInt, 1);
            param[8].Value = ns.isSys;
            param[9] = new SqlParameter("@shareNum", SqlDbType.Int, 4);
            param[9].Value = ns.shareNum;
            param[10] = new SqlParameter("@Pic", SqlDbType.NVarChar, 100);
            param[10].Value = ns.Pic;
            string sql = string.Empty;
            if (NewsID > 0)
            {
                sql = "update NT_News set title=@Title,Content=@Content,UserID=@UserID,ClassID=@ClassID,CreatTime=@CreatTime,islock=@IsLock,IP=@IP,click=@Click,isSys=@IsSys,shareNum=@shareNum,Pic=@Pic where ID=" + NewsID;
            }
            else
            {
                sql = "insert into NT_News (Title,Content,UserID,ClassID,CreatTime,IsLock,IP,Click,IsSys,shareNum,Pic)values(@Title,@Content,@UserID,@ClassID,@CreatTime,@IsLock,@IP,@Click,@IsSys,@shareNum,@Pic)";
            }
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }

        public DataTable GetNewsClassID(int ID)
        {
            string sql = "select ID,className,ParentID from NT_Newsclass where ID="+ID;
            return DbHelper.ExecuteTable(CommandType.Text, sql, null);
        }

        public int UpdateNewsClassID(string className, int id)
        {
            SqlParameter param = new SqlParameter("@className", className);
            string sql = string.Empty;
            if (id >0)
            {
                sql = "update NT_Newsclass set className=@className where ID=" + id;
            }
            else
            {
                sql = "insert into NT_Newsclass (className,ParentID)values(@className,0)";
            }
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }

        public int DeleteNews(int NewsID)
        {
            string sql = "delete from NT_Newsclass where ID=" + NewsID;
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        }
    }
}
