/** * 保存自动编号 * * @param wac * @return */ public boolean saveAutoCode(SysAutoCode wac) { try { SysAutoCode wac_t = this.loadAutoCodeById(wac.getCodeId()); wac_t.setCodeConfig(wac.getCodeConfig()); wac_t.setCount(wac.getCount()); wac_t.setReset(wac.getReset()); if (wac.getStart() == null) { wac_t.setStart(0); } else { wac_t.setStart(wac.getStart()); } wac_t.setStep(wac.getStep()); dbDao.updateObject(wac_t); return true; } catch (Exception e) { logger.error("保存自动编号失败!", e); } return false; }
/** * 获取自动编码 * * @param acd * @return */ private String getCode(SysAutoCode acd, boolean nextTrue) { if (acd.getStart().intValue() == 0) { return ""; } try { String[] strs = acd.getCodeConfig().split("\\+"); StringBuilder retStr = new StringBuilder(); for (String string : strs) { logger.debug("string==:" + string); if (Pattern.compile("^[\"|'|\\(]\\S+[\"|'|\\)]$").matcher(string).find()) { retStr.append(string.substring(1, string.length() - 1)); } else if (Pattern.compile("^Date\\([yy|yyyy|MM|dd|HH|mm]+\\)$").matcher(string).find()) { SimpleDateFormat dateformat1 = new SimpleDateFormat(string.substring(5, string.length() - 1)); retStr.append(dateformat1.format(new Date())); } else if (Pattern.compile("^Counter\\([0|#]+\\)$").matcher(string).find()) { int t = string.indexOf(")", 9); DecimalFormat df = new DecimalFormat(string.substring(8, t)); // 判断计数器重置 * 计数器重置标识 0 按年 1 按月 2 按日 switch (acd.getReset()) { case 0: logger.debug("按年重置"); if (DataUtils.getYear(new Date()) != DataUtils.getYear(acd.getCreateDate())) { acd.setCount(1); } break; case 1: logger.debug("按月重置"); if (DataUtils.getYear(new Date()) != DataUtils.getYear(acd.getCreateDate()) || DataUtils.getMonth(new Date()) != DataUtils.getMonth(acd.getCreateDate())) { acd.setCount(1); } break; case 2: logger.debug("按天重置"); if (DataUtils.getYear(new Date()) != DataUtils.getYear(acd.getCreateDate()) || DataUtils.getMonth(new Date()) != DataUtils.getMonth(acd.getCreateDate()) || DataUtils.getDay(new Date()) != DataUtils.getDay(acd.getCreateDate())) { acd.setCount(1); } break; case 3: logger.debug("按小时重置"); if (DataUtils.getYear(new Date()) != DataUtils.getYear(acd.getCreateDate()) || DataUtils.getMonth(new Date()) != DataUtils.getMonth(acd.getCreateDate()) || DataUtils.getDay(new Date()) != DataUtils.getDay(acd.getCreateDate()) || DataUtils.getHour(new Date()) != DataUtils.getHour(acd.getCreateDate())) { acd.setCount(1); } break; } // 格式化输出序号 retStr.append(df.format(acd.getCount())); // 设置下一次计数 acd.setCount(acd.getCount() + acd.getStep()); // 更新取数时间 acd.setCreateDate(new Date()); } } // 判断是否自动更新 if (nextTrue) { dbDao.updateObject(acd); } logger.info("输出自动单号:" + retStr); return retStr.toString(); } catch (Exception e) { logger.error("更新自动编号失败!", e); } return ""; }