org.openea.eap.module.system.service.dict.DictDataServiceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eap-module-system-biz Show documentation
Show all versions of eap-module-system-biz Show documentation
system 模块下,我们放通用业务,支撑上层的核心业务。
例如说:用户、部门、权限、数据字典等等
The newest version!
package org.openea.eap.module.system.service.dict;
import cn.hutool.core.collection.CollUtil;
import org.openea.eap.framework.common.enums.CommonStatusEnum;
import org.openea.eap.framework.common.pojo.PageResult;
import org.openea.eap.framework.common.util.collection.CollectionUtils;
import org.openea.eap.framework.common.util.object.BeanUtils;
import org.openea.eap.module.system.controller.admin.dict.vo.data.DictDataExportReqVO;
import org.openea.eap.module.system.controller.admin.dict.vo.data.DictDataPageReqVO;
import org.openea.eap.module.system.controller.admin.dict.vo.data.DictDataSaveReqVO;
import org.openea.eap.module.system.dal.dataobject.dict.DictDataDO;
import org.openea.eap.module.system.dal.dataobject.dict.DictTypeDO;
import org.openea.eap.module.system.dal.mysql.dict.DictDataMapper;
import com.google.common.annotations.VisibleForTesting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import static org.openea.eap.framework.common.exception.util.ServiceExceptionUtil.exception;
import static org.openea.eap.module.system.enums.ErrorCodeConstants.*;
/**
* 字典数据 Service 实现类
*
* @author ruoyi
*/
//@Service
@Slf4j
public class DictDataServiceImpl implements DictDataService {
/**
* 排序 dictType > sort
*/
private static final Comparator COMPARATOR_TYPE_AND_SORT = Comparator
.comparing(DictDataDO::getDictType)
.thenComparingInt(DictDataDO::getSort);
@Resource
protected DictTypeService dictTypeService;
@Resource
protected DictDataMapper dictDataMapper;
@Override
public List getDictDataList(Integer status, String dictType) {
try{
List list = dictDataMapper.selectListByStatusAndDictType(status, dictType);
list.sort(COMPARATOR_TYPE_AND_SORT);
return list;
} catch (Exception e) {
log.warn("getDictDataList fail dictType={}, error={}", dictType, e.getMessage());
}
return Collections.emptyList();
}
@Override
public Map getMapDictData(String dictType) {
List list = dictDataMapper.selectByDictType(dictType);
list.sort(COMPARATOR_TYPE_AND_SORT);
Map map = new HashMap<>();
list.forEach(dictDataDO -> {
map.put(dictDataDO.getValue(), dictDataDO.getLabel());
});
return map;
}
@Override
public PageResult getDictDataPage(DictDataPageReqVO pageReqVO) {
return dictDataMapper.selectPage(pageReqVO);
}
@Override
public List getDictDataList(DictDataExportReqVO reqVO) {
List list = dictDataMapper.selectList(reqVO);
list.sort(COMPARATOR_TYPE_AND_SORT);
return list;
}
/**
* 获得字典数据列表
*
* @return 字典数据全列表
*/
@Override
public List getDictDataList() {
List list = dictDataMapper.selectList();
list.sort(COMPARATOR_TYPE_AND_SORT);
return list;
}
@Override
public DictDataDO getDictData(Long id) {
return dictDataMapper.selectById(id);
}
@Override
public Long createDictData(DictDataSaveReqVO createReqVO) {
// 校验字典类型有效
validateDictTypeExists(createReqVO.getDictType());
// 校验字典数据的值的唯一性
validateDictDataValueUnique(null, createReqVO.getDictType(), createReqVO.getValue());
// 插入字典类型
DictDataDO dictData = BeanUtils.toBean(createReqVO, DictDataDO.class);
dictDataMapper.insert(dictData);
return dictData.getId();
}
@Override
public void updateDictData(DictDataSaveReqVO updateReqVO) {
// 校验自己存在
validateDictDataExists(updateReqVO.getId());
// 校验字典类型有效
validateDictTypeExists(updateReqVO.getDictType());
// 校验字典数据的值的唯一性
validateDictDataValueUnique(updateReqVO.getId(), updateReqVO.getDictType(), updateReqVO.getValue());
// 更新字典类型
DictDataDO updateObj = BeanUtils.toBean(updateReqVO, DictDataDO.class);
dictDataMapper.updateById(updateObj);
}
@Override
public void deleteDictData(Long id) {
// 校验是否存在
validateDictDataExists(id);
// 删除字典数据
dictDataMapper.deleteById(id);
}
@Override
public long getDictDataCountByDictType(String dictType) {
return dictDataMapper.selectCountByDictType(dictType);
}
@VisibleForTesting
public void validateDictDataValueUnique(Long id, String dictType, String value) {
DictDataDO dictData = dictDataMapper.selectByDictTypeAndValue(dictType, value);
if (dictData == null) {
return;
}
// 如果 id 为空,说明不用比较是否为相同 id 的字典数据
if (id == null) {
throw exception(DICT_DATA_VALUE_DUPLICATE);
}
if (!dictData.getId().equals(id)) {
throw exception(DICT_DATA_VALUE_DUPLICATE);
}
}
@VisibleForTesting
public void validateDictDataExists(Long id) {
if (id == null) {
return;
}
DictDataDO dictData = dictDataMapper.selectById(id);
if (dictData == null) {
throw exception(DICT_DATA_NOT_EXISTS);
}
}
@VisibleForTesting
public void validateDictTypeExists(String type) {
DictTypeDO dictType = dictTypeService.getDictType(type);
if (dictType == null) {
throw exception(DICT_TYPE_NOT_EXISTS);
}
if (!CommonStatusEnum.ENABLE.getStatus().equals(dictType.getStatus())) {
throw exception(DICT_TYPE_NOT_ENABLE);
}
}
@Override
public void validateDictDataList(String dictType, Collection values) {
if (CollUtil.isEmpty(values)) {
return;
}
Map dictDataMap = CollectionUtils.convertMap(
dictDataMapper.selectByDictTypeAndValues(dictType, values), DictDataDO::getValue);
// 校验
values.forEach(value -> {
DictDataDO dictData = dictDataMap.get(value);
if (dictData == null) {
throw exception(DICT_DATA_NOT_EXISTS);
}
if (!CommonStatusEnum.ENABLE.getStatus().equals(dictData.getStatus())) {
throw exception(DICT_DATA_NOT_ENABLE, dictData.getLabel());
}
});
}
@Override
public DictDataDO getDictData(String dictType, String value) {
return dictDataMapper.selectByDictTypeAndValue(dictType, value);
}
@Override
public DictDataDO parseDictData(String dictType, String label) {
return dictDataMapper.selectByDictTypeAndLabel(dictType, label);
}
@Override
public List getDictData(String dictType) {
return dictDataMapper.selectByDictType(dictType);
}
@Override
public List getDictDataListByDictType(String dictType) {
List list = dictDataMapper.selectList(DictDataDO::getDictType, dictType);
list.sort(Comparator.comparing(DictDataDO::getSort));
return list;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy