From 2cc0729bdc4cbc8da62e5c2e0c6436a8848e90c5 Mon Sep 17 00:00:00 2001 From: wanggeng <450292408@qq.com> Date: Thu, 22 Sep 2022 19:10:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/api/env/EnvController.java | 51 ++++ .../route/env/EnvRouteController.java | 28 ++ .../java/com/cm/common/dao/env/IEnvDao.java | 65 +++++ .../com/cm/common/manager/env/EnvManager.java | 83 ++++++ .../src/main/java/com/cm/common/pojo/Env.java | 83 ++++++ .../com/cm/common/pojo/dtos/env/EnvDTO.java | 46 ++++ .../com/cm/common/pojo/vos/env/EnvListVO.java | 38 +++ .../com/cm/common/pojo/vos/env/EnvVO.java | 59 ++++ .../cm/common/service/env/IEnvService.java | 32 +++ .../service/env/impl/EnvServiceImpl.java | 52 ++++ .../com/cm/common/start/CommonStartUp.java | 35 +++ .../java/com/cm/common/utils/RegexUtil.java | 14 + .../java/com/cm/common/utils/WStringUtil.java | 107 ++++++++ .../com/cm/common/utils/point/PointUtil.java | 141 +++++++++- .../mybatis/mapper/env/env-mapper.xml | 72 +++++ .../main/resources/templates/env/update.html | 252 ++++++++++++++++++ 16 files changed, 1157 insertions(+), 1 deletion(-) create mode 100644 cloud-common/src/main/java/com/cm/common/controller/api/env/EnvController.java create mode 100644 cloud-common/src/main/java/com/cm/common/controller/route/env/EnvRouteController.java create mode 100644 cloud-common/src/main/java/com/cm/common/dao/env/IEnvDao.java create mode 100644 cloud-common/src/main/java/com/cm/common/manager/env/EnvManager.java create mode 100644 cloud-common/src/main/java/com/cm/common/pojo/Env.java create mode 100644 cloud-common/src/main/java/com/cm/common/pojo/dtos/env/EnvDTO.java create mode 100644 cloud-common/src/main/java/com/cm/common/pojo/vos/env/EnvListVO.java create mode 100644 cloud-common/src/main/java/com/cm/common/pojo/vos/env/EnvVO.java create mode 100644 cloud-common/src/main/java/com/cm/common/service/env/IEnvService.java create mode 100644 cloud-common/src/main/java/com/cm/common/service/env/impl/EnvServiceImpl.java create mode 100644 cloud-common/src/main/java/com/cm/common/start/CommonStartUp.java create mode 100644 cloud-common/src/main/resources/mybatis/mapper/env/env-mapper.xml create mode 100644 cloud-common/src/main/resources/templates/env/update.html diff --git a/cloud-common/src/main/java/com/cm/common/controller/api/env/EnvController.java b/cloud-common/src/main/java/com/cm/common/controller/api/env/EnvController.java new file mode 100644 index 0000000..5366cff --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/controller/api/env/EnvController.java @@ -0,0 +1,51 @@ +package com.cm.common.controller.api.env; + +import com.cm.common.annotation.CheckRequestBodyAnnotation; +import com.cm.common.base.AbstractController; +import com.cm.common.constants.ISystemConstant; +import com.cm.common.pojo.dtos.env.EnvDTO; +import com.cm.common.pojo.vos.env.EnvListVO; +import com.cm.common.result.ErrorResult; +import com.cm.common.result.SuccessResult; +import com.cm.common.service.env.IEnvService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * @ClassName: EnvController + * @Description: 环境变量 + * @Author: wanggeng + * @Date: 2022/5/16 10:50 + * @Version: 1.0 + */ +@Api(tags = ISystemConstant.API_TAGS_SYSTEM_PREFIX + "环境变量") +@RestController +@RequestMapping(ISystemConstant.API_PREFIX + "/env") +public class EnvController extends AbstractController { + + @Autowired + private IEnvService envService; + + @ApiOperation(value = "更新", notes = "更新") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @PutMapping("update") + @CheckRequestBodyAnnotation + public SuccessResult update(@RequestBody EnvListVO envListVO) { + envService.update(envListVO); + return new SuccessResult(); + } + + @ApiOperation(value = "获取配置", notes = "获取配置接口") + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @GetMapping("list") + public List list() { + return envService.list(); + } + +} diff --git a/cloud-common/src/main/java/com/cm/common/controller/route/env/EnvRouteController.java b/cloud-common/src/main/java/com/cm/common/controller/route/env/EnvRouteController.java new file mode 100644 index 0000000..1c11fee --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/controller/route/env/EnvRouteController.java @@ -0,0 +1,28 @@ +package com.cm.common.controller.route.env; + +import com.cm.common.constants.ISystemConstant; +import io.swagger.annotations.Api; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.ModelAndView; + +/** + * @ClassName: EnvController + * @Description: 环境变量 + * @Author: wanggeng + * @Date: 2022/5/16 10:50 + * @Version: 1.0 + */ +@Api(tags = ISystemConstant.ROUTE_TAGS_PREFIX + "环境变量") +@RestController +@RequestMapping(ISystemConstant.ROUTE_PREFIX + "/env") +public class EnvRouteController { + + @GetMapping("update") + public ModelAndView update() { + ModelAndView modelAndView = new ModelAndView("env/update"); + return modelAndView; + } + +} diff --git a/cloud-common/src/main/java/com/cm/common/dao/env/IEnvDao.java b/cloud-common/src/main/java/com/cm/common/dao/env/IEnvDao.java new file mode 100644 index 0000000..98e2c56 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/dao/env/IEnvDao.java @@ -0,0 +1,65 @@ +package com.cm.common.dao.env; + +import com.cm.common.exception.RemoveException; +import com.cm.common.exception.SaveException; +import com.cm.common.exception.SearchException; +import com.cm.common.exception.UpdateException; +import com.cm.common.pojo.dtos.env.EnvDTO; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Map; + +/** + * @ClassName: IEnvDao + * @Description: 环境变量 + * @Author: wanggeng + * @Date: 2022/5/16 10:24 + * @Version: 1.0 + */ +@Repository +public interface IEnvDao { + + void createTable(); + + /** + * 新增 + * + * @param params + * @throws SaveException + */ + void save(Map params) throws SaveException; + + /** + * 修改 + * + * @param params + * @throws UpdateException + */ + void update(Map params) throws UpdateException; + + /** + * 删除 + * + * @throws RemoveException + */ + void delete() throws RemoveException; + + /** + * 列表 + * + * @return + * @throws SearchException + */ + List list() throws SearchException; + + /** + * 详情 + * + * @param envKey + * @return + * @throws SearchException + */ + EnvDTO get(String envKey) throws SearchException; + +} diff --git a/cloud-common/src/main/java/com/cm/common/manager/env/EnvManager.java b/cloud-common/src/main/java/com/cm/common/manager/env/EnvManager.java new file mode 100644 index 0000000..146d6cd --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/manager/env/EnvManager.java @@ -0,0 +1,83 @@ +package com.cm.common.manager.env; + +import com.cm.common.dao.env.IEnvDao; +import com.cm.common.pojo.Env; +import com.cm.common.pojo.dtos.env.EnvDTO; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @ClassName: EnvManager + * @Description: 环境变量管理 + * @Author: wanggeng + * @Date: 2022/5/16 10:47 + * @Version: 1.0 + */ +public class EnvManager { + + private static final Logger LOG = LoggerFactory.getLogger(EnvManager.class); + private static EnvManager envManager = EnvManagerBuilder.envManager; + private static Pattern ENV_VARIABLE = Pattern.compile("%[a-zA-Z\\d\\_\\-]+%"); + private IEnvDao envDao; + private Env env = new Env(); + + private EnvManager() {} + + public static EnvManager getInstance() { + return envManager; + } + + /** + * 刷新环境变量 + */ + public void refreshEnv() { + env.clear(); + List envDTOS = envDao.list(); + envDTOS.forEach(envDTO -> { + env.put(envDTO.getEnvKey(), envDTO.getEnvValue()); + }); + } + + /** + * 获得环境变量 + * + * @return + */ + public Env getEnv() { + return env; + } + + public boolean isKeyExist(String key) { + return env.containsKey(key); + } + + public String getValue(String key) { + String value = env.get(key); + value = value == null ? "" : value; + // 替换变量 + Matcher matcher = ENV_VARIABLE.matcher(value); + while (matcher.find()) { + String variable = matcher.group(); + String variableValue = env.get(variable.substring(1, variable.length() - 1)); + if (StringUtils.isBlank(variableValue)) { + continue; + } + value = value.replaceFirst(variable, variableValue); + } + return value; + } + + public void setEnvDao(IEnvDao envDao) { + this.envDao = envDao; + } + + private static class EnvManagerBuilder { + public static final EnvManager envManager = new EnvManager(); + } + +} diff --git a/cloud-common/src/main/java/com/cm/common/pojo/Env.java b/cloud-common/src/main/java/com/cm/common/pojo/Env.java new file mode 100644 index 0000000..1d6005f --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/pojo/Env.java @@ -0,0 +1,83 @@ +package com.cm.common.pojo; + +import java.util.Collection; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @ClassName: Env + * @Description: 环境变量 + * @Author: wanggeng + * @Date: 2022/5/16 14:46 + * @Version: 1.0 + */ +public class Env implements Map { + + Map envMap = new ConcurrentHashMap<>(); + + @Override + public int size() { + return envMap.size(); + } + + @Override + public boolean isEmpty() { + return envMap.isEmpty(); + } + + @Override + public boolean containsKey(Object key) { + return envMap.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return envMap.containsValue(value); + } + + @Override + public String get(Object key) { + return envMap.get(key); + } + + @Override + public String put(String key, String value) { + return envMap.put(key, value); + } + + @Override + public String remove(Object key) { + return envMap.remove(key); + } + + @Override + public void putAll(Map m) { + envMap.putAll(m); + } + + @Override + public void clear() { + envMap.clear(); + } + + @Override + public Set keySet() { + return envMap.keySet(); + } + + @Override + public Collection values() { + return envMap.values(); + } + + @Override + public Set> entrySet() { + return envMap.entrySet(); + } + + @Override + public String toString() { + return envMap.toString(); + } +} diff --git a/cloud-common/src/main/java/com/cm/common/pojo/dtos/env/EnvDTO.java b/cloud-common/src/main/java/com/cm/common/pojo/dtos/env/EnvDTO.java new file mode 100644 index 0000000..9c1510b --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/pojo/dtos/env/EnvDTO.java @@ -0,0 +1,46 @@ +package com.cm.common.pojo.dtos.env; + +import io.swagger.annotations.ApiModelProperty; + +import java.io.Serializable; + +/** + * @ClassName: EnvDTO + * @Description: 环境变量 + * @Author: wanggeng + * @Date: 2022/5/16 10:36 + * @Version: 1.0 + */ +public class EnvDTO implements Serializable { + + @ApiModelProperty(name = "envKey", value = "环境变量") + private String envKey; + @ApiModelProperty(name = "envExplain", value = "环境变量说明") + private String envExplain; + @ApiModelProperty(name = "envValue", value = "环境变量值") + private String envValue; + + public String getEnvKey() { + return envKey == null ? "" : envKey.trim(); + } + + public void setEnvKey(String envKey) { + this.envKey = envKey; + } + + public String getEnvExplain() { + return envExplain == null ? "" : envExplain.trim(); + } + + public void setEnvExplain(String envExplain) { + this.envExplain = envExplain; + } + + public String getEnvValue() { + return envValue == null ? "" : envValue.trim(); + } + + public void setEnvValue(String envValue) { + this.envValue = envValue; + } +} diff --git a/cloud-common/src/main/java/com/cm/common/pojo/vos/env/EnvListVO.java b/cloud-common/src/main/java/com/cm/common/pojo/vos/env/EnvListVO.java new file mode 100644 index 0000000..741db8e --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/pojo/vos/env/EnvListVO.java @@ -0,0 +1,38 @@ +package com.cm.common.pojo.vos.env; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +import java.util.ArrayList; +import java.util.List; + +/** + * @ClassName: EnvPO + * @Description: 环境变量 + * @Author: wanggeng + * @Date: 2022/5/16 10:36 + * @Version: 1.0 + */ +@ApiModel +public class EnvListVO { + + @ApiModelProperty(name = "envs", value = "环境变量列表") + private List envs; + + public List getEnvs() { + return envs == null ? new ArrayList() : envs; + } + + public void setEnvs(List envs) { + this.envs = envs; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("{"); + sb.append("\"envs\":") + .append(envs); + sb.append('}'); + return sb.toString(); + } +} diff --git a/cloud-common/src/main/java/com/cm/common/pojo/vos/env/EnvVO.java b/cloud-common/src/main/java/com/cm/common/pojo/vos/env/EnvVO.java new file mode 100644 index 0000000..0e10e62 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/pojo/vos/env/EnvVO.java @@ -0,0 +1,59 @@ +package com.cm.common.pojo.vos.env; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; + +/** + * @ClassName: EnvPO + * @Description: 环境变量 + * @Author: wanggeng + * @Date: 2022/5/16 10:36 + * @Version: 1.0 + */ +@ApiModel +public class EnvVO { + + @ApiModelProperty(name = "envKey", value = "环境变量") + private String envKey; + @ApiModelProperty(name = "envExplain", value = "环境变量说明") + private String envExplain; + @ApiModelProperty(name = "envValue", value = "环境变量值") + private String envValue; + + public String getEnvKey() { + return envKey == null ? "" : envKey.trim(); + } + + public void setEnvKey(String envKey) { + this.envKey = envKey; + } + + public String getEnvExplain() { + return envExplain == null ? "" : envExplain.trim(); + } + + public void setEnvExplain(String envExplain) { + this.envExplain = envExplain; + } + + public String getEnvValue() { + return envValue == null ? "" : envValue.trim(); + } + + public void setEnvValue(String envValue) { + this.envValue = envValue; + } + + @Override + public String toString() { + final StringBuilder sb = new StringBuilder("{"); + sb.append("\"envKey\":\"") + .append(envKey).append('\"'); + sb.append(",\"envExplain\":\"") + .append(envExplain).append('\"'); + sb.append(",\"envValue\":\"") + .append(envValue).append('\"'); + sb.append('}'); + return sb.toString(); + } +} diff --git a/cloud-common/src/main/java/com/cm/common/service/env/IEnvService.java b/cloud-common/src/main/java/com/cm/common/service/env/IEnvService.java new file mode 100644 index 0000000..d31b3b8 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/service/env/IEnvService.java @@ -0,0 +1,32 @@ +package com.cm.common.service.env; + + +import com.cm.common.pojo.dtos.env.EnvDTO; +import com.cm.common.pojo.vos.env.EnvListVO; + +import java.util.List; + +/** + * @ClassName: IEnvService + * @Description: 环境变量 + * @Author: wanggeng + * @Date: 2022/5/16 10:48 + * @Version: 1.0 + */ +public interface IEnvService { + + /** + * 更新 + * + * @param envMap + */ + void update(EnvListVO envListVO); + + /** + * 详情 + * + * @return + */ + List list(); + +} diff --git a/cloud-common/src/main/java/com/cm/common/service/env/impl/EnvServiceImpl.java b/cloud-common/src/main/java/com/cm/common/service/env/impl/EnvServiceImpl.java new file mode 100644 index 0000000..7105e2e --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/service/env/impl/EnvServiceImpl.java @@ -0,0 +1,52 @@ +package com.cm.common.service.env.impl; + +import com.cm.common.base.AbstractService; +import com.cm.common.dao.env.IEnvDao; +import com.cm.common.manager.env.EnvManager; +import com.cm.common.pojo.Env; +import com.cm.common.pojo.dtos.env.EnvDTO; +import com.cm.common.pojo.vos.env.EnvListVO; +import com.cm.common.pojo.vos.env.EnvVO; +import com.cm.common.service.env.IEnvService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.Map; + +/** + * @ClassName: EnvServiceImpl + * @Description: 环境变量 + * @Author: wanggeng + * @Date: 2022/5/16 10:48 + * @Version: 1.0 + */ +@Service +public class EnvServiceImpl extends AbstractService implements IEnvService { + + @Autowired + private IEnvDao envDao; + + @Override + public void update(EnvListVO envListVO) { + // 清空 + envDao.delete(); + // 新增 + Map params = getHashMap(5); + for (EnvVO envVO : envListVO.getEnvs()) { + params.put("envKey", envVO.getEnvKey()); + params.put("envExplain", envVO.getEnvExplain()); + params.put("envValue", envVO.getEnvValue()); + envDao.save(params); + } + // 刷新缓存 + EnvManager.getInstance().refreshEnv(); + } + + @Override + public List list() { + Env env = EnvManager.getInstance().getEnv(); + System.out.println(env.toString()); + return envDao.list(); + } +} diff --git a/cloud-common/src/main/java/com/cm/common/start/CommonStartUp.java b/cloud-common/src/main/java/com/cm/common/start/CommonStartUp.java new file mode 100644 index 0000000..62797d9 --- /dev/null +++ b/cloud-common/src/main/java/com/cm/common/start/CommonStartUp.java @@ -0,0 +1,35 @@ +package com.cm.common.start; + +import com.cm.common.dao.env.IEnvDao; +import com.cm.common.manager.env.EnvManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +/** + * @ClassName: CommonStartUp + * @Description: 启动 + * @Author: wanggeng + * @Date: 2022/9/22 17:32 + * @Version: 1.0 + */ +@Component +public class CommonStartUp implements ApplicationRunner { + + private static final Logger LOG = LoggerFactory.getLogger(CommonStartUp.class); + + @Autowired + private IEnvDao envDao; + + @Override + public void run(ApplicationArguments args) throws Exception { + LOG.debug(">>>>>> 初始化环境变量"); + envDao.createTable(); + EnvManager envManager = EnvManager.getInstance(); + envManager.setEnvDao(envDao); + envManager.refreshEnv(); + } +} diff --git a/cloud-common/src/main/java/com/cm/common/utils/RegexUtil.java b/cloud-common/src/main/java/com/cm/common/utils/RegexUtil.java index e1ce160..2828986 100644 --- a/cloud-common/src/main/java/com/cm/common/utils/RegexUtil.java +++ b/cloud-common/src/main/java/com/cm/common/utils/RegexUtil.java @@ -98,6 +98,10 @@ public class RegexUtil { * 强密码 */ private static final Pattern PASSWORD_STRONG = Pattern.compile(String.format("(?=.*[A-Za-z])(?=.*[0-9])(?=.*[%s]).{%d,%d}", SPECIAL_CHARACTERS, PASSWORD_LENGTH_MIN, PASSWORD_LENGTH_MAX)); + /** + * 字母和数字 + */ + private static final Pattern PATTERN_LETTER_OR_NUMBER = Pattern.compile("[a-zA-Z0-9]+"); /** * 判断弱密码强度 @@ -289,4 +293,14 @@ public class RegexUtil { return customPattern.matcher(input).matches(); } + /** + * 判断是字母或数字 + * + * @param input + * @return + */ + public static boolean isLetterOrNumber(String input) { + return PATTERN_LETTER_OR_NUMBER.matcher(input).matches(); + } + } diff --git a/cloud-common/src/main/java/com/cm/common/utils/WStringUtil.java b/cloud-common/src/main/java/com/cm/common/utils/WStringUtil.java index 70e0e79..2f0aad6 100644 --- a/cloud-common/src/main/java/com/cm/common/utils/WStringUtil.java +++ b/cloud-common/src/main/java/com/cm/common/utils/WStringUtil.java @@ -7,7 +7,10 @@ import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; +import org.apache.commons.lang3.StringUtils; +import java.util.List; +import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -282,4 +285,108 @@ public class WStringUtil { return strBuf.toString(); } + /** + * 从字符串中随机取出新的字符串 + * + * @param input 输入字符串 + * @param outLength 新字符串长度 + * @return + */ + public static String randomSubStr(String input, int outLength) { + if (input == null || input.isEmpty()) { + return null; + } + if (outLength >= input.length()) { + outLength = input.length(); + } + if (outLength <= 0) { + outLength = 6; + } + StringBuilder subStrSB = new StringBuilder(); + Random random = new Random(); + for (int i = 0; i < outLength; i++) { + subStrSB.append(input.charAt(random.nextInt(input.length()))); + } + return subStrSB.toString(); + } + + /** + * string对象转数组 + * + * @param strObj stirng 对象 + * @param separator 分隔符 + * @return 字符串数组 + */ + public static String[] strObjToArray(Object strObj, String separator) { + if (strObj == null || separator == null) { + throw new RuntimeException("参数不能为空!"); + } + if (!(strObj instanceof String)) { + throw new RuntimeException("第一个参数只能是String类型"); + } + return strObj.toString().split(separator); + } + + /** + * 字符串列表转字符串 + * + * @param strings 字符串列表 + * @param separator 分隔符 + * @return + */ + public static String listToStr(List strings, String separator) { + if (strings == null || strings.isEmpty()) { + return null; + } + StringBuilder sb = new StringBuilder(); + for (String str : strings) { + if (sb.length() > 0) { + sb.append(separator); + } + sb.append(str); + } + return sb.toString(); + } + + /** + * 倒序截断重复字符 + * + * @return + */ + public static String cutContinuityRepeatCharDesc(String str, char repeatChar) { + if (StringUtils.isBlank(str)) { + return str; + } + int unRepeatCharIndex = 0; + for (int i = str.length() - 1; i >= 0; i--) { + if (str.charAt(i) != repeatChar) { + unRepeatCharIndex = i; + break; + } + } + return str.substring(0, unRepeatCharIndex + 1); + } + + /** + * 倒叙截取重复字段 + * + * @param str + * @param repeatChar + * @param minRepeatCount + * @return + */ + public static String cutContinuityRepeatCharDesc(String str, char repeatChar, int minRepeatCount) { + int repeatCount = 0; + for (int i = str.length() - 1; i >= 0; i--) { + if (str.charAt(i) != repeatChar) { + break; + } + repeatCount++; + } + if (repeatCount < minRepeatCount) { + return str; + } + return cutContinuityRepeatCharDesc(str, repeatChar); + } + } diff --git a/cloud-common/src/main/java/com/cm/common/utils/point/PointUtil.java b/cloud-common/src/main/java/com/cm/common/utils/point/PointUtil.java index 6ee6418..37fb49d 100644 --- a/cloud-common/src/main/java/com/cm/common/utils/point/PointUtil.java +++ b/cloud-common/src/main/java/com/cm/common/utils/point/PointUtil.java @@ -1,6 +1,5 @@ package com.cm.common.utils.point; -import java.awt.*; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; @@ -163,6 +162,146 @@ public class PointUtil { return new BigDecimal(distance).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); } + /** + * 计算面积 + * + * @param points + * @return + */ + public static double getSqua(List points) { + double area = 0; + int size = points.size(); + if (size > 2) { + double LowX = 0.0; + double LowY = 0.0; + double MiddleX = 0.0; + double MiddleY = 0.0; + double HighX = 0.0; + double HighY = 0.0; + + double AM = 0.0; + double BM = 0.0; + double CM = 0.0; + + double AL = 0.0; + double BL = 0.0; + double CL = 0.0; + + double AH = 0.0; + double BH = 0.0; + double CH = 0.0; + + double CoefficientL = 0.0; + double CoefficientH = 0.0; + + double ALtangent = 0.0; + double BLtangent = 0.0; + double CLtangent = 0.0; + + double AHtangent = 0.0; + double BHtangent = 0.0; + double CHtangent = 0.0; + + double ANormalLine = 0.0; + double BNormalLine = 0.0; + double CNormalLine = 0.0; + + double OrientationValue = 0.0; + + double AngleCos = 0.0; + + double Sum1 = 0.0; + double Sum2 = 0.0; + double Count2 = 0; + double Count1 = 0; + + double Sum = 0.0; + double Radius = 6378000; + + for (int i = 0; i < size; i++) { + if (i == 0) { + LowX = points.get(size - 1).getX() * Math.PI / 180; + LowY = points.get(size - 1).getY() * Math.PI / 180; + MiddleX = points.get(0).getX() * Math.PI / 180; + MiddleY = points.get(0).getY() * Math.PI / 180; + HighX = points.get(1).getX() * Math.PI / 180; + HighY = points.get(1).getY() * Math.PI / 180; + } else if (i == size - 1) { + LowX = points.get(size - 2).getX() * Math.PI / 180; + LowY = points.get(size - 2).getY() * Math.PI / 180; + MiddleX = points.get(size - 1).getX() * Math.PI / 180; + MiddleY = points.get(size - 1).getY() * Math.PI / 180; + HighX = points.get(0).getX() * Math.PI / 180; + HighY = points.get(0).getY() * Math.PI / 180; + } else { + LowX = points.get(i - 1).getX() * Math.PI / 180; + LowY = points.get(i - 1).getY() * Math.PI / 180; + MiddleX = points.get(i).getX() * Math.PI / 180; + MiddleY = points.get(i).getY() * Math.PI / 180; + HighX = points.get(i + 1).getX() * Math.PI / 180; + HighY = points.get(i + 1).getY() * Math.PI / 180; + } + + AM = Math.cos(MiddleY) * Math.cos(MiddleX); + BM = Math.cos(MiddleY) * Math.sin(MiddleX); + CM = Math.sin(MiddleY); + AL = Math.cos(LowY) * Math.cos(LowX); + BL = Math.cos(LowY) * Math.sin(LowX); + CL = Math.sin(LowY); + AH = Math.cos(HighY) * Math.cos(HighX); + BH = Math.cos(HighY) * Math.sin(HighX); + CH = Math.sin(HighY); + + CoefficientL = (AM * AM + BM * BM + CM * CM) / (AM * AL + BM * BL + CM * CL); + CoefficientH = (AM * AM + BM * BM + CM * CM) / (AM * AH + BM * BH + CM * CH); + + ALtangent = CoefficientL * AL - AM; + BLtangent = CoefficientL * BL - BM; + CLtangent = CoefficientL * CL - CM; + AHtangent = CoefficientH * AH - AM; + BHtangent = CoefficientH * BH - BM; + CHtangent = CoefficientH * CH - CM; + + AngleCos = (AHtangent * ALtangent + BHtangent * BLtangent + CHtangent * CLtangent) / ( + Math.sqrt(AHtangent * AHtangent + BHtangent * BHtangent + CHtangent * CHtangent) + * Math.sqrt(ALtangent * ALtangent + BLtangent * BLtangent + + CLtangent * CLtangent)); + + AngleCos = Math.acos(AngleCos); + + ANormalLine = BHtangent * CLtangent - CHtangent * BLtangent; + BNormalLine = 0 - (AHtangent * CLtangent - CHtangent * ALtangent); + CNormalLine = AHtangent * BLtangent - BHtangent * ALtangent; + + if (AM != 0) { + OrientationValue = ANormalLine / AM; + } else if (BM != 0) { + OrientationValue = BNormalLine / BM; + } else { + OrientationValue = CNormalLine / CM; + } + + if (OrientationValue > 0) { + Sum1 += AngleCos; + Count1++; + + } else { + Sum2 += AngleCos; + Count2++; + //Sum +=2*Math.PI-AngleCos; + } + } + if (Sum1 > Sum2) { + Sum = Sum1 + (2 * Math.PI * Count2 - Sum2); + } else { + Sum = (2 * Math.PI * Count1 - Sum1) + Sum2; + } + //平方米 + area = (Sum - (size - 2) * Math.PI) * Radius * Radius; + } + return Math.abs(area); + } + public static void main(String[] args) { Point point = new Point(111.770495, 40.871839); List points = new ArrayList<>(); diff --git a/cloud-common/src/main/resources/mybatis/mapper/env/env-mapper.xml b/cloud-common/src/main/resources/mybatis/mapper/env/env-mapper.xml new file mode 100644 index 0000000..c6a17d1 --- /dev/null +++ b/cloud-common/src/main/resources/mybatis/mapper/env/env-mapper.xml @@ -0,0 +1,72 @@ + + + + + + + + + + + + + CREATE TABLE IF NOT EXISTS `sys_env` ( + `env_key` varchar(255) NOT NULL COMMENT '环境变量Key', + `env_explain` varchar(255) DEFAULT NULL COMMENT '环境变量说明', + `env_value` longtext COMMENT '环境变量值', + PRIMARY KEY (`env_key`), + UNIQUE KEY `env_key` (`env_key`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='系统环境变量'; + + + + + INSERT INTO sys_env( + env_key, + env_explain, + env_value + ) VALUES( + #{envKey}, + #{envExplain}, + #{envValue} + ) + + + + + DELETE FROM sys_env + + + + + UPDATE + sys_env + SET + env_value = #{envValue}, + env_explain = #{envExplain} + WHERE + env_key = #{envKey} + + + + + + + + \ No newline at end of file diff --git a/cloud-common/src/main/resources/templates/env/update.html b/cloud-common/src/main/resources/templates/env/update.html new file mode 100644 index 0000000..a6f5089 --- /dev/null +++ b/cloud-common/src/main/resources/templates/env/update.html @@ -0,0 +1,252 @@ + + + + + + + + + + + + + +
+
+ +
+
+ +
+
+
+
+ + + + \ No newline at end of file