public static string GetRuleTableRowClass(Rule rule)
 {
     if (rule.Production)
     {
         return "success";
     }
     if (!rule.Production && rule.Sandbox)
     {
         return "info";
     }
     return "danger";
 }
        public static void CreateRule()
        {
            var serializer = new JSSerializer();
            OrmLiteRepository<Rule> table = new OrmLiteRepository<Rule>();

            Rule rule = new Rule();
            rule.ActivateBonusInDays = 1;
            rule.ChargeAmount = 100;
            rule.BonusLifeTime = 30;
            rule.Production = true;
            rule.Sandbox = true;
            rule.ActivateInDate = DateTime.Now;
            rule.ActivateOnCharge = true;
            rule.ChargePercent = 50;
            rule.LifeTimeFrom = DateTime.Now;
            rule.LifeTimeTo = DateTime.Now.AddDays(40);
            rule.Priority = 2;
            rule.Name = "Rule 2";

            ConditionGroup mainGroup = new ConditionGroup();
            mainGroup.ExpressionType = ExpressionType.AND;

            Condition cond1 = new Condition(new BaseCondition());
            Dictionary<string, object> cond1Settings = new Dictionary<string, object>();
            cond1Settings.Add("Date", DateTime.Now.AddDays(-40));
            cond1.ConditionItem.Settings = cond1Settings;
            cond1.ConditionItem.ConditionId = new Guid("2be12ef8-00b9-47c5-a0cb-d6f15fb02aa5");
            mainGroup.Add(cond1);

            ConditionGroup subGroup1 = new ConditionGroup();
            subGroup1.ExpressionType = ExpressionType.AND;

            Condition cond2 = new Condition(new BaseCondition());
            Dictionary<string, object> cond2Settings = new Dictionary<string, object>();
            cond2Settings.Add("Date", DateTime.Now.AddDays(-40));
            cond2.ConditionItem.Settings = cond2Settings;
            cond2.ConditionItem.ConditionId = new Guid("2be12ef8-00b9-47c5-a0cb-d6f15fb02aa5");
            subGroup1.Add(cond2);
            mainGroup.Add(subGroup1);
            var conditionJson = serializer.Serialize(mainGroup);

            rule.Conditions = conditionJson;

            LocalRestrictionBase localRestriction = new LocalRestrictionBase();
            localRestriction.RestrictionId = new Guid("C6EB436E-1021-45A5-B937-30B6B7BE1970");
            localRestriction.Name = "LocalRestriction от бога";

            var restrictionSettings = new Dictionary<string, object>();
            restrictionSettings.Add("Date", DateTime.Now);
            localRestriction.Settings = restrictionSettings;

            LocalActionBase localAction = new LocalActionBase();
            localAction.ActionId = new Guid("EB54BD15-D953-4986-AC89-47A9F5E527C0");
            localAction.Name = "LocalAction от бога";

            var actionSettings = new Dictionary<string, object>();
            actionSettings.Add("Date", DateTime.Now);
            localAction.Settings = actionSettings;

            List<LocalRestrictionBase> restrictionList = new List<LocalRestrictionBase>();
            restrictionList.Add(localRestriction);

            List<LocalActionBase> actionList = new List<LocalActionBase>();
            actionList.Add(localAction);

            rule.LocalActions = serializer.Serialize(actionList);
            rule.LocalRestrictions = serializer.Serialize(restrictionList);

            table.SafeAdd(rule);

            //Console.WriteLine(json);
            //Console.ReadKey();
        }
        public JsonResult Save([ModelBinder(typeof(SaveRuleResponseBinder))] SaveRuleResponse response)
        {
            RuleEditMode mode = RuleEditMode.Edit;
            if (response.RuleId == Guid.Empty)
                mode = RuleEditMode.Create;

            //var serializer = ProcessingEngine.Instance.Container.Resolve<ISerializer>();

            Rule rule;
            var ruleRepository = new Orm.OrmLiteRepository<Rule>();

            switch (mode)
            {
                case RuleEditMode.Create:
                    rule = new Rule();
                    break;
                case RuleEditMode.Edit:
                    rule = ruleRepository.GetById(response.RuleId);
                    break;
                default:
                    rule = new Rule();
                    break;
            }

            //TODO Можно было бы биндить напрямую в Rule, если бы не RuleId
            rule.Name = response.Name;
            rule.ActivateBonusInDays = response.ActivateInDays;
            if(!response.ActivateInDate.Equals(DateTime.MinValue))
            {
                rule.ActivateInDate = response.ActivateInDate;
            }

            rule.ActivateOnCharge = response.ActivateOnCharge;
            rule.BonusLifeTime = response.BonusLifeTime;

            if (!response.LifeTimeFrom.Equals(DateTime.MinValue))
            {
                rule.LifeTimeFrom = response.LifeTimeFrom;
            }

            if (!response.LifeTimeTo.Equals(DateTime.MinValue))
            {
                rule.LifeTimeTo = response.LifeTimeTo;
            }

            rule.Priority = response.Priority;
            rule.Production = response.Production;
            rule.Sandbox = response.Sandbox;

            rule.Conditions = response.Conditions;
            rule.LocalActions = response.LocalActions;
            rule.LocalRestrictions = response.LocalRestrictions;

            switch (mode)
            {
                case RuleEditMode.Edit:
                    ruleRepository.Update(rule);
                    break;
                default:
                    ruleRepository.Add(rule);
                    break;
            }

            return Json("Success save");
        }