Browse Source

1、增加system错误码同步开启

rayson 1 year ago
parent
commit
377e20df9d

+ 3 - 3
citu-framework/citu-common/src/main/java/com/citu/framework/common/exception/util/ServiceExceptionUtil.java

@@ -12,11 +12,11 @@ import java.util.concurrent.ConcurrentMap;
 
 /**
  * {@link ServiceException} 工具类
- *
+ * <p>
  * 目的在于,格式化异常信息提示。
  * 考虑到 String.format 在参数不正确时会报错,因此使用 {} 作为占位符,并使用 {@link #doFormat(int, String, Object...)} 方法来格式化
  * 因为 {@link #MESSAGES} 里面默认是没有异常信息提示的模板的,所以需要使用方自己初始化进去。目前想到的有几种方式:
- *
+ * <p>
  * 1. 异常提示信息,写在枚举类中,例如说,cn.iocoder.oceans.user.api.constants.ErrorCodeEnum 类 + ServiceExceptionConfiguration
  * 2. 异常提示信息,写在 .properties 等等配置文件
  * 3. 异常提示信息,写在 Apollo 等等配置中心中,从而实现可动态刷新
@@ -67,7 +67,7 @@ public class ServiceExceptionUtil {
     /**
      * 创建指定编号的 ServiceException 的异常
      *
-     * @param code 编号
+     * @param code   编号
      * @param params 消息提示的占位符对应的参数
      * @return 异常
      */

+ 2 - 2
citu-framework/citu-spring-boot-starter-web/src/main/java/com/citu/framework/errorcode/config/CituErrorCodeAutoConfiguration.java

@@ -18,7 +18,7 @@ import org.springframework.scheduling.annotation.EnableScheduling;
  * @author 芋道源码
  */
 @AutoConfiguration
-@ConditionalOnProperty(prefix = "citu.error-code", value = "enable", matchIfMissing = true) // 允许使用 citu.error-code.enable=false 禁用访问日志
+@ConditionalOnProperty(prefix = "citu.error-code", value = "enable", matchIfMissing = true) // 允许使用 yudao.error-code.enable=false 禁用访问日志
 @EnableConfigurationProperties(ErrorCodeProperties.class)
 @EnableScheduling // 开启调度任务的功能,因为 ErrorCodeRemoteLoader 通过定时刷新错误码
 public class CituErrorCodeAutoConfiguration {
@@ -36,4 +36,4 @@ public class CituErrorCodeAutoConfiguration {
         return new ErrorCodeLoaderImpl(applicationName, errorCodeApi);
     }
 
-}
+}

+ 1 - 0
citu-module-mp/citu-module-mp-biz/pom.xml

@@ -111,6 +111,7 @@
             <groupId>com.github.binarywang</groupId>
             <artifactId>wx-java-mp-spring-boot-starter</artifactId> <!-- 微信登录(公众号) -->
         </dependency>
+
     </dependencies>
 
     <build>

+ 212 - 0
citu-module-mp/citu-module-mp-biz/src/main/java/com/citu/module/mp/dal/dataobject/BeanToGenerateTable.java

@@ -0,0 +1,212 @@
+package com.citu.module.mp.dal.dataobject;
+
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.List;
+import java.util.Locale;
+
+/**
+ * @author rayson
+ * @description generateTable
+ * @create 2024/5/11 下午3:10
+ **/
+public class BeanToGenerateTable {
+
+
+    public static void main(String[] args) {
+
+        String sql = generateTableMysql("com.citu.module.mp.dal.dataobject.account.MpAccountDO", true);
+        System.out.println(sql);
+
+        String sql2 = generateTableMysql("com.citu.module.mp.dal.dataobject.material.MpMaterialDO", true);
+        System.out.println(sql2);
+
+        String sql3 = generateTableMysql("com.citu.module.mp.dal.dataobject.menu.MpMenuDO", true);
+        System.out.println(sql3);
+
+        String sql4 = generateTableMysql("com.citu.module.mp.dal.dataobject.message.MpAutoReplyDO", true);
+        System.out.println(sql4);
+
+        String sql5 = generateTableMysql("com.citu.module.mp.dal.dataobject.message.MpMessageDO", true);
+        System.out.println(sql5);
+
+        String sql6 = generateTableMysql("com.citu.module.mp.dal.dataobject.tag.MpTagDO", true);
+        System.out.println(sql6);
+
+        String sql7 = generateTableMysql("com.citu.module.mp.dal.dataobject.user.MpUserDO", true);
+        System.out.println(sql7);
+
+    }
+
+    /***
+     * 根据实体类自动生成Mysql建表sql
+     * @param beanName  实体类路径
+     * @param isConvert  是否需要转换驼峰格式
+     * @return
+     */
+    public static String generateTableMysql(String beanName, boolean isConvert) {
+        StringBuilder sqlSb = new StringBuilder();
+
+        if (null != beanName && !"".equals(beanName)) {
+            Object obj = null;
+            try {
+                obj = Class.forName(beanName).newInstance();
+            } catch (InstantiationException e) {
+                e.printStackTrace();
+            } catch (IllegalAccessException e) {
+                e.printStackTrace();
+            } catch (ClassNotFoundException e) {
+                e.printStackTrace();
+            }
+            // 拿到该类
+            Class<?> clz = obj.getClass();
+            // 获取实体类的所有属性,返回Field数组
+            Field[] fields = clz.getDeclaredFields();
+            if (fields != null) {
+
+                //获取实体类的Table注解表名(导入persistence包)
+                TableName tableClass = clz.getAnnotation(TableName.class);
+                String tableName = "";
+                if (tableClass != null) {
+                    tableName = tableClass.value();
+                }
+                sqlSb.append("drop table if exists " + tableName + "; \n");
+
+                sqlSb.append("create table " + tableName + "( \n");
+
+                String idKey = "";
+
+                for (Field field : fields) {
+                    if (!field.isAccessible()) {
+                        field.setAccessible(true);
+                    }
+                    String type = field.getGenericType().toString();
+                    if (type.indexOf(".") == -1 || type.indexOf("java.") != -1) {//过滤实体对象
+                        //截取最后一个.后面的字符
+                        type = type.substring(type.lastIndexOf(".") + 1);
+                        if (!field.getType().equals(List.class)) {// 不匹配list类型
+                            //字段名称
+                            String name = field.getName();
+
+                            Method doSomeMethod = null;
+                            try {
+                                //获取get方法
+                                doSomeMethod = clz.getDeclaredMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1));
+                            } catch (NoSuchMethodException e) {
+                                e.printStackTrace();
+                            }
+                            boolean isExist = true;
+                            //属于表字段
+                            if (isExist) {
+                                StringBuilder convertName = new StringBuilder();
+                                convertName.append(name);
+                                //如果需要转换驼峰格式
+                                if (isConvert) {
+                                    convertName = new StringBuilder();
+                                    for (int i = 0; i < name.length(); i++) {
+                                        //如果是大写前面先加一个_
+                                        if (isUpperCase(name.charAt(i))) {
+                                            convertName.append("_");
+                                        }
+                                        convertName.append(name.charAt(i));
+                                    }
+                                }
+                                name = convertName.toString();
+                                sqlSb.append("    `" + name.toLowerCase() + "` ");
+                                //java 数据类型转换成 Oracle 字段数据类型
+                                sqlSb.append(" " + societyMysql(type));
+                                //判断该字段是否是Id主键(导入persistence包)
+                                if (field.isAnnotationPresent(TableId.class)) {
+                                    TableId id = field.getAnnotation(TableId.class);
+                                    idKey = name.toLowerCase();//id主键字段
+                                    sqlSb.append("  NOT NULL  ");
+                                }
+                                sqlSb.append(" comment ");
+                                //字段属性说明(没有ApiModelProperty包的把这段代码注释掉)
+//                                if(field.isAnnotationPresent(ApiModelProperty.class)){
+//                                    ApiModelProperty explain = field.getAnnotation(ApiModelProperty.class);
+//                                    System.out.println("字段说明:"+explain.value());
+//                                    sqlSb.append(" '"+explain.value()+"',\n" );
+//                                }else{
+                                sqlSb.append(" '',\n");
+//                                }
+
+                                /**
+                                 * 字段属性说明
+                                 * 没有ApiModelProperty包的把上面那段代码注释掉,用这个
+                                 */
+                                /*
+                                sqlSb.append(" '',\n" );
+                                */
+
+                            }
+
+                        }
+                    }
+
+                }
+                sqlSb.append("    `creator` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '创建者',\n" +
+                        "    `create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
+                        "    `updater` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT '' COMMENT '更新者',\n" +
+                        "    `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',\n" +
+                        "    `deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',\n" +
+                        "    `tenant_id` bigint NOT NULL DEFAULT 0 COMMENT '租户编号',");
+                if (null != idKey && !"".equals(idKey)) {
+                    sqlSb.append(" primary key (" + idKey + ") \n");
+                } else {
+                    String lastStr = sqlSb.substring(0, sqlSb.length() - 1);
+                    //如果最后一个字符是逗号结尾
+                    if (lastStr.equals(",")) {
+                        //删除最后一个字符
+                        sqlSb = sqlSb.deleteCharAt(sqlSb.length() - 1);
+                    }
+                }
+                sqlSb.append(" ); \n");
+
+            }
+        }
+
+        return sqlSb.toString();
+    }
+
+    //字母是否是大写
+    public static boolean isUpperCase(char c) {
+        return c >= 65 && c <= 90;
+    }
+
+    public static String societyMysql(String javaType) {
+        String oracleType = "";
+        if (javaType.equals("String")) {
+            oracleType = "varchar(255)";
+        }
+        //不区分大小写
+        else if (javaType.equalsIgnoreCase("BigDecimal")) {
+            oracleType = "decimal(6)";
+        } else if (javaType.equals("Date")) {
+            oracleType = "date";
+        } else if (javaType.equals("LocalDateTime")) {
+            oracleType = "datetime";
+        } else if (javaType.equals("Boolean") || javaType.equals("boolean")) {
+            oracleType = "bit(1)";
+        } else if (javaType.equalsIgnoreCase("Timestamp")) {
+            oracleType = "datetime";
+        } else if (javaType.equals("byte[]")) {
+            oracleType = "blob";
+        } else if (javaType.equals("int")) {
+            oracleType = "tinyint";
+        } else if (javaType.equals("Integer")) {
+            oracleType = "tinyint";
+        } else if (javaType.equals("Long") || javaType.equals("long")) {
+            oracleType = "bigint";
+        } else if (javaType.equalsIgnoreCase("double")) {
+            oracleType = "double";
+        } else if (javaType.equalsIgnoreCase("float")) {
+            oracleType = "float";
+        }
+        return oracleType;
+    }
+
+}

+ 10 - 0
citu-module-system/citu-module-system-biz/src/main/resources/application.yaml

@@ -147,6 +147,16 @@ citu:
     base-package: ${citu.info.base-package}
   captcha:
     enable: true # 验证码的开关,默认为 true;
+  error-code: # 错误码相关配置项
+    enable: true
+    constants-class-list:
+      - com.citu.module.bpm.enums.ErrorCodeConstants
+      - com.citu.module.infra.enums.ErrorCodeConstants
+      - com.citu.module.member.enums.ErrorCodeConstants
+      - com.citu.module.pay.enums.ErrorCodeConstants
+      - com.citu.module.system.enums.ErrorCodeConstants
+      - com.citu.module.mp.enums.ErrorCodeConstants
+      - com.citu.module.menduner.system.enums.ErrorCodeConstants
   tenant: # 多租户相关配置项
     enable: true
     ignore-urls:

+ 1 - 0
menduner/menduner-system-biz/src/main/resources/application.yaml

@@ -114,3 +114,4 @@ citu:
     ignore-tables:
 
 debug: false
+