Forráskód Böngészése

增加数据库全局序列

wangliang 1 éve
szülő
commit
84889c774c

+ 56 - 0
sop-business/src/main/java/com/qmth/sop/business/entity/Sequence.java

@@ -0,0 +1,56 @@
+package com.qmth.sop.business.entity;
+
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+
+import java.io.Serializable;
+
+/**
+ * <p>
+ * 序列表
+ * </p>
+ *
+ * @author wangliang
+ * @since 2023-08-14
+ */
+@ApiModel(value = "Sequence对象", description = "序列表")
+public class Sequence implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @ApiModelProperty(value = "名称")
+    private String name;
+
+    @ApiModelProperty(value = "当前值")
+    @JsonSerialize(using = ToStringSerializer.class)
+    private Long currentValue;
+
+    @ApiModelProperty(value = "增长值")
+    private Integer increment;
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String name) {
+        this.name = name;
+    }
+
+    public Long getCurrentValue() {
+        return currentValue;
+    }
+
+    public void setCurrentValue(Long currentValue) {
+        this.currentValue = currentValue;
+    }
+
+    public Integer getIncrement() {
+        return increment;
+    }
+
+    public void setIncrement(Integer increment) {
+        this.increment = increment;
+    }
+}

+ 11 - 0
sop-business/src/main/java/com/qmth/sop/business/entity/TGError.java

@@ -2,6 +2,7 @@ package com.qmth.sop.business.entity;
 
 import com.fasterxml.jackson.databind.annotation.JsonSerialize;
 import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
+import com.qmth.sop.common.contant.SystemConstant;
 import io.swagger.annotations.ApiModel;
 import io.swagger.annotations.ApiModelProperty;
 
@@ -38,6 +39,16 @@ public class TGError implements Serializable {
     @ApiModelProperty(value = "创建时间")
     private Long createTime;
 
+    public TGError() {
+
+    }
+
+    public TGError(String summary) {
+        this.id = SystemConstant.getDbUuid();
+        this.summary = summary;
+        this.createTime = System.currentTimeMillis();
+    }
+
     public Long getId() {
         return id;
     }

+ 24 - 0
sop-business/src/main/java/com/qmth/sop/business/mapper/SequenceMapper.java

@@ -0,0 +1,24 @@
+package com.qmth.sop.business.mapper;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.qmth.sop.business.entity.Sequence;
+import org.apache.ibatis.annotations.Param;
+
+/**
+ * <p>
+ * 序列表 Mapper 接口
+ * </p>
+ *
+ * @author wangliang
+ * @since 2023-08-14
+ */
+public interface SequenceMapper extends BaseMapper<Sequence> {
+
+    /**
+     * 获取序列号
+     *
+     * @param name
+     * @return
+     */
+    Long selectNextVal(@Param("name") String name);
+}

+ 23 - 0
sop-business/src/main/java/com/qmth/sop/business/service/SequenceService.java

@@ -0,0 +1,23 @@
+package com.qmth.sop.business.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.qmth.sop.business.entity.Sequence;
+
+/**
+ * <p>
+ * 序列表 服务类
+ * </p>
+ *
+ * @author wangliang
+ * @since 2023-08-14
+ */
+public interface SequenceService extends IService<Sequence> {
+
+    /**
+     * 获取序列号
+     *
+     * @param name
+     * @return
+     */
+    Long selectNextVal(String name) throws InterruptedException;
+}

+ 6 - 0
sop-business/src/main/java/com/qmth/sop/business/service/TGErrorService.java

@@ -13,4 +13,10 @@ import com.qmth.sop.business.entity.TGError;
  */
 public interface TGErrorService extends IService<TGError> {
 
+    /**
+     * 保存错误信息
+     *
+     * @param s
+     */
+    void saveExamTgError(Object... s);
 }

+ 60 - 0
sop-business/src/main/java/com/qmth/sop/business/service/impl/SequenceServiceImpl.java

@@ -0,0 +1,60 @@
+package com.qmth.sop.business.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.qmth.sop.business.entity.Sequence;
+import com.qmth.sop.business.mapper.SequenceMapper;
+import com.qmth.sop.business.service.SequenceService;
+import com.qmth.sop.business.service.TGErrorService;
+import com.qmth.sop.common.contant.SystemConstant;
+import com.qmth.sop.common.enums.ErrorEnum;
+import com.qmth.sop.common.enums.ExceptionResultEnum;
+import com.qmth.sop.common.lock.MemoryLock;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+
+/**
+ * <p>
+ * 序列表 服务实现类
+ * </p>
+ *
+ * @author wangliang
+ * @since 2023-08-14
+ */
+@Service
+public class SequenceServiceImpl extends ServiceImpl<SequenceMapper, Sequence> implements SequenceService {
+
+    @Resource
+    MemoryLock memoryLock;
+
+    @Resource
+    TGErrorService tgErrorService;
+
+    /**
+     * 获取序列号
+     *
+     * @param name
+     * @return
+     */
+    @Override
+    public Long selectNextVal(String name) throws InterruptedException {
+        boolean lock = false;
+        for (int i = 0; i < SystemConstant.MAX_SEQUENCE_CIRCLE_COUNT; i++) {
+            lock = memoryLock.lock(SystemConstant.LOCK_SEQUENCE_PREFIX + name, name, SystemConstant.LOCK_SEQUENCE_TIME_OUT);
+            if (lock) {
+                try {
+                    return this.baseMapper.selectNextVal(name);
+                } finally {
+                    memoryLock.unlock(SystemConstant.LOCK_SEQUENCE_PREFIX + name);
+                }
+            } else {
+                Thread.sleep(SystemConstant.SEQUENCE_THREAD_SLEEP);
+            }
+        }
+        if (!lock) {
+            tgErrorService.saveExamTgError(ErrorEnum.SEQUENCE.name(), ErrorEnum.SEQUENCE.getTitle());
+            throw ExceptionResultEnum.ERROR.exception("获取序列失败");
+        }
+        return null;
+    }
+}

+ 16 - 0
sop-business/src/main/java/com/qmth/sop/business/service/impl/TGErrorServiceImpl.java

@@ -1,10 +1,13 @@
 package com.qmth.sop.business.service.impl;
 
+import com.alibaba.fastjson.JSONObject;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.qmth.sop.business.entity.TGError;
 import com.qmth.sop.business.mapper.TGErrorMapper;
 import com.qmth.sop.business.service.TGErrorService;
+import com.qmth.sop.common.contant.SystemConstant;
 import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
 
 /**
  * <p>
@@ -17,4 +20,17 @@ import org.springframework.stereotype.Service;
 @Service
 public class TGErrorServiceImpl extends ServiceImpl<TGErrorMapper, TGError> implements TGErrorService {
 
+    /**
+     * 保存错误信息
+     *
+     * @param s
+     */
+    @Override
+    @Transactional
+    public void saveExamTgError(Object... s) {
+        JSONObject jsonObject = new JSONObject();
+        jsonObject.put(SystemConstant.TYPE, s[0]);
+        jsonObject.put(SystemConstant.ERROR_METHOD, s[1]);
+        this.save(new TGError(jsonObject.toJSONString()));
+    }
 }

+ 52 - 1
sop-business/src/main/resources/db/log/wangliang_update_log.sql

@@ -347,4 +347,55 @@ INSERT INTO sys_privilege
 VALUES(3015, '查询枚举接口', '/api/admin/common/query_enum', 'URL', 64, 23, 'SYS', NULL, 1, 1, 0);
 
 ALTER TABLE t_b_quality_problem_apply ADD problem_no VARCHAR(100)
-    COMMENT '问题编号' NOT NULL after crm_no;
+    COMMENT '问题编号' NOT NULL after crm_no;
+
+--2023.8.14update
+-- ----------------------------
+-- Table structure for sequence
+-- ----------------------------
+DROP TABLE IF EXISTS `sequence`;
+CREATE TABLE `sequence` (
+                            `name` varchar(50) CHARACTER SET utf8 NOT NULL COMMENT '名称',
+                            `current_value` bigint NOT NULL DEFAULT '0' COMMENT '当前值',
+                            `increment` int NOT NULL DEFAULT '1' COMMENT '增长值',
+                            PRIMARY KEY (`name`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='序列表';
+
+-- ----------------------------
+-- Records of sequence
+-- ----------------------------
+BEGIN;
+INSERT INTO `sequence` VALUES ('cloudMarkSopFlow', 0, 1);
+INSERT INTO `sequence` VALUES ('dingExceptionFlow', 0, 1);
+INSERT INTO `sequence` VALUES ('officeSopFlow', 0, 1);
+INSERT INTO `sequence` VALUES ('projectExchangeFlow', 0, 1);
+INSERT INTO `sequence` VALUES ('qualityProblemFlow', 0, 1);
+COMMIT;
+
+DROP FUNCTION IF EXISTS `currval`;
+delimiter ;;
+CREATE FUNCTION `currval`(seq_name VARCHAR(50)) RETURNS int
+    DETERMINISTIC
+BEGIN
+	DECLARE value INTEGER;
+	SET value = 0;
+SELECT current_value INTO value
+FROM sequence
+WHERE name = seq_name;
+RETURN value;
+END;
+;;
+delimiter ;
+
+DROP FUNCTION IF EXISTS `nextval`;
+delimiter ;;
+CREATE FUNCTION `nextval`(seq_name VARCHAR(50)) RETURNS int
+    DETERMINISTIC
+BEGIN
+UPDATE sequence
+SET current_value = current_value + increment
+WHERE name = seq_name;
+RETURN currval(seq_name);
+END;
+;;
+delimiter ;

+ 8 - 0
sop-business/src/main/resources/mapper/SequenceMapper.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.qmth.sop.business.mapper.SequenceMapper">
+
+    <select id="selectNextVal" resultType="java.lang.Long">
+        SELECT NEXTVAL(#{name});
+    </select>
+</mapper>

+ 5 - 1
sop-common/src/main/java/com/qmth/sop/common/contant/SystemConstant.java

@@ -87,6 +87,9 @@ public class SystemConstant {
     public static final int PAGE_NUMBER_MIN = 1;
     public static final String STATIC = "static";
     public static final String CATALOG_LINK = "->";
+    public static final int MAX_SEQUENCE_CIRCLE_COUNT = 20;
+    public static final int SEQUENCE_THREAD_SLEEP = 500;
+    public static final String ERROR_METHOD = "method";
 
     /**
      * 表达式
@@ -217,9 +220,10 @@ public class SystemConstant {
     public static final String LOCK_FLOW_TASK_PREFIX = "lock:flow:task:";//流程节点锁
     public static final String LOCK_FLOW_END_PREFIX = "lock:flow:end:";//流程结束锁
     public static final String LOCK_FLOW_EXCHANGE_PREFIX = "lock:flow:exchange:";//流程转办锁
+    public static final String LOCK_SEQUENCE_PREFIX = "lock:sequence:";//序列锁
 
     public static final Long LOCK_FLOW_TIME_OUT = 60L * 2 * 1000;
-
+    public static final Long LOCK_SEQUENCE_TIME_OUT = 60L * 1 * 1000;
     /**
      * 获取数据库uuid
      *

+ 23 - 0
sop-common/src/main/java/com/qmth/sop/common/enums/ErrorEnum.java

@@ -0,0 +1,23 @@
+package com.qmth.sop.common.enums;
+
+/**
+ * @Description: 错误enum
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2022/1/24
+ */
+public enum ErrorEnum {
+
+    SEQUENCE("selectNextVal");
+
+    private String title;
+
+    private ErrorEnum(String title) {
+        this.title = title;
+    }
+
+    public String getTitle() {
+        return title;
+    }
+}

+ 1 - 1
sop-server/src/main/java/com/qmth/sop/server/api/SysController.java

@@ -372,7 +372,7 @@ public class SysController {
     @ApiOperation(value = "查询枚举")
     @RequestMapping(value = "/query_enum", method = RequestMethod.POST)
     @ApiResponses({@ApiResponse(code = 200, message = "返回信息", response = Object.class)})
-    public Result queryEnum(@ApiParam(value = "枚举类型") @RequestParam(required = false) EnumList enumList) throws ClassNotFoundException {
+    public Result queryEnum(@ApiParam(value = "枚举类型", required = true) @RequestParam EnumList enumList) throws ClassNotFoundException {
         Map<Enum, String> map = new LinkedHashMap<>();
         switch (enumList) {
             case QUALITY_PROBLEM_TYPE_ENUM: