wuxw7
2018-05-22 9e0fa31a2b81a9d2fd2588b78b6de637f0352639
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.java110.common.util;
 
import com.java110.common.cache.MappingCache;
import com.java110.common.constant.MappingConstant;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
/**
 * 生成序列工具类
 * Created by wuxw on 2017/2/27.
 */
public class SequenceUtil {
 
    private static final long ONE_STEP = 1000000;
    private static final Lock LOCK = new ReentrantLock();
    private static short lastCount = 1;
    private static int count = 0;
    private static final String first = "10";
 
    /**
     *
     * 只有在不调用服务生成ID时有用
     */
    private static Map prefixMap = null;
    static {
        prefixMap = new HashMap();
        //10+yyyymmdd+八位序列
        prefixMap.put("oId","10");
        //(20+yyyymmdd+八位序列)
        prefixMap.put("bId","20");
        //(11+yyyymmdd+八位序列)
        prefixMap.put("attrId","11");
        prefixMap.put("transactionId","1000001");
        prefixMap.put("pageTransactionId","1000002");
        prefixMap.put("dataFlowId","2000");
        prefixMap.put("userId","3000");
    }
 
    private static String PLATFORM_CODE = "0001";
 
    @SuppressWarnings("finally")
    public static String nextId(String idLength) {
        LOCK.lock();
        try {
            if (lastCount == ONE_STEP) {
                lastCount = 1;
            }
            count = lastCount++;
        } finally {
            LOCK.unlock();
            return String.format(idLength, count);
        }
    }
 
    public static String nextId(){
        return nextId("%06d");
    }
 
    /**
     * 获取交易流水ID
     *
     * @return
     */
    public static String getTransactionId() {
 
        //从内存中获取平台随机码
 
        return prefixMap.get("transactionId") + DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H) + nextId();
    }
 
    /**
     * 获取交易流水ID
     *
     * @return
     */
    public static String getPageTransactionId() {
 
        //从内存中获取平台随机码
 
        return prefixMap.get("pageTransactionId") + DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H) + nextId();
    }
 
    public static String getOId(){
        if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){
            return prefixMap.get("oId") + DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H) + nextId("%08d");
        }
        //调用服务
        return null;
    }
 
    public static String getBId(){
        if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){
            return prefixMap.get("bId") + DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H) + nextId("%08d");
        }
        //调用服务
        return null;
    }
 
    public static String getAttrId(){
        if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){
            return prefixMap.get("attrId") + DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H) + nextId("%08d");
        }
        //调用服务
        return null;
    }
 
    public static String getDataFlowId(){
        if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){
            return prefixMap.get("dataFlowId") + DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H) + nextId("%08d");
        }
 
        return null;
 
    }
 
    public static String getUserId(){
        if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){
            return prefixMap.get("userId") + DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H) + nextId("%08d");
        }
        //调用服务
        return null;
    }
 
}