From aaf13296943844243f097703d0b6d416f3856746 Mon Sep 17 00:00:00 2001
From: 935090232@qq.com <ak473600000>
Date: Sat, 05 Mar 2022 20:26:02 +0800
Subject: [PATCH] fead:枚举改造
---
zq-erp/src/main/java/com/matrix/core/enums/EnumsShowVo.java | 24 +++
zq-erp/src/main/java/com/matrix/system/enums/SmsTypeEnum.java | 53 +++++++
zq-erp/src/main/java/com/matrix/system/enums/SmsPlatformEnum.java | 50 +++++++
zq-erp/src/main/java/com/matrix/core/enums/EnumApiShowAble.java | 24 +++
zq-erp/src/main/java/com/matrix/core/enums/EnumsManager.java | 155 ++++++++++++++++++++++
zq-erp/src/main/java/com/matrix/system/enums/PayMethodEnum.java | 55 +++++++
zq-erp/src/main/java/com/matrix/system/common/actions/CommonDataAction.java | 29 ++++
zq-erp/src/main/resources/config/application.properties | 2
8 files changed, 391 insertions(+), 1 deletions(-)
diff --git a/zq-erp/src/main/java/com/matrix/core/enums/EnumApiShowAble.java b/zq-erp/src/main/java/com/matrix/core/enums/EnumApiShowAble.java
new file mode 100644
index 0000000..dc74981
--- /dev/null
+++ b/zq-erp/src/main/java/com/matrix/core/enums/EnumApiShowAble.java
@@ -0,0 +1,24 @@
+package com.matrix.core.enums;
+
+import java.util.List;
+
+/**
+ * 通过统一获取key,value的能力
+ */
+public interface EnumApiShowAble {
+
+
+ /**
+ * 获取枚举的唯一编码
+ * @return
+ */
+ String getEnumCode();
+
+ /**
+ * 获取枚举对外展示对象列表
+ * @return
+ */
+ List<EnumsShowVo> getEnumsShowVos();
+
+
+}
diff --git a/zq-erp/src/main/java/com/matrix/core/enums/EnumsManager.java b/zq-erp/src/main/java/com/matrix/core/enums/EnumsManager.java
new file mode 100644
index 0000000..96fb9cc
--- /dev/null
+++ b/zq-erp/src/main/java/com/matrix/core/enums/EnumsManager.java
@@ -0,0 +1,155 @@
+package com.matrix.core.enums;
+
+
+import com.matrix.core.tools.LogUtil;
+import org.springframework.boot.ApplicationArguments;
+import org.springframework.boot.ApplicationRunner;
+import org.springframework.core.Ordered;
+import org.springframework.core.annotation.Order;
+import org.springframework.stereotype.Component;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.JarURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.*;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+
+
+@Component
+@Order(Ordered.HIGHEST_PRECEDENCE)
+public class EnumsManager implements ApplicationRunner {
+ private final List<String> CLASS_NAME = new ArrayList<>();
+ private ClassLoader classLoader;
+ private static final String SUFFIX = ".class";
+
+ private Map<String, List<EnumsShowVo>> showAbleMap = new HashMap<>();
+
+ @Override
+ public void run(ApplicationArguments args) throws Exception {
+ LogUtil.info("扫描自定义枚举------------------");
+ initEnum("com.matrix");
+ LogUtil.info("扫描自定义枚举结束===============");
+ }
+
+ public List<EnumsShowVo> getShowEnum(String emumCode) {
+ return showAbleMap.get(emumCode);
+ }
+
+
+ private void initEnum(String... packages) {
+ classLoader = Thread.currentThread().getContextClassLoader();
+
+ for (String basePackage : packages) {
+ Enumeration<URL> resources = null;
+ try {
+ resources = classLoader.getResources(basePackage.replaceAll("\\.", "/"));
+ } catch (IOException e) {
+ return;
+ }
+
+ // 扫描当前工程和jar包
+ while (resources.hasMoreElements()) {
+ URL url = resources.nextElement();
+ if ("file".equals(url.getProtocol())) {
+ doFileScan(url.getPath());
+ } else if ("jar".equals(url.getProtocol())) {
+ doJarScan(basePackage, url);
+ }
+ }
+ }
+
+ // 初始化枚举数据
+ enumValue();
+ }
+
+ /**
+ * 扫描当前工程对应包下的所有类
+ *
+ * @param path
+ */
+ private void doFileScan(String path) {
+ String rootPath = classLoader.getResource("").getPath();
+ File pathFile = new File(path);
+
+ File[] files = pathFile.listFiles();
+ if (files == null || files.length == 0) {
+ return;
+ }
+
+ for (File file : files) {
+ if (file.isDirectory()) {
+ String nextPath = path + "/" + file.getName();
+ doFileScan(nextPath);
+ } else if (file.getName().endsWith(SUFFIX)) {
+ if (!path.contains(rootPath)) {
+ return;
+ }
+ String subStr = path.substring(rootPath.length());
+ String className = (subStr + "/" + file.getName().replaceAll(SUFFIX, "")).replaceAll("/", "\\.");
+ CLASS_NAME.add(className);
+ }
+ }
+ }
+
+ /**
+ * 扫描jar包下对应包下所有类
+ *
+ * @param basePackage
+ * @param baseURL
+ */
+ public void doJarScan(String basePackage, URL baseURL) {
+ basePackage = basePackage.replaceAll("\\.", "/");
+ JarFile jarFile;
+ try {
+ URLConnection urlConnection = baseURL.openConnection();
+ JarURLConnection jarUrl = (JarURLConnection) urlConnection;
+ jarFile = jarUrl.getJarFile();
+ } catch (IOException e) {
+ throw new RuntimeException("未找到资源");
+ }
+
+ Enumeration<JarEntry> entries = jarFile.entries();
+ while (entries.hasMoreElements()) {
+ JarEntry entry = entries.nextElement();
+
+ String name = entry.getName();
+ if (name.startsWith(basePackage)) {
+ if (name.endsWith(SUFFIX)) {
+ String className = name.replaceAll(SUFFIX, "").replaceAll("/", "\\.");
+ CLASS_NAME.add(className);
+ }
+ }
+ }
+ }
+
+ public void enumValue() {
+ for (String className : CLASS_NAME) {
+ Class<?> clazz = null;
+ try {
+ clazz = classLoader.loadClass(className);
+ } catch (ClassNotFoundException e) {
+ continue;
+ }
+
+ // 判断类是否为枚举类型
+ if (!clazz.isEnum()) {
+ continue;
+ }
+
+ // 判断ApiShowAble是否为类的父类
+ if (!EnumApiShowAble.class.isAssignableFrom(clazz)) {
+ continue;
+ }
+
+ Object[] constants = clazz.getEnumConstants();
+ EnumApiShowAble enumApiShowAble = (EnumApiShowAble) constants[0];
+
+ showAbleMap.put(enumApiShowAble.getEnumCode(), enumApiShowAble.getEnumsShowVos());
+ }
+ }
+
+
+}
diff --git a/zq-erp/src/main/java/com/matrix/core/enums/EnumsShowVo.java b/zq-erp/src/main/java/com/matrix/core/enums/EnumsShowVo.java
new file mode 100644
index 0000000..0a9fca5
--- /dev/null
+++ b/zq-erp/src/main/java/com/matrix/core/enums/EnumsShowVo.java
@@ -0,0 +1,24 @@
+package com.matrix.core.enums;
+
+import lombok.Builder;
+import lombok.Data;
+
+/**
+ * 枚举值展示对象
+ */
+@Builder
+@Data
+public class EnumsShowVo {
+
+ /**
+ * 展示名称
+ */
+ private String displayName;
+
+ /**
+ * 提交值
+ */
+ private Integer value;
+
+
+}
diff --git a/zq-erp/src/main/java/com/matrix/system/common/actions/CommonDataAction.java b/zq-erp/src/main/java/com/matrix/system/common/actions/CommonDataAction.java
new file mode 100644
index 0000000..bacb2b6
--- /dev/null
+++ b/zq-erp/src/main/java/com/matrix/system/common/actions/CommonDataAction.java
@@ -0,0 +1,29 @@
+package com.matrix.system.common.actions;
+
+import com.matrix.core.enums.EnumsManager;
+import com.matrix.core.pojo.AjaxResult;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @author 姜友瑶
+ * @description 管理员总action
+ * @email 935090232@qq.com
+ * @date 2016-06-26
+ */
+@RestController
+@RequestMapping(value = "common/data")
+public class CommonDataAction {
+
+ @Autowired
+ private EnumsManager enumsManager;
+
+ @RequestMapping("/getEnums/{enumCode}")
+ public AjaxResult getEnums(@PathVariable String enumCode) throws ClassNotFoundException {
+ return AjaxResult.buildSuccessInstance(enumsManager.getShowEnum(enumCode));
+ }
+
+
+}
\ No newline at end of file
diff --git a/zq-erp/src/main/java/com/matrix/system/enums/PayMethodEnum.java b/zq-erp/src/main/java/com/matrix/system/enums/PayMethodEnum.java
new file mode 100644
index 0000000..41529e8
--- /dev/null
+++ b/zq-erp/src/main/java/com/matrix/system/enums/PayMethodEnum.java
@@ -0,0 +1,55 @@
+package com.matrix.system.enums;
+
+import com.google.common.collect.Lists;
+import com.matrix.core.enums.EnumApiShowAble;
+import com.matrix.core.enums.EnumsShowVo;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * 支付方式
+ * @author jyy
+ */
+public enum PayMethodEnum implements EnumApiShowAble {
+
+ CASH(1, "现金"),
+ WECHAT(2, "微信"),
+ ALIPAY(3, "支付宝"),
+ BANK_CARD(4, "银行卡"),
+ BANK_MT(5, "美团"),
+ VIP_CARD(6, "会员卡");
+
+ private Integer value;
+
+ private String displayName;
+
+ PayMethodEnum(Integer value, String displayName) {
+ this.value = value;
+ this.displayName = displayName;
+ }
+
+ @Override
+ public String getEnumCode() {
+ return "payMethod";
+ }
+
+ @Override
+ public List<EnumsShowVo> getEnumsShowVos() {
+ return Lists.newArrayList(values()).stream().map(item ->
+ EnumsShowVo.builder()
+ .displayName(item.getDisplayName())
+ .value(item.value)
+ .build()
+ ).collect(Collectors.toList());
+ }
+
+ public Integer getValue() {
+ return value;
+ }
+
+ public String getDisplayName() {
+ return displayName;
+ }
+
+}
diff --git a/zq-erp/src/main/java/com/matrix/system/enums/SmsPlatformEnum.java b/zq-erp/src/main/java/com/matrix/system/enums/SmsPlatformEnum.java
new file mode 100644
index 0000000..c936723
--- /dev/null
+++ b/zq-erp/src/main/java/com/matrix/system/enums/SmsPlatformEnum.java
@@ -0,0 +1,50 @@
+package com.matrix.system.enums;
+
+import com.google.common.collect.Lists;
+import com.matrix.core.enums.EnumApiShowAble;
+import com.matrix.core.enums.EnumsShowVo;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+
+public enum SmsPlatformEnum implements EnumApiShowAble {
+
+
+ ALIBABA(1 ,"阿里云短信"),
+ HUYIWUXIAN(2, "互亿无线");
+
+ private Integer value;
+
+ private String displayName;
+
+ SmsPlatformEnum(Integer value, String displayName) {
+ this.value = value;
+ this.displayName = displayName;
+ }
+
+
+
+ @Override
+ public String getEnumCode() {
+ return "smsPlatform";
+ }
+
+ @Override
+ public List<EnumsShowVo> getEnumsShowVos() {
+ return Lists.newArrayList(values()).stream().map(item ->
+ EnumsShowVo.builder()
+ .displayName(item.getDisplayName())
+ .value(item.value)
+ .build()
+ ).collect(Collectors.toList());
+ }
+
+ public Integer getValue() {
+ return value;
+ }
+
+ public String getDisplayName() {
+ return displayName;
+ }
+}
diff --git a/zq-erp/src/main/java/com/matrix/system/enums/SmsTypeEnum.java b/zq-erp/src/main/java/com/matrix/system/enums/SmsTypeEnum.java
new file mode 100644
index 0000000..0b84c69
--- /dev/null
+++ b/zq-erp/src/main/java/com/matrix/system/enums/SmsTypeEnum.java
@@ -0,0 +1,53 @@
+package com.matrix.system.enums;
+
+import com.google.common.collect.Lists;
+import com.matrix.core.enums.EnumApiShowAble;
+import com.matrix.core.enums.EnumsShowVo;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * 短信类型
+ */
+public enum SmsTypeEnum implements EnumApiShowAble {
+
+
+ VERIFY_CODE(1, "验证码"),
+ SMS_NOTICE(2, "短信通知"),
+ PROMOTE(3, "推广短信");
+
+ private Integer value;
+
+ private String displayName;
+
+ SmsTypeEnum(Integer value, String displayName) {
+ this.value = value;
+ this.displayName = displayName;
+ }
+
+
+
+ @Override
+ public String getEnumCode() {
+ return "smsType";
+ }
+
+ @Override
+ public List<EnumsShowVo> getEnumsShowVos() {
+ return Lists.newArrayList(values()).stream().map(item ->
+ EnumsShowVo.builder()
+ .displayName(item.getDisplayName())
+ .value(item.value)
+ .build()
+ ).collect(Collectors.toList());
+ }
+
+ public Integer getValue() {
+ return value;
+ }
+
+ public String getDisplayName() {
+ return displayName;
+ }
+}
diff --git a/zq-erp/src/main/resources/config/application.properties b/zq-erp/src/main/resources/config/application.properties
index 7ab9f40..ddb6608 100644
--- a/zq-erp/src/main/resources/config/application.properties
+++ b/zq-erp/src/main/resources/config/application.properties
@@ -1,5 +1,5 @@
-spring.profiles.active=meidu
+spring.profiles.active=test
evn=dev
server.port=8080
--
Gitblit v1.9.1