数据字典模块部分提交

This commit is contained in:
YunaiV 2019-03-03 12:54:03 +08:00
parent 5b07634cfd
commit 8089319f64
20 changed files with 759 additions and 7 deletions

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.admin.application.controller;
package cn.iocoder.mall.admin.application.controller.admins;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.AdminService;

View File

@ -0,0 +1,57 @@
package cn.iocoder.mall.admin.application.controller.admins;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.DataDictService;
import cn.iocoder.mall.admin.api.bo.DataDictBO;
import cn.iocoder.mall.admin.api.dto.DataDictAddDTO;
import cn.iocoder.mall.admin.application.convert.DataDictConvert;
import cn.iocoder.mall.admin.application.vo.DataDictVO;
import cn.iocoder.mall.admin.sdk.context.AdminSecurityContextHolder;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("admins/data_dict")
@Api("数据字典模块")
public class DataDictController {
@Reference(validation = "true")
private DataDictService dataDictService;
@GetMapping("/list")
@ApiOperation(value = "数据字典全列表")
public CommonResult<List<DataDictVO>> list() {
CommonResult<List<DataDictBO>> result = dataDictService.selectDataDictList();
return DataDictConvert.INSTANCE.convert(result);
}
@PostMapping("/add")
@ApiOperation(value = "创建数据字典")
@ApiImplicitParams({
@ApiImplicitParam(name = "enumValue", value = "大类枚举值", required = true, example = "gender"),
@ApiImplicitParam(name = "value", value = "小类数值", required = true, example = "1"),
@ApiImplicitParam(name = "displayName", value = "展示名", required = true, example = ""),
@ApiImplicitParam(name = "sort", required = true, value = "排序值", defaultValue = "10"),
@ApiImplicitParam(name = "memo", value = "备注", example = "你猜我猜不猜"),
})
public CommonResult<DataDictVO> add(@RequestParam("enumValue") String enumValue,
@RequestParam("value") String value,
@RequestParam("displayName") String displayName,
@RequestParam("sort") Integer sort,
@RequestParam(value = "memo", required = false) String memo) {
// 创建 DataDictAddDTO 对象
DataDictAddDTO dataDictAddDTO = new DataDictAddDTO().setEnumValue(enumValue).setValue(value).setDisplayName(displayName)
.setSort(sort).setMemo(memo);
// 保存数据字典
CommonResult<DataDictBO> result = dataDictService.addDataDict(AdminSecurityContextHolder.getContext().getAdminId(), dataDictAddDTO);
// 返回结果
return DataDictConvert.INSTANCE.convert2(result);
}
}

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.admin.application.controller;
package cn.iocoder.mall.admin.application.controller.admins;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.OAuth2Service;

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.admin.application.controller;
package cn.iocoder.mall.admin.application.controller.admins;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.ResourceService;

View File

@ -1,4 +1,4 @@
package cn.iocoder.mall.admin.application.controller;
package cn.iocoder.mall.admin.application.controller.admins;
import cn.iocoder.common.framework.util.CollectionUtil;
import cn.iocoder.common.framework.vo.CommonResult;

View File

@ -0,0 +1,29 @@
package cn.iocoder.mall.admin.application.convert;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.bo.DataDictBO;
import cn.iocoder.mall.admin.application.vo.DataDictVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface DataDictConvert {
DataDictConvert INSTANCE = Mappers.getMapper(DataDictConvert.class);
@Mappings({})
DataDictVO convert(DataDictBO dataDictBO);
@Mappings({})
List<DataDictVO> convert(List<DataDictBO> dataDictBOs);
@Mappings({})
CommonResult<List<DataDictVO>> convert(CommonResult<List<DataDictBO>> result);
@Mappings({})
CommonResult<DataDictVO> convert2(CommonResult<DataDictBO> result);
}

View File

@ -0,0 +1,76 @@
package cn.iocoder.mall.admin.application.vo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ApiModel("数据字典 VO")
public class DataDictVO {
@ApiModelProperty(value = "编号", required = true, example = "1")
private Integer id;
@ApiModelProperty(value = "大类枚举值", required = true, example = "gender")
private String enumValue;
@ApiModelProperty(value = "小类数值", required = true, example = "1")
private String value;
@ApiModelProperty(value = "展示名", required = true, example = "")
private String displayName;
@ApiModelProperty(value = "排序值", required = true, example = "10")
private Integer sort;
@ApiModelProperty(value = "备注", example = "你猜")
private Integer memo;
public Integer getId() {
return id;
}
public DataDictVO setId(Integer id) {
this.id = id;
return this;
}
public String getEnumValue() {
return enumValue;
}
public DataDictVO setEnumValue(String enumValue) {
this.enumValue = enumValue;
return this;
}
public String getValue() {
return value;
}
public DataDictVO setValue(String value) {
this.value = value;
return this;
}
public String getDisplayName() {
return displayName;
}
public DataDictVO setDisplayName(String displayName) {
this.displayName = displayName;
return this;
}
public Integer getSort() {
return sort;
}
public DataDictVO setSort(Integer sort) {
this.sort = sort;
return this;
}
public Integer getMemo() {
return memo;
}
public DataDictVO setMemo(Integer memo) {
this.memo = memo;
return this;
}
}

View File

@ -0,0 +1,20 @@
package cn.iocoder.mall.admin.api;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.bo.DataDictBO;
import cn.iocoder.mall.admin.api.dto.DataDictAddDTO;
import cn.iocoder.mall.admin.api.dto.DataDictUpdateDTO;
import java.util.List;
public interface DataDictService {
CommonResult<List<DataDictBO>> selectDataDictList();
CommonResult<DataDictBO> addDataDict(Integer adminId, DataDictAddDTO dataDictAddDTO);
CommonResult<Boolean> updateDataDict(Integer adminId, DataDictUpdateDTO dataDictUpdateDTO);
CommonResult<Boolean> deleteDataDict(Integer adminId, Integer dataDictId);
}

View File

@ -0,0 +1,102 @@
package cn.iocoder.mall.admin.api.bo;
import java.util.Date;
/**
* 数据字典 BO
*/
public class DataDictBO {
/**
* 编号
*/
private Integer id;
/**
* 大类枚举值
*/
private String enumValue;
/**
* 小类数值
*/
private String value;
/**
* 展示名
*/
private String displayName;
/**
* 排序值
*/
private Integer sort;
/**
* 备注
*/
private String memo;
/**
* 创建时间
*/
private Date createTime;
public Integer getId() {
return id;
}
public DataDictBO setId(Integer id) {
this.id = id;
return this;
}
public String getEnumValue() {
return enumValue;
}
public DataDictBO setEnumValue(String enumValue) {
this.enumValue = enumValue;
return this;
}
public String getValue() {
return value;
}
public DataDictBO setValue(String value) {
this.value = value;
return this;
}
public String getDisplayName() {
return displayName;
}
public DataDictBO setDisplayName(String displayName) {
this.displayName = displayName;
return this;
}
public Integer getSort() {
return sort;
}
public DataDictBO setSort(Integer sort) {
this.sort = sort;
return this;
}
public String getMemo() {
return memo;
}
public DataDictBO setMemo(String memo) {
this.memo = memo;
return this;
}
public Date getCreateTime() {
return createTime;
}
public DataDictBO setCreateTime(Date createTime) {
this.createTime = createTime;
return this;
}
}

View File

@ -38,6 +38,11 @@ public enum AdminErrorCodeEnum {
// ========== 角色模块 1002004000 ==========
ROLE_NOT_EXISTS(1002004000, "角色不存在"),
ROLE_ASSIGN_RESOURCE_NOT_EXISTS(1002004001, "分配角色资源时,有资源不存在"),
// ========== 数据字典模块 1002005000 ==========
DATA_DICT_EXISTS(1002005000, "该数据字典已经存在"),
DATA_DICT_NOT_EXISTS(1002005001, "该数据字典已经存在"),
;
private final int code;

View File

@ -0,0 +1,81 @@
package cn.iocoder.mall.admin.api.dto;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* 数据字典添加 DTO
*/
public class DataDictAddDTO {
/**
* 大类枚举值
*/
@NotEmpty(message = "大类枚举值不能为空")
private String enumValue;
/**
* 小类数值
*/
@NotEmpty(message = "小类数值不能为空")
private String value;
/**
* 展示名
*/
@NotEmpty(message = "展示名不能为空")
private String displayName;
/**
* 排序值
*/
@NotNull(message = "排序值不能为空")
private Integer sort;
/**
* 备注
*/
private String memo;
public String getEnumValue() {
return enumValue;
}
public DataDictAddDTO setEnumValue(String enumValue) {
this.enumValue = enumValue;
return this;
}
public String getValue() {
return value;
}
public DataDictAddDTO setValue(String value) {
this.value = value;
return this;
}
public String getDisplayName() {
return displayName;
}
public DataDictAddDTO setDisplayName(String displayName) {
this.displayName = displayName;
return this;
}
public Integer getSort() {
return sort;
}
public DataDictAddDTO setSort(Integer sort) {
this.sort = sort;
return this;
}
public String getMemo() {
return memo;
}
public DataDictAddDTO setMemo(String memo) {
this.memo = memo;
return this;
}
}

View File

@ -0,0 +1,81 @@
package cn.iocoder.mall.admin.api.dto;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
/**
* 数据字典更新 DTO
*/
public class DataDictUpdateDTO {
/**
* 编号
*/
@NotNull(message = "编号不能为空")
private Integer id;
/**
* 小类数值
*/
@NotEmpty(message = "小类数值不能为空")
private String value;
/**
* 展示名
*/
@NotEmpty(message = "展示名不能为空")
private String displayName;
/**
* 排序值
*/
@NotNull(message = "排序值不能为空")
private Integer sort;
/**
* 备注
*/
private String memo;
public Integer getId() {
return id;
}
public DataDictUpdateDTO setId(Integer id) {
this.id = id;
return this;
}
public String getValue() {
return value;
}
public DataDictUpdateDTO setValue(String value) {
this.value = value;
return this;
}
public String getDisplayName() {
return displayName;
}
public DataDictUpdateDTO setDisplayName(String displayName) {
this.displayName = displayName;
return this;
}
public Integer getSort() {
return sort;
}
public DataDictUpdateDTO setSort(Integer sort) {
this.sort = sort;
return this;
}
public String getMemo() {
return memo;
}
public DataDictUpdateDTO setMemo(String memo) {
this.memo = memo;
return this;
}
}

View File

@ -0,0 +1,25 @@
package cn.iocoder.mall.admin.convert;
import cn.iocoder.mall.admin.api.bo.DataDictBO;
import cn.iocoder.mall.admin.api.dto.DataDictAddDTO;
import cn.iocoder.mall.admin.api.dto.DataDictUpdateDTO;
import cn.iocoder.mall.admin.dataobject.DataDictDO;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;
import java.util.List;
@Mapper
public interface DataDictConvert {
DataDictConvert INSTANCE = Mappers.getMapper(DataDictConvert.class);
DataDictDO convert(DataDictAddDTO dataDictAddDTO);
DataDictDO convert(DataDictUpdateDTO dataDictUpdateDTO);
DataDictBO convert(DataDictDO dataDictDO);
List<DataDictBO> convert(List<DataDictDO> dataDictDOs);
}

View File

@ -0,0 +1,23 @@
package cn.iocoder.mall.admin.dao;
import cn.iocoder.mall.admin.dataobject.DataDictDO;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface DataDictMapper {
DataDictDO selectById(@Param("id") Integer id);
DataDictDO selectByEnumValueAndValue(@Param("enumValue") String enumValue,
@Param("value") String value);
List<DataDictDO> selectList();
void insert(DataDictDO dataDict);
int update(DataDictDO dataDict);
}

View File

@ -0,0 +1,94 @@
package cn.iocoder.mall.admin.dataobject;
import cn.iocoder.common.framework.dataobject.BaseDO;
/**
* 数据字典
*
* 使用 {@link #enumValue} 作为聚合例如说
*
* enumValue gender 性别
* value1
* value2
*/
public class DataDictDO extends BaseDO {
/**
* 编号
*/
private Integer id;
/**
* 大类枚举值
*/
private String enumValue;
/**
* 小类数值
*/
private String value;
/**
* 展示名
*/
private String displayName;
/**
* 排序值
*/
private Integer sort;
/**
* 备注
*/
private String memo;
public String getEnumValue() {
return enumValue;
}
public DataDictDO setEnumValue(String enumValue) {
this.enumValue = enumValue;
return this;
}
public String getValue() {
return value;
}
public DataDictDO setValue(String value) {
this.value = value;
return this;
}
public String getDisplayName() {
return displayName;
}
public DataDictDO setDisplayName(String displayName) {
this.displayName = displayName;
return this;
}
public Integer getSort() {
return sort;
}
public DataDictDO setSort(Integer sort) {
this.sort = sort;
return this;
}
public String getMemo() {
return memo;
}
public DataDictDO setMemo(String memo) {
this.memo = memo;
return this;
}
public Integer getId() {
return id;
}
public DataDictDO setId(Integer id) {
this.id = id;
return this;
}
}

View File

@ -28,7 +28,7 @@ import java.util.Set;
import java.util.stream.Collectors;
@Service
@com.alibaba.dubbo.config.annotation.Service
@com.alibaba.dubbo.config.annotation.Service(validation = "true")
public class AdminServiceImpl implements AdminService {
@Autowired

View File

@ -0,0 +1,89 @@
package cn.iocoder.mall.admin.service;
import cn.iocoder.common.framework.dataobject.BaseDO;
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.admin.api.DataDictService;
import cn.iocoder.mall.admin.api.bo.DataDictBO;
import cn.iocoder.mall.admin.api.constant.AdminErrorCodeEnum;
import cn.iocoder.mall.admin.api.dto.DataDictAddDTO;
import cn.iocoder.mall.admin.api.dto.DataDictUpdateDTO;
import cn.iocoder.mall.admin.convert.DataDictConvert;
import cn.iocoder.mall.admin.dao.DataDictMapper;
import cn.iocoder.mall.admin.dataobject.DataDictDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
/**
* 数据字典 Service
*/
@Service
@com.alibaba.dubbo.config.annotation.Service(validation = "true")
public class DataDictServiceImpl implements DataDictService {
@Autowired
private DataDictMapper dataDictMapper;
@Override
public CommonResult<List<DataDictBO>> selectDataDictList() {
List<DataDictDO> dataDicts = dataDictMapper.selectList();
return CommonResult.success(DataDictConvert.INSTANCE.convert(dataDicts));
}
@Override
public CommonResult<DataDictBO> addDataDict(Integer adminId, DataDictAddDTO dataDictAddDTO) {
// 校验数据字典重复
if (dataDictMapper.selectByEnumValueAndValue(dataDictAddDTO.getEnumValue(), dataDictAddDTO.getValue()) != null) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.DATA_DICT_EXISTS.getCode());
}
// 保存到数据库
DataDictDO dataDict = DataDictConvert.INSTANCE.convert(dataDictAddDTO);
dataDict.setCreateTime(new Date()).setDeleted(BaseDO.DELETED_NO);
dataDictMapper.insert(dataDict);
// TODO 插入操作日志
// 返回成功
return CommonResult.success(DataDictConvert.INSTANCE.convert(dataDict));
}
@Override
public CommonResult<Boolean> updateDataDict(Integer adminId, DataDictUpdateDTO dataDictUpdateDTO) {
// 校验数据字典不存在
DataDictDO existsDataDict = dataDictMapper.selectById(dataDictUpdateDTO.getId());
if (existsDataDict == null) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.DATA_DICT_NOT_EXISTS.getCode());
}
// 校验数据字典重复
DataDictDO duplicateDataDict = dataDictMapper.selectByEnumValueAndValue(existsDataDict.getEnumValue(), dataDictUpdateDTO.getValue());
if (duplicateDataDict != null && duplicateDataDict.getId().equals(dataDictUpdateDTO.getId())) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.DATA_DICT_EXISTS.getCode());
}
// 更新到数据库
DataDictDO updateDataDict = DataDictConvert.INSTANCE.convert(dataDictUpdateDTO);
dataDictMapper.update(updateDataDict);
// TODO 插入操作日志
// 返回成功
return CommonResult.success(true);
}
// 一般情况下不要删除数据字典
// 因为业务数据正在使用该数据字典删除后可能有不可预知的问题
@Override
public CommonResult<Boolean> deleteDataDict(Integer adminId, Integer dataDictId) {
// 校验数据字典不存在
DataDictDO existsDataDict = dataDictMapper.selectById(dataDictId);
if (existsDataDict == null) {
return ServiceExceptionUtil.error(AdminErrorCodeEnum.DATA_DICT_NOT_EXISTS.getCode());
}
// 更新到数据库
DataDictDO updateDataDict = new DataDictDO().setId(dataDictId);
updateDataDict.setDeleted(BaseDO.DELETED_YES);
dataDictMapper.update(updateDataDict);
// TODO 插入操作日志
// 返回成功
return CommonResult.success(true);
}
}

View File

@ -21,7 +21,7 @@ import java.util.Set;
import java.util.UUID;
@Service
@com.alibaba.dubbo.config.annotation.Service
@com.alibaba.dubbo.config.annotation.Service(validation = "true")
public class OAuth2ServiceImpl implements OAuth2Service {
/**

View File

@ -30,7 +30,7 @@ import java.util.Set;
import java.util.stream.Collectors;
@Service
@com.alibaba.dubbo.config.annotation.Service
@com.alibaba.dubbo.config.annotation.Service(validation = "true")
public class RoleServiceImpl implements RoleService {
@Autowired

View File

@ -0,0 +1,70 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.iocoder.mall.admin.dao.DataDictMapper">
<sql id="FIELDS">
id, enum_value, value, display_name, sort,
memo, create_time
</sql>
<select id="selectByEnumValueAndValue" resultType="DataDictDO">
SELECT
<include refid="FIELDS"/>
FROM data_dict
WHERE enum_value = #{enumValue}
AND value = #{value}
AND deleted = 0
LIMIT 1
</select>
<select id="selectById" resultType="DataDictDO">
SELECT
<include refid="FIELDS"/>
FROM data_dict
WHERE id = #{id}
AND deleted = 0
</select>
<select id="selectList" resultType="DataDictDO">
SELECT
<include refid="FIELDS"/>
FROM data_dict
WHERE deleted = 0
</select>
<insert id="insert" parameterType="DataDictDO" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
INSERT INTO data_dict (
id, enum_value, value, display_name, sort,
memo, create_time, deleted
) VALUES (
#{id}, #{enumValue}, #{value}, #{displayName}, #{sort},
#{memo}, #{createTime}, #{deleted}
)
</insert>
<update id="update" parameterType="DataDictDO">
UPDATE data_dict
<set>
<if test="enumValue != null">
enum_value = #{enumValue},
</if>
<if test="value != null">
value = #{value},
</if>
<if test="displayName != null">
display_name = #{displayName},
</if>
<if test="sort != null">
sort = #{sort},
</if>
<if test="memo != null">
memo = #{memo},
</if>
<if test="deleted != null">
deleted = #{deleted}
</if>
</set>
WHERE id = #{id}
</update>
</mapper>