WANG 6 years ago
parent
commit
87e47c31ef

+ 2 - 0
examcloud-core-basic-dao/src/main/java/cn/com/qmth/examcloud/core/basic/dao/entity/SysConfigEntity.java

@@ -1,5 +1,6 @@
 package cn.com.qmth.examcloud.core.basic.dao.entity;
 
+import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.EnumType;
 import javax.persistence.Enumerated;
@@ -27,6 +28,7 @@ public class SysConfigEntity extends JpaEntity {
 	private String key;
 
 	@Enumerated(EnumType.STRING)
+	@Column(nullable = false)
 	private DataType dataType;
 
 	@Lob

+ 16 - 0
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/SysConfigService.java

@@ -0,0 +1,16 @@
+package cn.com.qmth.examcloud.core.basic.service;
+
+/**
+ * 类注释
+ *
+ * @author WANGWEI
+ * @date 2018年12月3日
+ * @Copyright (c) 2018-? http://qmth.com.cn All Rights Reserved.
+ */
+public interface SysConfigService {
+
+	void set(String key, String value);
+
+	Object get(String key);
+
+}

+ 120 - 0
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/impl/SysConfigServiceImpl.java

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