package com.byd.mes.util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.byd.mes.bussiness.bo.main.NextNumberBO; import com.byd.mes.dev.nextNumber.NextNumberBaseConfig; /** * * ClassName:NextNumberArgUtils * @author kht * @Date 2011-6-1 下午01:39:44 */ public class NextNumberArgUtils { private static final Log log = LogFactory.getLog(NextNumberArgUtils.class); /** * * parse:(生成流水号) * @param String source * @param Object param * @param String seq * @param NextNumberBO bo * @return String */ public static String parse(String source,final Object param,final String seq, NextNumberBO bo){ return StringUtils.findAndReplace("\\x7B[\\s]*[\\w\\[\\]\\s\\:\\.]+[\\s]*}", source, new StringUtils.FindCallback() { @Override public String execute(String gr, NextNumberBO bo) { String s = StringUtils.substringBetween(gr,"{", "}").trim(); int si = s.indexOf("["); String varName = ""; String varParam = ""; if(si > -1){ varName = StringUtils.substringBefore(s, "["); //varParam = s.substring(si+1, s.length()-1).trim(); int index = s.indexOf("]"); if(index <= -1){ varParam = s.substring(si+1).trim(); //varParam = StringUtils.substringBetween(s, "[", "").trim(); }else{ varParam = StringUtils.substringBetween(s, "[", "]").trim(); } } else{ varName = s; } log.debug("var name:"+varName + ",var param:"+varParam+",var seq:"+seq); if(StringUtils.equals(varName,"seq")){ return seq; } else if(StringUtils.equals(varName,"date")){ return NextNumberArgUtils.toDate(varParam, bo); } else if(StringUtils.equals(varName,"var")){ Object o = BoPoPropertyUtil.getProperty(param, varParam); if(o != null){ return o.toString(); } } else{ } return gr; } }, bo); } /** * * parseTest:(抽象时产生的进制格式) * @param String source * @param Object param * @param String seq * @param NextNumberBO bo * @return String */ public static String parseTest(String source,final Object param,final String seq, NextNumberBO bo){ return StringUtils.findAndReplace("\\x7B[\\s]*[\\w\\[\\]\\s\\:\\.]+[\\s]*}", source, new StringUtils.FindCallback() { @Override public String execute(String gr, NextNumberBO bo) { String s = gr.replaceAll("[\\x7B\\}]", "").trim(); int si = s.indexOf("["); String varName = ""; String varParam = ""; if(si > -1){ varName = s.substring(0, si); varParam = s.substring(si+1, s.length()-1).trim(); } else{ varName = s; } log.debug("var name:"+varName + ",var param:"+varParam+",var seq:"+seq); if(StringUtils.equals(varName,"seq")){ return seq; } else if(StringUtils.equals(varName,"date")){ return NextNumberArgUtils.toDate(varParam, bo); } else if(StringUtils.equals(varName,"var")){ Object o = BoPoPropertyUtil.getProperty(param, varParam); if(o != null){ return o.toString(); } } else{ } return gr; } }, bo); } /** * * toDate:(转换成相对应的进制时间格式) * @param String partten * @param NextNumberBO bo * @return String */ public static String toDate(String partten, NextNumberBO bo){ String className = bo.getClassName(); if(StringUtils.isNotEmpty(className) && StringUtils.isNotBlank(className)){ NextNumberBaseConfig baseConfig = null; try{ baseConfig = (NextNumberBaseConfig) Class.forName(className).newInstance(); }catch(Exception ex){ log.debug(ex.getMessage()); } return baseConfig.toDate(partten); }else{ SimpleDateFormat format = new SimpleDateFormat(partten); String result = format.format(new Date()); StringBuffer buffer = new StringBuffer(); Date date = null; try { date = (Date)format.parseObject(result); } catch (ParseException e) { e.printStackTrace(); } if(partten.indexOf("yyyy") > -1){ int year = date.getYear()+1900; if(year == 2011){ year = 1; }else if(year == 2012){ year = 2; }else if(year == 2013){ year = 3; }else if(year == 2014){ year = 4; }else if(year == 2015){ year = 5; }else if(year == 2016){ year = 6; }else if(year == 2017){ year = 7; }else if(year == 2018){ year = 8; }else if(year == 2019){ year = 9; }else{ year = 0; } buffer.append(year); } if(partten.indexOf("MM") > -1){ String month = Integer.toString(date.getMonth()+1, bo.getSequenceBase()); buffer.append(month); } if(partten.indexOf("dd") > -1){ String day = Integer.toString(date.getDate(), bo.getSequenceBase()); buffer.append(day); } if(partten.indexOf("HH") > -1){ String hours = Integer.toString(date.getHours(), bo.getSequenceBase()); buffer.append(hours); } if(partten.indexOf("mm") > -1){ String minutes = Integer.toString(date.getMinutes(), bo.getSequenceBase()); buffer.append(minutes); } if(partten.indexOf("ss") > -1){ String seconds = Integer.toString(date.getSeconds(), bo.getSequenceBase()); buffer.append(seconds); } return buffer.toString(); } } }