Java生成獨一無二的工單號實例
更新時間:2024年09月04日 10:04:29 作者:碼農(nóng)研究僧
這篇文章主要介紹了Java生成獨一無二的工單號實例,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
Java生成獨一無二的工單號
此文主要以Demo為例,對此直奔主題
以下Demo有參考價值
Demo
接口文件
/** * 使用redis生成分布式ID */ public interface IdGeneratorService { /** * @param biz 業(yè)務名稱 */ long generatorId(String biz); /** * * @return */ long generatorId(); /** * 返回工單號 customizerStr-日期-序號 * @param customizerStr 自定義參數(shù) */ String generatorOrderNo(String customizerStr); }
其生成的工單函數(shù)
如下:
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) { // 轉成數(shù)字類型,可排序 return incrDistrId(biz); } @Override public long generatorId() { return incrDistrId(keyPrefix); } /** * 獲得前綴 * 去redis查看是否有對應的值 * 有的話取回來新增 001-》002 * 沒有的直接插入,值為001 * 設置過期時間24小時 * @param customizerStr 自定義參數(shù) * @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; } }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
springboot讀取自定義配置文件時出現(xiàn)亂碼解決方案
這篇文章主要介紹了springboot讀取自定義配置文件時出現(xiàn)亂碼解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Springboot接收?Form?表單數(shù)據(jù)的示例詳解
這篇文章主要介紹了Springboot接收?Form?表單數(shù)據(jù)的實例代碼,本文通過圖文實例代碼相結合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-08-08SpringBoot統(tǒng)一數(shù)據(jù)返回格式的實現(xiàn)示例
本文主要介紹了SpringBoot統(tǒng)一數(shù)據(jù)返回格式,它提高了代碼的可維護性和一致性,并改善了客戶端與服務端之間的通信,具有一定的參考價值,感興趣的可以了解一下2024-05-05