使用場景
xx快遞小哥未派件10票 應派總件100票 未派佔比10%。
通過讀取罰款配置表判斷是按單票罰款還是按佔比罰款,匹配區間及對應罰款金額,進行罰款。
罰款配置效果:
sql:
CREATE TABLE `nhm_site_fine_set` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`data_type` tinyint(3) DEFAULT '-1' COMMENT '數據類型:1無攬收;2無發件;',
`rule_type` tinyint(1) DEFAULT '-1' COMMENT '罰款規則,0:按佔比;1:按單票',
`start_date` date NOT NULL DEFAULT '1970-01-01' COMMENT '起始時間',
`section_money` text COMMENT '區間及對應金額',
`update_time` datetime DEFAULT NULL COMMENT '最新修改時間',
`operator` varchar(20) DEFAULT '' COMMENT '修改人',
PRIMARY KEY (`id`),
KEY `idx_data_type` (`data_type`),
KEY `idx_start_date` (`start_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='xxx';
INSERT INTO `nhm_site_fine_set` VALUES (1, 1, 0, '2018-08-01', '[{\"section\":\"(0,100]\",\"money\":0}]', NULL, NULL);
INSERT INTO `nhm_site_fine_set` VALUES (2, 2, 0, '2018-08-01', '[{\"section\":\"(0,100]\",\"money\":0}]', NULL, NULL);
INSERT INTO `nhm_site_fine_set` VALUES (3, 3, 0, '2018-08-01', '[{\"section\":\"(0,100]\",\"money\":0}]', NULL, NULL);
INSERT INTO `nhm_site_fine_set` VALUES (4, 4, 0, '2018-08-01', '[{\"section\":\"(0,100]\",\"money\":0}]', NULL, NULL);
INSERT INTO `nhm_site_fine_set` VALUES (5, 5, 0, '2018-08-01', '[{\"section\":\"(0,100]\",\"money\":0}]', NULL, NULL);
INSERT INTO `nhm_site_fine_set` VALUES (6, 6, 0, '2018-08-01', '[{\"section\":\"(0,100]\",\"money\":0}]', NULL, NULL);
INSERT INTO `nhm_site_fine_set` VALUES (7, 7, 0, '2018-08-01', '[{\"section\":\"(0,100]\",\"money\":0}]', NULL, NULL);
INSERT INTO `nhm_site_fine_set` VALUES (8, 8, 0, '2018-08-01', '[{\"section\":\"(0,100]\",\"money\":0}]', NULL, NULL);
INSERT INTO `nhm_site_fine_set` VALUES (9, 9, 0, '2018-08-01', '[{\"section\":\"(0,100]\",\"money\":0}]', NULL, NULL);
IntervalUtil.java
package com.xx.xx.xx.utils;
import org.apache.ibatis.ognl.Ognl;
/**
*
* @ClassName: IntervalUtil
* @author: billy
* @date:
*/
public class IntervalUtil {
/**
* 判斷data_value是否在interval區間範圍內
* @author:
* @date:
* @param data_value 數值類型的
* @param interval 正常的數學區間,包括無窮大等,如:(1,3)、>5%、(-∞,6]、(125%,135%)U(70%,80%)
* @return true:表示data_value在區間interval範圍內,false:表示data_value不在區間interval範圍內
*/
public static boolean isInTheInterval(String data_value,String interval) {
//將區間和data_value轉化爲可計算的表達式
String formula = getFormulaByAllInterval(data_value,interval,"||");
// ScriptEngine jse = new ScriptEngineManager().getEngineByName("JavaScript"); 效率較低
try {
return (Boolean)Ognl.getValue(formula, (Object) null,Boolean.class);
//計算表達式
// return (Boolean) jse.eval(formula);
} catch (Exception t) {
return false;
}
}
/**
* 將所有閥值區間轉化爲公式:如
* [75,80) =》 date_value < 80 && date_value >= 75
* (125%,135%)U(70%,80%) =》 (date_value < 1.35 && date_value > 1.25) || (date_value < 0.8 && date_value > 0.7)
* @param date_value
* @param interval 形式如:(125%,135%)U(70%,80%)
* @param connector 連接符 如:") || ("
*/
public static String getFormulaByAllInterval(String date_value, String interval, String connector) {
StringBuffer buff = new StringBuffer();
for(String limit:interval.split("U")){//如:(125%,135%)U (70%,80%)
buff.append("(").append(getFormulaByInterval(date_value, limit," && ")).append(")").append(connector);
}
String allLimitInvel = buff.toString();
int index = allLimitInvel.lastIndexOf(connector);
allLimitInvel = allLimitInvel.substring(0,index);
return allLimitInvel;
}
/**
* 將整個閥值區間轉化爲公式:如
* 145) =》 date_value < 145
* [75,80) =》 date_value < 80 && date_value >= 75
* @param date_value
* @param interval 形式如:145)、[75,80)
* @param connector 連接符 如:&&
*/
private static String getFormulaByInterval(String date_value, String interval, String connector) {
StringBuffer buff = new StringBuffer();
for(String halfInterval:interval.split(",")){//如:[75,80)、≥80
buff.append(getFormulaByHalfInterval(halfInterval, date_value)).append(connector);
}
String limitInvel = buff.toString();
int index = limitInvel.lastIndexOf(connector);
limitInvel = limitInvel.substring(0,index);
return limitInvel;
}
/**
* 將半個閥值區間轉化爲公式:如
* 145) =》 date_value < 145
* ≥80% =》 date_value >= 0.8
* [130 =》 date_value >= 130
* <80% =》 date_value < 0.8
* @param halfInterval 形式如:145)、≥80%、[130、<80%
* @param date_value
* @return date_value < 145
*/
private static String getFormulaByHalfInterval(String halfInterval, String date_value) {
halfInterval = halfInterval.trim();
if(halfInterval.contains("∞")){//包含無窮大則不需要公式
return "1 == 1";
}
StringBuffer formula = new StringBuffer();
String data = "";
String opera = "";
if(halfInterval.matches("^([<>≤≥\\[\\(]{1}(-?\\d+.?\\d*\\%?))$")){//表示判斷方向(如>)在前面 如:≥80%
opera = halfInterval.substring(0,1);
data = halfInterval.substring(1);
}else{//[130、145)
opera = halfInterval.substring(halfInterval.length()-1);
data = halfInterval.substring(0,halfInterval.length()-1);
}
double value = dealPercent(data);
formula.append(date_value).append(" ").append(opera).append(" ").append(value);
String a = formula.toString();
//轉化特定字符
return a.replace("[", ">=").replace("(", ">").replace("]", "<=").replace(")", "<").replace("≤", "<=").replace("≥", ">=");
}
/**
* 去除百分號,轉爲小數
* @param str 可能含百分號的數字
* @return
*/
private static double dealPercent(String str){
double d = 0.0;
if(str.contains("%")){
str = str.substring(0,str.length()-1);
d = Double.parseDouble(str)/100;
}else{
d = Double.parseDouble(str);
}
return d;
}
// public static void main(String[] args) {
// IntervalUtil a = new IntervalUtil();
// System.out.println(a.isInTheInterval("100", "[0,100)"));
// }
}
工具類調用:
/**
* 根據業務類型和監控日期 獲取對應的罰款金額配置
* @param set 數據庫查出的罰款數據
* @param billNum違規票數
* @param percent 違規佔比
* @return 獲取對應區間罰款金額
*/
private BigDecimal getSet(FineSetResult set, Long billNum, BigDecimal percent) {
BigDecimal fine = BigDecimal.ZERO;
List<xx> sectionMoneys = JSONObject.parseArray(set.getSectionMoney(),xx.class);
for (xx sectionMoney:sectionMoneys){
String dataValue = null;
//按佔比罰款
if (set.getRuleType().equals(0)){
if(percent.compareTo(BigDecimal.ZERO)==0){
return fine;
}
dataValue = percent.toString();
}
//按單票罰款
if (set.getRuleType().equals(1)){
if(billNum==0l){
return fine;
}
dataValue = billNum.toString();
}
boolean isInTheInterval = IntervalUtil.isInTheInterval(dataValue,sectionMoney.getSection());
if (isInTheInterval){
fine = sectionMoney.getMoney();
break;
}
}
return fine;
}