www.pudn.com > MyShop.rar > 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 + "
  • 数据不能为空
  • "; return 0; } bool isErr = false; if (string.IsNullOrEmpty(model.Title.Trim())) { msg = msg + "
  • 主题不能为空
  • "; isErr = true; } if (isErr) return 0; int count = 0; count = Add(model); if (count == 0) msg = "
  • 系统发生错误,请重新添加!
  • "; if (count == 1) msg = "
  • 添加成功!
  • "; 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 "); } /// /// 得到调查项目选项的集合 /// /// /// 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; } /// /// 返回最新调查 /// /// public DataSet GetDataSetSelected { get { return GetDataSet(" [IsSelected] = 1 "); } } /// /// 返回最新调查 /// /// public VoteInfo GetModelSelected { get { if (GetDataSetSelected.Tables[0].Rows.Count > 0) return GetModel(GetDataSetSelected.Tables[0].Rows[0]); else return null; } } /// /// 得到最新调查项目选项的集合 /// /// public VoteOption[] GetOptionsSelected { get { VoteInfo model = new VoteInfo(); model = GetModelSelected; if (model == null) return null; return GetOptions(model.ID); } } /// /// 把VoteOption[]数组转换为字符串 /// /// /// content 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)实例化后才能进行调用 /// /// 增加票数 /// /// 在选项集合中的位置,从0开始算起 /// 要增加或减少的票数,加用正数,减用负数 public void AddNumber(int optionIndex, int num) { VoteOption[] options = Options; if (optionIndex > options.Length) return; options[optionIndex].number += num; Options = options; } #region property /// /// 获取当前项目ID /// public int ID { get { return this._id; } } /// /// 获取调查项目的实体 /// public VoteInfo Model { get { return GetModel(this._id); } set { Update(value); } } /// /// 获取当前对象是否存在(数据库中有没有这条记录) /// public bool IsExist { get { return Model == null ? false : true; } } /// /// 获取项目的调查结束时间是否已到 /// public bool IsTimeOut { get { return DateTime.Compare(DateTime.Now, DateTime.Parse(Model.EndTime)) <= 0 ? true : false; } } /// /// 获取或设置调查项目的类型,多选为true,单选为false /// public DateTime EndTime { get { return DateTime.Parse(Model.EndTime); } set { VoteInfo model = Model; model.EndTime = value.ToString(); Update(model); } } /// /// 获取或设置调查项目的类型,多选为true,单选为false /// public bool IsMultiType { get { return Model.VoteType == 0 ? false : true; } set { VoteInfo model = new VoteInfo(); model = Model; model.VoteType = value ? 1 : 0; Update(model); } } /// /// 获取或设置调查项目选项的集合 /// /// 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; } } /// /// 获取调查项目投票总人数 /// public int VoterCount { get { int number = 0; foreach (VoteOption op in Options) { number += op.number; } return number; } } /// /// 获取项目的选项数量 /// public int OptionCount { get { return Options.Length; } } /// /// 获取或设置项目为最新调查项目,true为是;false为不是 /// 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; } } /// /// 获取是否允许投票 /// /// /// public bool IsAllowed(out string msg) { bool allowed = true; msg = ""; if (!TimeAllowed("9")) { msg = "
  • 当前时间不允许投票
  • "; allowed = false; } if (TimeAllowed("9") && IPAllowed("127.0.0.1", "127.0.0.1")) { msg = msg + "
  • 当前IP不允许投票
  • "; allowed = false; } return allowed; } #endregion #endregion } /// /// 投票规则 /// public class VoteRules { public VoteRules() { } /// /// 每天允许投票的时间(整点计算) /// /// 不允许投票的时间段,各时间用逗号相隔 /// 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; } /// /// 验证该Ip是否允许投票 /// /// 不允许投票的Ip段,各ip用逗号相隔 /// /// public bool IPAllowed(string forbiddenIp, string ip) { return forbiddenIp.IndexOf(ip) >= 0 ? false : true; } } /// /// 调查项目的选项结构 /// public struct VoteOption { /// /// 选项内容 /// public string content; /// /// 选项所得票数 /// public int number; public VoteOption(string content, int number) { this.content = content; this.number = number; } } }