규칙을 설정하여 각종 증빙서류 번호 생성(二)

11949 단어 중간부품
**        **
/**
*seqCode :       Code
*(code==0   ,      , msg=       ||     )
*/
public Map generateSequence(String seqCode){
        //    
        Map retMap = new HashMap();  
        //       
        String sequence = "";
        //     
        if (StringUtils.isEmpty(seqCode)){
            retMap.put("code", "1001");
            retMap.put("msg", "    :      ");
            return retMap;
        }
        //       code         
        SysSeq seq = getDao(SequenceMapper.class).findBySeqCode(seqCode);
        //         ,           
        if (seq!= null){
            List seqRuleList =getDao(SequenceRuleMapper.class).findBySeqCode(seq.getSeqCode());
            //       1,         
            if (seqRuleList.size()<1){
                retMap.put("code", "1005");
                retMap.put("msg", "                 ,          !");
                return retMap;
            }
            int delimiterNum = 0;
            for (SysSeqRule rule : seqRuleList){
                delimiterNum++;
                switch (rule.getRuleCode()) {
                    //    
                    case "const":               
                        sequence += rule.getRuleValue();
                        break;
                    //  
                    case "numbering":     
                        //          
                        Integer num = currentReset(seq, rule);      
                        //    
                        sequence += numberingSeqRule(rule, num);   
                        //      ,     +   
                        seq.setCurrentNo(num + seq.getStep());      
                        break;
                    //       
                    case "timestamp":           
                        sequence += DateUtils.getDate(rule.getRuleValue());
                        break;
                    //  
                    case "param":               
                        if(StringUtils.isNotEmpty(seqParam)){
                            sequence += seqParam;
                        }else {
                            retMap.put("code", "1008");
                            retMap.put("msg", "           ,     !");
                            return retMap;
                        }
                        break;
                }
                if (StringUtils.isNotEmpty(seq.getDelimiter()) && delimiterNum!= seqRuleList.size())
        {           //   
                    sequence += seq.getDelimiter();     
                }
            }
            //    
            seq.setCurrentCode(sequence);
            //                     
            seq.setCurrentReset(DateUtils.getDate("yyyyMMdd"));     

            SysSeqValue seqValue = new SysSeqValue(
                   null,null,null,
                    seq.getId(),seqCode,
                    null, null,
                    seq.getCurrentNo(),seq.getCurrentCode(),seq.getCurrentReset()
            );
            //        
            getDao(SequenceValueMapper.class).insert(seqValue);;     
            if (seqValue.getId() >0){
                //         
                getDao(SequenceMapper.class).updateCurrentCode(seq);    
                retMap.put("code", "0");
                retMap.put("msg", sequence);
                logger.info("       : "+sequence);
            }else{
                retMap.put("code", "1006");
                retMap.put("msg", "        ,    !");
            }
        }else{
            retMap.put("code", "1004");
            retMap.put("msg", "        ,             !");
        }
        return retMap;
    }
    /**
     *           
     * @return
     */
private Integer currentReset(SysSeq seq, SysSeqRule seqRule){
        Integer no = null;
        Integer ruleNo;
        try {
            ruleNo = Integer.parseInt(seqRule.getRuleValue());
        }catch (Exception e){
            return 1;
        }

        switch (seq.getResetRule()) {
            case "day":      //    
                String resetDate1 = seq.getCurrentReset();
                SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
                if(!StringUtils.isBlank(resetDate1)&&!resetDate1.equals(formatter.format(new Date()))){
                    return 1;
                }
                break;
            case "month":    //    
                String resetDate = seq.getCurrentReset().substring(seq.getCurrentReset().length() - 2);
                //      28/29/30/31         .          4   
                if (resetDate.equals("28") || resetDate.equals("29") ||
                        resetDate.equals("30") || resetDate.equals("31")) {
                    //               ,    :     ,       
                    if (DateUtils.getDay().equals("01")) {
                        no = ruleNo;
                    }
                }
                break;
            case "year":    //    
                //      = 1 1         != 1 1 
                if (DateUtils.getDate("yyyyMMdd").contains("0101") && !seq.getCurrentReset().contains("0101")) {
                    no = ruleNo;
                }
                break;
        }
        if (no == null){
            if (seq.getCurrentNo() == null || seq.getCurrentNo()==0){
                return ruleNo;
            }else {
                return seq.getCurrentNo();
            }
        }
        return no;
    }
 /**
     *     
     * @param seqRule
     * @return
     */
    private String numberingSeqRule(SysSeqRule seqRule, Integer code){
        String str = "" ;
        int codeConst = seqRule.getPaddingWide() - sizeOfInt(code);    //      
        if (codeConst > 0) {
            String st = org.apache.commons.lang3.StringUtils.repeat("0", codeConst);   //  
            if (seqRule.getPaddingSide().equals("left")){            //   
                str+= code + st;
            } else if (seqRule.getPaddingSide().equals("right")){    //   
                str+= st + code;
            }
        }else {
            str+= code;
        }

        return str;
    }

좋은 웹페이지 즐겨찾기