数据字典模块完成
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
package cn.iocoder.mall.admin.dao;
|
||||
|
||||
import cn.iocoder.mall.admin.dataobject.DataDictDO;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface DataDictMapper extends BaseMapper<DataDictDO> {
|
||||
|
||||
DataDictDO selectByEnumValueAndValue(
|
||||
@Param("enumValue") String enumValue,
|
||||
@Param("value") String value
|
||||
);
|
||||
|
||||
List<DataDictDO> selectByEnumValueAndValues(
|
||||
@Param("enumValue") String enumValue,
|
||||
@Param("values") Collection<String> values
|
||||
);
|
||||
|
||||
List<DataDictDO> selectByEnumValue(
|
||||
@Param("enumValue") String enumValue
|
||||
);
|
||||
|
||||
default List<DataDictDO> selectList() {
|
||||
return selectList(new QueryWrapper<>());
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package cn.iocoder.mall.admin.dataobject;
|
||||
|
||||
import cn.iocoder.common.framework.dataobject.DeletableDO;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
/**
|
||||
* 数据字典
|
||||
*
|
||||
* 使用 {@link #enumValue} 作为聚合。例如说:
|
||||
*
|
||||
* enumValue :gender 性别
|
||||
* value:1 男
|
||||
* value:2 女
|
||||
*/
|
||||
@TableName("data_dict")
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class DataDictDO extends DeletableDO {
|
||||
|
||||
/**
|
||||
* 编号
|
||||
*/
|
||||
private Integer id;
|
||||
/**
|
||||
* 大类枚举值
|
||||
*/
|
||||
private String enumValue;
|
||||
/**
|
||||
* 小类数值
|
||||
*/
|
||||
private String value;
|
||||
/**
|
||||
* 展示名
|
||||
*/
|
||||
private String displayName;
|
||||
/**
|
||||
* 排序值
|
||||
*/
|
||||
private Integer sort;
|
||||
/**
|
||||
* 备注
|
||||
*/
|
||||
private String memo;
|
||||
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
package cn.iocoder.mall.admin.service;
|
||||
|
||||
import cn.iocoder.common.framework.enums.DeletedStatusEnum;
|
||||
import cn.iocoder.common.framework.util.ServiceExceptionUtil;
|
||||
import cn.iocoder.common.framework.vo.CommonResult;
|
||||
import cn.iocoder.mall.system.api.DataDictService;
|
||||
import cn.iocoder.mall.system.api.bo.datadict.DataDictBO;
|
||||
import cn.iocoder.mall.system.api.constant.AdminErrorCodeEnum;
|
||||
import cn.iocoder.mall.system.api.dto.datadict.DataDictAddDTO;
|
||||
import cn.iocoder.mall.system.api.dto.datadict.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.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 数据字典 Service
|
||||
*/
|
||||
@Service
|
||||
@org.apache.dubbo.config.annotation.Service(validation = "true", version = "${dubbo.provider.DataDictService.version}")
|
||||
public class DataDictServiceImpl implements DataDictService {
|
||||
|
||||
@Autowired
|
||||
private DataDictMapper dataDictMapper;
|
||||
|
||||
@Override
|
||||
public List<DataDictBO> selectDataDictList() {
|
||||
List<DataDictDO> dataDicts = dataDictMapper.selectList();
|
||||
return DataDictConvert.INSTANCE.convert(dataDicts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataDictBO addDataDict(Integer adminId, DataDictAddDTO dataDictAddDTO) {
|
||||
// 校验数据字典重复
|
||||
if (dataDictMapper.selectByEnumValueAndValue(dataDictAddDTO.getEnumValue(), dataDictAddDTO.getValue()) != null) {
|
||||
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DATA_DICT_EXISTS.getCode());
|
||||
}
|
||||
// 保存到数据库
|
||||
DataDictDO dataDict = DataDictConvert.INSTANCE.convert(dataDictAddDTO);
|
||||
dataDict.setCreateTime(new Date());
|
||||
dataDict.setDeleted(DeletedStatusEnum.DELETED_NO.getValue());
|
||||
dataDictMapper.insert(dataDict);
|
||||
// TODO 插入操作日志
|
||||
// 返回成功
|
||||
return DataDictConvert.INSTANCE.convert(dataDict);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean updateDataDict(Integer adminId, DataDictUpdateDTO dataDictUpdateDTO) {
|
||||
// 校验数据字典不存在
|
||||
DataDictDO existsDataDict = dataDictMapper.selectById(dataDictUpdateDTO.getId());
|
||||
if (existsDataDict == null) {
|
||||
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DATA_DICT_NOT_EXISTS.getCode());
|
||||
}
|
||||
// 校验数据字典重复
|
||||
DataDictDO duplicateDataDict = dataDictMapper.selectByEnumValueAndValue(existsDataDict.getEnumValue(), dataDictUpdateDTO.getValue());
|
||||
if (duplicateDataDict != null && !duplicateDataDict.getId().equals(dataDictUpdateDTO.getId())) {
|
||||
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DATA_DICT_EXISTS.getCode());
|
||||
}
|
||||
// 更新到数据库
|
||||
DataDictDO updateDataDict = DataDictConvert.INSTANCE.convert(dataDictUpdateDTO);
|
||||
dataDictMapper.updateById(updateDataDict);
|
||||
// TODO 插入操作日志
|
||||
// 返回成功
|
||||
return true;
|
||||
}
|
||||
|
||||
// 一般情况下,不要删除数据字典。
|
||||
// 因为,业务数据正在使用该数据字典,删除后,可能有不可预知的问题。
|
||||
@Override
|
||||
public Boolean deleteDataDict(Integer adminId, Integer dataDictId) {
|
||||
// 校验数据字典不存在
|
||||
DataDictDO existsDataDict = dataDictMapper.selectById(dataDictId);
|
||||
if (existsDataDict == null) {
|
||||
throw ServiceExceptionUtil.exception(AdminErrorCodeEnum.DATA_DICT_NOT_EXISTS.getCode());
|
||||
}
|
||||
// 标记删除
|
||||
dataDictMapper.deleteById(dataDictId);
|
||||
// TODO 插入操作日志
|
||||
// 返回成功
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<DataDictBO> getDataDict(String dictKey, Object dictValue) {
|
||||
DataDictDO dataDictDO = dataDictMapper.selectByEnumValueAndValue(dictKey, String.valueOf(dictValue));
|
||||
DataDictBO dataDictBO = DataDictConvert.INSTANCE.convert(dataDictDO);
|
||||
return CommonResult.success(dataDictBO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<List<DataDictBO>> getDataDict(String dictKey) {
|
||||
List<DataDictDO> dataDictDOList = dataDictMapper.selectByEnumValue(dictKey);
|
||||
List<DataDictBO> dataDictBOList = DataDictConvert.INSTANCE.convert(dataDictDOList);
|
||||
return CommonResult.success(dataDictBOList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<List<DataDictBO>> getDataDictList(String dictKey, Collection<?> dictValueList) {
|
||||
Set<String> convertDictValueList = dictValueList.stream().map(String::valueOf).collect(Collectors.toSet());
|
||||
List<DataDictDO> dataDictDOList = dataDictMapper.selectByEnumValueAndValues(dictKey, convertDictValueList);
|
||||
List<DataDictBO> dataDictBOList = DataDictConvert.INSTANCE.convert(dataDictDOList);
|
||||
return CommonResult.success(dataDictBOList);
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?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="selectByEnumValueAndValues" resultType="cn.iocoder.mall.admin.dataobject.DataDictDO">
|
||||
SELECT
|
||||
<include refid="FIELDS"/>
|
||||
FROM data_dict
|
||||
WHERE deleted = 0
|
||||
AND enum_value = #{enumValue}
|
||||
AND `value` IN
|
||||
<foreach collection="values" item="value" separator="," open="(" close=")">
|
||||
#{value}
|
||||
</foreach>
|
||||
</select>
|
||||
|
||||
<select id="selectByEnumValue" resultType="cn.iocoder.mall.admin.dataobject.DataDictDO">
|
||||
SELECT
|
||||
<include refid="FIELDS"/>
|
||||
FROM data_dict
|
||||
WHERE deleted = 0
|
||||
AND enum_value = #{enumValue}
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user