From 9e68345a319881738956f1e33567b0f8c086a1f3 Mon Sep 17 00:00:00 2001 From: liuchengqian Date: Wed, 31 Aug 2022 08:55:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=B0=8F=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/xkrs/dao/GlobalConfigurationDao.java | 15 ++++ .../entity/GlobalConfigurationEntity.java | 80 +++++++++++++++++++ .../service/GlobalConfigurationService.java | 27 +++++++ .../impl/GlobalConfigurationServiceImpl.java | 69 ++++++++++++++++ .../java/com/xkrs/utils/LocalNullUtils.java | 16 ++++ 5 files changed, 207 insertions(+) create mode 100644 src/main/java/com/xkrs/dao/GlobalConfigurationDao.java create mode 100644 src/main/java/com/xkrs/model/entity/GlobalConfigurationEntity.java create mode 100644 src/main/java/com/xkrs/service/GlobalConfigurationService.java create mode 100644 src/main/java/com/xkrs/service/impl/GlobalConfigurationServiceImpl.java create mode 100644 src/main/java/com/xkrs/utils/LocalNullUtils.java diff --git a/src/main/java/com/xkrs/dao/GlobalConfigurationDao.java b/src/main/java/com/xkrs/dao/GlobalConfigurationDao.java new file mode 100644 index 0000000..42cab13 --- /dev/null +++ b/src/main/java/com/xkrs/dao/GlobalConfigurationDao.java @@ -0,0 +1,15 @@ +package com.xkrs.dao; + +import com.xkrs.model.entity.GlobalConfigurationEntity; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; +import org.springframework.stereotype.Component; + +import java.util.Optional; + +@Component +public interface GlobalConfigurationDao extends JpaRepository, JpaSpecificationExecutor { + + Optional findByKey(String key); + +} diff --git a/src/main/java/com/xkrs/model/entity/GlobalConfigurationEntity.java b/src/main/java/com/xkrs/model/entity/GlobalConfigurationEntity.java new file mode 100644 index 0000000..59485d1 --- /dev/null +++ b/src/main/java/com/xkrs/model/entity/GlobalConfigurationEntity.java @@ -0,0 +1,80 @@ +package com.xkrs.model.entity; + +import javax.persistence.*; +import java.io.Serializable; + +/** + * 全局配置表 + */ +@Entity +@Table(name = "global_configuration") +public class GlobalConfigurationEntity implements Serializable { + + /** + * 主键id + */ + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "global_configuration_seq_gen") + @SequenceGenerator(name = "global_configuration_seq_gen", sequenceName = "global_configuration_id_seq", allocationSize = 1) + private Long id; + + /** + * 全局配置项的键 + */ + private String key; + + /** + * 全局配置项的值 + */ + private String value; + + /** + * 备注信息 + */ + private String remark; + + public GlobalConfigurationEntity() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getKey() { + return key; + } + + public void setKey(String key) { + this.key = key; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public String getRemark() { + return remark; + } + + public void setRemark(String remark) { + this.remark = remark; + } + + @Override + public String toString() { + return "GlobalConfigurationEntity{" + + "id=" + id + + ", key='" + key + '\'' + + ", value='" + value + '\'' + + ", remark='" + remark + '\'' + + '}'; + } +} diff --git a/src/main/java/com/xkrs/service/GlobalConfigurationService.java b/src/main/java/com/xkrs/service/GlobalConfigurationService.java new file mode 100644 index 0000000..2aaaddf --- /dev/null +++ b/src/main/java/com/xkrs/service/GlobalConfigurationService.java @@ -0,0 +1,27 @@ +package com.xkrs.service; + +import com.xkrs.model.entity.GlobalConfigurationEntity; + +public interface GlobalConfigurationService { + + /** + * 新增全局配置 + */ + String insertGlobalConfiguration(GlobalConfigurationEntity globalConfiguration); + + /** + * 删除全局配置 + */ + String deleteGlobalConfiguration(GlobalConfigurationEntity globalConfiguration); + + /** + * 修改全局配置 + */ + String updateGlobalConfiguration(GlobalConfigurationEntity globalConfiguration); + + /** + * 查询全局配置 + */ + String queryGlobalConfiguration(GlobalConfigurationEntity globalConfiguration); + +} diff --git a/src/main/java/com/xkrs/service/impl/GlobalConfigurationServiceImpl.java b/src/main/java/com/xkrs/service/impl/GlobalConfigurationServiceImpl.java new file mode 100644 index 0000000..a2a6879 --- /dev/null +++ b/src/main/java/com/xkrs/service/impl/GlobalConfigurationServiceImpl.java @@ -0,0 +1,69 @@ +package com.xkrs.service.impl; + +import com.xkrs.common.encapsulation.PromptMessageEnum; +import com.xkrs.dao.GlobalConfigurationDao; +import com.xkrs.model.entity.GlobalConfigurationEntity; +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.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Locale; +import java.util.Optional; + +import static com.xkrs.common.encapsulation.OutputEncapsulation.outputEncapsulationObject; + +@Service +public class GlobalConfigurationServiceImpl implements GlobalConfigurationService { + + private final Locale locale = LocaleContextHolder.getLocale(); + + @Resource + private GlobalConfigurationDao globalConfigurationDao; + + @Override + public String insertGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) { + 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); + } + Optional duplicatedOptional = globalConfigurationDao.findByKey(key); + if (duplicatedOptional.isPresent()) { + return outputEncapsulationObject(PromptMessageEnum.PROCESS_FAIL, "key已存在", locale); + } + GlobalConfigurationEntity globalConfigurationEntity = new GlobalConfigurationEntity(); + globalConfigurationEntity.setKey(key); + globalConfigurationEntity.setValue(value); + globalConfigurationEntity.setRemark(remark); + globalConfigurationDao.save(globalConfigurationEntity); + 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()) { + 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); + } + + @Override + public String queryGlobalConfiguration(GlobalConfigurationEntity globalConfiguration) { + return null; + } +} diff --git a/src/main/java/com/xkrs/utils/LocalNullUtils.java b/src/main/java/com/xkrs/utils/LocalNullUtils.java new file mode 100644 index 0000000..47bddb8 --- /dev/null +++ b/src/main/java/com/xkrs/utils/LocalNullUtils.java @@ -0,0 +1,16 @@ +package com.xkrs.utils; + +public class LocalNullUtils { + + private LocalNullUtils() { + } + + public static String formatNullValue(String value) { + return formatNullValue(value, ""); + } + + public static String formatNullValue(String value, String defaultValue) { + return null == value ? defaultValue : value; + } + +}