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

namespace JuSNS.SQLServer
{
    /// <summary>
    /// 系统留言类
    /// </summary>
    public class SysNotepad : DbBase, ISysNotepad
    {
        /// <summary>
        /// 添加系统留言
        /// </summary>
        /// <param name="Info">系统留言实体类</param>
        /// <returns>添加成功返回1</returns>
        public int Add(SysNotepadInfo Info)
        {
            SqlParameter[] param = getParameters(Info);
            string Sql = "Insert Into [NT_SysNotepad]([UserID],[Content],[PostTime],[PostIP],[ReplyContent]," +
                         "[Replier],[ReplyTime],[ReplyIP]) " +
                         "Values" +
                         "(@UserID,@Content,@PostTime,@PostIP,null,0,null,null)";
            return Convert.ToInt32(DbHelper.ExecuteNonQuery(CommandType.Text, Sql, param));
        }

        /// <summary>
        /// 回复(修改)系统留言
        /// </summary>
        /// <param name="Info">系统留言实体类</param>
        /// <returns>回复(修改)成功返回1</returns>
        public int Reply(SysNotepadInfo Info)
        {
            SqlParameter[] param = getParameters(Info);
            string Sql = "insert into [NT_Gbook] (SendID,RecID,ParentID,Content,PostTime,PostIP,IsLock,isPub)values(@SendID,@RecID,@ParentID,@Content,@PostTime,@PostIP,0,0)";
            return Convert.ToInt32(DbHelper.ExecuteNonQuery(CommandType.Text, Sql, param));
        }

        /// <summary>
        /// 删除系统留言
        /// </summary>
        /// <param name="ID">留言编号</param>
        /// <returns>删除成功返回1</returns>
        public int Del(int ID)
        {
            string Sql = "Delete From [NT_Gbook] Where [ID]=" + ID;
            return Convert.ToInt32(DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null));
        }

        /// <summary>
        /// 取得留言详细信息
        /// </summary>
        /// <param name="ID">留言编号</param>
        /// <returns>返回系统留言实体类</returns>
        public SysNotepadInfo Info(int ID)
        {
            SysNotepadInfo info = new SysNotepadInfo();
            string Sql = "Select [ID],[SendID],[Content],[PostTime],[PostIP],ParentID,RecID" +
                         " From " +
                         "[NT_gbook] " +
                         "Where [ID]=" + ID;
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            if (rd.Read())
            {
                info.ID = Convert.ToInt32(rd["ID"].ToString());
                info.SendID = Convert.ToInt32(rd["SendID"]);
                info.ParentID = Convert.ToInt32(rd["ParentID"]);
                if (rd["RecID"] != DBNull.Value) info.RecID = Convert.ToInt32(rd["RecID"].ToString());
                if (rd["Content"] != DBNull.Value) info.Content = rd["Content"].ToString();
                if (rd["PostTime"] != DBNull.Value) info.PostTime = Convert.ToDateTime(rd["PostTime"].ToString());
                if (rd["PostIP"] != DBNull.Value) info.PostIP = rd["PostIP"].ToString();
            }
            rd.Close();
            return info;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Info">群组实体类</param>
        /// <returns>返回SQL参数</returns>
        private SqlParameter[] getParameters(SysNotepadInfo Info)
        {
            SqlParameter[] param = new SqlParameter[7];

            param[0] = new SqlParameter("@ID", SqlDbType.Int, 4);
            param[0].Value = Info.ID;
            param[1] = new SqlParameter("@SendID", SqlDbType.Int, 4);
            param[1].Value = Info.SendID;
            param[2] = new SqlParameter("@Content", SqlDbType.NVarChar, 200);
            param[2].Value = Info.Content;

            param[3] = new SqlParameter("@PostTime", SqlDbType.DateTime, 8);
            param[3].Value = Info.PostTime;
            param[4] = new SqlParameter("@PostIP", SqlDbType.VarChar, 15);
            param[4].Value = Info.PostIP;
            param[5] = new SqlParameter("@RecID", SqlDbType.Int, 4);
            param[5].Value = Info.RecID;
            param[6] = new SqlParameter("@ParentID", SqlDbType.Int, 4);
            param[6].Value = Info.ParentID;

            return param;
        }

    }
}
