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

namespace JuSNS.Home
{
    public class Movie
    {
        JuSNS.Factory.IMovie dal;
        public Movie()
        {
            dal = DataAccess.CreateMovie();
        }

        /// <summary>
        /// 取得排行榜数据
        /// </summary>
        /// <param name="Num">取出数量</param>
        /// <param name="flg">0hits,1comment,2rand</param>
        /// <returns></returns>
        public DataTable GetTopMovie(int Num, int flg)
        {
            return dal.GetTopMovie(Num, flg);
        }

        /// <summary>
        /// 取得随机电影
        /// </summary>
        /// <returns></returns>
        public DataTable RandMovie()
        {
            return dal.RandMovie();
        }

        /// <summary>
        /// 取得好友想看的电影
        /// </summary>
        /// <param name="UsreID">会员编号</param>
        /// <returns>返回数据表</returns>
        public DataTable FriendThinkLookMovie(int UsreID)
        {
            return dal.FriendThinkLookMovie(UsreID);
        }

        /// <summary>
        /// 取得我想看的电影
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public DataTable MyThinkLookMovie(int UserID)
        {
            return dal.MyThinkLookMovie(UserID);
        }

        /// <summary>
        /// 取得好友排行榜
        /// </summary>
        /// <param name="UserID">会员编号</param>
        /// <returns>返回数据表</returns>
        public DataTable getTopFriendMovie(int UserID)
        {
            return dal.getTopFriendMovie(UserID);
        }

        /// <summary>
        /// 取得朋友对某部电影的评论
        /// </summary>
        /// <param name="UserID">会员编号</param>
        /// <param name="MovieID">电影编号</param>
        /// <returns>返回数据表</returns>
        public DataTable FriendMovieComm(int UserID, int MovieID)
        {
            return dal.FriendMovieComm(UserID, MovieID);
        }

        /// <summary>
        /// 添加电影类型
        /// </summary>
        /// <param name="SortName">类型名称</param>
        /// <returns>添加成功返回1</returns>
        public int AddMovieSort(string SortName)
        {
            return dal.AddMovieSort(SortName);
        }

        /// <summary>
        /// 添加电影所属的类型
        /// </summary>
        /// <param name="Info">电影所属类型实体类</param>
        /// <returns>添加成功返回1</returns>
        public int AddSortForMovie(MovieSortInfo Info)
        {
            return dal.AddSortForMovie(Info);
        }

        /// <summary>
        /// 取得电影所对应的电影类型编号
        /// </summary>
        /// <param name="movieid">电影编号</param>
        /// <returns>返回数据表</returns>
        public DataTable getSortForMovie(int movieid)
        {
            return dal.getSortForMovie(movieid);
        }

        /// <summary>
        /// 取得电影类型列表
        /// </summary>
        /// <returns>返回数据表</returns>
        public DataTable getDictMovieSortList()
        {
            return dal.getDictMovieSortList();
        }

        /// <summary>
        /// 修改电影类型
        /// </summary>
        /// <param name="SortName">类型名称</param>
        /// <param name="ID">类型编号</param>
        /// <returns>修改成功返回1</returns>
        public int EditMovieSort(string SortName, int ID)
        {
            return dal.EditMovieSort(SortName, ID);
        }

        /// <summary>
        /// 取得电影类型信息
        /// </summary>
        /// <param name="ID">电影类型编号</param>
        /// <returns>返回电影类型实体类</returns>
        public Dict_MovieSortInfo MovieSortInfo(int ID)
        {
            return dal.MovieSortInfo(ID);
        }

        /// <summary>
        /// 删除电影类型
        /// </summary>
        /// <param name="ID">类型编号</param>
        /// <returns>删除成功返回1</returns>
        public int DelMovieSort(int ID)
        {
            DataTable dt = dal.getMoviePostFileForSort(ID);
            DataTable dt1 = dal.getMovieUpFileForSort(ID);
            int n = dal.DelMovieSort(ID);
            if (n == 1)
            {
                string s_Rootpath = ServerInfo.GetRootPath();
                if (dt != null)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        Public.DelFile(s_Rootpath + dt.Rows[i]["PosterFile"].ToString());
                    }
                    dt.Clear(); dt.Dispose();
                }
                if (dt1 != null)
                {
                    for (int j = 0; j < dt1.Rows.Count; j++)
                    {
                        Public.DelFile(s_Rootpath + dt1.Rows[j]["FileName"].ToString());
                    }
                    dt1.Clear(); dt1.Dispose();
                }
            }
            return n;
        }

        /// <summary>
        /// 取得电影的类型
        /// </summary>
        /// <param name="MovieID">电影编号</param>
        /// <returns>返回数据表</returns>
        public DataTable getMovieGut(int MovieID)
        {
            return dal.getMovieGut(MovieID);
        }
        /// <summary>
        /// 添加电影
        /// </summary>
        /// <param name="Info">电影实体类</param>
        /// <returns>添加成功返回电影编号</returns>
        public int AddMovie(MovieInfo Info)
        {
            return dal.AddMovie(Info);
        }

        /// <summary>
        /// 修改电影
        /// </summary>
        /// <param name="Info">电影实体类</param>
        /// <returns>修改成功返回1</returns>
        public int EditMovie(MovieInfo Info)
        {
            MovieInfo tempinfo = this.GetMovieInfo(Info.MovieID);
            int n = dal.EditMovie(Info);
            if (n == 1)
            {
                string s_Rootpath = ServerInfo.GetRootPath();
                Public.DelFile(s_Rootpath + tempinfo.PosterFile);
            }
            return n;
        }

        /// <summary>
        /// 删除电影
        /// </summary>
        /// <param name="MovieID">电影编号</param>
        /// <returns>删除成功返回1</returns>
        public int DelMovie(int MovieID)
        {
            MovieInfo movieinfo = new MovieInfo();
            movieinfo = GetMovieInfo(MovieID);
            DataTable dt = dal.getMoviePoster(MovieID);

            int n = dal.DelMovie(MovieID);
            if (n == 1)
            {
                Public.DelFile("~/" + movieinfo.PosterFile);
                if (dt != null)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        Public.DelFile("~/" + dt.Rows[i]["FileName"].ToString());
                    }
                    dt.Clear(); dt.Dispose();
                }
            }
            return n;
        }

        /// <summary>
        /// 获取电影海报实例
        /// </summary>
        /// <param name="ID">电影海报编号</param>
        /// <returns>返回电影海报实体类</returns>
        public MoviePosterInfo getMoviePosterInfo(int ID)
        {
            return dal.getMoviePosterInfo(ID);
        }

        /// <summary>
        /// 删除电影海报
        /// </summary>
        /// <param name="ID">电影海报编号</param>
        /// <returns>删除成功返回1</returns>
        public int DelMoviePoster(int ID)
        {
            MoviePosterInfo posterinfo = new MoviePosterInfo();
            posterinfo = getMoviePosterInfo(ID);
            int n = dal.DelMoviePoster(ID);
            if (n == 1)
            {
                Public.DelFile("../../" + posterinfo.FileName);
            }
            return n;
        }

        public int LockMovie(int id, int islock)
        {
            return dal.LockMovie(id,islock);
        }

        /// <summary>
        /// 修改电影海报
        /// </summary>
        /// <param name="MovieID">电影编号</param>
        /// <param name="PosterFile">海报地址</param>
        /// <returns>修改成功返回1</returns>
        public int SetPosterFile(int MovieID, string PosterFile)
        {
            MovieInfo movieinfo = new MovieInfo();
            movieinfo = GetMovieInfo(MovieID);
            int n = dal.SetPosterFile(MovieID, PosterFile);
            return n;
        }

        /// <summary>
        /// 保存评论
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <param name="MovieID">电影ID</param>
        /// <returns></returns>
        public int SaveComment(int UserID, int MovieID, string Comment)
        {
            return dal.SaveComment(UserID, MovieID, Comment);
        }

        /// <summary>
        /// 取得自已对某部电影的评论
        /// </summary>
        /// <param name="UserID">会员编号</param>
        /// <param name="MovieID">电影编号</param>
        /// <returns>返回评论信息</returns>
        public string getComment(int UserID, int MovieID)
        {
            return dal.getComment(UserID, MovieID);
        }

        /// <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)
        {
            return dal.SaveScore(UserID, MovieID, Score);
        }

        /// <summary>
        /// 更新想看该电影的人数
        /// </summary>
        /// <param name="MovieID">电影ID</param>
        /// <param name="UserID">用户ID</param>
        /// <param name="tag">true为增加1，false为减少1</param>
        /// <returns></returns>
        public int UpdateWanna(int MovieID, int UserID, bool tag)
        {
            return dal.UpdateWanna(MovieID, UserID, tag);
        }
        /// <summary>
        /// 更新不想看该电影的人数
        /// </summary>
        /// <param name="MovieID">电影ID</param>
        /// <param name="UserID">用户ID</param>
        /// <param name="tag">true为增加1，false为减少1</param>
        /// <returns></returns>
        public int UpdateWonna(int MovieID, int UserID, bool tag)
        {
            return dal.UpdateWonna(MovieID, UserID, tag);
        }
        /// <summary>
        /// 获取电影类实例
        /// </summary>
        /// <param name="MovieID">电影ID</param>
        /// <returns></returns>
        public MovieInfo GetMovieInfo(int MovieID)
        {
            return dal.GetMovieInfo(MovieID);
        }
        /// <summary>
        /// 上传电影海报
        /// </summary>
        /// <param name="Info">电影海报实体类</param>
        /// <returns>如果上传成功返回1</returns>
        public int AddMoviePoster(MoviePosterInfo Info)
        {
            return dal.AddMoviePoster(Info);
        }
        /// <summary>
        /// 取得电影的分数
        /// </summary>
        /// <param name="UserID">会员编号</param>
        /// <param name="MovieID">电影编号</param>
        /// <returns>如果存在此记录返回分数,不存在返回-2</returns>
        public int getScore(int UserID, int MovieID)
        {
            return dal.getScore(UserID, MovieID);
        }

        /// <summary>
        /// 插入影视地址
        /// </summary>
        /// <param name="MovieID"></param>
        /// <param name="UserID"></param>
        /// <param name="MovieName"></param>
        /// <param name="MovieURL"></param>
        /// <param name="isLock"></param>
        public int InsertMovieURL(int MovieID, int UserID, string MovieName, string MovieURL, int isLock)
        {
           return  dal.InsertMovieURL(MovieID, UserID, MovieName, MovieURL, isLock);
        }

        /// <summary>
        /// 获取分类名称
        /// </summary>
        /// <param name="SortID"></param>
        /// <returns></returns>
        public string GetMovieSortName(int SortID)
        {
            return dal.GetMovieSortName(SortID);
        }

        /// <summary>
        /// 得到用户上传的视频地址
        /// </summary>
        /// <param name="MovieID"></param>
        /// <returns></returns>
        public DataTable GetMovieUserList(int MovieID)
        {
            return dal.GetMovieUserList(MovieID);
        }

    }
}
