wuxw7
2018-07-08 80478815dcce0329fa192ffd53060e736f7b6700
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package com.java110.core.factory;
 
import com.alibaba.fastjson.JSONObject;
import com.java110.common.cache.MappingCache;
import com.java110.common.constant.MappingConstant;
import com.java110.common.constant.ResponseConstant;
import com.java110.common.exception.GenerateCodeException;
import com.java110.common.exception.ResponseErrorException;
import com.java110.common.factory.ApplicationContextFactory;
import com.java110.common.util.Assert;
import com.java110.common.util.DateUtil;
import com.java110.feign.code.ICodeApi;
import org.springframework.web.client.RestTemplate;
 
import java.rmi.NoSuchObjectException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
/**
 * 生成序列工具类
 * Created by wuxw on 2017/2/27.
 */
public class GenerateCodeFactory {
 
    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<String,String> prefixMap = null;
    static {
        prefixMap = new HashMap<String,String>();
        //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","30");
        prefixMap.put("storeId","40");
        prefixMap.put("storePhotoId","41");
        prefixMap.put("storeCerdentialsId","42");
        prefixMap.put("shopId","50");
        prefixMap.put("shopAttrId","51");
        prefixMap.put("shopPhotoId","52");
        prefixMap.put("shopAttrParamId","53");
        prefixMap.put("shopPreferentialId","54");
        prefixMap.put("shopDescId","55");
    }
 
    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();
    }
 
    /**
     * 获取内部平台 交易流水
     * @return
     * @throws NoSuchObjectException
     */
    public static String getInnerTransactionId() throws Exception{
        return codeApi().generateCode(prefixMap.get("transactionId"));
    }
 
    /**
     * 获取交易流水ID
     *
     * @return
     */
    public static String getPageTransactionId() {
 
        //从内存中获取平台随机码
 
        return prefixMap.get("pageTransactionId") + DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H) + nextId();
    }
 
    public static String getOId() throws GenerateCodeException{
        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 getCode(prefixMap.get("oId"));
    }
 
 
    /**
     * 查询Code
     * @param prefix
     * @return
     * @throws GenerateCodeException
     */
    private static String getCode(String prefix) throws GenerateCodeException{
        //调用服务
        String code = "-1";
        try {
            String responseMessage = restTemplate().postForObject(MappingCache.getValue(MappingConstant.KEY_CODE_PATH),
                    createCodeRequestJson(getTransactionId(),prefix,prefix).toJSONString(), String.class);
 
            if(ResponseConstant.RESULT_CODE_ERROR.equals(responseMessage)){
                throw new ResponseErrorException(ResponseConstant.RESULT_CODE_ERROR, "生成oId编码失败");
            }
            Assert.jsonObjectHaveKey(responseMessage, "code", "编码生成系统 返回报文错误" + responseMessage);
 
            JSONObject resJson = JSONObject.parseObject(responseMessage);
 
            if (!ResponseConstant.RESULT_CODE_SUCCESS.equals(resJson.getString("code"))) {
                throw new ResponseErrorException(resJson.getString("code"), "生成oId编码失败 "
                        + resJson.getString("message"));
            }
            code = resJson.getString("id");
        }catch (Exception e){
            throw new GenerateCodeException(ResponseConstant.RESULT_CODE_ERROR,e.getMessage());
        }
        finally {
            return code;
        }
 
    }
 
    public static String getBId()  throws GenerateCodeException{
        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 getCode(prefixMap.get("bId"));
    }
 
    public static String getAttrId()  throws GenerateCodeException{
        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 getCode(prefixMap.get("attrId"));
    }
 
    /**
     * 生成dataFlowId
     * @return
     * @throws GenerateCodeException
     */
    public static String getDataFlowId()  throws GenerateCodeException{
 
        return UUID.randomUUID().toString().replace("-","").toLowerCase();
 
    }
 
    public static String getUserId()  throws GenerateCodeException{
        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("%06d");
        }
        //调用服务
        return getCode(prefixMap.get("userId"));
    }
 
 
    public static String getStoreId()  throws GenerateCodeException{
        if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){
            return prefixMap.get("storeId") +DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H)+ nextId("%06d");
        }
        //调用服务
        return getCode(prefixMap.get("storeId"));
    }
 
 
    public static String getStorePhotoId()  throws GenerateCodeException{
        if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){
            return prefixMap.get("storePhotoId") +DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H)+ nextId("%06d");
        }
        //调用服务
        return getCode(prefixMap.get("storePhotoId"));
    }
 
    /**
     *
     * @return
     * @throws GenerateCodeException
     */
    public static String getStoreCerdentialsId()  throws GenerateCodeException{
        if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){
            return prefixMap.get("storeCerdentialsId") +DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H)+ nextId("%06d");
        }
        //调用服务
        return getCode(prefixMap.get("storeCerdentialsId"));
    }
 
    /**
     * 商品ID生成
     * @return
     * @throws GenerateCodeException
     */
    public static String getShopId()  throws GenerateCodeException{
        if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){
            return prefixMap.get("shopId") +DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H)+ nextId("%06d");
        }
        //调用服务
        return getCode(prefixMap.get("shopId"));
    }
 
    /**
     * 商品属性ID生成
     * @return
     * @throws GenerateCodeException
     */
    public static String getShopAttrId()  throws GenerateCodeException{
        if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){
            return prefixMap.get("shopAttrId") +DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H)+ nextId("%06d");
        }
        //调用服务
        return getCode(prefixMap.get("shopAttrId"));
    }
 
    /**
     * 商品优惠ID生成
     * @return
     * @throws GenerateCodeException
     */
    public static String getShopPreferentialId()  throws GenerateCodeException{
        if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){
            return prefixMap.get("shopPreferentialId") +DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H)+ nextId("%06d");
        }
        //调用服务
        return getCode(prefixMap.get("shopPreferentialId"));
    }
 
 
 
    /**
     * 商品属性参数ID生成
     * @return
     * @throws GenerateCodeException
     */
    public static String getShopAttrParamId()  throws GenerateCodeException{
        if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){
            return prefixMap.get("shopAttrParamId") +DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H)+ nextId("%06d");
        }
        //调用服务
        return getCode(prefixMap.get("shopAttrParamId"));
    }
 
    /**
     * 商品属性ID生成
     * @return
     * @throws GenerateCodeException
     */
    public static String getShopPhotoId()  throws GenerateCodeException{
        if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){
            return prefixMap.get("shopPhotoId") +DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H)+ nextId("%06d");
        }
        //调用服务
        return getCode(prefixMap.get("shopPhotoId"));
    }
 
    /**
     * 商品描述ID生成
     * @return
     * @throws GenerateCodeException
     */
    public static String getShopDescId()  throws GenerateCodeException{
        if(!MappingConstant.VALUE_ON.equals(MappingCache.getValue(MappingConstant.KEY_NEED_INVOKE_GENERATE_ID))){
            return prefixMap.get("shopDescId") +DateUtil.getNow(DateUtil.DATE_FORMATE_STRING_H)+ nextId("%06d");
        }
        //调用服务
        return getCode(prefixMap.get("shopDescId"));
    }
    /**
     * 获取restTemplate
     * @return
     * @throws NoSuchObjectException
     */
    private static RestTemplate restTemplate() throws NoSuchObjectException{
 
       Object bean = ApplicationContextFactory.getBean("restTemplate");
 
       if(bean == null){
           throw new NoSuchObjectException("没有找到restTemplate对象,请核实");
       }
 
       return (RestTemplate) bean;
    }
 
    /**
     * 获取codeApi
     * @return
     * @throws NoSuchObjectException
     */
    private static ICodeApi codeApi() throws NoSuchObjectException{
 
        Object bean = ApplicationContextFactory.getBean(ICodeApi.class.getName());
 
        if(bean == null){
            throw new NoSuchObjectException("codeApi,请核实");
        }
 
        return (ICodeApi) bean;
    }
 
 
 
 
    /**
     * ID生成请求报文
     * @param transactionId
     * @return
     */
    private static JSONObject createCodeRequestJson(String transactionId, String prefix, String name){
        JSONObject paramOut = JSONObject.parseObject("{}");
        paramOut.put("transactionId",transactionId);
        paramOut.put("prefix",prefix);
        paramOut.put("name",name);
        paramOut.put("requestTime",DateUtil.getNowDefault());
        return paramOut;
    }
}