码迷,mamicode.com
首页 > 编程语言 > 详细

java订单生成工具类

时间:2019-05-07 19:56:12      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:欢迎来到   生成   common   pac   新闻   time()   mss   final   tag   

 欢迎来到付宗乐个人博客网站。本个人博客网站提供最新的站长新闻,各种互联网资讯。 还提供个人博客模板,最新最全的java教程,java面试题。在此我将尽我最大所能将此个人博客网站做的最好! 谢谢大家,愿大家一起进步! 

 1 package com.hykj.common.app.utils;
 2 
 3 import java.text.DateFormat;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Date;
 6 import java.util.Random;
 7 
 8 /**
 9 * 
10 * @Description:订单生成类
11 * @author: fuzongle
12 * @date: 2019年5月7日 下午7:12:36
13 */
14 public class OrderCodeFactory {
15 
16 
17 /** 订单类别头 */
18 private static final String ORDER_CODE = "1";
19 /** 退货类别头 */
20 private static final String RETURN_ORDER = "2";
21 /** 退款类别头 */
22 private static final String REFUND_ORDER = "3";
23 /** 未付款重新支付别头 */
24 private static final String AGAIN_ORDER = "4";
25 /** 随即编码 */
26 private static final int[] r = new int[] { 7, 9, 6, 2, 8, 1, 3, 0, 5, 4 };
27 /** 用户id和随机数总长度 */
28 private static final int maxLength = 10;
29 
30 /** * 更具id进行加密+加随机数组成固定长度编码 */
31 private static String toCode(Long id) {
32 String idStr = id.toString();
33 StringBuilder idsbs = new StringBuilder();
34 for (int i = idStr.length() - 1; i >= 0; i--) {
35 idsbs.append(r[idStr.charAt(i) - ‘0‘]);
36 }
37 return idsbs.append(getRandom(maxLength - idStr.length())).toString();
38 }
39 
40 /** * 生成时间戳 */
41 private static String getDateTime() {
42 DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
43 return sdf.format(new Date());
44 }
45 
46 /** * 生成固定长度随机码 * @param n 长度 */
47 private static long getRandom(long n) {
48 long min = 1, max = 9;
49 for (int i = 1; i < n; i++) {
50 min *= 10;
51 max *= 10;
52 }
53 long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min;
54 return rangeLong;
55 }
56 
57 /** * 生成不带类别标头的编码 * @param userId */
58 private static synchronized String getCode(Long userId) {
59 userId = userId == null ? 10000 : userId;
60 return getDateTime() + toCode(userId);
61 }
62 
63 /** * 生成订单单号编码 * @param userId */
64 public static String getOrderCode(Long userId) {
65 return ORDER_CODE + getCode(userId);
66 }
67 
68 /** * 生成退货单号编码 * @param userId */
69 public static String getReturnCode(Long userId) {
70 return RETURN_ORDER + getCode(userId);
71 }
72 
73 /** * 生成退款单号编码 * @param userId */
74 public static String getRefundCode(Long userId) {
75 return REFUND_ORDER + getCode(userId);
76 }
77 
78 /** * 未付款重新支付 * @param userId */
79 public static String getAgainCode(Long userId) {
80 return AGAIN_ORDER + getCode(userId);
81 }
82 
83 }

 

java订单生成工具类

标签:欢迎来到   生成   common   pac   新闻   time()   mss   final   tag   

原文地址:https://www.cnblogs.com/fuzongle/p/10827443.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!