优化全局配置表的增删改查操作

This commit is contained in:
liuchengqian 2022-08-31 17:03:09 +08:00
parent e3bb203a5a
commit f58dfb5274
4 changed files with 131 additions and 8 deletions

View File

@ -33,6 +33,7 @@ class WebSecurityConfig extends WebSecurityConfigurerAdapter {
//.antMatchers("/").permitAll() //.antMatchers("/").permitAll()
// 所有OPTIONS请求都放行 // 所有OPTIONS请求都放行
.antMatchers(HttpMethod.OPTIONS).permitAll() .antMatchers(HttpMethod.OPTIONS).permitAll()
.antMatchers("/global/configuration/**").permitAll()
.antMatchers(HttpMethod.GET, "/selectGlobalConfigDict").permitAll() .antMatchers(HttpMethod.GET, "/selectGlobalConfigDict").permitAll()
.antMatchers(HttpMethod.GET, "/selectGlobalConfigValue").permitAll() .antMatchers(HttpMethod.GET, "/selectGlobalConfigValue").permitAll()
.antMatchers(HttpMethod.GET, "/selectGlobalConfig").permitAll() .antMatchers(HttpMethod.GET, "/selectGlobalConfig").permitAll()

View File

@ -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);
}
}

View File

@ -5,11 +5,11 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Optional; import java.util.List;
@Component @Component
public interface GlobalConfigurationDao extends JpaRepository<GlobalConfigurationEntity, Long>, JpaSpecificationExecutor<GlobalConfigurationEntity> { public interface GlobalConfigurationDao extends JpaRepository<GlobalConfigurationEntity, Long>, JpaSpecificationExecutor<GlobalConfigurationEntity> {
Optional<GlobalConfigurationEntity> findByKey(String key); List<GlobalConfigurationEntity> findByKey(String key);
} }

View File

@ -7,9 +7,14 @@ import com.xkrs.service.GlobalConfigurationService;
import com.xkrs.utils.LocalNullUtils; import com.xkrs.utils.LocalNullUtils;
import org.apache.hc.core5.util.TextUtils; import org.apache.hc.core5.util.TextUtils;
import org.springframework.context.i18n.LocaleContextHolder; 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 org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Optional; import java.util.Optional;
@ -23,6 +28,9 @@ public class GlobalConfigurationServiceImpl implements GlobalConfigurationServic
@Resource @Resource
private GlobalConfigurationDao globalConfigurationDao; private GlobalConfigurationDao globalConfigurationDao;
/**
* 新增全局配置
*/
@Override @Override
public String insertGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) { public String insertGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) {
final String key = LocalNullUtils.formatNullValue(globalConfiguration.getKey()); final String key = LocalNullUtils.formatNullValue(globalConfiguration.getKey());
@ -31,8 +39,11 @@ public class GlobalConfigurationServiceImpl implements GlobalConfigurationServic
if (TextUtils.isEmpty(key)) { if (TextUtils.isEmpty(key)) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "key不可为空", locale); return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "key不可为空", locale);
} }
Optional<GlobalConfigurationEntity> duplicatedOptional = globalConfigurationDao.findByKey(key); if (TextUtils.isEmpty(value)) {
if (duplicatedOptional.isPresent()) { return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "value不可为空", locale);
}
List<GlobalConfigurationEntity> duplicatedEntityList = globalConfigurationDao.findByKey(key);
if (duplicatedEntityList != null && duplicatedEntityList.size() > 0) {
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "key已存在", locale); return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "key已存在", locale);
} }
GlobalConfigurationEntity globalConfigurationEntity = new GlobalConfigurationEntity(); GlobalConfigurationEntity globalConfigurationEntity = new GlobalConfigurationEntity();
@ -43,27 +54,86 @@ public class GlobalConfigurationServiceImpl implements GlobalConfigurationServic
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "新增成功", locale); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "新增成功", locale);
} }
/**
* 删除全局配置
*/
@Override @Override
public String deleteGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) { public String deleteGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) {
Long id = globalConfiguration.getId(); Long id = globalConfiguration.getId();
if (null == id) { if (null == id) {
return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "id不可为空", locale); return outputEncapsulationObject(PromptMessageEnum.DATA_WRONG, "id不可为空", locale);
} }
Optional<GlobalConfigurationEntity> targetOptional = globalConfigurationDao.findById(id); Optional<GlobalConfigurationEntity> targetEntityOptional = globalConfigurationDao.findById(id);
if (targetOptional.isEmpty()) { if (targetEntityOptional.isEmpty()) {
return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "id不存在", locale); return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "id不存在", locale);
} }
globalConfigurationDao.deleteById(id); globalConfigurationDao.deleteById(id);
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "删除成功", locale); return outputEncapsulationObject(PromptMessageEnum.SUCCESS, "删除成功", locale);
} }
/**
* 修改全局配置
*/
@Override @Override
public String updateGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) { 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<GlobalConfigurationEntity> 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<GlobalConfigurationEntity> 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 @Override
public String queryGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) { 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<GlobalConfigurationEntity> specification = (root, criteriaQuery, criteriaBuilder) -> {
List<Predicate> 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<GlobalConfigurationEntity> globalConfigurationEntityList = globalConfigurationDao.findAll(specification, Sort.by(Sort.Direction.ASC, "id"));
return outputEncapsulationObject(PromptMessageEnum.SUCCESS, globalConfigurationEntityList, locale);
} }
} }