@ -1,11 +1,14 @@
package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.rulematch ;
package cn.estsh.i3plus.ext.mes.pcn.apiservice.serviceimpl.rulematch ;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesNumberRuleMatchDispatchService ;
import cn.estsh.i3plus.ext.mes.pcn.api.busi.IMesNumberRuleMatchDispatchService ;
import cn.estsh.i3plus.ext.mes.pcn.pojo.util.MesPcnExtConstWords ;
import com.google.common.base.Objects ;
import com.google.common.base.Objects ;
import lombok.extern.slf4j.Slf4j ;
import lombok.extern.slf4j.Slf4j ;
import org.springframework.stereotype.Service ;
import org.springframework.stereotype.Service ;
import org.springframework.util.StringUtils ;
import org.springframework.util.StringUtils ;
import java.util.HashMap ;
import java.util.Map ;
import java.util.regex.Matcher ;
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
import java.util.regex.Pattern ;
@ -17,32 +20,46 @@ import java.util.regex.Pattern;
public class MesNumberRuleMatchRegularExpressionService implements IMesNumberRuleMatchDispatchService {
public class MesNumberRuleMatchRegularExpressionService implements IMesNumberRuleMatchDispatchService {
@Override
@Override
public Boolean matchNumberRule ( String organizeCode , String sn , Object . . . params ) {
public Map < String , Object > matchNumberRule ( String organizeCode , String sn , Object . . . params ) {
Map < String , Object > result = new HashMap < > ( ) ;
result . put ( MesPcnExtConstWords . RESULT , false ) ;
if ( StringUtils . isEmpty ( params [ 1 ] ) ) {
result . put ( MesPcnExtConstWords . MESSAGE , "未维护匹配规则!" ) ;
return result ;
}
String matchRule = params [ 1 ] . toString ( ) ;
String matchRule = params [ 1 ] . toString ( ) ;
return match ( matchRule , sn ) ;
if ( ! match ( matchRule , sn ) ) {
result . put ( MesPcnExtConstWords . MESSAGE , String . format ( "条码[%s]不匹配规则[%s]!" , sn , matchRule ) ) ;
return result ;
}
result . put ( MesPcnExtConstWords . RESULT , true ) ;
return result ;
}
}
private /**static**/ boolean match ( String ruleStr , String barCode ) {
private /**static**/ B oolean match ( String ruleStr , String barCode ) {
if ( StringUtils . isEmpty ( ruleStr ) ) {
if ( StringUtils . isEmpty ( ruleStr ) ) {
return false ;
return false ;
}
}
if ( ! ruleStr . contains ( "*" ) & & ! ruleStr . contains ( "?" ) & & ! ruleStr . contains ( "," ) ) {
if ( ! ruleStr . contains ( MesPcnExtConstWords . ASTERISK ) & & ! ruleStr . contains ( MesPcnExtConstWords . QUESTION_MARK ) & & ! ruleStr . contains ( MesPcnExtConstWords . COMMA ) ) {
if ( Objects . equal ( ruleStr . toLowerCase ( ) , barCode . toLowerCase ( ) ) ) {
if ( Objects . equal ( ruleStr . toLowerCase ( ) , barCode . toLowerCase ( ) ) ) {
return true ;
return true ;
}
}
return false ;
return false ;
}
}
String ruleArray [ ] = ruleStr . split ( "," ) ;
String ruleArray [ ] = ruleStr . split ( MesPcnExtConstWords . COMMA ) ;
for ( String rule : ruleArray ) {
for ( String rule : ruleArray ) {
if ( rule . contains ( "yyyyMMdd" ) ) {
if ( rule . contains ( MesPcnExtConstWords . DATE_FORMAT_yyyyMMdd ) ) {
String pattern = "\\d{4}\\d{2}\\d{2}" ;
String pattern = "\\d{4}\\d{2}\\d{2}" ;
Pattern regex = Pattern . compile ( pattern ) ;
Pattern regex = Pattern . compile ( pattern ) ;
Matcher matcher = regex . matcher ( rule ) ;
Matcher matcher = regex . matcher ( rule ) ;
return matcher . matches ( ) ;
return matcher . matches ( ) ;
} else if ( rule . contains ( "yyyy.MM.dd" ) | | rule . contains ( "yyyy/MM/dd" ) | | rule . contains ( "yyyy-MM-dd" ) ) {
} else if ( rule . contains ( MesPcnExtConstWords . DATE_FORMAT_POINT ) | | rule . contains ( MesPcnExtConstWords . DATE_FORMAT_SLASH ) | | rule . contains ( MesPcnExtConstWords . DATE_FORMAT_SEPARATOR ) ) {
String pattern = "\\d{4}[./-]\\d{2}[./-]\\d{2}" ;
String pattern = "\\d{4}[./-]\\d{2}[./-]\\d{2}" ;
Pattern regex = Pattern . compile ( pattern ) ;
Pattern regex = Pattern . compile ( pattern ) ;
Matcher matcher = regex . matcher ( rule ) ;
Matcher matcher = regex . matcher ( rule ) ;
@ -52,7 +69,7 @@ public class MesNumberRuleMatchRegularExpressionService implements IMesNumberRul
String ruleNoSpecialChar = transfer ( rule ) ;
String ruleNoSpecialChar = transfer ( rule ) ;
String barcodeNoSpecialChar = transfer ( barCode ) ;
String barcodeNoSpecialChar = transfer ( barCode ) ;
///将规则转换为正则表达式,使用正则表达式进行匹配,正则中去除二维码的起始特殊符号
///将规则转换为正则表达式,使用正则表达式进行匹配,正则中去除二维码的起始特殊符号
String regStr = "^" + ruleNoSpecialChar . toLowerCase ( ) . replace ( ")" , "\\)" ) . replace ( "?" , "(?:.|\n){1}" ) . replace ( "*" , "(?:.|-\n)*" ) . replace ( "[" , "\\[" ) + "$" ;
String regStr = "^" + ruleNoSpecialChar . toLowerCase ( ) . replace ( MesPcnExtConstWords . BRACKET_R , "\\)" ) . replace ( MesPcnExtConstWords . QUESTION_MARK , "(?:.|\n){1}" ) . replace ( MesPcnExtConstWords . ASTERISK , "(?:.|-\n)*" ) . replace ( MesPcnExtConstWords . SQUARE_BRACKETS_L , "\\[" ) + "$" ;
Pattern regex = Pattern . compile ( regStr ) ;
Pattern regex = Pattern . compile ( regStr ) ;
//end update
//end update
if ( regex . matcher ( barcodeNoSpecialChar . toLowerCase ( ) ) . matches ( ) ) return true ;
if ( regex . matcher ( barcodeNoSpecialChar . toLowerCase ( ) ) . matches ( ) ) return true ;
@ -65,15 +82,14 @@ public class MesNumberRuleMatchRegularExpressionService implements IMesNumberRul
public /**static**/ String transfer ( String str ) {
public /**static**/ String transfer ( String str ) {
// 1. 定义正则表达式,匹配特殊字符
// 1. 定义正则表达式,匹配特殊字符
String regex = "[^a-zA-Z0-9^\\s^*?[)>.:^/]]" ;
String regex = "[^a-zA-Z0-9^\\s^*?[)>.:^/]]" ;
// 2. 使用正则表达式替换特殊字符为空字符串
// 2. 使用正则表达式替换特殊字符为空字符串
return str . replaceAll ( regex , "" ) ;
return str . replaceAll ( regex , "" ) ;
}
}
public static void main ( String [ ] args ) {
// public static void main(String[] args) {
// String sn = "M00061603-R80512000";
// String sn = "M00061603-R80512000";
// String matchRule = "M00061596*";
// String matchRule = "M00061596*";
// System.out.println(match(matchRule, sn));
// System.out.println(match(matchRule, sn));
}
// }
}
}