BarcodeRule.java 29.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
package com.myproject.util;

import com.google.common.base.Strings;
import com.myproject.api.SmbUtil;
import com.myproject.bean.CodeBean;
import com.myproject.bean.update.Barcode;
import com.myproject.webapp.controller.webService.DataCache;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.net.URI;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * 条码规则,可用字段有:
 * PN为物料编号即 PartNumber
 * RI 为唯一码即ReelId,[RI]为所有字符串作为一个唯一码
 * QTY 为数量
 * PRODATE为生产日期xxPRODATEyyyyMMdd
 * EXPDATE为过期日期xxxEXPDATEyyyyMMdd
 * SP 为供应商,
 * BATCH 为批次
 * MSL 为MSL等级
 * MEMO 备注信息
 *
 *
 *
 *条码规则,可用字段有:
 * PN为物料编号即 PartNumber
 * RI 为唯一码即ReelId,[RI]为所有字符串作为一个唯一码
 * QTY 为数量
 * PRODATE为生产日期xxPRODATEyyyyMMdd
 * EXPDATE为过期日期xxxEXPDATEyyyyMMdd
 * SP 为供应商,
 * BATCH 为批次
 * MSL 为MSL等级
 * 其中必须含有PN和 RI, QTY为空时使用产品档案的封装数量
 *
 * 前面一位数字大于0表示去除前面第n位, -1表示不去除,并且对字段长数不做限制, 等于0表示不去除,但对字段长度做限制
 * 中间一位数字0表示为变长,正值表示从前面截取,负值表示从后面开始截取,前缀和后缀及长度都有效时,需要验证字串总长度
 * 后面一位数字大于0表示去除后面第n位, -1表示不去除,并且对字段长数不做限制, 等于0表示不去除,但对字段长度做限制
 *
 * 例一: QTY[-1:5:-1]取前5位作为数量
 * 例二: QTY[-1:-5:-1]取后5位作为数量
 * 例三: QTY[1:5:-1]去除前面第1位后,取前5位作为数量
 * 例四: QTY[-1:-5:1]去除后面第1位后,取后5位作为数量
 * 例五: QTY[1:0:-1]去除前面1位后,剩余的作为数量
 * 例六: QTY[0:5:3]去除前面0位和后面3位,剩余的5位作为数量,也就是说只能为8位
 *
 * 示例:
 * 规则为: [RI]_PN_PRODATEyyMMdd_QTY[0:5:4]
 * 条码:  4500065747_CS000069_180101_030000041
 * 解析后: RI=4500065747_CS000069_180101_030000041
 *        PN=CS000069
 *        生产日期为: 2018年1月1日
 *        数量为:去掉前面0位,去掉后面4位,剩下03000正好为5位,所以数量是3000,
 *        如果条码变为4500065747_CS000069_180101_0300000410则会提示不合规则,因为去掉前面0位,去掉后面4位,剩下的030004是6位,不是5位
 *
 * 示例:
 *  规则为: BATCH;PRODATEyyyyMMdd[1:8:-1];PN[1:12:-1]SP[13:5:-1]QTY[-1:-5:-1];RI
 *  条码为: L00002019090199951797;E20190901 0365;B8C.R2003.V81506072019090103000;R506072019102200356
 *  解析后: PN=8C.R2003.V81
 *         RI=R506072019102200356
 *         QTY=3000
 *         BATCH=L00002019090199951797
 *         PRODATE=2019年9月1日
 *         供应商=50607
 *
 *
 *
 * 1@2@3@PN@5@6@7@8@9@10@11@12@13@14@15@16@RI@18@19@20@21@22@23@24
 * [)>@06@12S0002@P5292001000@1P1690215@31P1690215@12V527973628@10VCHN-YANTAI@2P@20P@6D20170626@14D20171223@30PY@ZN@K0@16K0@V815@3SB370000000EZZ@Q500GRM000@20T1@1TMT72543954@2T@1Z@@
 *
 * [@xx@xx@partNO.@xx@xx@xx@xx@xx@生产日期@过期日期@xx@xx@xx@xx@xx@psckid唯一码@重量@xx@xx@xx@xc@@' 
 *
 * 零包
 * [)>@06@12S0002@P8638516895@1P08EFP2285A@31P8638516895@12V315754465@10VCHN-KUNSHAN@2P@20P1-K-PR77-XD7179#QX0#@6D20180216@14D20180816@30PY@ZN@K0@16K0@V97219759@3SS000200062918@Q20NAR000@20T1@1T8021___0718@2T@1Z08EFP2285A@@
 * 整包
 * [)>@06@12S0002@P8638516895@1P08EFP2285A@31P8638516895@12V315754465@10VCHN-KUNSHAN@2P@20P1-K-PR77-XD7179#QX0#@6D20180216@14D20180816@30PY@ZN@K0@16K0@V97219759@3SS000200062909@Q15NAR000@20T1@1T@2T@1Z08EFP2285A@@
 *
 * Created by sunke on 2017/3/25.
 */
public class BarcodeRule {

    protected final static Logger log = LogManager.getLogger(BarcodeRule.class);

    private BarcodeRule() {

    }

    public static BarcodeRule newRule(String ruleStr){
        BarcodeRule newRule = new BarcodeRule();
        newRule.ruleStr = ruleStr;
        log.info("开始解析二维码规则:" + ruleStr);

        if(!Strings.isNullOrEmpty(ruleStr)){
            //先去除[prefix,length,suffix]这样的配置字段
            String canSplitStr =ruleStr.replaceAll("DATE.*?\\]","");

            canSplitStr = canSplitStr.replaceAll("\\[[^\\]]*?\\]","");
            //先找分割符,分割出来数组数量最多的放第一个
            String trySeparator = "";
            //找最多的字符
            String[] tempArr = new String[]{};
            for (int i = 0; i < canSplitStr.length(); i++) {
                char c = canSplitStr.charAt(i);
                if(!Character.isLetterOrDigit(c)){
                    //不是字母和数字才可以作为分割符
                    String separator = toRegexStr(c + "");
                    if(trySeparator.indexOf(c) == -1){
                        String[] ss = ruleStr.split(separator,-1);
                        if(ss.length > tempArr.length){
                            trySeparator = c + trySeparator;

                            newRule.separator = separator;
                            tempArr = ss;
                        }else{
                            trySeparator =  trySeparator + c;
                        }
                    };
                }
            }

            log.info("分割符为:"+newRule.separator+"长度为:"+tempArr.length);
            if(tempArr.length == 0){
                tempArr = new String[]{ruleStr};
            }

            for (int i=0; i<tempArr.length; i++) {
                String fieldValue = tempArr[i];
                if(!newRule.partNumber_item.hasThisField()){
                    if(newRule.partNumber_item.matchRule(fieldValue, i)){
                        log.info("partNumber: 为" +  newRule.partNumber_item.toString());
                    }
                }
                if(!newRule.whole_reelId_item.hasThisField() && !newRule.reelId_item.hasThisField()){
                    if(newRule.whole_reelId_item.matchRule(fieldValue, i)){
                        log.info("whole reelId: 为" +  newRule.whole_reelId_item.toString());
                    }else if(newRule.reelId_item.matchRule(fieldValue, i)){
                        log.info("reelId: 为" +  newRule.reelId_item.toString());
                    }
                }

                if(!newRule.batch_item.hasThisField()){
                    if(newRule.batch_item.matchRule(fieldValue, i)){
                        log.info("batch: 为" +  newRule.batch_item.toString());
                    }
                }

                if(!newRule.quantity_item.hasThisField()){
                    if(newRule.quantity_item.matchRule(fieldValue, i)){
                        log.info("quantity: 为" +  newRule.quantity_item.toString());
                    }
                }

                if(!newRule.supplier_item.hasThisField()){
                    if(newRule.supplier_item.matchRule(fieldValue, i)){
                        log.info("supplier: 为" +  newRule.supplier_item.toString());
                    }
                }

                if(!newRule.msl_item.hasThisField()){
                    if(newRule.msl_item.matchRule(fieldValue, i)){
                        log.info("MSL: 为" +  newRule.msl_item.toString());
                    }
                }

                if(!newRule.memo_item.hasThisField()){
                    if(newRule.memo_item.matchRule(fieldValue, i)){
                        log.info("MEMO: 为" +  newRule.memo_item.toString());
                    }
                }


                if(!newRule.produceDate_item.hasThisField()){
                    if(newRule.produceDate_item.matchRule(fieldValue,i)){
                        log.info("produceDate: 为" +  newRule.produceDate_item.toString());
                    }
                }

                if(!newRule.expireDate_item.matchRule(fieldValue,i)){
                    if(newRule.expireDate_item.matchRule(fieldValue,i)){
                        log.info("expireDate: 为" +  newRule.expireDate_item.toString());
                    }
                }

                if(!newRule.expireYear_item.hasThisField() && !newRule.expireMonth_item.hasThisField() && !newRule.expireDay_item.hasThisField()){
                    if(newRule.expireYear_item.matchRule(fieldValue,i)){
                        log.info("expireYear: 为" +  newRule.expireYear_item.toString());
                    }else if(newRule.expireMonth_item.matchRule(fieldValue,i)){
                        log.info("expireMonth: 为" +  newRule.expireMonth_item.toString());
                    }else if(newRule.expireDay_item.matchRule(fieldValue,i)){
                        log.info("expireDay: 为" +  newRule.expireDay_item.toString());
                    }
                }
            }
            boolean validRule = false;
            if(newRule.partNumber_item.hasThisField()){
                if(DataCache.isProductionFor(DataCache.CUSTOMER.NANRUI)){
                    validRule = true;
                }else if(newRule.reelId_item.hasThisField() || newRule.whole_reelId_item.hasThisField()){
                    validRule = true;
                }
            }
            if(validRule){
                newRule.length = tempArr.length;
            }else{
                log.warn("编码规则中必须包含 RI 和 PN");
            }
//            if(newRule.reelId_item.hasThisField()  && newRule.partNumber_item.hasThisField()){
//                newRule.length = tempArr.length;
//            }else{
//                if(DataCache.isProductionFor(DataCache.CUSTOMER.NANRUI)){
//                    //南瑞的没有RI
//                    if(newRule.partNumber_item.hasThisField()){
//                        newRule.length = tempArr.length;
//                    }
//                }else{
//                    log.warn("编码规则中必须包含 RI 和 PN");
//                }
//            }
        }
        return newRule;
    }

    private String ruleStr;
    private String separator;
    private int length = 0;
    //private String[] ruleArr = new String[]{};

    private RuleItem partNumber_item = new RuleItem("PN");
    private RuleItem reelId_item  = new RuleItem("RI");
    private RuleItem whole_reelId_item = new RuleItem("[RI]");
    private RuleItem quantity_item = new RuleItem("QTY");
    private RuleItem produceDate_item = new RuleItem("PRODATE");
    private RuleItem expireDate_item = new RuleItem("EXPDATE");
    private RuleItem expireYear_item = new RuleItem("EXPYEAR");
    private RuleItem expireMonth_item = new RuleItem("EXPMONTH");
    private RuleItem expireDay_item = new RuleItem("EXPDAY");
    private RuleItem supplier_item = new RuleItem("SP");
    private RuleItem batch_item  = new RuleItem("BATCH");
    private RuleItem msl_item  = new RuleItem("MSL");
    private RuleItem memo_item  = new RuleItem("MEMO");

    private class RuleItem{
        private RuleItem(String ruleName) {
            this.name = ruleName;
        }

        //名称
        private String name;
        //位置
        private int index = -1;
        //前缀(-1时表示没有前缀)如果要验证总长度,可设置为0
        private int prefix = -1;

        private int length = 0;
        //后缀(-1时表示没有前缀)如果要验证总长度,可设置为0
        private int suffix = -1;
        //日期格式
        private String formatStr = "";

        @Override
        public String toString() {
            return "{" +
                    "name='" + name + '\'' +
                    ", index=" + index +
                    ", prefix='" + prefix + '\'' +
                    ", length='" + length + '\'' +
                    ", suffix='" + suffix + '\'' +
                    '}';
        }

        /**
         * 解析规则中是否包含本字段
         */
        private boolean hasThisField(){
            return index != -1;
        }

        private boolean matchRule(String ruleValue, int ruleIndex){
            int nameIndex = ruleValue.indexOf(name);
            if(nameIndex >= 0){
                int startIndex = ruleValue.indexOf("[",nameIndex + 1 );
                if(startIndex > 0){
                    int endIndex = ruleValue.indexOf("]",nameIndex);
                    if(endIndex > startIndex){
                        index = ruleIndex;
                        String limitStr = ruleValue.substring(startIndex+1,endIndex);
                        String[] limitInfo = limitStr.split(":");
                        prefix = Integer.valueOf(limitInfo[0]);
                        length = Integer.valueOf(limitInfo[1]);
                        suffix = Integer.valueOf(limitInfo[2]);
                        if(isDateField()){
                            //取日期格式
                            formatStr = ruleValue.substring(nameIndex+name.length(),startIndex);
                        }
                    }

                }else{
                    prefix = ruleValue.substring(0,nameIndex).length();

                    if(isDateField()){
                        suffix = 0;
                        formatStr = ruleValue.substring(nameIndex + name.length());
                    }else{
                        suffix = ruleValue.substring(nameIndex + name.length()).length();
                    }

                    if(prefix == 0){
                        prefix = -1;
                    }
                    if(suffix == 0){
                        suffix = -1;
                    }
                    index = ruleIndex;
                }
                return true;
            }
            return false;
        }

        private boolean isDateField(){
            return name.contains("DATE");
        }

        private String getStrValue(String[] codeArr){
            try{
                if(index != -1){
                    String codeValue = codeArr[index];
                    if(codeValue.length() < prefix + suffix){
                        return null;
                    }
                    //如果有前缀和后缀
                    if(prefix > 0){
                        codeValue = codeValue.substring(prefix);
                    }

                    if(suffix > 0){
                        codeValue = codeValue.substring(0, codeValue.length() - suffix);
                    }

                    if(length > 0){
                        if(prefix >=0 && suffix >=0){
                            if(codeValue.length() != length){
                                log.info(codeValue + "的长度不是" + length);
                                return null;
                            }
                        }
                        //从前面取length位
                        if (codeValue.length() >= length){
                            return codeValue.substring(0, length);
                        }
                    }else if(length < 0){
                        return codeValue.substring(codeValue.length()+length, codeValue.length());
                    }

                    //是否是日期
//                    if(!isDateField()){
//                        if(codeValue.length() < suffix){
//                            return null;
//                        }
//                        codeValue = codeValue.substring(0,codeValue.length() - suffix);
//                    }
                    return codeValue;
                }

            }catch (Exception e){
                log.error("解析出错:",e);
            }
            return null;
        }

        private int getIntValue(String[] codeArr){
            String value = getStrValue(codeArr);
            if(!Strings.isNullOrEmpty(value)){
                try{
                    return Float.valueOf(value).intValue();
                }catch (Exception ex){
                    log.warn(value + " is not a validate int");
                }
            }
            return -1;
        }

        private Date getDateValue(String[] codeArr){
            String value = getStrValue(codeArr);
            if(!Strings.isNullOrEmpty(value)){
                try{
                    //阳光电源有可能是年周,需要转换一下
                    if(value.endsWith("00")){
                        log.info("转换日期");
                        Date time = DateUtil.toDate(value.substring(0,2),"yy");
                        Calendar c = Calendar.getInstance();
                        c.setTime(time);
                        int weekOfYear = Integer.valueOf(value.substring(2,4));
                        c.set(Calendar.WEEK_OF_YEAR,weekOfYear);
                        return c.getTime();
                    }
                    return DateUtil.toDate(value,formatStr);
                }catch (Exception ex){
                    log.warn(value + " is not a validate date");
                }
            }
            return null;
        }
    }

    public CodeBean toCodeBean(String codeStr){
        log.info("开始解析条码["+codeStr+"]");
        CodeBean codeBean = new CodeBean();
        codeBean.setCodeStr(codeStr);
        //是否带有位置信息
        float locationX = -1f;
        float locationY = -1f;
        int reelWidth = -1;
        int reelHeight = -1;
        //=摄像头编号+条码X坐标x条码Y坐标-料盘尺寸=CODE
        //=1+123.4x100.5-7x12=CODE或者7x12=CODE
        if(codeStr.startsWith("=") && codeStr.contains("x")){
            codeStr = codeStr.substring(1);
            String[] arr = codeStr.split("=", 2);
            String sizeStr = arr[0];
            if(sizeStr.contains("+") && sizeStr.contains("-")){
                String[] locationSizeArr = sizeStr.split("-",2);
                //尺寸信息
                sizeStr = locationSizeArr[1];

                String[] locationInfoArr = locationSizeArr[0].split("\\+");
                String cameraIndexStr = locationInfoArr[0];
                //摄像头编号信息
                int cameraIndex = Integer.valueOf(cameraIndexStr);

                codeBean.setCameraIndex(cameraIndex);

                String locationStr = locationInfoArr[1];

                //带有位置信息
                String[] locationArr = locationStr.split("x");
                locationX = Float.valueOf(locationArr[0].trim());
                locationY = Float.valueOf(locationArr[1].trim());
            }

            //带有料盘宽高信息
            if(!Strings.isNullOrEmpty(sizeStr)){
                String[] sizeInfoArr = sizeStr.split("x",2);
                reelWidth = Integer.parseInt(sizeInfoArr[0].trim());
                reelHeight = Integer.parseInt(sizeInfoArr[1].trim());
            }

            codeBean.setLocationX(locationX);
            codeBean.setLocationY(locationY);
            codeBean.setReelWidth(reelWidth);
            codeBean.setReelHeight(reelHeight);
            codeStr = arr[1];

        }
        codeBean.setCodeStr(codeStr);
        if(!isValidRule()){
            codeBean.setError("error.barcode.noRule","解析规则未定义");
            return codeBean;
        }
        String[] codeArr = new String[]{codeStr};
        if(!Strings.isNullOrEmpty(separator)){
            codeArr = codeStr.split(separator,-1);
            //条码与规则长度对应不上
            if(codeArr.length != length){
                log.info("条码["+codeStr+"]与规则【"+ruleStr+"】长度不同");
                codeBean.setError("error.barcode.wrongLength",new String[]{}, "条码["+codeStr+"]长度错误");
                return codeBean;
            }else{
                codeArr = codeStr.split(separator,length);
            }
        }


        Barcode b = new Barcode();

        String reelId = "";
        if(whole_reelId_item.hasThisField()){
            reelId = codeStr;
        }else{
            reelId = reelId_item.getStrValue(codeArr);
        }

        //String reelId = reelId_item.getStrValue(codeArr);
        if(!DataCache.isProductionFor(DataCache.CUSTOMER.NANRUI)){
            if(Strings.isNullOrEmpty(reelId)){
                log.info("条码解析失败,未找到RI字段");
                codeBean.setError("error.barcode.noField",new String[]{"RI"},"条码解析失败,未找到RI字段");
                return codeBean;
            }
        }

        String partNumber = partNumber_item.getStrValue(codeArr);

        if(Strings.isNullOrEmpty(partNumber)){
            log.info("条码解析失败,未找到PN字段");
            codeBean.setError("error.barcode.noField",new String[]{"PN"}, "条码解析失败,未找到PN字段");
            return codeBean;
        }
        if(batch_item.hasThisField()){
            String batch = batch_item.getStrValue(codeArr);
            b.setBatch(batch);
        }
        int quantity = 1;
        if(quantity_item.hasThisField()){
            quantity = quantity_item.getIntValue(codeArr);
            if(quantity == -1){
                log.info("条码解析失败,未找到QTY 字段");
                codeBean.setError("error.barcode.noField",new String[]{"QTY"},"条码解析失败,未找到QTY 字段");
                return codeBean;
            }
        }

        Date produceDate= produceDate_item.getDateValue(codeArr);
        if(produceDate_item.hasThisField() && produceDate == null){
            log.info("条码解析失败,PRODATE字段不合规则");
            codeBean.setError("error.barcode.noField",new String[]{"PRODATE"},"条码解析失败,未找到PRODATE字段");
            return codeBean;
        }
        Date expireDate= expireDate_item.getDateValue(codeArr);
        if(produceDate != null && expireDate == null){
            Calendar c = Calendar.getInstance();
            c.setTime(produceDate);
            int expYear = expireYear_item.getIntValue(codeArr);
            int expMonth = expireMonth_item.getIntValue(codeArr);
            int expDay = expireDay_item.getIntValue(codeArr);
            boolean hasExpire = false;
            if(expYear != -1){
                c.add(Calendar.YEAR, expYear);
                hasExpire = true;
            }else if(expMonth != -1){
                c.add(Calendar.MONTH, expMonth);
                hasExpire = true;
            }else if(expDay != -1){
                c.add(Calendar.DAY_OF_YEAR, expDay);
                hasExpire = true;
            }

            if(hasExpire){
                expireDate = c.getTime();
            }
        }
        String supplier = supplier_item.getStrValue(codeArr);
        String msl = msl_item.getStrValue(codeArr);

        if(memo_item.hasThisField()){
            String memo = memo_item.getStrValue(codeArr);
            b.setMemo(memo);
        }


        //南瑞使用整个条码作为唯一码
        if(DataCache.isProductionFor(DataCache.CUSTOMER.NANRUI)){
            b.setBarcode(codeStr);
            codeBean.setCodeStr(codeStr);
        }else{
            b.setBarcode(reelId);
            codeBean.setCodeStr(reelId);
        }
        b.setPartNumber(partNumber);
        b.setAmount(quantity);
        b.setProduceDate(produceDate);
        b.setExpireDate(expireDate);
        b.setProvider(supplier);
        b.setPlateSize(reelWidth);
        b.setHeight(reelHeight);
        //b.setMsl(msl);

        codeBean.setBarcode(b);
        return codeBean;
    }

    /**
     * 转义正则特殊字符 ($()*+.[]?\^{}
     * @return
     */
    private static String toRegexStr(String separator){
        //转义正则特殊字符 ($()*+.[]?\^{}
        return separator.replace("\\", "\\\\").replace("*", "\\*")
                .replace("+", "\\+").replace("|", "\\|")
                .replace("{", "\\{").replace("}", "\\}")
                .replace("(", "\\(").replace(")", "\\)")
                .replace("^", "\\^").replace("$", "\\$")
                .replace("[", "\\[").replace("]", "\\]")
                .replace("?", "\\?").replace(",", "\\,")
                .replace(".", "\\.").replace("&", "\\&");
    }

    /**
     * 是否是有效的规则
     */
    public boolean isValidRule(){
        return length > 0;
    }

    public static void main(String args[]) throws Exception{

        //[)>@06@12S001@P8909000244@1P@31P@10V@2P@20P@6D@14D@30PY@Z @K@16K@V000000@3SA20180913-01554@Q03000NAR000@20T1@1TH000000000B172621166@Q03000@2T@1Z@@

        //1@2@3@PPN@5@6@7@8@9@10@11@12@13@14@15@16@17@xxRI@xQTYxxxxxx@20@21@22@23@@
        String rule = "1@2@3@PN@5@6@7@8@9@10@xxPRODATEyyyyMMdd@xxxEXPDATEyyyyMMdd@13@14@15@16@17@RI@xQTYxxxxxx@20@21@22@23";
        rule = "1@2@3@PPN@5@6@7@8@9@10@xxPRODATEyyyyMMdd@xxxEXPDATEyyyyMMdd@13@14@15@16@17@RI@xQTYxxxxxx@20@21@22@23";
        rule = "1@2@3@PPN@5@6@7@8@9@10@11@12@13@14@15@16@17@xxRI@xQTYxxxxxx@20@21@22@23";
        rule = "1@2@3@PPN@5@6@7@8@9@10@11@12@13@14@15@16@xxRI@xxQTYxxxxxx@19@20@21@22@23@@";
        rule = "1;2;3;PN;QTY;6";
        rule = "PN|RI|3|4|5|6|7|8|9|10|11|12|13|14|15|QTYxxx|17";
        rule="1@2@3@PPN@5@6@7@8@9@10@xxPRODATEyyMMdd@xxxEXPDATEyyMMdd@13@14@15@16@VSP@3S[RI]@xQTYxxxxxx@20@21@22@23";




        //String codeStr = "[)>@06@12S0002@P5292001000@1P1690215@31P1690215@12V527973628@10VCHN-YANTAI@2P@20P@6D20170626@14D20171223@30PY@ZN@K0@16K0@V815@3SB370000000EZZ@Q500GRM000@20T1@1TMT72543954@2T@1Z@@";

        String codeStr = "[)>@06@12S001@P8909000244@1P@31P@10V@2P@20P@6D@14D@30PY@Z @K@16K@V000000@3SA20180913-01554@Q03000NAR000@20T1@1TH000000000B172621166@Q03000@2T@1Z@@\n" +
                "\n" +
                "1@2@3@PPN@5@6@7@8@9@10@11@12@13@14@15@16@17@xxRI@xQTYxxxxxx@20@21@22@23@@";
        //codeStr = "[)>@06@12S0002@P8638516894@1PB025008SS5107-04@31P8638516894@12V865379333@10VCHN-WUXI@2PV01@20P#QX0#PROBIMER77MA-1@6D20180214@14D20180805@30PY@ZN@K55158066@16K0@V97198306@3SS500000570451@Q20NAR000@20T1@1T35H-0618@2T@1ZTRIPOD@@";
        codeStr = "[)>@06@12S001@P8909000244@1P@31P@10V@2P@20P@6D@14D@30PY@Z @K@16K@V000000@3SA20180913-01554@Q03000NAR000@20T1@1TH000000000B172621166@Q03000@2T@1Z@@";
        codeStr = ")》@06@120S002@P8908018028@1PBLM18PG121SH1D@31PBLM18PG121SH1D@12V690588124@10VJPN-AM@2P@20P@6D192400@14D210205@30PY@Z1@K0@16K0@V97294419@3SS191908055X1Z@Q4000NAR000@20T1@1TAM961458E@2T@1Z@@";
//        codeStr = "=1+0x0-0x0=A0002";
        //阳光电源
        rule = "[RI]_PN_3_QTYxxxx";
        rule = "[RI]_PN_PRODATEyyMMdd_QTY[0:5:4]";
        codeStr = "4500065747_CS000069_180101_030000041";
        codeStr = "4500089600_DC000045_190528_003000006";
        //博郎  QR = [VEE PN] + [Quantity] + [UID] + [Expire date] + [MSD level] + [Light class] + [Color class] + [Forward voltage] + [00000000]
//        codeStr = "001967370080049250852020112000010P130BT200MH00000000";
//
//        rule = "PN[0:8:-1]QTY[8:5:-1]RI[13:7:-1]EXPDATEyyyyMMdd[20:8:-1]MSL[28:4:-1]BATCH[32:4:-1]SP[36:4:-1]";
//        //佳世达
        rule ="BATCH;PRODATEyyyy-MM-dd[1:10:-1]EXPD[-1:-4:-1];PN[1:12:-1]SP[13:5:-1]QTY[-1:-5:-1];RI";
        codeStr = "L00002019090199951797;E2019-09-01 0365;B8C.R2003.V81506072019090103000;R506072019102200356";

        //rule = "1;PN;BATCH;PRODATEyyyyMMdd;EXPDATEyyyyMMdd;QTY;RI";
        //codeStr = ";RMK1608-K-B-10300;7B16081217B0;20200506;20250203;5000;000";

        //rule = "1;2;PN;BATCH[2:0:-1];5;6;EXPDATEyyyy/MM/dd[2:0:8];QTY[2:0:-1];8;9;10;RI[2:0:-1]";
        //codeStr = "锘緿M;IN13浠跺\uE69C缁勮灪涓濇壒09913涓栬揪;BM;PH11A-ZZG02;DDDDMO180525;GG624-71;SX2019/8/25 0:00:00;SL2;HG;DL;ZP;QT90CECB112D1448DA91F485D0AA984B08";

        codeStr = "??M8431CZ8133ZHMJQ21EJ3545-2016;IN?????BM6005K01;PH20012005;SC;CJ;LH;DDMO20072200041;CB;SX2023-2-27 0:00:00;SL1;JL;KG;HG;DL;ZP;QT;XT1001AS10000000517DT2;";
        rule = "1;2;PN[-1:0:-1];BATCH[-1:0:-1];5;6;7;8;9;EXPDATEyyyy-MM-dd[2:0:8];QTY[2:0:-1];12;13;14;15;16;17;RI[-1:0:-1]";

        //rule = "1>PN[-1:0:6]>3>RI[-1:0:6]";
        //codeStr = "<ckdh>CC200612000194</ckdh><jybh>ADK20-\n" +"JY19060012~/jybh>";

        //rule = "1;2;PN[-1:0:-1];BATCH[-1:0:-1];5;6;7;8;9;EXPDATEyyyy-MM-dd[2:0:8];QTY[2:0:-1];12;13;14;15;16;17;RI[-1:0:-1]";
        //codeStr = "DM8431CZ8133ZHMJQ21EJ3545-2016;IN;BM6005K01;PH20012005;SC;CJ;LH;DDMO20072200041;CB;SX2023-2-27 00:10:00;SL1;JL;KG;HG;DL;ZP;QT;XT1001AS10000000517DT2;";
        //803
        //rule = "PN[6:0:-1],EXPDATEyyyy-MM-dd[-1:0:-1],BATCH,RI";
        //codeStr = "ALPHA OL107E,2021-11-11,00714026Z,002";
        codeStr = "IAC;6013A0073981;20200810;R2032E1128;10000;MI020A600568;";
        //codeStr = "IAC;B23D45;20200204;DCRFFD;45;WER23;";
        rule = "SP;PN;PRODATEyyw[0:4:0];BATCH;QTY;RI;";
        //rule = "SP;PN;PRODATEyyyyMMdd[0:8:0];BATCH;QTY;RI;";

        codeStr = "L0000000000SB4371503H;E20201026 0365;B7H.22424.211821872020102603000;R821872020102602187";
        rule = "BATCH;2;PN[1:12:-1]SP[13:5:-1]QTY[-1:-5:-1];RI";

//        codeStr = "64.10R05.6DL-960286|20213239|5000|W64960286132194G0|CYNTEC";
//        rule = "PN|BATCH|QTY|RI|SP";

        BarcodeRule br = BarcodeRule.newRule(rule);
        Barcode b = br.toCodeBean(codeStr).getBarcode();
        if(b != null){
            System.out.println("PN:"+b.getPartNumber());
            System.out.println("RI:"+b.getBarcode());
            System.out.println("Amount:"+b.getAmount());
            System.out.println("BATCH:"+b.getBatch());
            //System.out.println("MSL:"+b.getMsl());
            System.out.println("PRODATE:"+b.getProduceDate());
            System.out.println("EXPDATE:"+b.getExpireDate());
            System.out.println("Supllier:"+b.getProvider());
            System.out.println("Memo:"+b.getMemo());
        }else{
            log.info("解析失败");
        }



//        URI uri = new URI("smb://materialtower:Flextronics1@10.222.42.48/materialtower/re20190822161258_result.txt");
//        String smbFileName = uri.getScheme() + "://" + uri.getHost() + "" + uri.getPath() + "abc.txt";
//        System.out.println(smbFileName);

        //System.out.println("host : "+SmbUtil.getDomainFromUrl("\\\\192.168.1.100\\materialtower\\"));

    }

}