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

namespace JuSNS.SQLServer
{
    public class GBook : DbBase, IGBook
    {
        /// <summary>
        /// 新增留言
        /// </summary>
        /// <param name="id"></param>
        /// <param name="senderId"></param>
        /// <param name="recieverId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        int IGBook.Edit(int id, int senderId, int receiverId, string content, int pid,int isPub)
        {
            SqlConnection cn = new SqlConnection(DBConfig.CnString);
            cn.Open();
            SqlTransaction trans = cn.BeginTransaction();
            try
            {
                string Sql = "insert into NT_gbook (SendID,RecID,ParentID,Content,PostTime,PostIP,IsLock,isPub)";
                Sql += " values (" + senderId + "," + receiverId + "," + pid + ",@Content,'" + DateTime.Now + "','" + Public.GetClientIP() + "',0," + isPub + ");SELECT @@IDENTITY";
                if (id > 0)
                {
                    Sql = "update NT_gbook set SendID=" + senderId + ",RecID=" + receiverId + ",Content=@Content,PostTime='" + DateTime.Now + "',PostIP='" + Public.GetClientIP() + "' where ID=" + id;
                }
                SqlParameter Param = new SqlParameter("@Content", SqlDbType.NVarChar, 200);
                Param.Value = content;
                int n = DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, Param);
                if (n > 0 && id == 0)
                {
                    if (receiverId != senderId)
                    {
                        Notify(trans, EnumNotifyType.Leaveword, senderId, receiverId, "给你留了言。", n);
                    }
                }
                trans.Commit(); return n;
            }
            catch
            {
                trans.Rollback();
                throw;
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }
        /// <summary>
        /// 删除留言
        /// </summary>
        /// <param name="userId">操作用户的ID</param>
        /// <param name="id">要删除的留言ID</param>
        /// <returns></returns>
        int IGBook.Delete(int userId, int id)
        {
            SqlConnection cn = new SqlConnection(DBConfig.CnString);
            cn.Open();
            try
            {
                string Sql = "select count(ID) from NT_gbook where ID=" + id + " and (SendID=" + userId + " or RecID=" + userId + ")";
                int n = (int)DbHelper.ExecuteScalar(cn, CommandType.Text, Sql, null);
                if (n < 1)
                {
                    return -1;
                }
                Sql = "delete from NT_gbook where ID=" + id;
                Sql += "delete from NT_gbook where ParentID=" + id;
                return DbHelper.ExecuteNonQuery(cn, CommandType.Text, Sql, null);
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }
        /// <summary>
        /// 查看是否有权查看某人留言
        /// </summary>
        /// <param name="guestId"></param>
        /// <param name="masterId"></param>
        /// <returns></returns>
        bool IGBook.CheckAuthority(int guestId, int masterId)
        {
            SqlConnection cn = new SqlConnection(DBConfig.CnString);
            cn.Open();
            try
            {
                EnumPrivacy priv = EnumPrivacy.ForFriends;
                string Sql = "select PrivLeaveWord from NT_UserSetting where UserID=" + masterId;
                IDataReader rd = DbHelper.ExecuteReader(cn, CommandType.Text, Sql, null);
                if (rd.Read())
                {
                    if (!rd.IsDBNull(0)) priv = (EnumPrivacy)rd.GetInt32(0);
                }
                rd.Close();
                return CheckPrivacy(cn, priv, guestId, masterId);
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }

        public IDataReader GetRepayBook(int UserID, int ParentID)
        {
            SqlParameter[] Param = new SqlParameter[2];
            Param[0] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            Param[0].Value = UserID;
            Param[1] = new SqlParameter("@ParentID", SqlDbType.Int, 4);
            Param[1].Value = ParentID;
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, "JuSNS_GetRepayBook", Param);
        }
    }
}
