• java订单生成工具类


    技术交流QQ群:422167709 想学习的朋友可以进入,各路IT大牛帮忙解决问题

     

    欢迎来到付宗乐个人博客网站。本个人博客网站提供最新的站长新闻,各种互联网资讯。 还提供个人博客模板,最新最全的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 }
  • 相关阅读:
    8.2.8 A Simple Game
    8.2.7 Be the Winner
    8.2.6 John
    8.2.5 kiki's game
    8.2.2 Good Luck in CET-4 Everybody!
    8.2.4 悼念512汶川大地震遇难同胞——选拔志愿者
    8.1.8 Team Queue
    8.2.1 Brave Game
    8.1.7 愚人节的礼物
    8.1.6 Windows Message Queue
  • 原文地址:https://www.cnblogs.com/fuzongle/p/10827443.html
Copyright © 2020-2023  润新知