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;

namespace JuSNS.SQLServer
{
    public class Movie : DbBase, IMovie
    {

         /// <summary>
        /// 取得排行榜数据
        /// </summary>
        /// <param name="Num">取出数量</param>
        /// <param name="flg">0hits,1comment,2rand</param>
        /// <returns></returns>
        public DataTable GetTopMovie(int Num, int flg)
        {
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@Num", SqlDbType.Int, 4);
            param[0].Value = Num;
            param[1] = new SqlParameter("@flg", SqlDbType.Int, 4);
            param[1].Value = flg;
            return DbHelper.ExecuteTable(CommandType.StoredProcedure, "JuSNS_GetTopMovie", param);
        }

        /// <summary>
        /// 取得随机电影
        /// </summary>
        /// <returns>返回数据表</returns>
        public DataTable RandMovie()
        {
            string sql = "select Top 4 MovieID,Title,PosterFile from NT_Movie Order By Rand(MovieID)";
            return DbHelper.ExecuteTable(CommandType.Text, sql, null);
        }

        /// <summary>
        /// 取得好友想看的电影
        /// </summary>
        /// <param name="UsreID">会员编号</param>
        /// <returns>返回数据表</returns>
        public DataTable FriendThinkLookMovie(int UsreID)
        {
            string Sql = "Select Top 200 a.MovieID,a.Title," +
                         "(Select Count(ID) From NT_MovieComment Where UserID In " +
                         "(Select FriendID From NT_Friend Where UserID=" + UsreID + ") And Score=0) As Cnt " +
                         "From NT_Movie AS a INNER JOIN NT_MovieComment AS b " +
                         "ON a.[MovieID]=b.[MovieID] INNER JOIN NT_User AS c " +
                         "ON b.[UserID]=c.[UserID] where b.[UserID] In (Select FriendID From NT_Friend Where UserID=" + UsreID + ") " +
                         "And a.Score=0";
            return DbHelper.ExecuteTable(CommandType.Text, Sql, null);
        }

        /// <summary>
        /// 取得朋友对某部电影的评论
        /// </summary>
        /// <param name="UserID">会员编号</param>
        /// <param name="MovieID">电影编号</param>
        /// <returns>返回数据表</returns>
        public DataTable FriendMovieComm(int UserID, int MovieID)
        {
            string Sql = "Select a.MovieID,a.Title,a.PosterFile,b.Score,b.Comment,c.UserID,c.UserName,d.FilePath " +
                         "From NT_Movie AS a INNER JOIN NT_MovieComment AS b ON a.[MovieID]=b.[MovieID] " +
                         "INNER JOIN NT_User AS c ON b.[UserID]=c.[UserID] " +
                         "Left Join Nt_Photo As d On c.Portrait=d.PhotoID And d.IsLock=0 where b.[UserID] In " +
                         "(Select FriendID From NT_Friend Where UserID=" + UserID + ") " +
                         "And a.MovieID=" + MovieID + " order by b.[PostTime] desc";
            return DbHelper.ExecuteTable(CommandType.Text, Sql, null);
        }

        /// <summary>
        /// 取得我想看的电影
        /// </summary>
        /// <param name="UserID">会员编号</param>
        /// <returns>返回数据表</returns>
        public DataTable MyThinkLookMovie(int UserID)
        {
            string Sql = "Select Top 200 a.MovieID,a.Title " +
                         "From NT_Movie AS a INNER JOIN NT_MovieComment AS b " +
                         "ON a.[MovieID]=b.[MovieID] where b.[UserID]=" + UserID + " " +
                         "And b.Score=0";
            return DbHelper.ExecuteTable(CommandType.Text, Sql, null);
        }

        /// <summary>
        /// 取得好友排行榜
        /// </summary>
        /// <param name="UserID">会员编号</param>
        /// <returns>返回数据表</returns>
        public DataTable getTopFriendMovie(int UserID)
        {
            string Sql = "select a.userid,count(id) as cnt,b.Username from NT_MOVIECOMMENT" +
                         " as a inner join Nt_User as b on a.UserID=b.UserID where " +
                         "((a.UserID In(Select FriendID From Nt_Friend Where userID=" + UserID + ")) or " +
                         "a.UserID=" + UserID + ") and a.Score>0 group by a.userid,b.Username order by cnt desc";
            DataTable dt = DbHelper.ExecuteTable(CommandType.Text, Sql, null);
            dt.Columns.Add("Num", typeof(System.Int32));
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dt.Rows[i]["Num"] = i + 1;
            }
            return dt;
        }

        /// <summary>
        /// 添加电影类型
        /// </summary>
        /// <param name="SortName">类型名称</param>
        /// <returns>添加成功返回1</returns>
        public int AddMovieSort(string SortName)
        {
            SqlParameter param = new SqlParameter("@SortName", SqlDbType.NVarChar, 50);
            param.Value = SortName;
            return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, "JuSNS_AddMovieSort", param);
        }

        /// <summary>
        /// 取得电影类型列表
        /// </summary>
        /// <returns>返回数据表</returns>
        public DataTable getDictMovieSortList()
        {
            return DbHelper.ExecuteTable(CommandType.StoredProcedure, "JuSNS_GetDictMovieSortList", null);
        }

        /// <summary>
        /// 修改电影类型
        /// </summary>
        /// <param name="SortName">类型名称</param>
        /// <param name="ID">类型编号</param>
        /// <returns>修改成功返回1</returns>
        public int EditMovieSort(string SortName, int ID)
        {
            SqlParameter param = new SqlParameter("@SortName", SqlDbType.NVarChar, 50);
            param.Value = SortName;
            string Sql = string.Empty;
            if (ID > 0)
            {
                Sql = "Update NT_MovieSort Set SortName=@SortName Where ID=" + ID;
            }
            else
            {
                Sql = "insert into NT_MovieSort (SortName)values(@SortName)";
            }
            return DbHelper.ExecuteNonQuery(CommandType.Text, Sql, param);
        }

        /// <summary>
        /// 取得电影类型信息
        /// </summary>
        /// <param name="ID">电影类型编号</param>
        /// <returns>返回电影类型实体类</returns>
        public Dict_MovieSortInfo MovieSortInfo(int ID)
        {
            Dict_MovieSortInfo Info = new Dict_MovieSortInfo();
            string Sql = "select ID,SortName from NT_MovieSort where ID=" + ID;
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            while (rd.Read())
            {
                Info.ID = Convert.ToInt32(rd["ID"]);
                if (rd["SortName"] != DBNull.Value) Info.SortName = rd["SortName"].ToString();
            }
            rd.Close();
            return Info;
        }

        /// <summary>
        /// 取得电影类型所对应的电影
        /// </summary>
        /// <param name="SortID">类型编号</param>
        /// <returns>返回数据表</returns>
        public DataTable getMoviePostFileForSort(int SortID)
        {
            string Sql = "Select PosterFile From NT_Movie Where MovieID In(Select MovieID From NT_MovieSort Where SortID=" + SortID + ")";
            return DbHelper.ExecuteTable(CommandType.Text, Sql, null);
        }

        /// <summary>
        /// 取得电影类型所对应的电影海报
        /// </summary>
        /// <param name="SortID">类型编号</param>
        /// <returns>返回数据表</returns>
        public DataTable getMovieUpFileForSort(int SortID)
        {
            string Sql = "Select FileName From NT_MoviePoster Where MovieID In(Select MovieID From NT_MovieSort Where ID=" + SortID + ")";
            return DbHelper.ExecuteTable(CommandType.Text, Sql, null);
        }

        /// <summary>
        /// 删除电影类型
        /// </summary>
        /// <param name="ID">类型编号</param>
        /// <returns>删除成功返回1</returns>
        public int DelMovieSort(int ID)
        {
            SqlConnection sqlConn = new SqlConnection(DBConfig.CnString);
            sqlConn.Open();
            SqlTransaction trans = sqlConn.BeginTransaction();
            try
            {
                string Sql = "Delete From NT_MovieSort Where ID=" + ID;
                int n = DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                Sql = "Delete From NT_Movie Where MovieID In(Select MovieID From NT_MovieSort Where ID=" + ID + ")";
                DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                Sql = "Delete From NT_MovieComment Where MovieID In(Select MovieID From NT_MovieSort Where ID=" + ID + ")";
                DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                Sql = "Delete From NT_MoviePoster Where MovieID In(Select MovieID From NT_MovieSort Where ID=" + ID + ")";
                DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                trans.Commit();
                return n;
            }
            catch (SqlException sqlEX)
            {
                trans.Rollback();
                throw sqlEX;
            }
            finally
            {
                if (sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                }
            }
        }

        /// <summary>
        /// 取得电影的类型
        /// </summary>
        /// <param name="MovieID">电影编号</param>
        /// <returns>返回数据表</returns>
        public DataTable getMovieGut(int MovieID)
        {
            string Sql = "Select a.id,a.SortName from NT_MovieSort as a inner join " +
                         "NT_MovieSort as b on a.id=b.SortID where b.movieid=" + MovieID;
            return DbHelper.ExecuteTable(CommandType.Text, Sql, null);
        }

        /// <summary>
        /// 添加电影
        /// </summary>
        /// <param name="Info">电影实体类</param>
        /// <returns>添加成功返回电影编号</returns>
        public int AddMovie(MovieInfo Info)
        {
            SqlParameter[] param = getMovieParameters(Info);
            string Sql = "Insert Into NT_Movie(Title,Director,Billing,YearOnShow,Gut,Comments,Seen," +
                         "Wanna,Wonna,Score,PosterFile,zt_dy,zt_dy1,zt_url,zt_url1,Hits,UserID,CreatTime,Comment,SortID) " +
                         "Values(@Title,@Director,@Billing,@YearOnShow,@Gut,@Comments,@Seen,@Wanna,@Wonna,@Score," +
                         "@PosterFile,@ztdy,@ztdy1,@zturl,@zturl1,@Hits,@UserID,@CreatTime,0,@SortID);" +
                         "Select @@IDENTITY";
            return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, Sql, param));
        }

        /// <summary>
        /// 修改电影
        /// </summary>
        /// <param name="Info">电影实体类</param>
        /// <returns>修改成功返回1</returns>
        public int EditMovie(MovieInfo Info)
        {

            SqlConnection sqlConn = new SqlConnection(DBConfig.CnString);
            sqlConn.Open();
            SqlTransaction trans = sqlConn.BeginTransaction();
            try
            {
                SqlParameter[] param = getMovieParameters(Info);
                string Sql = "Update NT_Movie Set Title=@Title,Director=@Director,Billing=@Billing," +
                             "YearOnShow=@YearOnShow,Gut=@Gut,PosterFile=@PosterFile," +
                             "zt_dy=@ztdy,zt_dy1=@ztdy1,zt_url=@zturl,zt_url1=@zturl1,hits=@Hits,CreatTime=@CreatTime,SortID=@SortID Where MovieID=@MovieID";
                int n = Convert.ToInt32(DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param));

                trans.Commit();
                return n;
            }
            catch (SqlException sqlEX)
            {
                trans.Rollback();
                throw sqlEX;
            }
            finally
            {
                if (sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                }
            }
        }

        /// <summary>
        /// 删除电影
        /// </summary>
        /// <param name="MovieID">电影编号</param>
        /// <returns>删除成功返回1</returns>
        public int DelMovie(int MovieID)
        {
            SqlConnection sqlConn = new SqlConnection(DBConfig.CnString);
            sqlConn.Open();
            SqlTransaction trans = sqlConn.BeginTransaction();
            try
            {
                string Sql = "Delete From NT_Movie Where MovieID=" + MovieID;
                int n = DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                Sql = "Delete From NT_MovieComment Where MovieID=" + MovieID;
                DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                trans.Commit();
                return n;
            }
            catch (SqlException sqlEX)
            {
                trans.Rollback();
                throw sqlEX;
            }
            finally
            {
                if (sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                }
            }
        }

        /// <summary>
        /// 添加电影所属的类型
        /// </summary>
        /// <param name="Info">电影所属类型实体类</param>
        /// <returns>添加成功返回1</returns>
        public int AddSortForMovie(MovieSortInfo Info)
        {
            SqlParameter[] param = getMovieSortParameters(Info);
            string Sql = "Insert Into NT_MovieSort(MovieID,SortID,PostTime,PostIP,CreatorID) " +
                         "Values(@MovieID,@SortID,@PostTime,@PostIP,@CreatorID)";
            return Convert.ToInt32(DbHelper.ExecuteNonQuery(CommandType.Text, Sql, param));
        }

        /// <summary>
        /// 取得电影所对应的电影类型编号
        /// </summary>
        /// <param name="movieid">电影编号</param>
        /// <returns>返回数据表</returns>
        public DataTable getSortForMovie(int movieid)
        {
            string Sql = "Select SortID From NT_MovieSort Where MovieID=" + movieid;
            return DbHelper.ExecuteTable(CommandType.Text, Sql, null);
        }

        /// <summary>
        /// 取得电影类型所对应的电影编号
        /// </summary>
        /// <param name="SortID">电影类型编号</param>
        /// <returns>返回数据表</returns>
        public DataTable getMovieForSort(int SortID)
        {
            string Sql = "Select MovieID From NT_MovieSort Where SortID=" + SortID;
            return DbHelper.ExecuteTable(CommandType.Text, Sql, null);
        }

        /// <summary>
        /// 取得会员所上传的此电影海报地址
        /// </summary>
        /// <param name="movieid">电影编号</param>
        /// <returns>返回数据表</returns>
        public DataTable getMoviePoster(int movieid)
        {
            string Sql = "Select FileName From NT_MoviePoster Where MovieID=" + movieid;
            return DbHelper.ExecuteTable(CommandType.Text, Sql, null);
        }

        /// <summary>
        /// 修改电影海报
        /// </summary>
        /// <param name="MovieID">电影编号</param>
        /// <param name="PosterFile">海报地址</param>
        /// <returns>修改成功返回1</returns>
        public int SetPosterFile(int MovieID, string PosterFile)
        {
            SqlParameter param = new SqlParameter("@PosterFile", SqlDbType.NVarChar, 255);
            param.Value = PosterFile;
            string Sql = "Update NT_Movie Set PosterFile=@PosterFile Where MovieID=" + MovieID;
            return Convert.ToInt32(DbHelper.ExecuteNonQuery(CommandType.Text, Sql, param));
        }


        /// <summary>
        /// 保存评论
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <param name="MovieID">电影ID</param>
        /// <returns></returns>
        public int SaveComment(int UserID, int MovieID, string Comment)
        {
            SqlConnection sqlConn = new SqlConnection(DBConfig.CnString);
            sqlConn.Open();
            SqlTransaction trans = sqlConn.BeginTransaction();
            try
            {
                SqlParameter param = new SqlParameter("@Comment", SqlDbType.NVarChar, 255);
                param.Value = Comment;
                string sqlIsExist = "select ID from NT_MovieComment where UserID=" + UserID + " and MovieID=" + MovieID;
                int ID = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, sqlIsExist, null));
                if (ID > 0)//如果有过评论则更新，否则insert新记录
                {
                    string sqlUpdate = "update NT_MovieComment " +
                                       "set Comment=@Comment,PostTime='" + DateTime.Now.ToString() + "'," +
                                       "PostIP='" + JuSNS.Common.Public.GetClientIP() + "' where ID=" + ID;
                    DbHelper.ExecuteNonQuery(trans, CommandType.Text, sqlUpdate, param);
                }
                else
                {
                    string sqlInsert = "insert into NT_MovieComment" +
                                        "(MovieID,UserID,Score,Comment,PostTime,PostIP) " +
                                        "values" +
                                        " (" + MovieID + "," + UserID + ",0,@Comment," +
                                        "'" + DateTime.Now.ToString() + "','" + JuSNS.Common.Public.GetClientIP() + "');Select @@IDENTITY";
                    int cid = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, sqlInsert, param));

                    //更新评论数
                    string ucommsql = "update NT_Movie set comment=comment+1 where MovieID=" + MovieID;
                    DbHelper.ExecuteNonQuery(CommandType.Text, ucommsql, null);
                    JUser ju = new JUser();
                    if (ju.CheckPrivacyInfo(trans, UserID, "ActMovieComment"))
                    {
                        UserDyn(trans, 0, 0, "commentmovie", UserID, 0, cid, "评论了电影", cid.ToString());
                    }
                }
                trans.Commit();
                return 1;
            }
            catch (SqlException sqlEX)
            {
                trans.Rollback();
                throw sqlEX;
            }
            finally
            {
                if (sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                }
            }
        }

        /// <summary>
        /// 取得电影的分数
        /// </summary>
        /// <param name="UserID">会员编号</param>
        /// <param name="MovieID">电影编号</param>
        /// <returns>如果存在此记录返回分数,不存在返回-2</returns>
        public int getScore(int UserID, int MovieID)
        {
            string Sql = "Select Count(ID) From NT_MovieComment Where MovieID=" + MovieID + " And UserID=" + UserID;
            int n = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, Sql, null));
            if (n > 0)
            {
                Sql = "Select Score From NT_MovieComment Where MovieID=" + MovieID + " And UserID=" + UserID;
                n = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, Sql, null));
            }
            else
                n = -2;
            return n;
        }

        /// <summary>
        /// 取得自已对某部电影的评论
        /// </summary>
        /// <param name="UserID">会员编号</param>
        /// <param name="MovieID">电影编号</param>
        /// <returns>返回评论信息</returns>
        public string getComment(int UserID, int MovieID)
        {
            string s_Comment = null;
            string Sql = "Select Count(ID) From NT_MovieComment Where MovieID=" + MovieID + " And UserID=" + UserID;
            int n = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, Sql, null));
            if (n > 0)
            {
                Sql = "Select Comment From NT_MovieComment Where MovieID=" + MovieID + " And UserID=" + UserID;
                s_Comment = Convert.ToString(DbHelper.ExecuteScalar(CommandType.Text, Sql, null));
            }
            return s_Comment;
        }

        /// <summary>
        /// 保存电影分数
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <param name="MovieID">电影ID</param>
        /// <param name="Score">电影分数</param>
        /// <returns></returns>
        public int SaveScore(int UserID, int MovieID, int Score)
        {
            string sqlIsExist = "select ID from NT_MovieComment " +
                                "where UserID=" + UserID + " and MovieID=" + MovieID;
            int ID = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, sqlIsExist, null));

            string Sql = "Select score From NT_MovieComment Where MovieID=" + MovieID + " And UserID=" + UserID;
            int score = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, Sql, null));
            if (score <= 0)
            {
                Sql = "update NT_Movie set Seen=Seen+1 where MovieID=" + MovieID;
                DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null);
            }

            if (ID > 0)//如果有过评论则更新，否则insert新记录
            {
                string sqlScore = "update NT_MovieComment set Score=" + Score + " where ID=" + ID;
                DbHelper.ExecuteNonQuery(CommandType.Text, sqlScore, null);
            }
            else
            {
                string sqlScore = "insert into NT_MovieComment" +
                                  "(MovieID,UserID,Score,Comment,PostTime,PostIP) " +
                                  "values " +
                                  "(" + MovieID + "," + UserID + "," + Score + ",''," +
                                  "'" + DateTime.Now.ToString() + "','" + JuSNS.Common.Public.GetClientIP() + "')";
                DbHelper.ExecuteNonQuery(CommandType.Text, sqlScore, null);
            }

            //更新电影平均分                
            UpdateScore(MovieID);
            return 1;
        }

        /// <summary>
        /// 更新电影评分
        /// </summary>
        /// <param name="MovieID"></param>
        protected void UpdateScore(int MovieID)
        {
            string sqlGetScore = "select Score from NT_MovieComment where Score>0 And MovieID=" + MovieID;
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, sqlGetScore, null);
            int num = 0;
            int i = 0;
            while (rd.Read())
            {
                num += Convert.ToInt32(rd["Score"]);
                i++;
            }
            rd.Close();
            if (num != 0)
                num = num / i;
            string sqlUpAve = "update NT_Movie set Score=" + num + " where MovieID=" + MovieID;
            DbHelper.ExecuteNonQuery(CommandType.Text, sqlUpAve, null);
        }

        /// <summary>
        /// 更新看过人数
        /// </summary>
        /// <param name="trans"></param>
        protected void UpdateSeen(SqlTransaction trans, int MovieID, int UserID)
        {
            string Sql = "Select score From NT_MovieComment Where MovieID=" + MovieID + " And UserID=" + UserID;
            int score = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, Sql, null));
            if (score >= 1)
            {
                Sql = "update NT_Movie set Seen=Seen-1 where MovieID=" + MovieID;
                DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
            }
        }


        /// <summary>
        /// 更新想看该电影的人数
        /// </summary>
        /// <param name="MovieID">电影ID</param>
        /// <param name="UserID">会员编号</param>
        /// <param name="tag">true为增加1，false为减少1</param>
        /// <returns></returns>
        public int UpdateWanna(int MovieID, int UserID, bool tag)
        {
            SqlConnection sqlConn = new SqlConnection(DBConfig.CnString);
            sqlConn.Open();
            SqlTransaction trans = sqlConn.BeginTransaction();
            try
            {
                string Sql = "Select Count(ID) From NT_MovieComment Where MovieID=" + MovieID + " And UserID=" + UserID;
                int n = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, Sql, null));
                if (tag)
                {
                    //检查是否存在记录
                    if (n > 0)
                    {
                        UpdateSeen(trans, MovieID, UserID);
                        //更新评级状态
                        Sql = "Update NT_MovieComment Set Score=0 Where MovieID=" + MovieID + " And UserID=" + UserID;
                        DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                    }
                    else
                    {
                        //不存在记录直接添加
                        Sql = "Insert Into NT_MovieComment(MovieID,UserID,Score,PostTime,PostIP) Values" +
                              "(" + MovieID + "," + UserID + ",0,'" + DateTime.Now + "','" + JuSNS.Common.Public.GetClientIP() + "')";
                        DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                    }
                    //更新想看人数
                    Sql = "update NT_Movie set Wanna=Wanna+1 where MovieID=" + MovieID;
                    DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                }
                else
                {
                    UpdateSeen(trans, MovieID, UserID);
                    //更新评级状态
                    Sql = "Update NT_MovieComment Set Score=-3 Where MovieID=" + MovieID + " And UserID=" + UserID;
                    DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                    //更新想看人数
                    Sql = "update NT_Movie set Wanna=Wanna-1 where MovieID=" + MovieID;
                    DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                }
                trans.Commit();
                UpdateScore(MovieID);
                return n;
            }
            catch (SqlException sqlEX)
            {
                trans.Rollback();
                throw sqlEX;
            }
            finally
            {
                if (sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                }
            }
        }
        /// <summary>
        /// 更新不想看该电影的人数
        /// </summary>
        /// <param name="MovieID">电影ID</param>
        /// <param name="tag">true为增加1，false为减少1</param>
        /// <returns></returns>
        public int UpdateWonna(int MovieID, int UserID, bool tag)
        {
            SqlConnection sqlConn = new SqlConnection(DBConfig.CnString);
            sqlConn.Open();
            SqlTransaction trans = sqlConn.BeginTransaction();
            try
            {
                string Sql = "Select Count(ID) From NT_MovieComment Where MovieID=" + MovieID + " And UserID=" + UserID;
                int n = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, Sql, null));
                if (tag)
                {
                    //检查是否存在记录
                    if (n > 0)
                    {
                        UpdateSeen(trans, MovieID, UserID);
                        //更新评级状态
                        Sql = "Update NT_MovieComment Set Score=-1 Where MovieID=" + MovieID + " And UserID=" + UserID;
                        DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                    }
                    else
                    {
                        //不存在记录直接添加
                        Sql = "Insert Into NT_MovieComment(MovieID,UserID,Score,PostTime,PostIP) Values" +
                              "(" + MovieID + "," + UserID + ",-1,'" + DateTime.Now + "','" + JuSNS.Common.Public.GetClientIP() + "')";
                        DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                    }
                    //更新不想看人数
                    Sql = "update NT_Movie set Wonna=Wonna+1 where MovieID=" + MovieID;
                    DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                }
                else
                {
                    UpdateSeen(trans, MovieID, UserID);
                    //更新评级状态
                    Sql = "Update NT_MovieComment Set Score=-3 Where MovieID=" + MovieID + " And UserID=" + UserID;
                    DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                    //更新不想看人数
                    Sql = "update NT_Movie set Wonna=Wonna-1 where MovieID=" + MovieID;
                    DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                }
                trans.Commit();
                UpdateScore(MovieID);
                return n;
            }
            catch (SqlException sqlEX)
            {
                trans.Rollback();
                throw sqlEX;
            }
            finally
            {
                if (sqlConn.State == ConnectionState.Open)
                {
                    sqlConn.Close();
                }
            }
        }

        /// <summary>
        /// 上传电影海报
        /// </summary>
        /// <param name="Info">电影海报实体类</param>
        /// <returns>如果上传成功返回1</returns>
        public int AddMoviePoster(MoviePosterInfo Info)
        {
            SqlParameter[] param = getMoviePosterParameters(Info);
            string Sql = "Insert Into NT_MoviePoster([MovieID],[UserID],[PostTime],[PostIP],[FileName],isLock) Values" +
                         "(@MovieID,@UserID,@PostTime,@PostIP,@FileName,@IsLock)";
            return Convert.ToInt32(DbHelper.ExecuteNonQuery(CommandType.Text, Sql, param));
        }

        /// <summary>
        /// 获取电影类实例
        /// </summary>
        /// <param name="MovieID">电影ID</param>
        /// <returns></returns>
        public MovieInfo GetMovieInfo(int MovieID)
        {
            MovieInfo MovInfo = new MovieInfo();
            MovInfo.MovieID = MovieID;
            string sql = "select *,(select count(id) from NT_movieposter where NT_movieposter.MovieID=NT_Movie.MovieID and islock=0) as MoviePic from NT_Movie where MovieID=" + MovieID;
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, sql, null);
            if (rd.Read())
            {
                MovInfo.Title = Convert.ToString(rd["Title"]);
                MovInfo.Director = Convert.ToString(rd["Director"]);
                MovInfo.Billing = Convert.ToString(rd["Billing"]);
                if (rd["YearOnShow"] != null) MovInfo.YearOnShow = Convert.ToInt32(rd["YearOnShow"]);
                MovInfo.Gut = Convert.ToString(rd["Gut"]);
                if (rd["Comments"] != null) MovInfo.Comments = Convert.ToInt32(rd["Comments"]);
                if (rd["Seen"] != null) MovInfo.Seen = Convert.ToInt32(rd["Seen"]);
                if (rd["Wanna"] != null) MovInfo.Wanna = Convert.ToInt32(rd["Wanna"]);
                if (rd["Wonna"] != null) MovInfo.Wonna = Convert.ToInt32(rd["Wonna"]);
                if (rd["Score"] != null) MovInfo.Score = Convert.ToInt32(rd["Score"]);
                MovInfo.PosterFile = Convert.ToString(rd["PosterFile"]);
                if (rd["comment"] != null) MovInfo.comment = Convert.ToInt32(rd["comment"]);
                MovInfo.zt_dy = Convert.ToString(rd["zt_dy"]);
                MovInfo.zt_dy1 = Convert.ToString(rd["zt_dy1"]);
                MovInfo.zt_url = Convert.ToString(rd["zt_url"]);
                MovInfo.zt_url1 = Convert.ToString(rd["zt_url1"]);
                if (rd["SortID"] != null) MovInfo.SortID = Convert.ToInt32(rd["SortID"]);
                if (rd["hits"] != null) MovInfo.hits = Convert.ToInt32(rd["hits"]);
                if (rd["UserID"] != null) MovInfo.UserID = Convert.ToInt32(rd["UserID"]);
                if (rd["CreatTime"]!=null) MovInfo.CreatTime = Convert.ToDateTime(rd["CreatTime"]);
                if (rd["MoviePic"] != null) MovInfo.MoviePic = Convert.ToInt32(rd["MoviePic"]);
            }
            rd.Close();
            return MovInfo;
        }

        public int LockMovie(int id, int islock)
        {
            string sql = "update NT_MoviePoster set islock=" + islock + " where ID=" + id;
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        }

        /// <summary>
        /// 获取电影海报实例
        /// </summary>
        /// <param name="ID">电影海报编号</param>
        /// <returns>返回电影海报实体类</returns>
        public MoviePosterInfo getMoviePosterInfo(int ID)
        {
            MoviePosterInfo Info = new MoviePosterInfo();
            Info.ID = ID;
            string sql = "select ID,MovieID,UserID,PostTime,PostIP,FileName,isLock from NT_MoviePoster where ID=" + ID;
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, sql, null);
            if (rd.Read())
            {
                Info.MovieID = Convert.ToInt32(rd["MovieID"]);
                Info.UserID = Convert.ToInt32(rd["UserID"]);
                if (rd["PostTime"] != DBNull.Value) Info.PostTime = Convert.ToDateTime(rd["PostTime"]);
                if (rd["PostIP"] != DBNull.Value) Info.PostIP = Convert.ToString(rd["PostIP"]);
                if (rd["FileName"] != DBNull.Value) Info.FileName = Convert.ToString(rd["FileName"]);
                if (rd["isLock"] != DBNull.Value) Info.IsLock = Convert.ToInt32(rd["isLock"]);
            }
            rd.Close();
            return Info;
        }

        /// <summary>
        /// 删除电影海报
        /// </summary>
        /// <param name="ID">电影海报编号</param>
        /// <returns>删除成功返回1</returns>
        public int DelMoviePoster(int ID)
        {
            string Sql = "Delete From NT_MoviePoster Where ID=" + ID;
            return Convert.ToInt32(DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null));
        }

        public string GetMovieSortName(int SortID)
        {
            if (SortID == 0)
            {
                return "未分类";
            }
            else
            {
                SqlParameter param = new SqlParameter("@SortID", SortID);
                return Convert.ToString(DbHelper.ExecuteScalar(CommandType.StoredProcedure, "JuSNS_GetMovieSortName", param));
            }
        }

        public DataTable GetMovieUserList(int MovieID)
        {
            SqlParameter param = new SqlParameter("@MovieID", MovieID);
            return DbHelper.ExecuteTable(CommandType.StoredProcedure, "JuSNS_GetMovieUserList", param);
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Info">私信实体类</param>
        /// <returns>返回SQL参数</returns>
        private SqlParameter[] getMovieParameters(MovieInfo Info)
        {
            SqlParameter[] param = new SqlParameter[20];

            param[0] = new SqlParameter("@MovieID", SqlDbType.Int, 4);
            param[0].Value = Info.MovieID;
            param[1] = new SqlParameter("@Title", SqlDbType.NVarChar, 50);
            param[1].Value = Info.Title;
            param[2] = new SqlParameter("@Director", SqlDbType.NVarChar, 255);
            param[2].Value = Info.Director;

            param[3] = new SqlParameter("@Billing", SqlDbType.NVarChar, 255);
            param[3].Value = Info.Billing;
            param[4] = new SqlParameter("@YearOnShow", SqlDbType.Int, 4);
            param[4].Value = Info.YearOnShow;
            param[5] = new SqlParameter("@Gut", SqlDbType.NVarChar, 4000);
            param[5].Value = Info.Gut;

            param[6] = new SqlParameter("@Comments", SqlDbType.Int, 4);
            param[6].Value = Info.Comments;
            param[7] = new SqlParameter("@Seen", SqlDbType.Int, 4);
            param[7].Value = Info.Seen;
            param[8] = new SqlParameter("@Wanna", SqlDbType.Int, 4);
            param[8].Value = Info.Wanna;

            param[9] = new SqlParameter("@Wonna", SqlDbType.Int, 4);
            param[9].Value = Info.Wonna;
            param[10] = new SqlParameter("@Score", SqlDbType.Int, 4);
            param[10].Value = Info.Score;
            param[11] = new SqlParameter("@PosterFile", SqlDbType.NVarChar, 50);
            param[11].Value = Info.PosterFile;

            param[12] = new SqlParameter("@ztdy", SqlDbType.NVarChar, 50);
            param[12].Value = Info.zt_dy;
            param[13] = new SqlParameter("@ztdy1", SqlDbType.NVarChar, 50);
            param[13].Value = Info.zt_dy1;
            param[14] = new SqlParameter("@zturl", SqlDbType.NText);
            param[14].Value = Info.zt_url;
            param[15] = new SqlParameter("@zturl1", SqlDbType.NText);
            param[15].Value = Info.zt_url1;
            param[16] = new SqlParameter("@Hits", SqlDbType.Int);
            param[16].Value = Info.hits;
            param[17] = new SqlParameter("@UserID", SqlDbType.Int);
            param[17].Value = Info.UserID;
            param[18] = new SqlParameter("@CreatTime", SqlDbType.DateTime, 8);
            param[18].Value = Info.CreatTime;
            param[19] = new SqlParameter("@SortID", SqlDbType.Int);
            param[19].Value = Info.SortID;

            return param;
        }


        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Info">电影海报实体类</param>
        /// <returns>返回SQL参数</returns>
        private SqlParameter[] getMoviePosterParameters(MoviePosterInfo Info)
        {
            SqlParameter[] param = new SqlParameter[7];

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

            param[3] = new SqlParameter("@PostTime", SqlDbType.DateTime, 8);
            param[3].Value = Info.PostTime;
            param[4] = new SqlParameter("@PostIP", SqlDbType.VarChar, 15);
            param[4].Value = Info.PostIP;
            param[5] = new SqlParameter("@FileName", SqlDbType.NVarChar, 255);
            param[5].Value = Info.FileName;
            param[6] = new SqlParameter("@IsLock", SqlDbType.TinyInt, 1);
            param[6].Value = Info.IsLock;
            return param;
        }

        public int InsertMovieURL(int MovieID, int UserID, string MovieName, string MovieURL, int isLock)
        {
            SqlParameter[] param = new SqlParameter[6];

            param[0] = new SqlParameter("@MovieID", SqlDbType.Int, 4);
            param[0].Value = MovieID;
            param[1] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[1].Value = UserID;
            param[2] = new SqlParameter("@MovieName", SqlDbType.NVarChar, 50);
            param[2].Value = MovieName;

            param[3] = new SqlParameter("@MovieURL", SqlDbType.NVarChar, 300);
            param[3].Value = MovieURL;
            param[4] = new SqlParameter("@isLock", SqlDbType.TinyInt, 1);
            param[4].Value = isLock;
            param[5] = new SqlParameter("@CreatTime", SqlDbType.DateTime, 8);
            param[5].Value = DateTime.Now;
            return DbHelper.ExecuteNonQuery(CommandType.StoredProcedure, "JuSNS_InsertMovieURL", param);
        }

        /// <summary>
        /// 添加电影所属的类型
        /// </summary>
        /// <param name="Info">电影所属类型实体类</param>
        /// <returns>返回SQL参数</returns>
        private SqlParameter[] getMovieSortParameters(MovieSortInfo Info)
        {
            SqlParameter[] param = new SqlParameter[5];

            param[0] = new SqlParameter("@MovieID", SqlDbType.Int, 4);
            param[0].Value = Info.MovieID;
            param[1] = new SqlParameter("@SortID", SqlDbType.Int, 4);
            param[1].Value = Info.SortID;
            param[2] = new SqlParameter("@PostTime", SqlDbType.DateTime, 8);
            param[2].Value = Info.PostTime;

            param[3] = new SqlParameter("@PostIP", SqlDbType.VarChar, 15);
            param[3].Value = Info.PostIP;
            param[4] = new SqlParameter("@CreatorID", SqlDbType.Int, 4);
            param[4].Value = Info.CreatorID;

            return param;
        }

        //public IDataReader getMovieInfo(int MovieID)
        //{
        //    string sql = "select * from NT_Movie where MovieID=" + MovieID;
        //    return DbHelper.ExecuteReader(CommandType.Text, sql, null);
        //}
    }
}
