|
@@ -0,0 +1,120 @@
|
|
|
+package cn.com.qmth.examcloud.core.basic.service.impl;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.commons.base.exception.StatusException;
|
|
|
+import cn.com.qmth.examcloud.commons.base.helpers.DataType;
|
|
|
+import cn.com.qmth.examcloud.commons.base.util.DateUtil;
|
|
|
+import cn.com.qmth.examcloud.commons.base.util.DateUtil.DatePatterns;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.SysConfigRepo;
|
|
|
+import cn.com.qmth.examcloud.core.basic.dao.entity.SysConfigEntity;
|
|
|
+import cn.com.qmth.examcloud.core.basic.service.SysConfigService;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 系统配置服务
|
|
|
+ *
|
|
|
+ * @author WANGWEI
|
|
|
+ * @date 2018年12月3日
|
|
|
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class SysConfigServiceImpl implements SysConfigService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ SysConfigRepo sysConfigRepo;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void set(String key, String value) {
|
|
|
+ if (StringUtils.isBlank(key)) {
|
|
|
+ throw new StatusException("B-350001", "key is blank");
|
|
|
+ }
|
|
|
+
|
|
|
+ SysConfigEntity sysConf = sysConfigRepo.getOne(key);
|
|
|
+ if (null == sysConf) {
|
|
|
+ throw new StatusException("B-350002", "key is wrong");
|
|
|
+ }
|
|
|
+ DataType dataType = sysConf.getDataType();
|
|
|
+
|
|
|
+ if (dataType.equals(DataType.STRING)) {
|
|
|
+ sysConf.setValue(value);
|
|
|
+ } else if (dataType.equals(DataType.LONG)) {
|
|
|
+ try {
|
|
|
+ sysConf.setValue(String.valueOf(Long.parseLong(value)));
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new StatusException("B-350002", "value is wrong");
|
|
|
+ }
|
|
|
+ } else if (dataType.equals(DataType.INTEGER)) {
|
|
|
+ try {
|
|
|
+ sysConf.setValue(String.valueOf(Integer.parseInt(value)));
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new StatusException("B-350002", "value is wrong");
|
|
|
+ }
|
|
|
+ } else if (dataType.equals(DataType.DATE)) {
|
|
|
+ try {
|
|
|
+ sysConf.setValue(
|
|
|
+ DateUtil.format(DateUtil.parse(value, DatePatterns.ISO), DatePatterns.ISO));
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("B-350002", "value is wrong");
|
|
|
+ }
|
|
|
+ } else if (dataType.equals(DataType.BOOLEAN)) {
|
|
|
+ if ("true".equals(value)) {
|
|
|
+ sysConf.setValue(value);
|
|
|
+ } else if ("false".equals(value)) {
|
|
|
+ sysConf.setValue(value);
|
|
|
+ } else {
|
|
|
+ throw new StatusException("B-350002", "value is wrong");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Object get(String key) {
|
|
|
+ if (StringUtils.isBlank(key)) {
|
|
|
+ throw new StatusException("B-350001", "key is blank");
|
|
|
+ }
|
|
|
+
|
|
|
+ SysConfigEntity sysConf = sysConfigRepo.getOne(key);
|
|
|
+ if (null == sysConf) {
|
|
|
+ throw new StatusException("B-350002", "key is wrong");
|
|
|
+ }
|
|
|
+
|
|
|
+ String value = sysConf.getValue();
|
|
|
+ DataType dataType = sysConf.getDataType();
|
|
|
+
|
|
|
+ if (dataType.equals(DataType.STRING)) {
|
|
|
+ return value;
|
|
|
+ } else if (dataType.equals(DataType.LONG)) {
|
|
|
+ try {
|
|
|
+ return Long.parseLong(value);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new StatusException("B-350002", "value is wrong");
|
|
|
+ }
|
|
|
+ } else if (dataType.equals(DataType.INTEGER)) {
|
|
|
+ try {
|
|
|
+ return Integer.parseInt(value);
|
|
|
+ } catch (NumberFormatException e) {
|
|
|
+ throw new StatusException("B-350002", "value is wrong");
|
|
|
+ }
|
|
|
+ } else if (dataType.equals(DataType.DATE)) {
|
|
|
+ try {
|
|
|
+ return DateUtil.parse(value, DatePatterns.ISO);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new StatusException("B-350002", "value is wrong");
|
|
|
+ }
|
|
|
+ } else if (dataType.equals(DataType.BOOLEAN)) {
|
|
|
+ if ("true".equals(value)) {
|
|
|
+ return true;
|
|
|
+ } else if ("false".equals(value)) {
|
|
|
+ return false;
|
|
|
+ } else {
|
|
|
+ throw new StatusException("B-350002", "value is wrong");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ throw new StatusException("B-350003", "dataType is undefined");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|