**获取业务编号代码**
/**
*seqCode : 业务单据类型Code
*(code==0 成功, 其他均失败, msg= 新业务编号 || 错误提示)
*/
public Map<String, String> generateSequence(String seqCode){
Map<String, String> retMap = new HashMap<String, String>();
String sequence = "";
if (StringUtils.isEmpty(seqCode)){
retMap.put("code", "1001");
retMap.put("msg", "参数错误:业务编码编号");
return retMap;
}
SysSeq seq = getDao(SequenceMapper.class).findBySeqCode(seqCode);
if (seq!= null){
List<SysSeqRule> seqRuleList =getDao(SequenceRuleMapper.class).findBySeqCode(seq.getSeqCode());
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);
if (resetDate.equals("28") || resetDate.equals("29") ||
resetDate.equals("30") || resetDate.equals("31")) {
if (DateUtils.getDay().equals("01")) {
no = ruleNo;
}
}
break;
case "year":
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;
}