//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 PhotoLasso : DbBase, IPhotoLasso
    {
        /// <summary>
        /// 添加照片圈人信息
        /// </summary>
        /// <param name="Info">照片圈人实体类</param>
        /// <returns>添加成功返回1</returns>
        public int Add(PhotoLassoInfo Info)
        {
            SqlConnection Conn = new SqlConnection(DBConfig.CnString);
            Conn.Open();

            SqlTransaction trans = Conn.BeginTransaction();
            try
            {
                SqlParameter[] param = getParameters(Info);
                string Sql = "select Count(*) from [NT_PhotoLasso] where PhotoID=@PhotoID and FriendID=@FriendID";
                int Cnum = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, Sql, param));
                if (Cnum > 0)
                {
                    trans.Commit();
                    return 0;
                }
                Sql = "Insert Into [NT_PhotoLasso]" +
                             "([PhotoID],[FriendID],[UserID],[PosX],[PosY],[PosW],[PosH],[PostTime],[PostIP],[IsLock],[Description])" +
                             "Values" +
                             "(@PhotoID,@FriendID,@UserID,@PosX,@PosY,@PosW,@PosH,@PostTime,@PostIP,@IsLock,@Description)";
                int n = DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param);
                if (Info.FriendID == Info.UserID)
                {
                    Notify(trans, EnumNotifyType.LassoOneself, Info.UserID, Info.FriendID, "在照片中圈了自己", Info.PhotoID);
                }
                else
                {
                    Notify(trans, EnumNotifyType.LassoOther, Info.UserID, Info.FriendID, "在照片中被{UserID=" + Info.UserID + "}圈了出来", Info.PhotoID);
                }
                JUser ju = new JUser();
                if (ju.CheckPrivacyInfo(trans, Info.UserID, "ActPhotoLasso"))
                {
                    UserDyn(trans, 0, 0, "quanren", Info.UserID, Info.FriendID, Info.PhotoID, "圈人", Info.FriendID.ToString());
                }
                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>
        /// <param name="FriendID">被圈好友编号</param>
        /// <returns>删除成功返回1</returns>
        public int Del(int ID, int UserID, int FriendID)
        {
            SqlConnection Conn = new SqlConnection(DBConfig.CnString);
            Conn.Open();

            SqlTransaction trans = Conn.BeginTransaction();
            try
            {
                //删除照片圈人表
                string Sql = "Delete From [NT_PhotoLasso] " +
                             "Where [ID]=" + ID + " And ([UserID]=" + UserID + " Or [FriendID]=" + FriendID + ")";
                DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);
                Sql = "delete from NT_Dyn where cID="+FriendID+" and UserID ="+UserID+" and dynType='quanren'";
                DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, null);

                trans.Commit();
                return 1;
            }
            catch (SqlException e)
            {
                trans.Rollback();
                throw e;
            }
            finally
            {
                if (Conn.State == ConnectionState.Open)
                    Conn.Close();
            }
        }

        /// <summary>
        /// 取得照片圈人信息
        /// </summary>
        /// <param name="ID">照片圈人编号</param>
        /// <returns>返回照片圈人实体类</returns>
        public PhotoLassoInfo Info(int ID)
        {
            PhotoLassoInfo info = new PhotoLassoInfo();
            string Sql = "Select [ID],[PhotoID],[FriendID],[UserID],[PosX],[PosY],[PosW],[PosH],[PostTime],[PostIP],[IsLock],[Description] " +
                         "From " +
                         "[NT_PhotoLasso] " +
                         "Where [ID]=" + ID;
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            if (rd.Read())
            {
                info.ID = Convert.ToInt32(rd["ID"].ToString());
                info.PhotoID = Convert.ToInt32(rd["PhotoID"].ToString());
                info.FriendID = Convert.ToInt32(rd["FriendID"].ToString());
                info.UserID = Convert.ToInt32(rd["UserID"].ToString());
                info.PosX = Convert.ToInt32(rd["PosX"].ToString());
                info.PosY = Convert.ToInt32(rd["PosY"].ToString());
                try
                {
                    info.PosW = Convert.ToInt32(rd["PosW"].ToString());
                    info.PosH = Convert.ToInt32(rd["PosH"].ToString());
                }
                catch { };
                info.PostTime = Convert.ToDateTime(rd["PostTime"].ToString());
                if (rd["PostIP"] != DBNull.Value) info.PostIP = rd["PostIP"].ToString();
                if (rd["IsLock"].ToString() == "true")
                {
                    info.IsLock = EnumIsLock.Lock;
                }
                else
                {
                    info.IsLock = EnumIsLock.UnLock;
                }
                if (rd["Description"] != DBNull.Value) info.Description = rd["Description"].ToString();
            }
            rd.Dispose();
            return info;
        }

        /// <summary>
        /// 取得相应照片圈人信息
        /// </summary>
        /// <param name="PhotoID">照片ID</param>
        /// <returns></returns>
        public List<PhotoLassoInfo> InfoList(int PhotoID)
        {
            List<PhotoLassoInfo> infolist = new List<PhotoLassoInfo>();
            string Sql = "Select [ID],[PhotoID],[FriendID],[UserID],[PosX],[PosY],[PosW],[PosH],[PostTime],[PostIP],[IsLock],[Description] " +
                         "From " +
                         "[NT_PhotoLasso] " +
                         "Where IsLock=(0) and [PhotoID]=" + PhotoID;
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            while (rd.Read())
            {
                PhotoLassoInfo info = new PhotoLassoInfo();
                info.ID = Convert.ToInt32(rd["ID"].ToString());
                info.PhotoID = Convert.ToInt32(rd["PhotoID"].ToString());
                info.FriendID = Convert.ToInt32(rd["FriendID"].ToString());
                info.UserID = Convert.ToInt32(rd["UserID"].ToString());
                info.PosX = Convert.ToInt32(rd["PosX"].ToString());
                info.PosY = Convert.ToInt32(rd["PosY"].ToString());
                try
                {
                    info.PosW = Convert.ToInt32(rd["PosW"].ToString());
                    info.PosH = Convert.ToInt32(rd["PosH"].ToString());
                }
                catch { };
                info.PostTime = Convert.ToDateTime(rd["PostTime"].ToString());
                if (rd["PostIP"] != DBNull.Value) info.PostIP = rd["PostIP"].ToString();
                if (rd["IsLock"].ToString() == "true")
                {
                    info.IsLock = EnumIsLock.Lock;
                }
                else
                {
                    info.IsLock = EnumIsLock.UnLock;
                }
                if (rd["Description"] != DBNull.Value) info.Description = rd["Description"].ToString();
                infolist.Add(info);
            }
            rd.Dispose();
            return infolist;

        }

        /// <summary>
        /// 取得相册圈人信息
        /// </summary>
        /// <param name="AlbumID">相册ID</param>
        /// <returns></returns>
        public List<PhotoLassoInfo> AlbumLasso(int AlbumID)
        {
            List<PhotoLassoInfo> infolist = new List<PhotoLassoInfo>();
            string Sql = "Select [ID],[PhotoID],[FriendID],[UserID],[PosX],[PosY],[PosW],[PosH],[PostTime],[PostIP],[IsLock],[Description] " +
                         "From " +
                         "[NT_PhotoLasso] " +
                         "Where  IsLock=(0) and [PhotoID] in (Select PhotoID from [NT_Photo] where AlbumID=" + AlbumID + ")";
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            while (rd.Read())
            {
                PhotoLassoInfo info = new PhotoLassoInfo();
                info.ID = Convert.ToInt32(rd["ID"].ToString());
                info.PhotoID = Convert.ToInt32(rd["PhotoID"].ToString());
                info.FriendID = Convert.ToInt32(rd["FriendID"].ToString());
                info.UserID = Convert.ToInt32(rd["UserID"].ToString());
                info.PosX = Convert.ToInt32(rd["PosX"].ToString());
                info.PosY = Convert.ToInt32(rd["PosY"].ToString());
                try
                {
                    info.PosW = Convert.ToInt32(rd["PosW"].ToString());
                    info.PosH = Convert.ToInt32(rd["PosH"].ToString());
                }
                catch { };
                info.PostTime = Convert.ToDateTime(rd["PostTime"].ToString());
                if (rd["PostIP"] != DBNull.Value) info.PostIP = rd["PostIP"].ToString();
                if (rd["IsLock"].ToString() == "true")
                {
                    info.IsLock = EnumIsLock.Lock;
                }
                else
                {
                    info.IsLock = EnumIsLock.UnLock;
                }
                if (rd["Description"] != DBNull.Value) info.Description = rd["Description"].ToString();
                infolist.Add(info);
            }
            rd.Dispose();
            return infolist;
        }


        /// <summary>
        /// 取得相应照片圈人信息
        /// </summary>
        /// <param name="PhotoID">用户ID</param>
        /// <returns></returns>
        public List<PhotoLassoInfo> FriendInfoList(int UserID)
        {
            List<PhotoLassoInfo> infolist = new List<PhotoLassoInfo>();
            string Sql = "Select [ID],[PhotoID],[FriendID],[UserID],[PosX],[PosY],[PosW],[PosH],[PostTime],[PostIP],[IsLock],[Description] " +
                         "From " +
                         "[NT_PhotoLasso] " +
                         "Where  IsLock=(0) and [FriendID] in (select FriendID from [NT_Friend] where UserID=" + UserID + ")";
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            while (rd.Read())
            {
                PhotoLassoInfo info = new PhotoLassoInfo();
                info.ID = Convert.ToInt32(rd["ID"].ToString());
                info.PhotoID = Convert.ToInt32(rd["PhotoID"].ToString());
                info.FriendID = Convert.ToInt32(rd["FriendID"].ToString());
                info.UserID = Convert.ToInt32(rd["UserID"].ToString());
                info.PosX = Convert.ToInt32(rd["PosX"].ToString());
                info.PosY = Convert.ToInt32(rd["PosY"].ToString());
                try
                {
                    info.PosW = Convert.ToInt32(rd["PosW"].ToString());
                    info.PosH = Convert.ToInt32(rd["PosH"].ToString());
                }
                catch { };
                info.PostTime = Convert.ToDateTime(rd["PostTime"].ToString());
                if (rd["PostIP"] != DBNull.Value) info.PostIP = rd["PostIP"].ToString();
                if (rd["IsLock"].ToString() == "true")
                {
                    info.IsLock = EnumIsLock.Lock;
                }
                else
                {
                    info.IsLock = EnumIsLock.UnLock;
                }
                if (rd["Description"] != DBNull.Value) info.Description = rd["Description"].ToString();
                infolist.Add(info);
            }
            rd.Dispose();
            return infolist;

        }

        /// <summary>
        /// 取得某人被圈照片圈人信息
        /// </summary>
        /// <param name="UserID">照片用户</param>
        /// <param name="isSelf">是否在自己的相册中</param>
        /// <returns></returns>
        public List<PhotoLassoInfo> UserInfoList(int UserID, bool isSelf)
        {
            List<PhotoLassoInfo> infolist = new List<PhotoLassoInfo>();
            string Sql = string.Empty;
            if (isSelf)
            {
                Sql = "Select [ID],[PhotoID],[FriendID],[UserID],[PosX],[PosY],[PosW],[PosH],[PostTime],[PostIP],[IsLock],[Description] " +
                         "From " +
                         "[NT_PhotoLasso] " +
                         "Where  IsLock=(0) and [FriendID]=" + UserID + " and PhotoID in (select PhotoID from [NT_Photo] where UserID=" + UserID + " )";
            }
            else
            {
                Sql = "Select [ID],[PhotoID],[FriendID],[UserID],[PosX],[PosY],[PosW],[PosH],[PostTime],[PostIP],[IsLock],[Description] " +
                         "From " +
                         "[NT_PhotoLasso] " +
                         "Where  IsLock=(0) and [FriendID]=" + UserID + " and PhotoID not in (select PhotoID from [NT_Photo] where UserID=" + UserID + " )";
            }
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            while (rd.Read())
            {
                PhotoLassoInfo info = new PhotoLassoInfo();
                info.ID = Convert.ToInt32(rd["ID"].ToString());
                info.PhotoID = Convert.ToInt32(rd["PhotoID"].ToString());
                info.FriendID = Convert.ToInt32(rd["FriendID"].ToString());
                info.UserID = Convert.ToInt32(rd["UserID"].ToString());
                info.PosX = Convert.ToInt32(rd["PosX"].ToString());
                info.PosY = Convert.ToInt32(rd["PosY"].ToString());
                try
                {
                    info.PosW = Convert.ToInt32(rd["PosW"].ToString());
                    info.PosH = Convert.ToInt32(rd["PosH"].ToString());
                }
                catch { };
                info.PostTime = Convert.ToDateTime(rd["PostTime"].ToString());
                if (rd["PostIP"] != DBNull.Value) info.PostIP = rd["PostIP"].ToString();
                if (rd["IsLock"].ToString() == "true")
                {
                    info.IsLock = EnumIsLock.Lock;
                }
                else
                {
                    info.IsLock = EnumIsLock.UnLock;
                }
                if (rd["Description"] != DBNull.Value) info.Description = rd["Description"].ToString();
                infolist.Add(info);
            }
            rd.Dispose();
            return infolist;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Info">照片圈人实体类</param>
        /// <returns>返回SQL参数</returns>
        private SqlParameter[] getParameters(PhotoLassoInfo Info)
        {
            SqlParameter[] param = new SqlParameter[12];

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

            param[3] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[3].Value = Info.UserID;
            param[4] = new SqlParameter("@PosX", SqlDbType.Int, 4);
            param[4].Value = Info.PosX;
            param[5] = new SqlParameter("@PosY", SqlDbType.Int, 4);
            param[5].Value = Info.PosY;

            param[6] = new SqlParameter("@PostTime", SqlDbType.DateTime, 8);
            param[6].Value = Info.PostTime;
            param[7] = new SqlParameter("@PostIP", SqlDbType.VarChar, 15);
            param[7].Value = Info.PostIP;
            param[8] = new SqlParameter("@IsLock", SqlDbType.Bit);
            param[8].Value = (int)Info.IsLock;

            param[9] = new SqlParameter("@Description", SqlDbType.NVarChar, 50);
            param[9].Value = Info.Description;

            param[10] = new SqlParameter("@PosW", SqlDbType.Int, 4);
            param[10].Value = Info.PosW;
            param[11] = new SqlParameter("@PosH", SqlDbType.Int, 4);
            param[11].Value = Info.PosH;

            return param;
        }
    }
}
