In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Today, I will talk to you about how to implement a simple e-commerce discount rule engine based on Spring EL. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Implementation of a simple e-commerce discount rule engine based on Spring EL
In daily work, there are many places to use the rule engine. For example, the discount scenarios in e-commerce are complex and changeable. For example, each department has a different analysis strategy in attendance analysis. There is also salary calculation, which is calculated differently in different positions and offices in the company. These scenarios are very suitable for using the rule engine.
When it comes to rules engines, Drools is probably the first thing that comes to mind for most people, but Spring EL is also very useful.
WTF? Can Spring EL still be a rule engine?
You read it right. Spring EL not only works as a rule engine, but I also use it on a large scale in a production environment.
Why use Spring EL as a rule engine?
Compared with other rule engines, Spring EL is lighter and cheaper to learn, and its performance with functional programming may be beyond your imagination!
Usage scenario of rule engine
When a business rule of a system is changeable, the design needs to be very flexible. If you do it in the traditional hard-coding way, the code logic may be extremely complex and need to be changed very frequently.
In order to solve this problem, I intend to introduce a script engine to simplify development and reduce system complexity. After investigation, I finally decided to use Spring EL.
How to do it
First, let's define a basic object for settlement.
@ Data
@ AllArgsConstructor
@ NoArgsConstructor
Public class Order {
Private String userId
Private Integer age
/ / is it a new guest?
Private Boolean isNew
Private LocalDate orderDate
Private BigDecimal price
}
If a supplier has a settlement rule that is divided into 80% of the order amount, we can calculate it this way.
Public static void main (String [] args) {
ExpressionParser expressionParser = new SpelExpressionParser ()
Expression expression = expressionParser.parse_Expression ("price * 0.8")
Order order = new Order ()
Order.setPrice (BigDecimal.TEN)
BigDecimal value = expression.getValue (order, BigDecimal.class)
System.out.println (value)
}
In this way, we can figure out that 8 yuan should be settled. Of course, this example is too simple. Let's define several complex settlement rules and see how to do it.
Rule 1: new customer settlement on Monday and Friday, the settlement amount is price * 0.2
Rule 2: for those who are older than 18 years old and the amount is greater than 10, the settlement amount is (price-10) * 0.8. let's take a look at the above rules, which are actually divided into two steps: the first step is to filter out the unsettled orders, and the second step is to calculate the real amount, so the code can also be divided into these two steps.
Let me define a few test cases first.
List orders = new ArrayList () {{
/ / Age 19, not a new customer, order was issued on Monday, the amount is 11
Add (new Order ("Zhang San", 19MagneLocalDate.of (2020pc11), new BigDecimal (11)
/ / Age 17, new customer, order issued on Friday, amount 19
Add (new Order ("Li Si", 17jue Lock date. Of (2020pc11 new BigDecimal 13)
/ / Age 17, not a new customer, order was issued on Saturday, the amount is 9
Add (new Order ("Wangwu", 17 Reagle LocalDate.of (2020 pr 11 14), new BigDecimal (9))
}}
The main logic is as follows: first filter out the unwanted orders, and then settle the remaining orders.
Public static void settle (List orders, List filterRule
String settleRule, Map expressionCache) {
Stream stream = orders.stream ()
For (String rule: filterRule) {
Expression expression = FunctionUtil
.cacheFunction (s-> expressionParser.parse_Expression (s), rule, expressionCache)
Stream = filter (stream, expression)
}
Expression expression = FunctionUtil
.cacheFunction (s-> expressionParser.parse_Expression (s), settleRule, expressionCache)
Stream.forEach (o-> System.out.println (o.getUserId () + expression.getValue (o)
}
Public static Stream filter (Stream stream, Expression expression) {
Return stream.filter (s-> expression.getValue (s, Boolean.class))
}
The purpose of FunctionUtil.cacheFunction () is to cache Expression, because it is expensive to create an Expression, so cache the String rule as a key,Expression as a value.
Execute the code.
Public static void main (String [] args) {
Map expressionCache = new HashMap ()
System.out.println ("settlement rule1")
List filterRule1 =
Arrays.asList ("orderDate.getDayOfWeek () .getValue () = = 1 | | orderDate.getDayOfWeek () .getValue () = = 5", "isNew")
String settleRule1 = "price * 0.2"
Settle (orders, filterRule1, settleRule1, expressionCache)
System.out.println ("settlement rule2")
List filterRule2 = Arrays.asList ("age > 18", "price > 10")
String settleRule2 = "(price-10) * 0.8"
Settle (orders, filterRule2, settleRule2, expressionCache)
}
Execute the result.
Settlement rule1
Li Si 3.8
Settlement rule2
Zhang San 0.8
As you can see, through Spring EL and functional programming, we only need to write rules to implement complex settlement logic.
After reading the above, do you have any further understanding of how to implement a simple e-commerce discount rule engine based on Spring EL? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.