当前位置: 代码网 > it编程>编程语言>Java > Java生成独一无二的工单号实例

Java生成独一无二的工单号实例

2024年09月04日 Java 我要评论
java生成独一无二的工单号此文主要以demo为例,对此直奔主题以下demo有参考价值demo接口文件/** * 使用redis生成分布式id */public interface idgenerat

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;
	}


}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com