java生成独一无二的工单号
此文主要以demo为例,对此直奔主题
以下demo有参考价值
demo
接口文件
/** * 使用redis生成分布式id */ public interface idgeneratorservice { /** * @param biz 业务名称 */ long generatorid(string biz); /** * * @return */ long generatorid(); /** * 返回工单号 customizerstr-日期-序号 * @param customizerstr 自定义参数 */ string generatororderno(string customizerstr); }
其生成的工单函数
如下:
import com.google.common.base.strings; import org.springblade.equipment.service.idgeneratorservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.data.redis.core.redistemplate; import org.springframework.data.redis.core.valueoperations; import org.springframework.stereotype.service; import java.time.localdate; import java.util.calendar; import java.util.date; import java.util.concurrent.timeunit; import lombok.extern.slf4j.slf4j; @service @slf4j public class redisidgeneratorservice implements idgeneratorservice { private static final string keyprefix = "smart"; /** * jedisclient对象 */ @autowired private redistemplate<string, object> redistemplate; /** * @description * @author butterfly */ private string getidprefix() { date date = new date(); calendar c = calendar.getinstance(); c.settime(date); int year = c.get(calendar.year); int day = c.get(calendar.day_of_year); // 今天是第多少天 int hour = c.get(calendar.hour_of_day); int minute = c.get(calendar.minute); int second = c.get(calendar.second); string dayfmt = string.format("%1$03d", day); // 0补位操作 必须满足三位 string hourfmt = string.format("%1$02d", hour); // 0补位操作 必须满足2位 string minutefmt = string.format("%1$02d", minute); // 0补位操作 必须满足2位 string secondfmt = string.format("%1$02d", second); // 0补位操作 必须满足2位 stringbuffer prefix = new stringbuffer(); prefix.append((year - 2000)).append(dayfmt).append(hourfmt).append(minutefmt).append(secondfmt); return prefix.tostring(); } /** * 自定义工单前缀 */ private string getordernoprefix(string customizerstr){ stringbuffer prefix = new stringbuffer(); localdate now = localdate.now(); prefix.append(customizerstr).append("-").append(now.getyear()).append(now.getmonthvalue()).append(now.getdayofmonth()); return prefix.tostring(); } /** * @author butterfly */ private long incrdistrid(string biz) { string prefix = getidprefix(); string orderid = null; string key = "#{biz}:id:".replace("#{biz}", biz).concat(prefix); // 00001 try { valueoperations<string, object> valueoper = redistemplate.opsforvalue(); long index = valueoper.increment(key, 1); orderid = prefix.concat(string.format("%1$05d", index)); // 补位操作 保证满足5位 } catch (exception ex) { log.error("分布式订单号生成失败异常。。。。。", ex); } finally { redistemplate.expire(key, 600, timeunit.seconds);//保留10分钟内的key } if (strings.isnullorempty(orderid)) return 0; return long.parselong(orderid); } /** * @description 生成分布式id * @author butterfly */ @override public long generatorid(string biz) { // 转成数字类型,可排序 return incrdistrid(biz); } @override public long generatorid() { return incrdistrid(keyprefix); } /** * 获得前缀 * 去redis查看是否有对应的值 * 有的话取回来新增 001-》002 * 没有的直接插入,值为001 * 设置过期时间24小时 * @param customizerstr 自定义参数 * @return */ @override public string generatororderno(string customizerstr) { string ordernoprefix = this.getordernoprefix(customizerstr); string key = ordernoprefix; string value =""; try{ valueoperations<string, object> valueoper = redistemplate.opsforvalue(); object o = valueoper.get(key); //没有的直接插入,值为001 if(o==null){ valueoper.set(key,1); value="001"; }else{ //有的话取回来新增 001-》002 integer oldvalue = integer.valueof(o.tostring()); int newvalue = oldvalue + 1; valueoper.set(key,newvalue); //其中0表示补零而不是补空格,6表示至少6位 value=string.format("%03d",newvalue); } }catch (exception e){ log.error("工单no生成失败,reason:"+e); }finally { redistemplate.expire(key,24,timeunit.hours); } return key+"-"+value; } }
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论