|
|
@@ -0,0 +1,187 @@
|
|
|
+package cn.iocoder.yudao.module.system.service.dictTenant;
|
|
|
+
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|
|
+import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.dictTenant.vo.data.DictDataTenantPageReqVO;
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.dictTenant.vo.data.DictDataTenantSaveReqVO;
|
|
|
+
|
|
|
+import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictTypeDO;
|
|
|
+import cn.iocoder.yudao.module.system.dal.dataobject.dictTenant.DictDataTenantDO;
|
|
|
+import cn.iocoder.yudao.module.system.dal.dataobject.dictTenant.DictTypeTenantDO;
|
|
|
+import cn.iocoder.yudao.module.system.dal.mysql.dictTenant.DictDataTenantMapper;
|
|
|
+import com.google.common.annotations.VisibleForTesting;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import javax.annotation.Resource;
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+
|
|
|
+import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
|
|
+import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
|
|
+
|
|
|
+
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * 租户字典数据 Service 实现类
|
|
|
+ *
|
|
|
+ * @author 芋道源码
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Validated
|
|
|
+public class DictDataTenantServiceImpl implements DictDataTenantService {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 排序 dictType > sort
|
|
|
+ */
|
|
|
+ private static final Comparator<DictDataTenantDO> COMPARATOR_TYPE_AND_SORT = Comparator
|
|
|
+ .comparing(DictDataTenantDO::getDictType)
|
|
|
+ .thenComparingInt(DictDataTenantDO::getSort);
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private DictDataTenantMapper dictDataTenantMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private DictTypeTenantService dictTypeTenantService;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<DictDataTenantDO> getDictDataList(Integer status, String dictType) {
|
|
|
+ List<DictDataTenantDO> list = dictDataTenantMapper.selectListByStatusAndDictType(status, dictType);
|
|
|
+ list.sort(COMPARATOR_TYPE_AND_SORT);
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public DictDataTenantDO getDictData(Long id) {
|
|
|
+ return dictDataTenantMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageResult<DictDataTenantDO> getDictDataPage(DictDataTenantPageReqVO pageReqVO) {
|
|
|
+ return dictDataTenantMapper.selectPage(pageReqVO);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Long createDictData(DictDataTenantSaveReqVO createReqVO) {
|
|
|
+ // 校验字典类型有效
|
|
|
+ validateDictTypeExists(createReqVO.getDictType());
|
|
|
+ // 校验字典数据的值的唯一性
|
|
|
+ validateDictDataValueUnique(null, createReqVO.getDictType(), createReqVO.getValue());
|
|
|
+
|
|
|
+ // 插入字典类型
|
|
|
+ DictDataTenantDO dictData = BeanUtils.toBean(createReqVO, DictDataTenantDO.class);
|
|
|
+ dictDataTenantMapper.insert(dictData);
|
|
|
+ return dictData.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateDictData(DictDataTenantPageReqVO updateReqVO) {
|
|
|
+ // 校验自己存在
|
|
|
+ validateDictDataExists(updateReqVO.getId());
|
|
|
+ // 校验字典类型有效
|
|
|
+ validateDictTypeExists(updateReqVO.getDictType());
|
|
|
+ // 校验字典数据的值的唯一性
|
|
|
+ validateDictDataValueUnique(updateReqVO.getId(), updateReqVO.getDictType(), updateReqVO.getValue());
|
|
|
+
|
|
|
+ // 更新字典类型
|
|
|
+ DictDataTenantDO updateObj = BeanUtils.toBean(updateReqVO, DictDataTenantDO.class);
|
|
|
+ dictDataTenantMapper.updateById(updateObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteDictData(Long id) {
|
|
|
+ // 校验是否存在
|
|
|
+ validateDictDataExists(id);
|
|
|
+
|
|
|
+ // 删除字典数据
|
|
|
+ dictDataTenantMapper.deleteById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public long getDictDataCountByDictType(String dictType) {
|
|
|
+ return dictDataTenantMapper.selectCountByDictType(dictType);
|
|
|
+ }
|
|
|
+
|
|
|
+ @VisibleForTesting
|
|
|
+ public void validateDictDataValueUnique(Long id, String dictType, String value) {
|
|
|
+ DictDataTenantDO dictData = dictDataTenantMapper.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;
|
|
|
+ }
|
|
|
+ DictDataTenantDO dictData = dictDataTenantMapper.selectById(id);
|
|
|
+ if (dictData == null) {
|
|
|
+ throw exception(DICT_DATA_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @VisibleForTesting
|
|
|
+ public void validateDictTypeExists(String type) {
|
|
|
+ DictTypeTenantDO dictType = dictTypeTenantService.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<String> values) {
|
|
|
+ if (CollUtil.isEmpty(values)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Map<String, DictDataTenantDO> dictDataMap = CollectionUtils.convertMap(
|
|
|
+ dictDataTenantMapper.selectByDictTypeAndValues(dictType, values), DictDataTenantDO::getValue);
|
|
|
+ // 校验
|
|
|
+ values.forEach(value -> {
|
|
|
+ DictDataTenantDO 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 DictDataTenantDO getDictData(String dictType, String value) {
|
|
|
+ return dictDataTenantMapper.selectByDictTypeAndValue(dictType, value);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public DictDataTenantDO parseDictData(String dictType, String label) {
|
|
|
+ return dictDataTenantMapper.selectByDictTypeAndLabel(dictType, label);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<DictDataTenantDO> getDictDataListByDictType(String dictType) {
|
|
|
+ List<DictDataTenantDO> list = dictDataTenantMapper.selectList(DictDataTenantDO::getDictType, dictType);
|
|
|
+ list.sort(Comparator.comparing(DictDataTenantDO::getSort));
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|