using System;
using System.Collections.Generic;
using System.Text;
using JuSNS.Factory.ApiInterface;
using System.Data;
using System.Data.SqlClient;
using JuSNS.Profile;
namespace JuSNS.SQLServer.ApiInterface
{
    public class Friend:DbBase,IFriend
    {
        /// <summary>
        /// 得所指定用户所有好友
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <returns></returns>
        public int[] Get(int UserID)
        {
            int[] arr=new int[1];
            SqlParameter param = new SqlParameter("@UserID", UserID);
            string sql = "Select FriendID From Nt_Friend where UserID=@UserID";
            IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sql, param);
            int i = 0;
            while (dr.Read())
            {                
                i++;
                Array.Resize<int>(ref arr, i);
                arr[i - 1] = Convert.ToInt32(dr[0]);
            }
            dr.Dispose();
            return arr;
        }
        /// <summary>
        /// 是否是朋友
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <param name="FriendID">朋友ID</param>
        /// <returns></returns>
        public bool AreFriends(int UserID, int FriendID)
        {
            SqlParameter[] param =
                {
                    new SqlParameter("@UserID",UserID),
                    new SqlParameter("@FriendID",FriendID)
                };
            string sql = "Select Count(*) from Nt_Friend where UserID=@UserID and FriendID=@FriendID";
            int n = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, sql, param));
            if (n > 0)
                return true;
            return false;
        }
        /// <summary>
        /// 取得安装了某APP的好友ID
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="AppID"></param>
        /// <returns></returns>
        public int[] GetAppUsers(int UserID, int AppID)
        {
            int[] arr = new int[1];
            SqlParameter[] param ={
                new SqlParameter("@UserID",UserID),
                new SqlParameter("@AppID",AppID)
            };
            string sql = "Select FriendID From Nt_Friend A where UserID=@UserID and Exists(Select AppID from App_UserApp where UserID=A.UserID and AppID=@AppID)";
            IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sql, param);
            int i = 0;
            while (dr.Read())
            {
                i++;
                Array.Resize<int>(ref arr, i);
                arr[i - 1] = Convert.ToInt32(dr[0]);
            }
            dr.Dispose();
            return arr;
        }
    }
}
