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 Comment : DbBase, IComment
    {
        /// <summary>
        /// 获取评论
        /// </summary>
        /// <param name="UserID">被评论的ID</param>
        /// <param name="DateNum">mini表示心情日记的回复。其他的，写的人补上</param>
        /// <returns></returns>
        public IDataReader GetCommentList(int id, string Type)
        {
            string sql = string.Empty;
            switch (Type)
            { 
                case "mini":
                    sql = "select ID,Commentator,Content,MiniBlogID,PostTime from NT_MoodComment where MiniBlogID=" + id;
                    break;
            }
            return DbHelper.ExecuteReader(CommandType.Text, sql, null);
        }

                /// <summary>
        /// 得到评论数
        /// </summary>
        /// <param name="id">信息ID</param>
        /// <param name="Type">mini表示心情日记。其他的，写的人补上</param>
        /// <returns></returns>
        public int GetCommentCount(int id, string Type)
        {
            string sql = string.Empty;
            switch (Type)
            {
                case "mini":
                    sql = "select count(ID) from NT_MoodComment where MiniBlogID=" + id;
                    break;
            }
            return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, sql, null));
        }

       /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="id">信息ID</param>
        /// <param name="Type">mini表示心情日记。其他的，写的人补上</param>
        /// <returns></returns>
        public void DelComment(int UserID, int id, string Type)
        {
            SqlConnection Conn = new SqlConnection(DBConfig.CnString);
            Conn.Open();
            SqlTransaction trans = Conn.BeginTransaction();
            try
            {
                string ssql = "select MiniBlogID from NT_MoodComment where ID=" + id;
                int bID = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, ssql, null));

                string sql = string.Empty;
                switch (Type)
                {
                    case "mini":
                        sql = "delete from NT_MoodComment where Commentator=" + UserID + " and ID=" + id;
                        break;
                }
                DbHelper.ExecuteNonQuery(trans,CommandType.Text, sql, null);

                if (bID > 0)
                {
                    string usql = "update NT_Mood set Comments=Comments-1 where ID=" + bID;
                    DbHelper.ExecuteNonQuery(trans, CommandType.Text, usql, null);
                }
                trans.Commit();
                User us = new User();
                us.updateinte(UserID, 1, 0,1,"删除心情日记评论");
            }
            catch (SqlException e)
            {
                trans.Rollback();
                throw e;
            }
            finally
            {
                if (Conn.State == ConnectionState.Open)
                    Conn.Close();
            }

        }

        /// <summary>
        /// 插入评论
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <param name="Type">mini表示心情日记。其他的，写的人补上</param>
        /// <param name="InfoID">被评论的ID</param>
        /// <param name="Content">评论内容</param>
        /// <param name="CommentID">回复的ID</param>
        /// <returns></returns>
        public int AddComment(int UserID, string Type, int InfoID, string Content, int CommentID)
        {
            SqlConnection Conn = new SqlConnection(DBConfig.CnString);
            Conn.Open();
            SqlTransaction trans = Conn.BeginTransaction();
            try
            {
                string sql = string.Empty;
                switch (Type)
                {
                    case "mini":
                        sql = "insert into  NT_MoodComment (Commentator,MiniBlogID,Content,CommentID,PostTime,PostIP) values (" + UserID + "," + InfoID + ",'" + Content + "'," + CommentID + ",'" + DateTime.Now + "','" + JuSNS.Common.Public.GetClientIP() + "')";
                        break;
                }
                switch (Type)
                {
                    case "mini":
                        string miniSQL = "select UserID from NT_Mood where ID=" + InfoID;
                        int miniUser = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, miniSQL, null));
                        int n = Notify(trans, EnumNotifyType.MiniComment, UserID, miniUser, "评论了您的心情日记", InfoID);
                        break;
                }
                int CommentN= Convert.ToInt32(DbHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null));
                string usql = "update NT_Mood set Comments=Comments+1 where ID=" + InfoID;
                DbHelper.ExecuteNonQuery(trans, CommandType.Text, usql, null);
                trans.Commit();
                User us = new User();
                us.updateinte(UserID, 1, 0, 0,"发表心情日记评论");
                return CommentN;
            }
            catch (SqlException e)
            {
                trans.Rollback();
                throw e;
            }
            finally
            {
                if (Conn.State == ConnectionState.Open)
                    Conn.Close();
            }
        }
    }
}
