From f9ee49431f3754037c3b20a235993f040d24c7cd Mon Sep 17 00:00:00 2001 From: xiangwb Date: Mon, 26 Feb 2018 15:53:55 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=AD=97?= =?UTF-8?q?=E5=85=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/entity/sys/DataDictionary.java | 42 ++++++ .../domain/entity/vo/DataDictionaryVo.java | 19 +++ .../entity/vo/ListDataDictionaryVo.java | 21 +++ .../sys/DataDictionaryRepository.java | 22 +++ .../service/sys/DataDictionaryService.java | 127 ++++++++++++++++++ .../controller/sys/DataDictionaryControl.java | 102 ++++++++++++++ 6 files changed, 333 insertions(+) create mode 100644 boot-service/src/main/java/com/xwbing/domain/entity/sys/DataDictionary.java create mode 100644 boot-service/src/main/java/com/xwbing/domain/entity/vo/DataDictionaryVo.java create mode 100644 boot-service/src/main/java/com/xwbing/domain/entity/vo/ListDataDictionaryVo.java create mode 100644 boot-service/src/main/java/com/xwbing/domain/repository/sys/DataDictionaryRepository.java create mode 100644 boot-service/src/main/java/com/xwbing/service/sys/DataDictionaryService.java create mode 100644 boot-web/src/main/java/com/xwbing/controller/sys/DataDictionaryControl.java diff --git a/boot-service/src/main/java/com/xwbing/domain/entity/sys/DataDictionary.java b/boot-service/src/main/java/com/xwbing/domain/entity/sys/DataDictionary.java new file mode 100644 index 0000000..0ca7a79 --- /dev/null +++ b/boot-service/src/main/java/com/xwbing/domain/entity/sys/DataDictionary.java @@ -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; +} diff --git a/boot-service/src/main/java/com/xwbing/domain/entity/vo/DataDictionaryVo.java b/boot-service/src/main/java/com/xwbing/domain/entity/vo/DataDictionaryVo.java new file mode 100644 index 0000000..97f0ff3 --- /dev/null +++ b/boot-service/src/main/java/com/xwbing/domain/entity/vo/DataDictionaryVo.java @@ -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; +} diff --git a/boot-service/src/main/java/com/xwbing/domain/entity/vo/ListDataDictionaryVo.java b/boot-service/src/main/java/com/xwbing/domain/entity/vo/ListDataDictionaryVo.java new file mode 100644 index 0000000..d240b6a --- /dev/null +++ b/boot-service/src/main/java/com/xwbing/domain/entity/vo/ListDataDictionaryVo.java @@ -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 data; +} diff --git a/boot-service/src/main/java/com/xwbing/domain/repository/sys/DataDictionaryRepository.java b/boot-service/src/main/java/com/xwbing/domain/repository/sys/DataDictionaryRepository.java new file mode 100644 index 0000000..1d18ce3 --- /dev/null +++ b/boot-service/src/main/java/com/xwbing/domain/repository/sys/DataDictionaryRepository.java @@ -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 getByCode(String code); + + DataDictionary getByCodeAndEnable(String code,String enable); + + List getByParentIdAndEnable(String parentId, String enable); + + List getByParentId(String parentId); +} diff --git a/boot-service/src/main/java/com/xwbing/service/sys/DataDictionaryService.java b/boot-service/src/main/java/com/xwbing/service/sys/DataDictionaryService.java new file mode 100644 index 0000000..d0588ad --- /dev/null +++ b/boot-service/src/main/java/com/xwbing/service/sys/DataDictionaryService.java @@ -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 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 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(); + } + } +} diff --git a/boot-web/src/main/java/com/xwbing/controller/sys/DataDictionaryControl.java b/boot-web/src/main/java/com/xwbing/controller/sys/DataDictionaryControl.java new file mode 100644 index 0000000..c4c631a --- /dev/null +++ b/boot-web/src/main/java/com/xwbing/controller/sys/DataDictionaryControl.java @@ -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 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 listByParent = dataDictionaryService.findListByCode(code, enable); + return JSONObjResult.toJSONObj(listByParent, ""); + } +}