Exemplo n.º 1
0
 /// <summary>
 /// compare 2 TblRule if same ColumnName, Relation, Condition, CollectionID
 /// </summary>
 /// <param name="rule1"></param>
 /// <param name="rule2"></param>
 /// <returns></returns>
 public bool Equals(TblRule rule1, TblRule rule2)
 {
     try
     {
         if (CheckRuleValid(rule1) && CheckRuleValid(rule2))
         {
             if (rule1.ColumnName.Equals(rule2.ColumnName))
             {
                 if (rule1.Relation.Equals(rule2.Relation))
                 {
                     if (rule1.ConditionValue.Equals(rule2.ConditionValue))
                     {
                         if (rule1.CollectionID == rule2.CollectionID)
                         {
                             return(true);
                         }
                     }
                 }
             }
         }
         return(false);
     }
     catch (Exception ex)
     {
         LogService.WriteException(ex);
         return(false);
     }
 }
Exemplo n.º 2
0
        public ActionResult create()
        {
            try
            {
                Collection collection = new Collection();
                collection.Type            = true;
                collection.CollectionState = true;
                collection.TemplateLayouts = new SelectList(new List <SelectListItem>
                {
                    new SelectListItem {
                        Selected = true, Text = "collection", Value = "collection"
                    },
                    new SelectListItem {
                        Selected = true, Text = "collection.list", Value = "collection.list"
                    },
                }, "Value", "Text", "1");

                TblRule rule = new TblRule();
                //rule.ColumnName = "ProductName";
                //rule.Relation = "equals";
                collection.TblRules.Add(rule);

                return(View(collection));
            }
            catch (Exception ex)
            {
                LogService.WriteException(ex);
                return(RedirectToAction("index", "collections"));
            }
        }
Exemplo n.º 3
0
        public ActionResult addRule(int index)
        {
            TblRule tblRule = new TblRule();

            ViewBag.index = index;
            return(PartialView(tblRule));
        }
Exemplo n.º 4
0
 public TblRule GetByPrimaryKey(int ruleID)
 {
     try
     {
         string  query   = "select * from TblRule where RuleID = " + SNumber.ToNumber(ruleID);
         TblRule tblRule = connect.Query <TblRule>(query).FirstOrDefault <TblRule>();
         return(tblRule);
     }
     catch (Exception ex)
     {
         LogService.WriteException(ex);
         return(null);
     }
 }
Exemplo n.º 5
0
 public bool Update(TblRule tblRule)
 {
     try
     {
         string query = "update TblRule set CollectionID=@CollectionID,ColumnName=@ColumnName,Relation=@Relation," +
                        " ConditionValue=@ConditionValue where RuleID = @RuleID";
         int temp = connect.Execute(query, new { tblRule.CollectionID, tblRule.ColumnName, tblRule.Relation, tblRule.ConditionValue, tblRule.RuleID });
         return(temp > 0);
     }
     catch (Exception ex)
     {
         LogService.WriteException(ex);
         return(false);
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// get string contains condition for a rule
        /// </summary>
        /// <param name="rule"></param>
        /// <returns></returns>
        public static string GetCondition(TblRule rule)
        {
            try
            {
                string result = "";

                if (!string.IsNullOrEmpty(rule.ColumnName) && !string.IsNullOrEmpty(rule.Relation) && !string.IsNullOrEmpty(rule.ConditionValue))
                {
                    switch (rule.ColumnName)
                    {
                    case "ProductName":
                        result += "Tên sản phẩm ";
                        break;

                    case "ProductStyle":
                        result += "Loại sản phẩm ";
                        break;

                    case "Supplier":
                        result += "Nhà sản xuất ";
                        break;

                    default:
                        break;
                    }
                    switch (rule.Relation)
                    {
                    case "equals":
                        result = "bằng ";
                        break;

                    case "contains":
                        result += "chứa ";
                        break;

                    default:
                        break;
                    }
                    result += rule.ConditionValue;
                }
                return(result);
            }
            catch (Exception ex)
            {
                LogService.WriteException(ex);
                return("");
            }
        }
Exemplo n.º 7
0
 public int Insert(TblRule tblRule)
 {
     try
     {
         string query = "insert into TblRule(CollectionID,ColumnName,Relation,ConditionValue)" +
                        " values (@CollectionID,@ColumnName,@Relation,@ConditionValue)" +
                        " SELECT @@IDENTITY";
         int tblRuleID = connect.Query <int>(query, new { tblRule.CollectionID, tblRule.ColumnName, tblRule.Relation, tblRule.ConditionValue }).Single();
         return(tblRuleID);
     }
     catch (Exception ex)
     {
         LogService.WriteException(ex);
         return(0);
     }
 }
Exemplo n.º 8
0
 /// <summary>
 /// check all information of rule exist
 /// </summary>
 /// <param name="rule"></param>
 /// <returns></returns>
 public bool CheckRuleValid(TblRule rule)
 {
     try
     {
         if (string.IsNullOrEmpty(rule.ColumnName) || string.IsNullOrEmpty(rule.Relation) || string.IsNullOrEmpty(rule.ConditionValue))
         {
             return(false);
         }
         return(true);
     }
     catch (Exception ex)
     {
         LogService.WriteException(ex);
         throw;
     }
 }
Exemplo n.º 9
0
        /// <summary>
        /// get condition with a rule to select list product in collection
        /// </summary>
        /// <param name="rule">rule will gen conditon</param>
        /// <returns>condition find</returns>
        public static string GetConditionProductByARule(TblRule rule)
        {
            try
            {
                string condition = "";
                if (!string.IsNullOrEmpty(rule.ColumnName) && !string.IsNullOrEmpty(rule.Relation) && !string.IsNullOrEmpty(rule.ConditionValue))
                {
                    switch (rule.ColumnName)
                    {
                    case "ProductName":
                        switch (rule.Relation)
                        {
                        case "equals":
                            condition = string.Format(" ProductName like N'{0}' ", rule.ConditionValue);
                            break;

                        case "contains":
                            condition = string.Format(" ProductName like N'%{0}%' ", rule.ConditionValue);
                            break;

                        default:
                            break;
                        }
                        break;

                    case "ProductStyle":
                        switch (rule.Relation)
                        {
                        case "equals":
                            condition = string.Format(" ProductStyleID in (select ProductStyleID from ProductStyle where ProductStyleName like N'{0}') ", rule.ConditionValue);
                            break;

                        case "contains":
                            condition = string.Format(" ProductStyleID in (select ProductStyleID from ProductStyle where ProductStyleName like N'%{0}%') ", rule.ConditionValue);
                            break;

                        default:
                            break;
                        }
                        break;

                    case "Supplier":
                        switch (rule.Relation)
                        {
                        case "equals":
                            condition = string.Format(" SupplierID in (select SupplierID from Supplier where SupplierName like N'{0}') ", rule.ConditionValue);
                            break;

                        case "contains":
                            condition = string.Format(" SupplierID in (select SupplierID from Supplier where SupplierName like N'%{0}%') ", rule.ConditionValue);
                            break;

                        default:
                            break;
                        }
                        break;

                    default:
                        break;
                    }
                }
                return(condition);
            }
            catch (Exception ex)
            {
                LogService.WriteException(ex);
                return("");
            }
        }
Exemplo n.º 10
0
        public string create(Collection collection, HttpPostedFileBase file)
        {
            collection.TemplateLayouts = new SelectList(new List <SelectListItem>
            {
                new SelectListItem {
                    Selected = true, Text = "collection", Value = "collection"
                },
                new SelectListItem {
                    Selected = true, Text = "collection.list", Value = "collection.list"
                },
            }, "Value", "Text", "1");
            string strErrorMessage = "";

            if (ModelState.IsValid)
            {
                // insert collection
                if (collection.CollectionType == "custom")
                {
                    collection.ConditionForCollection = false;
                }
                else
                {
                    bool flg = false;
                    for (int i = 0; i < collection.TblRules.Count; i++)
                    {
                        if (string.IsNullOrEmpty(collection.TblRules[i].ConditionValue.Trim()))
                        {
                            strErrorMessage += "Giá trị lọc không được để trống<br/>";
                            flg              = true;
                        }
                    }
                    if (flg)
                    {
                        return(strErrorMessage);
                    }
                }
                collection.CreatedDateTime = SDateTime.GetYYYYMMddHmmSSNow();
                int collectionID = collectionService.Insert(collection);

                if (collectionID > 0)
                {
                    collection.CollectionID = collectionID;
                    LogService.WriteLog2DB(accountService.GetUserId(User.Identity.GetUserName()), (int)Common.ActionID.Insert, collectionID, SDateTime.GetYYYYMMddHmmSSNow(), General.GetIPAddress(), TableNameID, collection.CollectionName);
                    // update collection image url
                    string imageUrl = UploadImage(collectionID, file);
                    if (!string.IsNullOrEmpty(imageUrl))
                    {
                        collection.CollectionImage = imageUrl;
                        collectionService.Update(collection);
                    }
                    // add rule for collection
                    if (collection.CollectionType == "smart")
                    {
                        if (collection.TblRules != null && collection.TblRules.Count > 0)
                        {
                            for (int i = 0; i < collection.TblRules.Count; i++)
                            {
                                TblRule rule = collection.TblRules[i];
                                rule.CollectionID = collectionID;
                                if (tblRuleService.CheckRuleValid(rule))
                                {
                                    tblRuleService.Insert(rule);
                                }
                            }
                        }

                        string linkCondition = "";
                        if (collection.ConditionForCollection)
                        {
                            linkCondition = "and";
                        }
                        else
                        {
                            linkCondition = "or";
                        }
                        string         strConditionProductByRule = "1=1 and " + tblRuleService.GetConditionProductByListRule(collection.TblRules, linkCondition);
                        List <Product> listProduct = productService.GetByWhere(strConditionProductByRule);
                        foreach (var item in listProduct)
                        {
                            CollectionProduct collectionProduct = new CollectionProduct();
                            collectionProduct.CollectionID = collectionID;
                            collectionProduct.ProductID    = item.ProductID;
                            collectionProductService.Insert(collectionProduct);
                        }
                    }
                    return(collectionID.ToString());
                }
            }
            foreach (ModelState modelState in ViewData.ModelState.Values)
            {
                foreach (ModelError error in modelState.Errors)
                {
                    strErrorMessage += error.ErrorMessage;
                }
            }
            return(strErrorMessage);
        }