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 VoteComm : DbBase, IVoteComm
    {
        /// <summary>
        /// 添加投票评论
        /// </summary>
        /// <param name="Info">投票评论实体类</param>
        /// <returns>添加成功返回1</returns>
        public int Add(VoteCommInfo Info,int uid)
        {
            SqlConnection Conn = new SqlConnection(DBConfig.CnString);
            Conn.Open();
            SqlTransaction trans = Conn.BeginTransaction();
            try
            {
                SqlParameter[] param = getParameters(Info);
                string Sql = "Insert Into [NT_VoteComm]([UserID],[VoteID],[Content],[PID],[AddTime]) Values(@UserID,@VoteID,@Content,@PID,@AddTime)";
                int n = Convert.ToInt32(DbHelper.ExecuteNonQuery(trans,CommandType.Text, Sql, param));
                //string usql = "select UserID from NT_ToVote where VoteID=" + Info.VoteID;
                //int uID = Convert.ToInt32(DbHelper.ExecuteScalar(trans,CommandType.Text, usql, null));
                if (uid != Info.UserID)
                {
                    Notify(trans, EnumNotifyType.VoteComment, Info.UserID, uid, "评论了投票", Info.VoteID);
                }
                trans.Commit();
                return n;
            }
            catch (SqlException e)
            {
                trans.Rollback();
                throw e;
            }
            finally
            {
                if (Conn.State == ConnectionState.Open)
                    Conn.Close();
            }

        }

        /// <summary>
        /// 回复数量
        /// </summary>
        /// <param name="PID">评论编号</param>
        public int ReplyCnt(int PID)
        {
            string Sql = "Select Count([ID]) From [NT_VoteComm] Where [PID]=" + PID;
            return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, Sql, null));
        }

        /// <summary>
        /// 删除回复
        /// </summary>
        /// <param name="ID">回复编号</param>
        /// <param name="UserID">会员编号</param>
        public void Del(int ID, int UserID)
        {
            string Sql = "Delete From [NT_VoteComm] Where [ID]=" + ID + " And [UserID]=" + UserID;
            DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null);
        }

        /// <summary>
        /// 取得会员评论内容信息
        /// </summary>
        /// <param name="UserID">会员编号</param>
        /// <param name="VoteID">投票编号</param>
        /// <returns>返回投票评论实体类</returns>
        public VoteCommInfo Info(int UserID, int VoteID)
        {
            VoteCommInfo info = new VoteCommInfo();
            string Sql = "Select [ID],[UserID],[VoteID],[Content],[PID],[AddTime] " +
                         "From [NT_VoteComm] " +
                         "Where [UserID]=" + UserID + " And VoteID=" + VoteID + " And [PID]=0";
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            if (rd.Read())
            {
                info.ID = Convert.ToInt32(rd["ID"].ToString());
                info.UserID = Convert.ToInt32(rd["UserID"].ToString());
                info.VoteID = Convert.ToInt32(rd["VoteID"].ToString());
                if (rd["Content"] != DBNull.Value) info.Content = rd["Content"].ToString();
                info.PID = Convert.ToInt32(rd["PID"].ToString());
                info.AddTime = Convert.ToDateTime(rd["AddTime"].ToString());
            }
            rd.Close();
            return info;
        }

        /// <summary>
        /// 取得评论列表
        /// </summary>
        /// <param name="PID">父级编号</param>
        /// <returns></returns>
        public List<VoteCommInfo> List(int PID)
        {
            List<VoteCommInfo> list = new List<VoteCommInfo>();
            string Sql = "Select [ID],[UserID],[VoteID],[Content],[PID],[AddTime] " +
                         "From [NT_VoteComm] " +
                         "Where [PID]=" + PID + " Order By [ID] Asc";
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            while (rd.Read())
            {
                VoteCommInfo info = new VoteCommInfo();
                info.ID = Convert.ToInt32(rd["ID"].ToString());
                info.UserID = Convert.ToInt32(rd["UserID"].ToString());
                info.VoteID = Convert.ToInt32(rd["VoteID"].ToString());
                if (rd["Content"] != DBNull.Value) info.Content = rd["Content"].ToString();
                info.PID = Convert.ToInt32(rd["PID"].ToString());
                info.AddTime = Convert.ToDateTime(rd["AddTime"].ToString());
                list.Add(info);
            }
            rd.Close();
            return list;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Info">投票评论实体类</param>
        /// <returns>返回SQL参数</returns>
        private SqlParameter[] getParameters(VoteCommInfo Info)
        {
            SqlParameter[] param = new SqlParameter[6];

            param[0] = new SqlParameter("@ID", SqlDbType.Int, 4);
            param[0].Value = Info.ID;
            param[1] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[1].Value = Info.UserID;
            param[2] = new SqlParameter("@VoteID", SqlDbType.Int, 4);
            param[2].Value = Info.VoteID;

            param[3] = new SqlParameter("@Content", SqlDbType.NVarChar, 200);
            param[3].Value = Info.Content;
            param[4] = new SqlParameter("@PID", SqlDbType.Int, 4);
            param[4].Value = Info.PID;
            param[5] = new SqlParameter("@AddTime", SqlDbType.DateTime, 8);
            param[5].Value = Info.AddTime;

            return param;
        }
    }
}
