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;
using JuSNS.Common;

namespace JuSNS.SQLServer
{
    public class Gift : DbBase, IGift
    {
        public DataTable GiftListPage(int UserID,string classID,string n, int PageIndex, int PageSize, out int RecordCount, out int PageCount)
        {
            string allFields = "*";
            string whereSTR = string.Empty;
            if (!string.IsNullOrEmpty(classID) && JuSNS.Common.Input.IsInteger(classID))
            {
                whereSTR = " and ClassID=" + Convert.ToInt32(classID) + "";
            }
            string tablesAndWhere = " NT_Gift Where isLock=0" + whereSTR;
            string indexField = "ID";
            string orderField = "Order By ID desc";
            switch (n)
            {
                case "use":
                    orderField = "Order by UseNumber Desc,ID desc";
                    break;
                case "ph":
                    orderField = "Order by point Desc,ID desc";
                    break;
                case "pl":
                    orderField = "Order by point asc,ID desc";
                    break;
            }
            return DbHelper.ExecutePage(allFields, tablesAndWhere, indexField, orderField, PageIndex, PageSize, out RecordCount, out PageCount, null);
        }

        public IDataReader GiftInfo(int ID)
        {
            SqlParameter param = new SqlParameter("@ID", ID);
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, "JuSNS_GiftInfo", param);
        }

        public IDataReader GiftUserInfo(int ID)
        {
            SqlParameter param = new SqlParameter("@ID", ID);
            return DbHelper.ExecuteReader(CommandType.StoredProcedure, "JuSNS_GiftUserInfo", param);
        }

        public int SaveGift(int UserID, int gID, int SendUserID, int pType, string Content, DateTime CreatTime)
        {
            SqlConnection Conn = new SqlConnection(DBConfig.CnString);
            Conn.Open();
            SqlTransaction trans = Conn.BeginTransaction();
            try
            {
                SqlParameter[] Param = new SqlParameter[6];
                Param[0] = new SqlParameter("@UserID", SqlDbType.Int);
                Param[0].Value = UserID;
                Param[1] = new SqlParameter("@gID", SqlDbType.Int);
                Param[1].Value = gID;
                Param[2] = new SqlParameter("@SendUserID", SqlDbType.Int);
                Param[2].Value = SendUserID;
                Param[3] = new SqlParameter("@pType", SqlDbType.TinyInt);
                Param[3].Value = pType;
                Param[4] = new SqlParameter("@Content", SqlDbType.NVarChar, 200);
                Param[4].Value = Content;
                Param[5] = new SqlParameter("@CreatTime", SqlDbType.DateTime, 8);
                Param[5].Value = CreatTime;

                int n = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.StoredProcedure, "JuSNS_SaveGift", Param));
                if (pType == 0)
                {
                    JUser ju = new JUser();
                    if (ju.CheckPrivacyInfo(trans, SendUserID, "ActSecede"))
                    {
                        UserDyn(trans, 0, 0, "gift", SendUserID, UserID, gID, "送了一个礼物", n.ToString());
                    }
                    n += Notify(trans, EnumNotifyType.SendGift, SendUserID, UserID, "给你送了一个礼物{GiftName}", gID);
                }
                trans.Commit();
                return n;
            }
            catch (SqlException e)
            {
                trans.Rollback();
                throw e;
            }
            finally
            {
                if (Conn.State == ConnectionState.Open)
                    Conn.Close();
            }
        }

        public DataTable ShowGiftList(string t,int UserID, int PageIndex, int PageSize, out int RecordCount, out int PageCount)
        {
            string allFields = "ID,UserID,SendUserID,gID,pType,Content,CreatTime,(select Picture from NT_Gift where NT_GiftUser.gID=NT_Gift.ID) as pic,(select gName from NT_Gift where NT_GiftUser.gID=NT_Gift.ID) as gName";
            string tablesAndWhere = string.Empty;
            if (t == "in")
            {
                tablesAndWhere += " NT_GiftUser Where UserID=" + UserID + " and isDel=0";
            }
            else if (JuSNS.Common.Input.IsInteger(t))
            {
                if (t != UserID.ToString())
                {
                    tablesAndWhere += " NT_GiftUser Where UserID=" + Convert.ToInt32(t) + " and pType!=1  and isDel=0 ";
                }
                else
                {
                    tablesAndWhere += " NT_GiftUser Where UserID=" + Convert.ToInt32(t) + " and isDel=0";
                }
            }
            else
            {
                tablesAndWhere += " NT_GiftUser Where SendUserID=" + UserID + "";
            }
            string indexField = "ID";
            string orderField = "Order By ID Desc";
            return DbHelper.ExecutePage(allFields, tablesAndWhere, indexField, orderField, PageIndex, PageSize, out RecordCount, out PageCount, null);
        }

        public int DelGift(int UserID, int ID)
        {
            string sql = "update NT_GiftUser Set IsDel=1 where ID=" + ID + " and UserID=" + UserID + "";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        }

        public IDataReader GetGiftClass()
        {
            string sql = "select ID,CName,(select count(id) from NT_Gift where NT_Gift.ClassID=NT_GiftClass.ID) as Count,(select count(id) from NT_Gift where NT_Gift.ClassID=0) as Count1 from NT_GiftClass order by id asc";
            return DbHelper.ExecuteReader(CommandType.Text, sql, null);
        }
        public DataTable GetGiftClassT()
        {
            string sql = "select ID,CName,(select count(id) from NT_Gift where NT_Gift.ClassID=NT_GiftClass.ID) as Count,(select count(id) from NT_Gift where NT_Gift.ClassID=0) as Count1 from NT_GiftClass order by id asc";
            return DbHelper.ExecuteTable(CommandType.Text, sql, null);
        }

        public int LockGift(int ID, int flg, int isdel)
        {
            string sql = "update nt_gift set islock=" + flg + " where id=" + ID;
            if (isdel == 1)
            {
                sql = "delete from NT_GIFT where ID=" + ID;
            }
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        }

        public int addGift(string GName, string Picture, int point, string Content, int ClassID, int GID)
        {
            SqlParameter[] Param = new SqlParameter[7];
            Param[0] = new SqlParameter("@GName", SqlDbType.NVarChar,20);
            Param[0].Value = GName;
            Param[1] = new SqlParameter("@Picture", SqlDbType.NVarChar,30);
            Param[1].Value = Picture;
            Param[2] = new SqlParameter("@UseNumber", SqlDbType.Int);
            Param[2].Value = 0;
            Param[3] = new SqlParameter("@isLock", SqlDbType.TinyInt);
            Param[3].Value = 0;
            Param[4] = new SqlParameter("@point", SqlDbType.Int);
            Param[4].Value = point;
            Param[5] = new SqlParameter("@Content", SqlDbType.NVarChar, 200);
            Param[5].Value = Content;
            Param[6] = new SqlParameter("@ClassID", SqlDbType.Int);
            Param[6].Value = ClassID;
            string sql = string.Empty;
            if (GID > 0)
            {
                sql = "update NT_Gift set GName=@GName,Picture=@Picture,point=@point,Content=@Content,ClassID=@ClassID where ID=" + GID;
            }
            else
            {
                string gsql = "select count(id) from nt_gift where Picture=@Picture";
                if (Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, gsql, Param).ToString()) > 0)
                    return 2;
                sql = "insert into NT_GIFT(GName,Picture,useNumber,islock,point,content,classid)values(@GName,@Picture,@UseNumber,0,@point,@Content,@ClassID)";
            }
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, Param);
        }

        public int DelGiftClass(int id)
        {
            string sql = "delete from NT_Giftclass where ID=" + id;
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        }

        public DataTable getGiftClassID(int ID)
        {
            string sql = "select id,cName from NT_Giftclass where ID=" + ID;
            return DbHelper.ExecuteTable(CommandType.Text, sql, null);
        }

        public int UpdateGiftClassID(string className, int id)
        {
            SqlParameter param = new SqlParameter("@className", className);
            string sql = string.Empty;
            if (id >0)
            {
                sql = "update NT_GIFTCLASS set cName=@className where ID=" + id;
            }
            else
            {
                sql = "insert into NT_GIFTCLASS (cName)values(@className)";
            }
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }
    }
}
