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

namespace JuSNS.SQLServer
{
    public class Notepad : DbBase, INotepad
    {
        /// <summary>
        /// 获取记事本内容
        /// </summary>
        /// <param name="UserID">会员编号</param>
        /// <returns></returns>
        public IDataReader GetContent(int UserID, int NoteID)
        {
            string sql = "select * from NT_NotePad where ID=" + NoteID + " and UserID=" + UserID;
            return DbHelper.ExecuteReader(CommandType.Text, sql, null);
        }
        /// <summary>
        /// 保存记事本信息
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <param name="Content">内容</param>
        /// <returns></returns>
        public int SaveNote(int UserID, string title, string Content, DateTime txtime, int flg, int noteID)
        {
            SqlConnection sqlConn = new SqlConnection(DBConfig.CnString);
            sqlConn.Open();
            try
            {
                SqlParameter[] param = new SqlParameter[4];
                param[0] = new SqlParameter("@Content", SqlDbType.NVarChar, 2000);
                param[0].Value = Content;
                param[1] = new SqlParameter("@UserID", SqlDbType.Int, 4);
                param[1].Value = UserID;
                param[2] = new SqlParameter("@Time", SqlDbType.DateTime, 8);
                param[2].Value = txtime;
                param[3] = new SqlParameter("@Title", SqlDbType.NVarChar, 50);
                param[3].Value = title;
                string sqlInsert = string.Empty;
                if (flg == 0)
                {
                    sqlInsert = "insert into NT_NotePad (UserID,Content,LastPostTime,LastPostIP,IsLock,title,TXTime) values (@UserID,@Content,'" + DateTime.Now + "','" + JuSNS.Common.Public.GetClientIP() + "',0,@Title,@Time)";
                }
                else if (flg == 1)
                {
                    sqlInsert = "update NT_NotePad set Content=@Content,LastPostTime='" + DateTime.Now + "',LastPostIP='" + JuSNS.Common.Public.GetClientIP() + "',title=@Title,TXTime=@Time where UserID=@UserID and  ID=" + noteID;
                }
                int n = DbHelper.ExecuteNonQuery(sqlConn, CommandType.Text, sqlInsert, param);
                return n;
            }
            catch (SqlException sqlEx)
            {
                throw sqlEx;
            }
            finally
            {
                if (sqlConn.State == ConnectionState.Open)
                    sqlConn.Close();
            }
        }
        /// <summary>
        /// 获取Notepad实例
        /// </summary>
        /// <param name="MiniID">NotepadID</param>
        /// <returns></returns>
        public NotePadInfo GetNoteInfo(int NoteID)
        {
            NotePadInfo NoteInfo = new NotePadInfo();
            NoteInfo.ID = NoteID;
            string sql = "select * from NT_NotePad where ID=" + NoteID.ToString();
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, sql, null);
            if (rd.Read())
            {
                NoteInfo.UserID = Convert.ToInt32(rd["UserID"].ToString());
                NoteInfo.Content = Convert.ToString(rd["Content"].ToString());
                NoteInfo.LastPostTime = Convert.ToDateTime(rd["LastPostTime"].ToString());
                NoteInfo.LastPostIP = Convert.ToString(rd["LastPostIP"].ToString());
                NoteInfo.IsLock = Convert.ToBoolean(rd["IsLock"].ToString());
            }
            rd.Close();
            return NoteInfo;
        }

        public int DelNote(int UserID, int NoteID)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[0].Value = UserID;
            param[1] = new SqlParameter("@NoteID", SqlDbType.Int, 4);
            param[1].Value = NoteID;
            return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, "JuSNS_DelNote", param);
        }

        /// <summary>
        /// 检查是否有查看的权限
        /// </summary>
        /// <param name="guest"></param>
        /// <param name="masterid"></param>
        /// <returns></returns>
        bool INotepad.Authority(int guest, int masterid)
        {
            return true;
        }
    }
}
