//by JuSNS v1.0系列
//官方站：www.juSNS.com，演示站:www.ju001.com，www.julala.cn
//QQ:655071(技术)，QQ:9170617（客服）
//MSN:simplt@163.com EMail:simplt@163.com
//program design by Simplt.Xie，arjun
//========================================

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 ShareComment : DbBase, IShareComment
    {
        /// <summary>
        /// 添加分享评论
        /// </summary>
        /// <param name="Info">分享评论实体类</param>
        /// <returns>添加成功返回1</returns>
        public int Add(ShareCommentInfo Info)
        {
            SqlConnection Conn = new SqlConnection(DBConfig.CnString);
            SqlTransaction trans = Conn.BeginTransaction();
            Conn.Open();

            try
            {
                Share share = new Share();
                ShareInfo shareinfo = new ShareInfo();
                shareinfo = share.Info(Info.ShareID);

                if (!CheckPrivacy(Conn, shareinfo.Privacy, Info.UserID, shareinfo.UserID))
                    return 0;
                try
                {
                    string Sql = "Update [NT_Share] Set [Comments]=[Comments]+1 Where [ID]=" + Info.ShareID;
                    DbHelper.ExecuteScalar(trans, CommandType.Text, Sql, null);

                    SqlParameter[] param = getParameters(Info);
                    Sql = "Insert Into [NT_ShareComment]" +
                          "([ShareID],[UserID],[Content],[PostTime],[PostIP],[IsLock]) " +
                          "Values(@ShareID,@UserID,@Content,@PostTime,@PostIP,@IsLock);" +
                          "Select @@IDENTITY";
                    int ID = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, Sql, param));
                    string gsql = "select UserID from NT_share where ID=" + ID;
                    int rID = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, gsql, null));
                    JUser ju = new JUser();
                    if (ju.CheckPrivacyInfo(trans, Info.UserID, "ActShareComment"))
                    {
                        UserDyn(trans, 0, 0, "commentShare", Info.UserID, 0, ID, "分享评论", shareinfo.ID.ToString());
                    }
                    int m = Notify(trans, EnumNotifyType.ShareComment, shareinfo.UserID, rID, "评论了你的分享", shareinfo.ID);
                    trans.Commit();
                    User us = new User();
                    us.updateinte(Info.UserID, 1, 0, 0,"评论了分享");
                    return ID;
                }
                catch (SqlException e)
                {
                    trans.Rollback();
                    throw e;
                }
            }
            finally
            {
                if (Conn.State == ConnectionState.Open)
                    Conn.Close();
            }
        }
        /// <summary>
        /// 删除分享评论(后台)
        /// </summary>
        /// <param name="ID">分享评论编号</param>
        /// <returns>删除成功返回1</returns>
        public int Del(int ID)
        {
            SqlConnection Conn = new SqlConnection(DBConfig.CnString);
            Conn.Open();

            SqlTransaction trans = Conn.BeginTransaction();
            try
            {
                string Sql = "Delete From [NT_ShareComment] Where [ID]=" + ID;
                int n = DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                Sql = "delete from NT_Dyn where cID=" + ID + " and dynType='commentShare'";
                DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                trans.Commit();
                return n;
            }
            catch (SqlException e)
            {
                trans.Rollback();
                throw e;
            }
            finally
            {
                if (Conn.State == ConnectionState.Open)
                    Conn.Close();
            }
        }

        /// <summary>
        /// 删除分享评论(前台)
        /// </summary>
        /// <param name="ID">分享评论编号</param>
        /// <param name="UserID">用户编号</param>
        /// <returns>删除成功返回1</returns>
        public int Del(int ID, int UserID)
        {
            SqlConnection Conn = new SqlConnection(DBConfig.CnString);
            Conn.Open();

            SqlTransaction trans = Conn.BeginTransaction();
            try
            {
                string Sql = "Delete From [NT_ShareComment] Where [UserID]=" + UserID + " And [ID]=" + ID;
                int n = DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                Sql = "delete from NT_Dyn where cID=" + ID + " and UserID=" + UserID + " and dynType='commentShare'";
                DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                trans.Commit();
                User us = new User();
                us.updateinte(UserID, 1, 0, 1,"删除了分享评论");
                return n;
            }
            catch (SqlException e)
            {
                trans.Rollback();
                throw e;
            }
            finally
            {
                if (Conn.State == ConnectionState.Open)
                    Conn.Close();
            }
        }

        /// <summary>
        /// 取得分享评论信息
        /// </summary>
        /// <param name="ID">分享评论编号</param>
        /// <returns>返回分享评论实体类</returns>
        public ShareCommentInfo Info(int ID)
        {
            ShareCommentInfo info = new ShareCommentInfo();
            string Sql = "Select [ID],[ShareID],[UserID],[Content],[PostTime],[PostIP],[IsLock] " +
                         "From " +
                         "[NT_ShareComment] " +
                         "Where [ID]=" + ID;
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            if (rd.Read())
            {
                info.ID = Convert.ToInt32(rd["ID"].ToString());
                info.ShareID = Convert.ToInt32(rd["ShareID"].ToString());
                info.UserID = Convert.ToInt32(rd["UserID"].ToString());
                if (rd["Content"] != DBNull.Value) info.Content = rd["Content"].ToString();
                info.PostTime = Convert.ToDateTime(rd["PostTime"].ToString());
                if (rd["PostIP"] != DBNull.Value) info.PostIP = rd["PostIP"].ToString();
                info.IsLock = (EnumIsLock)Convert.ToInt32(rd["IsLock"].ToString());
            }
            rd.Close();
            return info;
        }

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

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

            param[3] = new SqlParameter("@Content", SqlDbType.NVarChar, 255);
            param[3].Value = Info.Content;
            param[4] = new SqlParameter("@PostTime", SqlDbType.DateTime, 8);
            param[4].Value = Info.PostTime;
            param[5] = new SqlParameter("@PostIP", SqlDbType.VarChar, 15);
            param[5].Value = Info.PostIP;

            param[6] = new SqlParameter("@IsLock", SqlDbType.Bit);
            param[6].Value = Info.IsLock;

            return param;
        }

    }
}
