Procházet zdrojové kódy

统计基础类调整

gaodm před 5 roky
rodič
revize
7d3be1187e

+ 2 - 1
common/src/main/java/com/diagbot/enums/VisibleIdTypeEnum.java

@@ -12,7 +12,8 @@ public enum VisibleIdTypeEnum implements KeyedNamed {
     IS_IMG_VER(1, "图片验证码"),
     IS_ORDER(2, "订单编号"),
     IS_RENEWALS(3,"续费单号"),
-    IS_AUTH(4,"认证申请单号");
+    IS_AUTH(4,"认证申请单号"),
+    PATIENT_NO(5,"病人自动编号");
 
     @Setter
     private int key;

+ 36 - 0
prec-service/src/main/java/com/diagbot/idc/AbstractIdCreater.java

@@ -0,0 +1,36 @@
+package com.diagbot.idc;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * @Description: ID生成抽象类接口
+ * @author: gaodm
+ * @time: 2018/9/5 9:20
+ */
+public abstract class AbstractIdCreater<T> implements IdCreater<T> {
+
+    protected int machineId;
+    protected int dataCenterId;
+
+    public AbstractIdCreater(int machineId, int dataCenterId) {
+        this.machineId = machineId;
+        this.dataCenterId = dataCenterId;
+    }
+
+    @Override
+    public abstract Long getNextId(T param);
+
+    @Override
+    public List<Long> getNextIds(T param, int size) {
+        List<Long> longs = new ArrayList<>(size);
+        for (int i = 0; i < size; i++) {
+            longs.add(getNextId(param));
+        }
+        return longs;
+    }
+
+    protected long timeGen() {
+        return System.currentTimeMillis();
+    }
+}

+ 26 - 0
prec-service/src/main/java/com/diagbot/idc/IdCreater.java

@@ -0,0 +1,26 @@
+package com.diagbot.idc;
+
+import java.util.List;
+
+/**
+ * @Description: ID生成接口
+ * @author: gaodm
+ * @time: 2018/9/5 9:21
+ */
+public interface IdCreater<T> {
+
+    /**
+     * 生成一个id
+     *
+     * @return 生成的id
+     */
+    Long getNextId(T param);
+
+    /**
+     * 批量生成id
+     *
+     * @param size 生成数量
+     * @return 生成的id列表
+     */
+    List<Long> getNextIds(T param, int size);
+}

+ 76 - 0
prec-service/src/main/java/com/diagbot/idc/VisibleIdCreater.java

@@ -0,0 +1,76 @@
+package com.diagbot.idc;
+
+
+import com.diagbot.enums.VisibleIdTypeEnum;
+import com.diagbot.util.DateUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.RandomUtils;
+import org.apache.commons.lang3.time.DateUtils;
+
+import java.util.Calendar;
+
+/**
+ * @Description: 可见id每秒递增
+ * @author: gaodm
+ * @time: 2018/9/5 10:45
+ */
+@Slf4j
+public class VisibleIdCreater extends AbstractIdCreater<Integer> {
+
+    private long lastTimestamp = -1L;
+
+    private long sequence = 0L;
+    private long sequenceMask = 9999;
+
+    public VisibleIdCreater(int machineId, int dataCenterId) {
+        super(machineId, dataCenterId);
+    }
+
+    @Override
+    /**
+     *  对外id生成规则
+     *  1809051234550001
+     *  180905 - 12345 -  1  - 0001
+     *   日期 -  秒数  - 业务 - 秒内自增
+     *  @param type 业务id 1.订单 2.交易 3.退款
+     *  @return 生成的id
+     */
+    public synchronized Long getNextId(Integer type) {
+
+        Calendar calendar = Calendar.getInstance();
+
+        long timestamp = timeGen() / 1000;
+
+        if (timestamp < lastTimestamp) {
+            log.error(String.format("clock is moving backwards. Rejecting requests until %d.", lastTimestamp));
+            throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp));
+        }
+
+        if (lastTimestamp == timestamp) {
+            sequence = (sequence + 1);
+            if (sequence > sequenceMask) {
+                //timestamp = tilNextMillis(lastTimestamp);
+                log.error(String.format("id creater sequence is full. wait to next time"));
+                return null;
+            }
+        } else {
+            sequence = getNewSequence();
+        }
+
+        lastTimestamp = timestamp;
+
+        String date = DateUtil.format(calendar.getTime(), "yyMMdd");
+        long seconds = DateUtils.getFragmentInSeconds(calendar, Calendar.DAY_OF_YEAR);
+
+        return Long.valueOf(date + String.format("%05d", seconds) + String.valueOf(type) + sequence);
+    }
+
+    private long getNewSequence() {
+        return RandomUtils.nextInt(1000, 2000);
+    }
+
+    public static void main(String args[]) {
+        VisibleIdCreater visibleIdCreater = new VisibleIdCreater(0, 0);
+        System.out.println(visibleIdCreater.getNextId(VisibleIdTypeEnum.PATIENT_NO.getKey()));
+    }
+}