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

namespace JuSNS.SQLServer.ApiInterface
{
    public class Notification:DbBase,INotification
    {
        /// <summary>
        /// 发送通知
        public int Send(NoticeInfo info)
        {
            SqlParameter[] param = GetParameter(info);
            string sql = "insert into NT_Notice(Sender,Receiver,Content,Correspond,PostTime,PostIP) values(@Sender,@Receiver,@Content,@Correspond,@PostTime,@PostIP)";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
        /// <summary>
        /// 取得通知信息
        /// </summary>
        /// <param name="UserID">用户ID</param>
        /// <param name="AppID">应用程序ID</param>
        /// <returns></returns>
        public List<NoticeInfo> GetNotice(int UserID,int AppID)
        {
            List<NoticeInfo> infolist = new List<NoticeInfo>();
            SqlParameter[] param = {
                new SqlParameter("@UserID", UserID),
                new SqlParameter("@AppID",AppID)
            };
            string sql = "Select ID,Sender,Receiver,Content,Correspond,PostTime,PostIP from NT_Notice where Receiver=@UserID and Correspond=@AppID";
            IDataReader dr = DbHelper.ExecuteReader(CommandType.Text, sql, param);
            while (dr.Read())
            {
                NoticeInfo info = new NoticeInfo();
                info.ID = dr.GetInt32(0);
                info.Sender = dr.GetInt32(1);
                info.Receiver = dr.GetInt32(2);
                info.Notice = dr.GetString(3);
                if(!dr.IsDBNull(4))info.AppID = dr.GetInt32(4);
                if (!dr.IsDBNull(5)) info.PostTime = dr.GetDateTime(5);
                if (!dr.IsDBNull(6)) info.PostIP = dr.GetString(6);
                infolist.Add(info);
            }
            dr.Dispose();
            return infolist;
        }

        /// <summary>
        /// 通知参数
        /// </summary>
        /// <param name="info">App信息</param>
        /// <returns></returns>
        private static SqlParameter[] GetParameter(NoticeInfo info)
        {
            SqlParameter[] param = new SqlParameter[7];
            param[0] = new SqlParameter("@ID", SqlDbType.Int, 4);
            param[0].Value = info.ID;
            param[1] = new SqlParameter("@Sender", SqlDbType.Int, 4);
            param[1].Value = info.Sender;
            param[2] = new SqlParameter("@Receiver", SqlDbType.Int, 4);
            param[2].Value = info.Receiver;
            param[3] = new SqlParameter("@Notice", SqlDbType.NVarChar, 200);
            param[3].Value = info.Notice == null ? "" : info.Notice;
            param[4] = new SqlParameter("@AppID", SqlDbType.Int, 4);
            param[4].Value = info.AppID;
            param[5] = new SqlParameter("@PostTime", SqlDbType.DateTime, 8);
            param[5].Value = info.PostTime;
            param[6] = new SqlParameter("@PostIP", SqlDbType.NVarChar, 18);
            param[6].Value = info.PostIP == null ? "" : info.PostIP;
            return param;
        }
    }
}
