买赠活动配置与解析<一>

电子商务促销,买赠活动配置:

规则实例:[2012-08-24 09:30:00~2012-08-27 09:30:00](rg000816)[(5N,{N=}),(,{N-1=})]|(rg001963)[(2N,{2N=bz000022}),(2N-1,{2N-1=bz000022})]

解释:商品rg000816在2012-08-24 09:30:00到2012-08-27 09:30:00之间进行买5送一;商品rg001963不限制时间,买一件送一件商品bz000022

相关类:

ProductRule.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Model.LocalRule
{
    /// <summary>
    /// 产品规则
    /// </summary>
    [Serializable]
    public class ProductRule
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        [XmlElement("start_time")]
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        [XmlElement("end_time")]
        public DateTime EndTime { get; set; }

        /// <summary>
        /// 产品编码
        /// </summary>
        [XmlElement("id")]
        public string ProductID { get; set; }

        /// <summary>
        /// 规则原型
        /// </summary>
        [XmlElement("rule")]
        public string RuleString { get; set; }

        /// <summary>
        /// 规则集合
        /// </summary>
        [XmlArray("rules")]
        [XmlArrayItem("rule")]
        public IList<Rule> Rules { get; set; }

    }
}

 

Rule.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Model.LocalRule
{
    /// <summary>
    /// 具体规则
    /// </summary>
    public class Rule
    {
        /// <summary>
        /// 规则条件
        /// </summary>
        [XmlElement("condition")]
        public RuleCondition Condition { get; set; }

        /// <summary>
        /// 规则结果
        /// </summary>
        [XmlElement("result")]
        public RuleResult Result { get; set; }

        /// <summary>
        /// 配置表达式
        /// </summary>
        [XmlElement("expression")]
        public string Expression { get; set; }
    }
}
RuleCondition.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Text.RegularExpressions;

namespace Model.LocalRule
{
    /// <summary>
    /// 规则表达式(只限于3N(+|-)1类型)
    /// </summary>
    [Serializable]
    public class RuleCondition
    {
        //匹配正则
        const string RegStr = "(?<F>\\d*)(?<C>N)?\\s*(((?<O>-)|(?<O>\\+)|(?<O>\\u002a)|(?<O>/))\\s*(?<V>\\d+))?";
        /// <summary>
        /// 倍率
        /// </summary>
        [XmlElement("factor")]
        public int Factor { get; set; }

        /// <summary>
        /// 是否常量
        /// </summary>
        [XmlElement("is_const")]
        public bool Const { get; set; }

        /// <summary>
        /// 操作符
        /// </summary>
        [XmlElement("operate")]
        public char Operate { get; set; }

        /// <summary>
        /// 操作数
        /// </summary>
        [XmlElement("value2")]
        public int Value2 { get; set; }

        /// <summary>
        /// 表达式
        /// </summary>
        [XmlElement("expression")]
        public string Expression { get; set; }

        public RuleCondition()
        {
            Factor = 0;
            Const = true;
            Operate = '-';
            Value2 = 0;
        }

        public RuleCondition(string exp)
        {
            if (!string.IsNullOrEmpty(exp) && Regex.IsMatch(exp,RegStr))
            {
                Match m = Regex.Match(exp, RegStr);
                string v = null;
                int f = 0;
                v = m.Result("${F}");
                if (v.Length > 0)
                {
                    int.TryParse(v, out f);
                    Factor = f;
                }

                Const = true;
                v = m.Result("${C}");
                if (v.Length > 0)
                {
                    Const = false;

                    if (Factor == 0)
                    {
                        Factor = 1;//匹配N-1这种情况
                    }
                }

                if (!Const)
                {
                    v = m.Result("${O}");
                    if (v.Length > 0)
                    {
                        char c = char.Parse(v);
                        if (c.Equals('-')
                            || c.Equals('+')
                            || c.Equals('*')
                            || c.Equals('/'))
                        {
                            Operate = c;
                        }
                        else
                        {
                            Operate = '-';
                        }


                        int.TryParse(m.Result("${V}"), out f);

                        Value2 = f;
                    }
                    else
                    {
                        Operate = '-';
                        Value2 = 0;
                    }
                }
                Expression = exp;
            }
        }
    }
}
RuleResult.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Text.RegularExpressions;

namespace Model.LocalRule
{
    /// <summary>
    /// 规则表达式(只限于3N(+|-)1类型)
    /// </summary>
    [Serializable]
    public class RuleResult
    {
        //匹配正则
        //const string RegStr = "[^,]+";一定包含=
        const string RegStr = "(?<F>\\d*)(?<C>N)?\\s*(((?<O>-)|(?<O>\\+)|(?<O>\\u002a)|(?<O>/))\\s*(?<V>\\d+))?=(?<pid>[^,]+)?";
        
        /// <summary>
        /// 应用规则
        /// </summary>
        [XmlArray("Results")]
        [XmlArrayItem("Result")]
        public IList<KeyValuePair<RuleCondition, string>> Rules
        {
            get;
            set;
        }

        /// <summary>
        /// 表达式
        /// </summary>
        [XmlElement("expression")]
        public string Expression { get; set; }

        public RuleResult()
        {
            Rules = new List<KeyValuePair<RuleCondition, string>>();
        }

        public RuleResult(string exp)
        {
            Rules = new List<KeyValuePair<RuleCondition, string>>();
            if (!string.IsNullOrEmpty(exp) && Regex.IsMatch(exp, RegStr))
            {
                MatchCollection mc = Regex.Matches(exp, RegStr);
                string v = null;
                RuleCondition rc = null;
                foreach (Match m in mc)
                {
                    int f = 0;
                    v = m.Result("${F}");
                    rc = new RuleCondition();
                    if (v.Length > 0)
                    {
                        int.TryParse(v, out f);
                        rc.Factor = f;
                    }

                    rc.Const = true;
                    v = m.Result("${C}");
                    if (v.Length > 0)
                    {
                        rc.Const = false;

                        if (rc.Factor == 0)
                        {
                            rc.Factor = 1;//匹配N-1这种结果
                        }
                    }

                    if (!rc.Const)
                    {
                        v = m.Result("${O}");
                        if (v.Length > 0)
                        {
                            char c = char.Parse(v);
                            if (c.Equals('-')
                                || c.Equals('+')
                                || c.Equals('*')
                                || c.Equals('/'))
                            {
                                rc.Operate = c;
                            }
                            else
                            {
                                rc.Operate = '-';
                            }


                            int.TryParse(m.Result("${V}"), out f);

                            rc.Value2 = f;
                        }
                        else
                        {
                            rc.Operate = '-';
                            rc.Value2 = 0;
                        }
                    }

                    v = m.Result("${pid}");//没有配置产品的时候是产品本身
                    rc.Expression = m.Value;
                    Rules.Add(new KeyValuePair<RuleCondition, string>(rc, v));
                }
            }
            Expression = exp;
        }
    }
}
posted @ 2012-08-26 09:12  z.seven  阅读(249)  评论(0编辑  收藏  举报