添加数据字典

This commit is contained in:
xiangwb
2018-02-26 15:53:55 +08:00
parent cd4bed46e2
commit f9ee49431f
6 changed files with 333 additions and 0 deletions
@@ -0,0 +1,42 @@
package com.xwbing.domain.entity.sys;
import lombok.Data;
import lombok.EqualsAndHashCode;
import javax.persistence.Column;
import javax.persistence.Entity;
/**
* 项目名称: boot-module-demo
* 创建时间: 2018/2/26 10:26
* 作者: xiangwb
* 说明: 数据字典
*/
@EqualsAndHashCode(callSuper = true)
@Data
@Entity(name = "data_dict")
public class DataDictionary extends BaseEntity {
private static final long serialVersionUID = -3409347240188002427L;
public static String table = "data_dict";
/**
* 英文名称 编码 唯一
*/
private String code;
/**
* 名称
*/
private String name;
/**
* 描述
*/
private String description;
/**
* 父键
*/
@Column(name = "parent_id")
private String parentId;
/**
* 是否启用
*/
private String enable;
}
@@ -0,0 +1,19 @@
package com.xwbing.domain.entity.vo;
import com.xwbing.domain.entity.sys.DataDictionary;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* 项目名称: boot-module-demo
* 创建时间: 2018/2/26 15:27
* 作者: xiangwb
* 说明:
*/
@Data
@ApiModel
public class DataDictionaryVo extends RestMessageVo {
@ApiModelProperty(value = "返回数据")
private DataDictionary data;
}
@@ -0,0 +1,21 @@
package com.xwbing.domain.entity.vo;
import com.xwbing.domain.entity.sys.DataDictionary;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
/**
* 项目名称: boot-module-demo
* 创建时间: 2018/2/26 15:29
* 作者: xiangwb
* 说明:
*/
@Data
@ApiModel
public class ListDataDictionaryVo extends RestMessageVo{
@ApiModelProperty(value = "返回数据")
private List<DataDictionary> data;
}
@@ -0,0 +1,22 @@
package com.xwbing.domain.repository.sys;
import com.xwbing.domain.entity.sys.DataDictionary;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
/**
* 项目名称: boot-module-demo
* 创建时间: 2018/2/26 10:43
* 作者: xiangwb
* 说明:
*/
public interface DataDictionaryRepository extends JpaRepository<DataDictionary, String> {
DataDictionary getByCode(String code);
DataDictionary getByCodeAndEnable(String code,String enable);
List<DataDictionary> getByParentIdAndEnable(String parentId, String enable);
List<DataDictionary> getByParentId(String parentId);
}
@@ -0,0 +1,127 @@
package com.xwbing.service.sys;
import com.xwbing.constant.CommonConstant;
import com.xwbing.domain.entity.sys.DataDictionary;
import com.xwbing.domain.repository.sys.DataDictionaryRepository;
import com.xwbing.exception.BusinessException;
import com.xwbing.util.PassWordUtil;
import com.xwbing.util.RestMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
* 项目名称: boot-module-demo
* 创建时间: 2018/2/26 10:29
* 作者: xiangwb
* 说明: 数据字典服务层
*/
@Service
public class DataDictionaryService {
@Resource
private DataDictionaryRepository dataDictionaryRepository;
public RestMessage save(DataDictionary dictionary) {
RestMessage result = new RestMessage();
//检查编码
boolean b = uniqueCode(dictionary.getCode(), null);
if (!b)
throw new BusinessException("该编码已存在");
//添加必要参数
String id = PassWordUtil.createId();
dictionary.setId(id);
dictionary.setCreateTime(new Date());
if (StringUtils.isEmpty(dictionary.getParentId()))
dictionary.setParentId(CommonConstant.ROOT);
//保存
DataDictionary save = dataDictionaryRepository.save(dictionary);
if (save != null) {
result.setSuccess(true);
result.setId(id);
result.setMessage("保存数据字典成功");
} else {
result.setMessage("保存数据字典失败");
}
return result;
}
/**
* 更新数据字典 此处code是不允许修改的,否则要出错
*
* @param dictionary
* @return
*/
public RestMessage update(DataDictionary dictionary) {
RestMessage result = new RestMessage();
String id = dictionary.getId();
//判断该字典是否存在
DataDictionary old = getById(id);
if (old == null)
throw new BusinessException("该字典不存在");
old.setName(dictionary.getName());
old.setDescription(dictionary.getDescription());
old.setEnable(dictionary.getEnable());
old.setModifiedTime(new Date());
DataDictionary save = dataDictionaryRepository.save(old);
if (save != null) {
result.setMessage("更新成功");
result.setSuccess(true);
} else {
result.setMessage("更新失败");
}
return result;
}
public DataDictionary getById(String id) {
return dataDictionaryRepository.findOne(id);
}
/**
* true为唯一
*
* @param id
* @param code
* @return
*/
public boolean uniqueCode(String code, String id) {
if (StringUtils.isEmpty(code))
throw new BusinessException("code不能为空");
DataDictionary one = dataDictionaryRepository.getByCode(code);
return one == null || StringUtils.isNotEmpty(id) && id.equals(one.getId());
}
/**
* 根据父类,是否启用查询
*
* @param parentId
* @param enable
* @return
*/
public List<DataDictionary> findListByParent(String parentId, String enable) {
if (StringUtils.isNotEmpty(enable))
return dataDictionaryRepository.getByParentIdAndEnable(parentId, enable);
else
return dataDictionaryRepository.getByParentId(parentId);
}
/**
* 根据编码查询字典列表
*
* @param code
* @param enable
* @return
*/
public List<DataDictionary> findListByCode(String code, String enable) {
DataDictionary dictionary = dataDictionaryRepository.getByCodeAndEnable(code, "Y");
if (dictionary != null) {
String id = dictionary.getId();
return findListByParent(id, enable);
} else {
return Collections.emptyList();
}
}
}
@@ -0,0 +1,102 @@
package com.xwbing.controller.sys;
import com.alibaba.fastjson.JSONObject;
import com.xwbing.annotation.LogInfo;
import com.xwbing.constant.CommonConstant;
import com.xwbing.domain.entity.sys.DataDictionary;
import com.xwbing.domain.entity.vo.DataDictionaryVo;
import com.xwbing.domain.entity.vo.ListDataDictionaryVo;
import com.xwbing.domain.entity.vo.RestMessageVo;
import com.xwbing.service.sys.DataDictionaryService;
import com.xwbing.util.JSONObjResult;
import com.xwbing.util.RestMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* 项目名称: boot-module-demo
* 创建时间: 2018/2/26 14:09
* 作者: xiangwb
* 说明:
*/
@Api(tags = "dictionaryApi", description = "数据字典相关接口")
@RestController
@RequestMapping("/dictionary/")
public class DataDictionaryControl {
@Resource
private DataDictionaryService dataDictionaryService;
@LogInfo("添加字典")
@PostMapping("save")
@ApiOperation(value = "添加字典", response = RestMessageVo.class)
public JSONObject save(@RequestBody DataDictionary dictionary) {
RestMessage result = dataDictionaryService.save(dictionary);
return JSONObjResult.toJSONObj(result);
}
@LogInfo("修改字典")
@PostMapping("update")
@ApiOperation(value = "修改字典", response = RestMessageVo.class)
public JSONObject update(@RequestBody DataDictionary dictionary) {
if (StringUtils.isEmpty(dictionary.getId()))
return JSONObjResult.toJSONObj("主键不能为空");
RestMessage result = dataDictionaryService.update(dictionary);
return JSONObjResult.toJSONObj(result);
}
@LogInfo("获取字典详情")
@ApiOperation(value = "获取字典详情", response = DataDictionaryVo.class)
@GetMapping("getById")
public JSONObject getById(@RequestParam String id) {
if (StringUtils.isEmpty(id))
return JSONObjResult.toJSONObj("主键不能为空");
DataDictionary dataDictionary = dataDictionaryService.getById(id);
if (dataDictionary == null)
return JSONObjResult.toJSONObj("该字典不存在");
return JSONObjResult.toJSONObj(dataDictionary, "");
}
@LogInfo("校验编码")
@ApiOperation(value = "校验编码", response = RestMessageVo.class)
@ApiImplicitParam(name = "id", value = "主键", paramType = "query", dataType = "string")
@PostMapping("uniqueCode")
public JSONObject uniqueCode(@RequestParam String code, String id) {
boolean b = dataDictionaryService.uniqueCode(code, id);
RestMessage result = new RestMessage();
if (b)
result.setSuccess(true);
return JSONObjResult.toJSONObj(result);
}
@LogInfo("根据父id列表查询")
@ApiOperation(value = "根据父id列表查询", response = ListDataDictionaryVo.class)
@ApiImplicitParams({
@ApiImplicitParam(name = "parentId", value = "父id", paramType = "query", dataType = "string"),
@ApiImplicitParam(name = "enable", value = "是否启用,格式为Y|N", paramType = "query", dataType = "string")
})
@PostMapping("findListByParent")
public JSONObject findListByParent(String enable, String parentId) {
if (StringUtils.isEmpty(parentId))
parentId = CommonConstant.ROOT;
List<DataDictionary> listByParent = dataDictionaryService.findListByParent(parentId, enable);
return JSONObjResult.toJSONObj(listByParent, "");
}
@LogInfo("根据编码查询字典列表")
@ApiOperation(value = "根据编码查询字典列表", response = ListDataDictionaryVo.class)
@ApiImplicitParam(name = "enable", value = "是否启用,格式为Y|N", paramType = "query", dataType = "string")
@PostMapping("findListByCode")
public JSONObject findListByCode(@RequestParam String code, String enable) {
if (StringUtils.isEmpty(code))
return JSONObjResult.toJSONObj("编码不能为空");
List<DataDictionary> listByParent = dataDictionaryService.findListByCode(code, enable);
return JSONObjResult.toJSONObj(listByParent, "");
}
}