Quellcode durchsuchen

添加参数配置功能后台代码

lideyin vor 6 Jahren
Ursprung
Commit
406855c1fa

+ 90 - 49
examcloud-core-basic-api-provider/src/main/java/cn/com/qmth/examcloud/core/basic/api/controller/SystemPropertyController.java

@@ -2,10 +2,20 @@ package cn.com.qmth.examcloud.core.basic.api.controller;
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import javax.persistence.criteria.Predicate;
+import javax.validation.Valid;
 
+import cn.com.qmth.examcloud.api.commons.enums.BasicDataType;
+import cn.com.qmth.examcloud.commons.exception.StatusException;
+import cn.com.qmth.examcloud.core.basic.dao.entity.SpecialtyEntity;
+import cn.com.qmth.examcloud.core.basic.service.bean.SystemPropertyInfo;
+import cn.com.qmth.examcloud.web.helpers.GlobalHelper;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.domain.Page;
 import org.springframework.data.domain.PageRequest;
@@ -13,11 +23,7 @@ import org.springframework.data.domain.Sort;
 import org.springframework.data.domain.Sort.Direction;
 import org.springframework.data.jpa.domain.Specification;
 import org.springframework.transaction.annotation.Transactional;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.PathVariable;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.bind.annotation.*;
 
 import cn.com.qmth.examcloud.core.basic.dao.SystemPropertyRepo;
 import cn.com.qmth.examcloud.core.basic.dao.entity.SystemPropertyEntity;
@@ -37,48 +43,83 @@ import io.swagger.annotations.ApiOperation;
 @RequestMapping("${$rmp.ctr.basic}/systemProperty")
 public class SystemPropertyController extends ControllerSupport {
 
-	@Autowired
-	SystemPropertyService systemPropertyService;
-
-	@Autowired
-	SystemPropertyRepo systemPropertyRepo;
-
-	@ApiOperation(value = "查询系统配置")
-	@GetMapping("{key}")
-	public Object get(@PathVariable String key) {
-		Object object = systemPropertyService.get(key);
-		return object;
-	}
-
-	@ApiOperation(value = "查询系统配置")
-	@GetMapping("page/{curPage}/{pageSize}")
-	public Page<SystemPropertyEntity> get(@PathVariable Integer curPage,
-			@PathVariable Integer pageSize, @RequestParam(required = false) String propKey,
-			@RequestParam(required = false) String description,
-			@RequestParam(required = false) String propValue) {
-
-		Specification<SystemPropertyEntity> specification = (root, query, cb) -> {
-			List<Predicate> predicates = new ArrayList<>();
-
-			if (StringUtils.isNotBlank(propKey)) {
-				predicates.add(cb.like(root.get("propKey"), toSqlSearchPattern(propKey)));
-			}
-			if (StringUtils.isNotBlank(description)) {
-				predicates.add(cb.like(root.get("description"), toSqlSearchPattern(description)));
-			}
-			if (StringUtils.isNotBlank(propValue)) {
-				predicates.add(cb.like(root.get("propValue"), toSqlSearchPattern(propValue)));
-			}
-
-			return cb.and(predicates.toArray(new Predicate[predicates.size()]));
-		};
-
-		PageRequest pageRequest = PageRequest.of(curPage, pageSize,
-				new Sort(Direction.DESC, "updateTime"));
-
-		Page<SystemPropertyEntity> page = systemPropertyRepo.findAll(specification, pageRequest);
-
-		return page;
-	}
-
+    @Autowired
+    SystemPropertyService systemPropertyService;
+
+    @Autowired
+    SystemPropertyRepo systemPropertyRepo;
+
+    @ApiOperation(value = "查询系统配置")
+    @GetMapping("{key}")
+    public Object get(@PathVariable String key) {
+        Object object = systemPropertyService.get(key);
+        return object;
+    }
+
+    @ApiOperation(value = "查询系统配置")
+    @GetMapping("page/{curPage}/{pageSize}")
+    public Page<SystemPropertyEntity> get(@PathVariable Integer curPage,
+                                          @PathVariable Integer pageSize, @RequestParam(required = false) String propKey,
+                                          @RequestParam(required = false) String description,
+                                          @RequestParam(required = false) String propValue) {
+
+        Specification<SystemPropertyEntity> specification = (root, query, cb) -> {
+            List<Predicate> predicates = new ArrayList<>();
+
+            if (StringUtils.isNotBlank(propKey)) {
+                predicates.add(cb.like(root.get("propKey"), toSqlSearchPattern(propKey)));
+            }
+            if (StringUtils.isNotBlank(description)) {
+                predicates.add(cb.like(root.get("description"), toSqlSearchPattern(description)));
+            }
+            if (StringUtils.isNotBlank(propValue)) {
+                predicates.add(cb.like(root.get("propValue"), toSqlSearchPattern(propValue)));
+            }
+
+            return cb.and(predicates.toArray(new Predicate[predicates.size()]));
+        };
+
+        PageRequest pageRequest = PageRequest.of(curPage, pageSize,
+                new Sort(Direction.DESC, "updateTime"));
+
+        Page<SystemPropertyEntity> page = systemPropertyRepo.findAll(specification, pageRequest);
+
+        return page;
+    }
+
+    @ApiOperation(value = "新增系统参数")
+    @PostMapping
+    public void addSysProp(@RequestBody @Valid SystemPropertyInfo info) {
+        systemPropertyService.addSysProp(info);
+    }
+
+    @ApiOperation(value = "更新系统参数")
+    @PutMapping
+    public void updateSysProp(@RequestBody @Valid SystemPropertyInfo info) {
+        systemPropertyService.updateSysProp(info);
+    }
+
+    @ApiOperation(value = "删除系统参数")
+    @DeleteMapping("{ids}")
+    public void deleteSysProp(@PathVariable @Required String ids) {
+        List<String> propKeys = Stream.of(ids.split(",")).map(s -> s.trim())
+                .collect(Collectors.toList());
+        for (String key : propKeys) {
+            SystemPropertyEntity prop = GlobalHelper.getEntity(systemPropertyRepo, key,
+                    SystemPropertyEntity.class);
+            if (null == prop) {
+                continue;
+            }
+            systemPropertyRepo.delete(prop);
+        }
+    }
+
+    @ApiOperation(value = "获取基本数据类型集合")
+    @GetMapping("getBasicDataTypes")
+    public List<Map<String, String>> getBasicDataTypes(@RequestParam(required = false) String name) {
+        if (StringUtils.isNotBlank(name)) {
+            return BasicDataType.getBasicDataTypes().stream().filter(p -> p.get("name").contains(name)).collect(Collectors.toList());
+        }
+        return BasicDataType.getBasicDataTypes();
+    }
 }

+ 5 - 0
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/SystemPropertyService.java

@@ -1,5 +1,7 @@
 package cn.com.qmth.examcloud.core.basic.service;
 
+import cn.com.qmth.examcloud.core.basic.service.bean.SystemPropertyInfo;
+
 /**
  * 类注释
  *
@@ -13,4 +15,7 @@ public interface SystemPropertyService {
 
 	Object get(String key);
 
+	void addSysProp(SystemPropertyInfo info);
+
+	void updateSysProp(SystemPropertyInfo info);
 }

+ 65 - 0
examcloud-core-basic-service/src/main/java/cn/com/qmth/examcloud/core/basic/service/bean/SystemPropertyInfo.java

@@ -0,0 +1,65 @@
+package cn.com.qmth.examcloud.core.basic.service.bean;
+
+import cn.com.qmth.examcloud.api.commons.enums.BasicDataType;
+import cn.com.qmth.examcloud.api.commons.exchange.JsonSerializable;
+import io.swagger.annotations.ApiModelProperty;
+
+import javax.persistence.Column;
+import javax.persistence.EnumType;
+import javax.persistence.Enumerated;
+import javax.persistence.Id;
+import javax.validation.constraints.NotNull;
+
+/**
+ * 系统参数信息
+ */
+public class SystemPropertyInfo implements JsonSerializable {
+    @NotNull(message = "参数名称不能为空")
+    @ApiModelProperty(value = "参数名称")
+    private String propKey;
+
+    @NotNull(message = "参数描述不能为空")
+    @ApiModelProperty(value = "参数描述")
+    private String description;
+
+    @NotNull(message = "参数类型不能为空")
+    @ApiModelProperty(value = "参数类型")
+    @Enumerated(EnumType.STRING)
+    private BasicDataType propValueType;
+
+    @NotNull(message = "参数值不能为空")
+    @ApiModelProperty(value = "参数值")
+    private String propValue;
+
+    public String getPropKey() {
+        return propKey;
+    }
+
+    public void setPropKey(String propKey) {
+        this.propKey = propKey;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public BasicDataType getPropValueType() {
+        return propValueType;
+    }
+
+    public void setPropValueType(BasicDataType propValueType) {
+        this.propValueType = propValueType;
+    }
+
+    public String getPropValue() {
+        return propValue;
+    }
+
+    public void setPropValue(String propValue) {
+        this.propValue = propValue;
+    }
+}

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

@@ -1,5 +1,6 @@
 package cn.com.qmth.examcloud.core.basic.service.impl;
 
+import cn.com.qmth.examcloud.core.basic.service.bean.SystemPropertyInfo;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -84,4 +85,30 @@ public class SystemPropertyServiceImpl implements SystemPropertyService {
 		return sysPropertyCacheBean.getValue();
 	}
 
+	@Override
+	public void addSysProp(SystemPropertyInfo info) {
+		SystemPropertyEntity sysPropEntity = GlobalHelper.getEntity(systemPropertyRepo, info.getPropKey(), SystemPropertyEntity.class);
+		if (null!=sysPropEntity){
+			throw new StatusException("70001","参数名称已存在");
+		}
+		sysPropEntity= new SystemPropertyEntity();
+		sysPropEntity.setPropKey(info.getPropKey());
+		sysPropEntity.setPropValueType(info.getPropValueType());
+		sysPropEntity.setPropValue(info.getPropValue());
+		sysPropEntity.setDescription(info.getDescription());
+		systemPropertyRepo.save(sysPropEntity);
+	}
+
+	@Override
+	public void updateSysProp(SystemPropertyInfo info) {
+		SystemPropertyEntity sysPropEntity = GlobalHelper.getEntity(systemPropertyRepo, info.getPropKey(), SystemPropertyEntity.class);
+		if (null==sysPropEntity){
+			throw new StatusException("70001","参数名称不存在");
+		}
+		sysPropEntity.setPropValueType(info.getPropValueType());
+		sysPropEntity.setPropValue(info.getPropValue());
+		sysPropEntity.setDescription(info.getDescription());
+		systemPropertyRepo.save(sysPropEntity);
+	}
+
 }