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 ToVote : DbBase, IToVote
    {
        /// <summary>
        /// 用户投票
        /// </summary>
        /// <param name="Info">用户投票实体类</param>
        /// <returns>投票成功返回1</returns>
        public int Add(ToVoteInfo Info)
        {
            SqlConnection Conn = new SqlConnection(DBConfig.CnString);
            Conn.Open();
            SqlTransaction trans = Conn.BeginTransaction();
            try
            {
                SqlParameter[] param = getParameters(Info);
                //检测是否已投过票，投过票直接返回-1
                string Sql = "Select Count([ID]) From [NT_ToVote] Where [UserID]=@UserID And [VoteID]=@VoteID";
                int n = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, Sql, param));
                if (n == 0)
                {
                    if (Info.OptionID != "" && Info.Content != "")
                    {
                        Sql = "Insert Into [NT_ToVote]([UserID],[VoteID],[OptionID],[Content]) Values(@UserID,@VoteID,@OptionID,@Content)";
                        n = Convert.ToInt32(DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param));

                        Sql = "Update [NT_Vote] Set [JCnt]=[JCnt]+1,[Vcnt]=[Vcnt]+1 Where ID=@VoteID";
                        DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param);

                        //增加投票选项数量
                        Sql = "Update [Nt_VoteOption] Set [Cnt]=[Cnt]+1 Where [ID] In(" + Info.OptionID + ")";
                        DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param);
                    }
                    //检测用户是否已评论过此投票，如果投票过直接更新投票数,否则同时更新投票数与参与人数
                    else if (!string.IsNullOrEmpty(Info.OptionID))
                    {
                        Sql = "Insert Into [NT_ToVote]([UserID],[VoteID],[OptionID]) Values(@UserID,@VoteID,@OptionID)";
                        n = Convert.ToInt32(DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param));

                        Sql = "Update [NT_Vote] Set [JCnt]=[JCnt]+1,[Vcnt]=[Vcnt]+1 Where ID=@VoteID";
                        DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param);

                        //增加投票选项数量
                        Sql = "Update [Nt_VoteOption] Set [Cnt]=[Cnt]+1 Where [ID] In(" + Info.OptionID + ")";
                        DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param);
                    }
                    else
                    {
                        Sql = "Insert Into [NT_ToVote]([UserID],[VoteID],[Content]) Values(@UserID,@VoteID,@Content)";
                        n = Convert.ToInt32(DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param));

                        Sql = "Update [NT_Vote] Set [JCnt]=[JCnt]+1 Where ID=@VoteID";
                        DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param);
                    }

                    //取得投票发起者编号
                    Sql = "Select [UserID] From [NT_Vote] Where [ID]=" + Info.VoteID;
                    int suid = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, Sql, null));
                    JUser ju = new JUser();
                    if (ju.CheckPrivacyInfo(trans, Info.UserID, "ActMovieComment"))
                    {
                        UserDyn(trans, 0, 0, "joinvote", Info.UserID, 0, Info.VoteID, "参与了一个投票", Info.VoteID.ToString());
                    }
                    if (suid != Info.UserID)
                    {
                        Notify(trans, EnumNotifyType.ToVote, Info.UserID, suid, "参与了一个投票", Info.VoteID);
                    }
                    //                    Notify(trans, EnumNotifyType.VoteComment, Info.UserID, uid, "回复了评论", id);
                }
                else
                {
                    Sql = "Select Count([ID]) From [NT_ToVote] Where [UserID]=@UserID And [VoteID]=@VoteID And [OptionID] is Not Null";
                    int k = Convert.ToInt32(DbHelper.ExecuteScalar(trans, CommandType.Text, Sql, param));
                    if (k > 0)
                    {
                        Sql = "Update [NT_ToVote] Set [Content]=@Content Where [UserID]=@UserID And [VoteID]=@VoteID";
                        n = Convert.ToInt32(DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param));
                    }
                    else
                    {
                        if (Info.OptionID != "" && Info.Content != "")
                        {
                            Sql = "Update [NT_ToVote] Set [OptionID]=@OptionID,[Content]=@Content Where [UserID]=@UserID And [VoteID]=@VoteID";
                            n = Convert.ToInt32(DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param));

                            Sql = "Update [NT_Vote] Set [Vcnt]=[Vcnt]+1 Where ID=@VoteID";
                            DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param);

                            //增加投票选项数量
                            Sql = "Update [Nt_VoteOption] Set [Cnt]=[Cnt]+1 Where [ID] In(" + Info.OptionID + ")";
                            DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param);
                        }
                        //检测用户是否已评论过此投票，如果投票过直接更新投票数,否则同时更新投票数与参与人数
                        else if (Info.OptionID != "")
                        {
                            Sql = "Update [NT_ToVote] Set [OptionID]=@OptionID Where [UserID]=@UserID And [VoteID]=@VoteID";
                            n = Convert.ToInt32(DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param));

                            Sql = "Update [NT_Vote] Set [Vcnt]=[Vcnt]+1 Where ID=@VoteID";
                            DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param);

                            //增加投票选项数量
                            Sql = "Update [Nt_VoteOption] Set [Cnt]=[Cnt]+1 Where [ID] In(" + Info.OptionID + ")";
                            DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param);
                        }
                        else
                        {
                            Sql = "Update [NT_ToVote] Set [Content]=@Content Where [UserID]=@UserID And [VoteID]=@VoteID";
                            n = Convert.ToInt32(DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param));

                            Sql = "Update [NT_Vote] Set [JCnt]=[JCnt]+1 Where ID=@VoteID";
                            DbHelper.ExecuteNonQuery(trans, CommandType.Text, Sql, param);
                        }
                    }
                }
                trans.Commit();
                return n;
            }
            catch (SqlException e)
            {
                trans.Rollback();
                throw e;
            }
            finally
            {
                if (Conn.State == ConnectionState.Open)
                    Conn.Close();
            }
        }


        /// <summary>
        /// 取得用户是否已投票
        /// </summary>
        /// <param name="UserID">用户编号</param>
        /// <param name="VoteID">投票编号</param>
        /// <returns>如果已投票返回true,否则false</returns>
        public bool IsVote(int UserID, int VoteID)
        {
            bool tf = false;
            string Sql = "Select Count([ID]) From [NT_ToVote] Where [UserID]=" + UserID + " And [VoteID]=" + VoteID + " And [OptionID] is Not Null";
            int n = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, Sql, null));
            if (n > 0)
                tf = true;
            return tf;
        }

        /// <summary>
        /// 投票信息
        /// </summary>
        /// <param name="VoteID">投票编号</param>
        /// <param name="UserID">用户编号</param>
        /// <returns></returns>
        public ToVoteInfo Info(int VoteID, int UserID)
        {
            ToVoteInfo info = new ToVoteInfo();
            string Sql = "Select [ID],[UserID],[VoteID],[OptionID],[Content] From [NT_ToVote] Where [UserID]=" + UserID + " And [VoteID]=" + VoteID;
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            if (rd.Read())
            {
                info.ID = Convert.ToInt32(rd["ID"].ToString());
                info.UserID = Convert.ToInt32(rd["UserID"].ToString());
                info.VoteID = Convert.ToInt32(rd["VoteID"].ToString());
                if (rd["OptionID"] != DBNull.Value) info.OptionID = rd["OptionID"].ToString();
                if (rd["Content"] != DBNull.Value) info.Content = rd["Content"].ToString();
            }
            rd.Close();
            return info;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="Info">投票选项实体类</param>
        /// <returns>返回SQL参数</returns>
        private SqlParameter[] getParameters(ToVoteInfo Info)
        {
            SqlParameter[] param = new SqlParameter[5];

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

            param[3] = new SqlParameter("@OptionID", SqlDbType.NVarChar,3000);
            param[3].Value = Info.OptionID;
            param[4] = new SqlParameter("@Content", SqlDbType.NVarChar, 200);
            param[4].Value = Info.Content;

            return param;
        }
    }
}
