您目前尚未登陆,请选择【登陆】或【注册
首页->电子商务->MyShop网络商城源码(mvc开发)>>BLL/Vote.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:MyShop网络商城源码(mvc开发)
当前文件:文件类型 MyShop/BLL/Vote.cs打开代码结构图
高亮显示
		            using System;
using System.Collections.Generic;
using System.Data;
using MyShop.DALFactory;
using MyShop.IDAL;
using MyShop.Model;

namespace MyShop.BLL
{
    public class Vote:VoteRules
    {
        private IVote dal = DataAccess.CreateVote();
        private int _id;

        public Vote (){}

        public Vote(int id)
        {
            this._id = id;
        }

        #region  IVote

        public int Add(VoteInfo model)
        {
            if (model == null)
            {
                return 0;
            }
            return dal.Add(model);
        }

        public int Delete(string filter)
        {
            if (string.IsNullOrEmpty(filter))
                return 0;
            return dal.Delete(filter);
        }

        public bool Exist(string filter)
        {
            filter = filter.Trim();
            if (string.IsNullOrEmpty(filter))
                return false;
            return dal.Exist(filter);
        }
        public DataSet GetDataSet()
        {
            return dal.GetDataSet();
        }

        public DataSet GetDataSet(string filter)
        {
            filter = filter.Trim();
            if (string.IsNullOrEmpty(filter))
                return null;
            return dal.GetDataSet(filter);
        }

        public VoteInfo GetModel(DataRow dr)
        {
            if (dr == null)
                return null;
            return dal.GetModel(dr);
        }

        private DataSet Query(string sql)
        {
            sql = sql.Trim();
            if (string.IsNullOrEmpty(sql))
                return null;
            return dal.Query(sql);
        }

        public int Update(VoteInfo model, string filter)
        {
            if (model == null)
                return 0;
            filter = filter.Trim();
            if (string.IsNullOrEmpty(filter))
                return 0;
            return dal.Update(model, filter);
        }

        #endregion


        #region common

        public int Add(VoteInfo model, out string msg)
        {
            msg = "";
            if (model == null)
            {
                msg = msg + "<li>数据不能为空</li>";
                return 0;
            }
            bool isErr = false;

            if (string.IsNullOrEmpty(model.Title.Trim()))
            {
                msg = msg + "<li>主题不能为空</li>";
                isErr = true;
            }
            if (isErr)
                return 0;

            int count = 0;
            count = Add(model);
            if (count == 0)
                msg = "<li>系统发生错误,请重新添加!</li>";
            if (count == 1)
                msg = "<li>添加成功!</li>";
            return count;
        }

        public int Delete(int id)
        {
            if (string.IsNullOrEmpty(id.ToString()))
                return 0;
            string filer;
            filer = " id =" + id;
            return Delete(filer);
        }
        public int Update(VoteInfo model)
        {

            if (model == null)
            {
                return 0;
            }
            string filter;
            filter = " id=" + model.ID;
            return Update(model, filter);
        }

        public VoteInfo GetModel(int id)
        {
            DataSet dataset = new DataSet();
            dataset = GetDataSet(" id=" + id);
            if (dataset != null && dataset.Tables[0].Rows.Count > 0)
                return GetModel(dataset.Tables[0].Rows[0]);
            return null;
        }

        #endregion




        #region 后台管理

        public DataSet GetDataSetDesc()
        {
            return GetDataSet(" [id] > 0  order by [Id] desc ");
        }

        /// <summary>
        ///  得到调查项目选项的集合
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public VoteOption[] GetOptions(int id)
        {
            VoteInfo model = new VoteInfo();
            model = GetModel(id);
            if (model == null)
                return null;
            string[] content = Utils.SplitString(model.Content, "§c§");

            VoteOption[] options = new VoteOption[content.Length];

            for (int i = 0; i < content.Length; i++)
            {
                string[] op = Utils.SplitString(content[i], "◎n◎");

                if (op.Length > 1)
                {
                    //防止程序出错
                    options[i].content = op[0];
                    options[i].number = Utils.StrToInt(op[1], 0);
                }
                else
                    options[i].content = op[0];
            }

            return options;
        }


        /// <summary>
        /// 返回最新调查
        /// </summary>
        /// <returns></returns>
        public DataSet GetDataSetSelected
        {
            get
            {
                return GetDataSet(" [IsSelected] = 1  ");
            }
        }

        /// <summary>
        /// 返回最新调查
        /// </summary>
        /// <returns></returns>
        public VoteInfo GetModelSelected
        {
            get
            {
                if (GetDataSetSelected.Tables[0].Rows.Count > 0)
                    return GetModel(GetDataSetSelected.Tables[0].Rows[0]);
                else
                    return null;
            }
        }

        /// <summary>
        ///  得到最新调查项目选项的集合
        /// </summary>
        /// <returns></returns>
        public VoteOption[] GetOptionsSelected
        {
            get
            {
                VoteInfo model = new VoteInfo();
                model = GetModelSelected;
                if (model == null)
                    return null;
                return GetOptions(model.ID);
            }
        }

        /// <summary>
        /// 把VoteOption[]数组转换为字符串
        /// </summary>
        /// <param name="options"></param>
        /// <returns>content</returns>
        public string OptionsToContent(VoteOption[] options)
        {
            string content = "";
            for (int i = 0; i < options.Length; i++)
            {
                content = content + options[i].content + "◎n◎" + options[i].number;
                if (i < options.Length - 1)
                    content = content + "§c§";
            }
            return content;
        }

        #endregion    
    
        #region 以下过程要用Vote vote = new Vote(id)实例化后才能进行调用


            /// <summary>
            /// 增加票数
            /// </summary>
            /// <param name="optionIndex">在选项集合中的位置,从0开始算起</param>
            /// <param name="num">要增加或减少的票数,加用正数,减用负数</param>
            public void AddNumber(int optionIndex, int num)
            {
                VoteOption[] options = Options;
                if (optionIndex > options.Length)
                    return;
                options[optionIndex].number += num;
                Options = options;
            }

            #region property

            /// <summary>
            /// 获取当前项目ID
            /// </summary>
            public int ID
            {
                get
                {
                    return this._id;
                }
            }

            /// <summary>
            ///  获取调查项目的实体
            /// </summary>
            public VoteInfo Model
            {
                get
                {
                    return GetModel(this._id);
                }
                set
                {
                    Update(value);
                }
            }

            /// <summary>
            /// 获取当前对象是否存在(数据库中有没有这条记录)
            /// </summary>
            public bool IsExist
            {
                get
                {
                    return Model == null ? false : true;
                }
            }

            /// <summary>
            /// 获取项目的调查结束时间是否已到
            /// </summary>
            public bool IsTimeOut
            {
                get
                {
                    return DateTime.Compare(DateTime.Now, DateTime.Parse(Model.EndTime)) <= 0 ? true : false;
                }

            }

            /// <summary>
            /// 获取或设置调查项目的类型,多选为true,单选为false
            /// </summary>
            public DateTime EndTime
            {
                get
                {
                    return DateTime.Parse(Model.EndTime);
                }
                set
                {
                    VoteInfo model = Model;
                    model.EndTime = value.ToString();
                    Update(model);
                }
            }

            /// <summary>
            /// 获取或设置调查项目的类型,多选为true,单选为false
            /// </summary>
            public bool IsMultiType
            {
                get
                {
                    return Model.VoteType == 0 ? false : true;
                }
                set
                {
                    VoteInfo model = new VoteInfo();
                    model = Model;
                    model.VoteType = value ? 1 : 0;

                    Update(model);
                }
            }


            /// <summary>
            ///  获取或设置调查项目选项的集合
            /// </summary>
            /// <returns></returns>
            public  VoteOption[] Options
            {
                get
                {
                    return GetOptions(ID);
                }

                set
                {
                    string content = "";
                    for (int i = 0; i < value.Length ; i++)
                    {
                        content = content + value[i].content + "◎n◎" + value[i].number;
                        if (i < value.Length -1)
                            content = content + "§c§";
                    }

                    VoteInfo model = new VoteInfo();
                    model = Model;
                    model.Content = content;
                    Model = model;
                }
            }

            /// <summary>
            /// 获取调查项目投票总人数
            /// </summary>
            public int VoterCount
            {
                get
                {
                    int number = 0;
                    foreach (VoteOption op in Options)
                    {
                        number += op.number;
                    }
                    return number;
                }
            }


            /// <summary>
            /// 获取项目的选项数量
            /// </summary>
            public int OptionCount
            {
                get { return Options.Length; }
            }


            /// <summary>
            ///  获取或设置项目为最新调查项目,true为是;false为不是
            /// </summary>
            public bool IsSelected
            {
                get
                {
                    return Model.IsSelected == 1 ? true : false;
                }

                set
                {
                    VoteInfo model = new VoteInfo();
                    if (value)
                    {
                        string filter = " [IsSelected] = 1 ";
                        DataSet dataset = new DataSet();

                        dataset = GetDataSet(filter);
                        //把其它的改为不是最新调查
                        foreach (DataRow dr in dataset.Tables[0].Rows)
                        {
                            if (!dr["id"].ToString().Equals(this._id.ToString()))
                            {
                                model = GetModel(dr);
                                model.IsSelected = 0;
                                Update(model);
                            }
                        }

                    }

                    model = Model;
                    model.IsSelected = value ? 1 : 0;
                    Model = model;
                }

            }

            /// <summary>
            /// 获取是否允许投票
            /// </summary>
            /// <param name="msg"></param>
            /// <returns></returns>
            public bool IsAllowed(out string msg)
            {
                bool allowed = true;
                msg = "";
                if (!TimeAllowed("9"))
                {
                    msg = "<li>当前时间不允许投票<li>";
                    allowed = false;
                }
                if (TimeAllowed("9") && IPAllowed("127.0.0.1", "127.0.0.1"))
                {
                    msg = msg + "<li>当前IP不允许投票<li>";
                    allowed = false;
                }
                return allowed;
            }

            #endregion

        #endregion
    }

    /// <summary>
    /// 投票规则
    /// </summary>
    public class VoteRules
    {

        public VoteRules() { }

        /// <summary>
        /// 每天允许投票的时间(整点计算)
        /// </summary>
        /// <param name="forbiddenTime">不允许投票的时间段,各时间用逗号相隔</param>
        /// <returns></returns>
        public bool TimeAllowed(string forbiddenTime)
        {
            string[] time = forbiddenTime.Split(',');
            for (int i = 0; i < time.Length; i++)
            {
                if(time[i].Equals(DateTime.Now.Hour ) )
                    return false;
            }
            return true;
        }

        /// <summary>
        /// 验证该Ip是否允许投票
        /// </summary>
        /// <param name="forbiddenIp">不允许投票的Ip段,各ip用逗号相隔</param>
        /// <param name="ip"></param>
        /// <returns></returns>
        public bool IPAllowed(string forbiddenIp, string ip)
        {
            return forbiddenIp.IndexOf(ip) >= 0 ? false : true;
            
        }




    }

    /// <summary>
    /// 调查项目的选项结构
    /// </summary>
    public  struct VoteOption
    {
        /// <summary>
        /// 选项内容
        /// </summary>
        public string content;
        /// <summary>
        /// 选项所得票数
        /// </summary>
        public int number;


        public VoteOption(string content, int number)
        {
            this.content = content;
            this.number = number;
        }
    }


}


        
还没有找到您心仪的内容?请用.net源码大搜捕
代码片断 打包下载该项目完整源码:MyShop网络商城源码(mvc开发)
51Aspx.com 版权所有 CopyRight © 2000-2008. 京ICP备06046876号