From f58dfb5274b9fc62806590ab2b02f915c8d4e52d Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Wed, 31 Aug 2022 17:03:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=85=A8=E5=B1=80=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E8=A1=A8=E7=9A=84=E5=A2=9E=E5=88=A0=E6=94=B9=E6=9F=A5?= =?UTF-8?q?=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xkrs/common/config/WebSecurityConfig.java | 1 + .../GlobalConfigurationController.java | 52 ++++++++++++ .../com/xkrs/dao/GlobalConfigurationDao.java | 4 +- .../impl/GlobalConfigurationServiceImpl.java | 82 +++++++++++++++++-- 4 files changed, 131 insertions(+), 8 deletions(-) create mode 100644 src/main/java/com/xkrs/controller/GlobalConfigurationController.java diff --git a/src/main/java/com/xkrs/common/config/WebSecurityConfig.java b/src/main/java/com/xkrs/common/config/WebSecurityConfig.java index 9f01c00..f0e768b 100644 --- a/src/main/java/com/xkrs/common/config/WebSecurityConfig.java +++ b/src/main/java/com/xkrs/common/config/WebSecurityConfig.java @@ -33,6 +33,7 @@ class WebSecurityConfig extends WebSecurityConfigurerAdapter { //.antMatchers("/").permitAll() // 所有OPTIONS请求都放行 .antMatchers(HttpMethod.OPTIONS).permitAll() + .antMatchers("/global/configuration/**").permitAll() .antMatchers(HttpMethod.GET, "/selectGlobalConfigDict").permitAll() .antMatchers(HttpMethod.GET, "/selectGlobalConfigValue").permitAll() .antMatchers(HttpMethod.GET, "/selectGlobalConfig").permitAll() diff --git a/src/main/java/com/xkrs/controller/GlobalConfigurationController.java b/src/main/java/com/xkrs/controller/GlobalConfigurationController.java new file mode 100644 index 0000000..4e4b927 --- /dev/null +++ b/src/main/java/com/xkrs/controller/GlobalConfigurationController.java @@ -0,0 +1,52 @@ +package com.xkrs.controller; + +import com.xkrs.model.entity.GlobalConfigurationEntity; +import com.xkrs.service.GlobalConfigurationService; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; + +@RestController +@RequestMapping(value = "/global/configuration") +public class GlobalConfigurationController { + + @Resource + private GlobalConfigurationService globalConfigurationService; + + /** + * 新增全局配置 + */ + @PostMapping("/insert") + public String insertGlobalConfiguration(@RequestBody GlobalConfigurationEntity globalConfiguration) { + return globalConfigurationService.insertGlobalConfiguration(globalConfiguration); + } + + /** + * 删除全局配置 + */ + @PostMapping("/delete") + public String deleteGlobalConfiguration(@RequestBody GlobalConfigurationEntity globalConfiguration) { + return globalConfigurationService.deleteGlobalConfiguration(globalConfiguration); + } + + /** + * 修改全局配置 + */ + @PostMapping("/update") + public String updateGlobalConfiguration(@RequestBody GlobalConfigurationEntity globalConfiguration) { + return globalConfigurationService.updateGlobalConfiguration(globalConfiguration); + } + + /** + * 查询全局配置 + */ + @GetMapping("/query") + public String queryGlobalConfiguration(@RequestParam(required = false, value = "id") Long id, @RequestParam(required = false, value = "key") String key, @RequestParam(required = false, value = "value") String value, @RequestParam(required = false, value = "remark") String remark) { + GlobalConfigurationEntity globalConfiguration = new GlobalConfigurationEntity(); + globalConfiguration.setId(id); + globalConfiguration.setKey(key); + globalConfiguration.setValue(value); + globalConfiguration.setRemark(remark); + return globalConfigurationService.queryGlobalConfiguration(globalConfiguration); + } +} diff --git a/src/main/java/com/xkrs/dao/GlobalConfigurationDao.java b/src/main/java/com/xkrs/dao/GlobalConfigurationDao.java index 42cab13..440661e 100644 --- a/src/main/java/com/xkrs/dao/GlobalConfigurationDao.java +++ b/src/main/java/com/xkrs/dao/GlobalConfigurationDao.java @@ -5,11 +5,11 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.stereotype.Component; -import java.util.Optional; +import java.util.List; @Component public interface GlobalConfigurationDao extends JpaRepository, JpaSpecificationExecutor { - Optional findByKey(String key); + List findByKey(String key); } diff --git a/src/main/java/com/xkrs/service/impl/GlobalConfigurationServiceImpl.java b/src/main/java/com/xkrs/service/impl/GlobalConfigurationServiceImpl.java index a2a6879..2694448 100644 --- a/src/main/java/com/xkrs/service/impl/GlobalConfigurationServiceImpl.java +++ b/src/main/java/com/xkrs/service/impl/GlobalConfigurationServiceImpl.java @@ -7,9 +7,14 @@ import com.xkrs.service.GlobalConfigurationService; import com.xkrs.utils.LocalNullUtils; import org.apache.hc.core5.util.TextUtils; import org.springframework.context.i18n.LocaleContextHolder; +import org.springframework.data.domain.Sort; +import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import javax.persistence.criteria.Predicate; +import java.util.ArrayList; +import java.util.List; import java.util.Locale; import java.util.Optional; @@ -23,6 +28,9 @@ public class GlobalConfigurationServiceImpl implements GlobalConfigurationServic @Resource private GlobalConfigurationDao globalConfigurationDao; + /** + * 新增全局配置 + */ @Override public String insertGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) { final String key = LocalNullUtils.formatNullValue(globalConfiguration.getKey()); @@ -31,8 +39,11 @@ public class GlobalConfigurationServiceImpl implements GlobalConfigurationServic if (TextUtils.isEmpty(key)) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "key不可为空", locale); } - Optional duplicatedOptional = globalConfigurationDao.findByKey(key); - if (duplicatedOptional.isPresent()) { + if (TextUtils.isEmpty(value)) { + return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "value不可为空", locale); + } + List duplicatedEntityList = globalConfigurationDao.findByKey(key); + if (duplicatedEntityList != null && duplicatedEntityList.size() > 0) { return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "key已存在", locale); } GlobalConfigurationEntity globalConfigurationEntity = new GlobalConfigurationEntity(); @@ -43,27 +54,86 @@ public class GlobalConfigurationServiceImpl implements GlobalConfigurationServic return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "新增成功", locale); } + /** + * 删除全局配置 + */ @Override public String deleteGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) { Long id = globalConfiguration.getId(); if (null == id) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "id不可为空", locale); } - Optional targetOptional = globalConfigurationDao.findById(id); - if (targetOptional.isEmpty()) { + Optional targetEntityOptional = globalConfigurationDao.findById(id); + if (targetEntityOptional.isEmpty()) { return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "id不存在", locale); } globalConfigurationDao.deleteById(id); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "删除成功", locale); } + /** + * 修改全局配置 + */ @Override public String updateGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) { - return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "新增成功", locale); + Long id = globalConfiguration.getId(); + if (null == id) { + return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "id不可为空", locale); + } + Optional targetEntityOptional = globalConfigurationDao.findById(id); + if (targetEntityOptional.isEmpty()) { + return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "id不存在", locale); + } + final String key = LocalNullUtils.formatNullValue(globalConfiguration.getKey()); + final String value = LocalNullUtils.formatNullValue(globalConfiguration.getValue()); + final String remark = LocalNullUtils.formatNullValue(globalConfiguration.getRemark()); + if (TextUtils.isEmpty(key)) { + return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "key不可为空", locale); + } + if (TextUtils.isEmpty(value)) { + return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "value不可为空", locale); + } + List duplicatedEntityList = globalConfigurationDao.findByKey(key); + if (duplicatedEntityList != null && duplicatedEntityList.size() > 0) { + return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "key已存在", locale); + } + GlobalConfigurationEntity targetEntity = targetEntityOptional.get(); + targetEntity.setKey(key); + targetEntity.setValue(value); + if (!TextUtils.isEmpty(remark)) { + targetEntity.setRemark(remark); + } + globalConfigurationDao.save(targetEntity); + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "修改成功", locale); } + /** + * 查询全局配置 + */ @Override public String queryGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) { - return null; + final Long id = globalConfiguration.getId(); + final String key = globalConfiguration.getKey(); + final String value = globalConfiguration.getValue(); + final String remark = globalConfiguration.getRemark(); + Specification specification = (root, criteriaQuery, criteriaBuilder) -> { + List predicateList = new ArrayList<>(); + if (null != id) { + predicateList.add(criteriaBuilder.equal(root.get("id").as(Long.class), id)); + } + if (!TextUtils.isEmpty(key)) { + predicateList.add(criteriaBuilder.like(root.get("key").as(String.class), "%" + key + "%")); + } + if (!TextUtils.isEmpty(value)) { + predicateList.add(criteriaBuilder.like(root.get("value").as(String.class), "%" + value + "%")); + } + if (!TextUtils.isEmpty(remark)) { + predicateList.add(criteriaBuilder.like(root.get("remark").as(String.class), "%" + remark + "%")); + } + Predicate[] predicateArray = new Predicate[predicateList.size()]; + return criteriaBuilder.and(predicateList.toArray(predicateArray)); + }; + List globalConfigurationEntityList = globalConfigurationDao.findAll(specification, Sort.by(Sort.Direction.ASC, "id")); + return outputEncapsulationObject(PromptMessageEnum.SUCCESS, globalConfigurationEntityList, locale); } }