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 VoteOption : DbBase, IVoteOption
    {
        /// <summary>
        /// 添加投票选项
        /// </summary>
        /// <param name="Info">投票选项实体类</param>
        /// <returns>添加成功返回1</returns>
        public int Add(VoteOptionInfo Info)
        {
            SqlParameter[] param = getParameters(Info);
            string Sql = "Insert Into [NT_VoteOption]([VoteID],[OptionName],[Cnt]) Values(@VoteID,@OptionName,@Cnt)";
            return Convert.ToInt32(DbHelper.ExecuteNonQuery(CommandType.Text, Sql, param));
        }

        /// <summary>
        /// 添加投票选项被选次数
        /// </summary>
        /// <param name="ID">投票选项编号</param>
        /// <returns>添加成功返回1</returns>
        public int AddCnt(int ID)
        {
            string Sql = "Update [NT_VoteOption] Set [Cnt]=[Cnt]+1 Where [ID]=" + ID;
            return Convert.ToInt32(DbHelper.ExecuteNonQuery(CommandType.Text, Sql, null));
        }

        /// <summary>
        /// 投票选项信息
        /// </summary>
        /// <param name="ID">投票选项编号</param>
        /// <returns>返回投票选项实体类</returns>
        public VoteOptionInfo Info(int ID)
        {
            VoteOptionInfo info = new VoteOptionInfo();
            string Sql = "Select [ID],[VoteID],[OptionName],[Cnt] From [NT_VoteOption] Where [ID]=" + ID;
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            if (rd.Read())
            {
                info.ID = Convert.ToInt32(rd["ID"].ToString());
                info.VoteID = Convert.ToInt32(rd["VoteID"].ToString());
                if (rd["OptionName"] != DBNull.Value) info.OptionName = rd["OptionName"].ToString();
                info.Cnt = Convert.ToInt32(rd["Cnt"].ToString());
            }
            rd.Close();
            return info;
        }

        /// <summary>
        /// 选项列表
        /// </summary>
        /// <param name="VoteID">投票编号</param>
        /// <returns>返回列表</returns>
        public List<VoteOptionInfo> OptionList(int VoteID)
        {
            List<VoteOptionInfo> list = new List<VoteOptionInfo>();
            string Sql = "Select [ID],[VoteID],[OptionName],[Cnt] From [NT_VoteOption] Where [VoteID]=" + VoteID;
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            while (rd.Read())
            {
                VoteOptionInfo info = new VoteOptionInfo();
                info.ID = Convert.ToInt32(rd["ID"].ToString());
                info.VoteID = Convert.ToInt32(rd["VoteID"].ToString());
                if (rd["OptionName"] != DBNull.Value) info.OptionName = rd["OptionName"].ToString();
                info.Cnt = Convert.ToInt32(rd["Cnt"].ToString());
                list.Add(info);
            }
            rd.Close();
            return list;
        }

        /// <summary>
        /// 取得用户投票选项
        /// </summary>
        /// <param name="VoteID">投票编号</param>
        /// <param name="UserID">用户编号</param>
        /// <param name="tf">如果用户已投票返回true,否则返回false</param>
        /// <returns>返回列表</returns>
        public List<VoteOptionInfo> OptionList(int VoteID, int UserID, out bool tf)
        {
            List<VoteOptionInfo> list = new List<VoteOptionInfo>();
            tf = false;
            string Sql = "Select Count([ID]) From [NT_ToVote] Where [VoteID]=" + VoteID + " And [UserID]=" + UserID + " And [OptionID] is Not  Null";
            int n = Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.Text, Sql, null));
            if (n > 0)
            {
                tf = true;
                Sql = "Select a.[ID],a.[VoteID],[OptionName],[Cnt] From " +
                      "[NT_VoteOption] as a inner join [NT_ToVote] as b " +
                      "on charindex(','+cast(a.id as varchar(20))+',', ','+b.OptionID+',')>0 " +
                      "and b.[VoteID]=" + VoteID + " And b.[UserID]=" + UserID;
            }
            else
            {
                Sql = "Select [ID],[VoteID],[OptionName],[Cnt] From [NT_VoteOption] Where [VoteID]=" + VoteID;
            }
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            while (rd.Read())
            {
                VoteOptionInfo info = new VoteOptionInfo();
                info.ID = Convert.ToInt32(rd["ID"].ToString());
                info.VoteID = Convert.ToInt32(rd["VoteID"].ToString());
                if (rd["OptionName"] != DBNull.Value) info.OptionName = rd["OptionName"].ToString();
                info.Cnt = Convert.ToInt32(rd["Cnt"].ToString());
                list.Add(info);
            }
            rd.Close();
            return list;
        }

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

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

            param[3] = new SqlParameter("@Cnt", SqlDbType.Int, 4);
            param[3].Value = Info.Cnt;

            return param;
        }
    }
}
