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 Ask : DbBase, IAsk
    {
        public DataTable GetAskClass(int ParentID,int Num)
        {
            string sql = string.Empty;
            if (ParentID > 0)
            {
                if (Num == 0)
                {
                    sql = "select top 6 ID,className,ParentID from NT_askClass where ParentID=" + ParentID + " order by id asc";
                }
                else
                {
                    sql = "select ID,className,ParentID,(select count(id) from NT_ask where NT_ask.ClassID=NT_askClass.ID) as AskCount from NT_askClass where ParentID=" + ParentID + " order by id asc";
                }
            }
            else
            {
                sql = "select ID,className,ParentID from NT_askClass where ParentID=" + ParentID + " order by id asc";
            }
            return DbHelper.ExecuteTable(CommandType.Text, sql, null);
        }

        public DataTable GetXAskList(int Flag, int Num, int ClassID)
        {
            string sql = string.Empty;
            string whereSTR = string.Empty;
            if (ClassID > 0)
            {
                whereSTR = " and ClassID=" + ClassID;
            }
            if (Flag == 0)
            {
                sql = "select top " + Num + " ID,title,classID,isClose,UserID,(select ClassName from NT_askClass where NT_askClass.ID=NT_ask.ClassID) as ClassName from [NT_ask] where ParentID=0 and isLock=0 and isClose=0 " + whereSTR + " Order by newid()";
            }
            else
            {
                sql = "select top " + Num + " ID,title,classID,isClose,UserID,(select ClassName from NT_askClass where NT_askClass.ID=NT_ask.ClassID) as ClassName from [NT_ask] where ParentID=0 and isLock=0 and isClose=0 " + whereSTR + " and JIFEN>=(50*" + Convert.ToInt32(JuSNS.Common.Public.GetXMLValue("intel")) + ") Order by  newid()";
            }
            return DbHelper.ExecuteTable(CommandType.Text, sql, null);
        }

        public int Add(JuSNS.Model.AskInfo ask, int rJiFen, int flg)
        {
            SqlConnection cn = new SqlConnection(DBConfig.CnString);
            cn.Open();
            SqlTransaction trans = cn.BeginTransaction();
            try
            {
                SqlParameter[] param = GetParam(ask);
                string sql = "insert into NT_ask(ClassID,Click,Content,CreatTime,isClose,IsJinji,IsLock,jiFen,ParentID,Pic,Tag,Title,UserID)values(@ClassID,@Click,@Content,@CreatTime,@isClose,@IsJinji,@IsLock,@jiFen,@ParentID,@Pic,@Tag,@Title,@UserID);Select @@IDENTITY;";
                int n = 0;
                if (rJiFen != 10000)
                {
                    JUser ju = new JUser();
                    if (flg == 1)
                    {
                        sql += "update NT_User set integral=integral+" + rJiFen + " where UserID=@UserID";
                        string gsql = "select UserID from NT_ask where ID=" + ask.ParentID;
                        int nUserID = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, gsql, null));
                        n = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, sql, param));
                        if (nUserID != ask.UserID)
                        {
                            int m = Notify(trans, EnumNotifyType.ReplayAsk, ask.UserID, nUserID, "回复了问题", ask.ParentID);
                            if (ju.CheckPrivacyInfo(trans, ask.UserID, "ActLeaveWord"))
                            {
                                UserDyn(trans, 0, 0, "ReplayAsk", ask.UserID, nUserID, ask.ParentID, "回复了问题", n.ToString());
                            }
                        }
                    }
                    else
                    {
                        n = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, sql, param));
                        sql += "update NT_User set integral=integral-" + rJiFen + " where UserID=@UserID";
                        if (ju.CheckPrivacyInfo(trans, ask.UserID, "ActLeaveWord"))
                        {
                            UserDyn(trans, 0, 0, "Creatask", ask.UserID, 0, n, "增加了一个问答", n.ToString());
                        }
                    }
                }
                trans.Commit();
                return n;
            }
            catch
            {
                trans.Rollback();
                throw;
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }

        public int GetChildCount(int UserID, int ID)
        {
            string sql = "select count(ID) from NT_ask Where UserID=" + UserID + " and ParentID=" + ID;
            return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, sql, null));
        }

        public int Del(int UserID, int ID)
        {
            string sql = "delete from NT_ask where ID=" + ID + " and UserID=" + UserID + "";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        }

        public AskInfo GetAskInfo(int ID)
        {
            AskInfo aInfo = new AskInfo();
            string sql = "select a.*,(select count(b.id) from NT_ask b where b.ParentID=a.ID) as qCount from NT_ask a where a.ID=" + ID;
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, sql, null);
            if (rd.Read())
            {
                aInfo.ClassID = (int)rd["ClassID"];
                aInfo.Click = (int)rd["Click"];
                aInfo.Content = (string)rd["Content"];
                if (rd["CreatTime"] != DBNull.Value) aInfo.CreatTime = DateTime.Parse(rd["CreatTime"].ToString());
                aInfo.ID = (int)rd["ID"];
                aInfo.isClose = Convert.ToInt32(rd["isClose"]);
                aInfo.IsJinji = Convert.ToInt32(rd["IsJinji"]);
                aInfo.IsLock = Convert.ToInt32(rd["IsLock"]);
                aInfo.jiFen = (int)rd["jiFen"];
                aInfo.ParentID = (int)rd["ParentID"];
                if (rd["Pic"] != DBNull.Value) aInfo.Pic = (string)rd["Pic"];
                if (rd["Tag"] != DBNull.Value) aInfo.Tag = (string)rd["Tag"];
                aInfo.Title = rd["title"].ToString();
                aInfo.UserID = (int)rd["UserID"];
                aInfo.qCount = (int)rd["qCount"];
                if (rd["isBest"] != DBNull.Value) aInfo.isBest = Convert.ToInt32(rd["isBest"]);
            }
            rd.Close();
            return aInfo;
        }

        public void UpdateAskBest(int ID, int UserID)
        {
            SqlConnection cn = new SqlConnection(DBConfig.CnString);
            cn.Open();
            SqlTransaction trans = cn.BeginTransaction();
            try
            {
                string sqltf = "select ParentID from NT_ask where ID=" + ID;
                int gID = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, sqltf, null));
                int gUserID = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, "select UserID from NT_ask where isclose=0 and ID=" + gID, null));
                int JiFen = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, "select JiFen from NT_ask where isclose=0 and ID=" + gID, null));
                int DUserID = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, "select UserID from NT_ask where ID=" + ID, null));
                if (gUserID == UserID)
                {
                    string sql = "update NT_ask set isBest=1 where ID=" + ID;
                    sql += "update NT_ask set isclose=1 where ID=" + gID;
                    //给相应用户积分并给他发通知
                    //sql += "update NT_User set integral=integral+" + JiFen + " where UserID=" + DUserID;
                    JUser ju = new JUser();
                    if (ju.CheckPrivacyInfo(trans, UserID, "ActLeaveWord"))
                    {
                        UserDyn(trans, 0, 0, "askBest", UserID, DUserID, ID, "设为最佳答案", gID.ToString());
                    }
                    if (UserID != DUserID)
                    {
                        Notify(trans, EnumNotifyType.AskBest, UserID, DUserID, "设为最佳答案", gID);
                        //增加积分
                        User us = new User();
                        us.updateinte(DUserID, JiFen, 0, 0, "问题被设为最佳答案");
                    }
                    DbHelper.ExecuteNonQuery(trans, CommandType.Text, sql, null);
                }
                trans.Commit();
            }
            catch
            {
                trans.Rollback();
                throw;
            }
            finally
            {
                if (cn.State == ConnectionState.Open)
                    cn.Close();
            }
        }

        public void UpdateAskClick(int ID)
        {
            string sql = "update NT_ask set click=click+1 where ID=" + ID;
            DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        }

        public List<AskInfo> GetChildAskList(int ID)
        {
            List<AskInfo> InfoList = new List<AskInfo>();
            string sql = "Select * from NT_ask Where [ParentID]=" + ID + " order by isBest desc,id asc";
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, sql, null);
            while (rd.Read())
            {
                AskInfo aInfo = new AskInfo();
                aInfo.ClassID = (int)rd["ClassID"];
                aInfo.Click = (int)rd["Click"];
                aInfo.Content = (string)rd["Content"];
                if (rd["CreatTime"] != DBNull.Value) aInfo.CreatTime = DateTime.Parse(rd["CreatTime"].ToString());
                aInfo.ID = (int)rd["ID"];
                aInfo.isClose = Convert.ToInt32(rd["isClose"]);
                aInfo.IsJinji = Convert.ToInt32(rd["IsJinji"]);
                aInfo.IsLock = Convert.ToInt32(rd["IsLock"]);
                aInfo.jiFen = (int)rd["jiFen"];
                aInfo.ParentID = (int)rd["ParentID"];
                if (rd["Pic"] != DBNull.Value) aInfo.Pic = (string)rd["Pic"];
                if (rd["Tag"] != DBNull.Value) aInfo.Tag = (string)rd["Tag"];
                aInfo.Title = rd["title"].ToString();
                aInfo.UserID = (int)rd["UserID"];
                if (rd["isBest"] != DBNull.Value) aInfo.isBest = Convert.ToInt32(rd["isBest"]);
                InfoList.Add(aInfo);
            }
            rd.Close();
            return InfoList;
        }

        public int askClassAdd(int ParentID, string className)
        {
            string sql = "insert into NT_askClass(ParentID,className)values(" + ParentID + ",'" + className + "')";
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
        }

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

        public int EditInfo(int UserID, int qID, string Content)
        {
            SqlParameter param = new SqlParameter("@Content", Content);
            string sql = "update NT_ask Set Content=@Content where UserID=" + UserID + " and ID=" + qID;
            return DbHelper.ExecuteNonQuery(CommandType.Text, sql, param);
        }

        private SqlParameter[] GetParam(AskInfo ui)
        {
            SqlParameter[] param = new SqlParameter[14];
            param[0] = new SqlParameter("@ID", SqlDbType.Int, 4);
            param[0].Value = ui.ID;
            param[1] = new SqlParameter("@ClassID", SqlDbType.Int, 4);
            param[1].Value = ui.ClassID;
            param[2] = new SqlParameter("@Click", SqlDbType.Int, 4);
            param[2].Value = ui.Click;
            param[3] = new SqlParameter("@Content", SqlDbType.NVarChar, 2000);
            param[3].Value = ui.Content;
            param[4] = new SqlParameter("@CreatTime", SqlDbType.DateTime, 8);
            param[4].Value = ui.CreatTime;
            param[5] = new SqlParameter("@isClose", SqlDbType.TinyInt, 1);
            param[5].Value = ui.isClose;
            param[6] = new SqlParameter("@IsJinji", SqlDbType.TinyInt, 1);
            param[6].Value = ui.IsJinji;
            param[7] = new SqlParameter("@IsLock", SqlDbType.TinyInt, 1);
            param[7].Value = ui.IsLock;
            param[8] = new SqlParameter("@jiFen", SqlDbType.Int, 4);
            param[8].Value = ui.jiFen;
            param[9] = new SqlParameter("@ParentID", SqlDbType.Int, 4);
            param[9].Value = ui.ParentID;
            param[10] = new SqlParameter("@Pic", SqlDbType.NVarChar, 50);
            param[10].Value = ui.Pic;
            param[11] = new SqlParameter("@Tag", SqlDbType.NVarChar, 100);
            param[11].Value = ui.Tag;
            param[12] = new SqlParameter("@Title", SqlDbType.NVarChar, 100);
            param[12].Value = ui.Title;
            param[13] = new SqlParameter("@UserID", SqlDbType.Int, 4);
            param[13].Value = ui.UserID;
            return param;
        }
    }
}
