|
@@ -1,11 +1,16 @@
|
|
|
package org.dromara.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.lang.tree.Tree;
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
-import org.dromara.common.core.utils.MapstructUtils;
|
|
|
-import org.dromara.common.core.utils.StringUtils;
|
|
|
+import org.dromara.common.core.constant.SystemConstants;
|
|
|
+import org.dromara.common.core.domain.R;
|
|
|
+import org.dromara.common.core.exception.ServiceException;
|
|
|
+import org.dromara.common.core.utils.*;
|
|
|
import org.dromara.common.mybatis.core.page.PageQuery;
|
|
|
import org.dromara.common.mybatis.core.page.TableDataInfo;
|
|
|
import org.dromara.domain.asset.AssetType;
|
|
@@ -19,6 +24,8 @@ import java.util.Collection;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
+import static org.dromara.common.core.constant.Constants.SUCCESS;
|
|
|
+
|
|
|
/**
|
|
|
* 资产类型Service业务层处理
|
|
|
*
|
|
@@ -88,6 +95,23 @@ public class AssetTypeServiceImpl implements IAssetTypeService {
|
|
|
*/
|
|
|
@Override
|
|
|
public Boolean insertByBo(AssetTypeBo bo) {
|
|
|
+ bo.setCreateTime(DateUtils.getNowDate());
|
|
|
+ AssetTypeVo info = baseMapper.selectAssetTypeByAssetId(bo.getParentId());
|
|
|
+ int i = 0;
|
|
|
+ if (info != null) {
|
|
|
+ // 如果父节点不为正常状态,则不允许新增子节点
|
|
|
+ // 如果父节点不为正常状态,则不允许新增子节点
|
|
|
+ if (!SUCCESS.equals(info.getStatus())) {
|
|
|
+ throw new ServiceException("部门停用,不允许新增");
|
|
|
+ }
|
|
|
+ bo.setAncestors(info.getAncestors() + "," + bo.getParentId());
|
|
|
+ bo.setCreateTime(DateUtils.getNowDate());
|
|
|
+ } else {
|
|
|
+ bo.setParentId(0L);
|
|
|
+ bo.setAncestors("0");
|
|
|
+ bo.setCreateTime(DateUtils.getNowDate());
|
|
|
+ }
|
|
|
+
|
|
|
AssetType add = MapstructUtils.convert(bo, AssetType.class);
|
|
|
validEntityBeforeSave(add);
|
|
|
boolean flag = baseMapper.insert(add) > 0;
|
|
@@ -105,11 +129,37 @@ public class AssetTypeServiceImpl implements IAssetTypeService {
|
|
|
*/
|
|
|
@Override
|
|
|
public Boolean updateByBo(AssetTypeBo bo) {
|
|
|
+ AssetTypeVo newParentAssetType = baseMapper.selectAssetTypeByAssetId(bo.getParentId());
|
|
|
+ AssetTypeVo oldParentAssetType = baseMapper.selectAssetTypeByAssetId(bo.getAssetId());
|
|
|
+ if (StringUtils.isNotNull(newParentAssetType) && StringUtils.isNotNull(oldParentAssetType)) {
|
|
|
+ String newAncestors = newParentAssetType.getAncestors() + "," + newParentAssetType.getAncestors();
|
|
|
+ String oldAncestors = oldParentAssetType.getAncestors();
|
|
|
+ bo.setAncestors(newAncestors);
|
|
|
+ updateAssetTypeChildren(bo.getAssetId(), newAncestors, oldAncestors);
|
|
|
+ }
|
|
|
+ bo.setUpdateTime(DateUtils.getNowDate());
|
|
|
AssetType update = MapstructUtils.convert(bo, AssetType.class);
|
|
|
validEntityBeforeSave(update);
|
|
|
return baseMapper.updateById(update) > 0;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 修改子元素关系
|
|
|
+ *
|
|
|
+ * @param assetId 被修改的资产ID
|
|
|
+ * @param newAncestors 新的父ID集合
|
|
|
+ * @param oldAncestors 旧的父ID集合
|
|
|
+ */
|
|
|
+ public void updateAssetTypeChildren(Long assetId, String newAncestors, String oldAncestors) {
|
|
|
+ List<AssetTypeVo> children = baseMapper.selectChildrenAssetTypeById(assetId);
|
|
|
+ for (AssetTypeVo child : children) {
|
|
|
+ child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
|
|
|
+ }
|
|
|
+ if (children.size() > 0) {
|
|
|
+ baseMapper.updateAssetTypeChildren(children);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 保存前的数据校验
|
|
|
*/
|
|
@@ -131,4 +181,70 @@ public class AssetTypeServiceImpl implements IAssetTypeService {
|
|
|
}
|
|
|
return baseMapper.deleteByIds(ids) > 0;
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public R<List<Tree<Long>>> selectAssetTypeTreeList(AssetTypeBo bo) {
|
|
|
+ LambdaQueryWrapper<AssetType> lqw = buildQueryWrapper(bo);
|
|
|
+ List<AssetTypeVo> assetTypes = baseMapper.selectVoList(lqw);
|
|
|
+ List<Tree<Long>> assetTypeTree = buildDeptTreeSelect(assetTypes);
|
|
|
+ return R.ok(assetTypeTree);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检验资产名称的唯一性
|
|
|
+ *
|
|
|
+ * @param bo
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public boolean checkAssetTypeNameUnique(AssetTypeBo bo) {
|
|
|
+
|
|
|
+ Long assetId = StringUtils.isNull(bo.getAssetId()) ? -1L : bo.getAssetId();
|
|
|
+ AssetTypeVo info = baseMapper.checkAssetNameUnique(bo.getAssetName(), bo.getParentId());
|
|
|
+ if (StringUtils.isNotNull(info) && info.getAssetId().longValue() != assetId.longValue()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int selectNormalChildrenAssetById(Long assetId) {
|
|
|
+ return baseMapper.selectNormalChildrenAssetById(assetId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 是否存在子节点
|
|
|
+ *
|
|
|
+ * @param assetId 资产ID
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public boolean hasChildByAssetId(Long assetId) {
|
|
|
+ int result = baseMapper.hasChildByAssetId(assetId);
|
|
|
+ return result > 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ private List<Tree<Long>> buildDeptTreeSelect(List<AssetTypeVo> assetTypes) {
|
|
|
+ if (CollUtil.isEmpty(assetTypes)) {
|
|
|
+ return CollUtil.newArrayList();
|
|
|
+ }
|
|
|
+ // 获取当前列表中每一个节点的parentId,然后在列表中查找是否有id与其parentId对应,若无对应,则表明此时节点列表中,该节点在当前列表中属于顶级节点
|
|
|
+ List<Tree<Long>> treeList = CollUtil.newArrayList();
|
|
|
+ for (AssetTypeVo d : assetTypes) {
|
|
|
+ Long parentId = d.getParentId();
|
|
|
+ AssetTypeVo assetTypeVo = StreamUtils.findFirst(assetTypes, it -> it.getAssetId().longValue() == parentId);
|
|
|
+ if (ObjectUtil.isNull(assetTypeVo)) {
|
|
|
+ List<Tree<Long>> trees = TreeBuildUtils.build(assetTypes, parentId, (assetType, tree) ->
|
|
|
+ tree.setId(assetType.getAssetId())
|
|
|
+ .setParentId(assetType.getParentId())
|
|
|
+ .setName(assetType.getAssetName())
|
|
|
+ .setWeight(assetType.getOrderNum())
|
|
|
+ .putExtra("disabled", SystemConstants.DISABLE.equals(assetType.getStatus())));
|
|
|
+ Tree<Long> tree = StreamUtils.findFirst(trees, it -> it.getId().longValue() == d.getAssetId());
|
|
|
+ treeList.add(tree);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return treeList;
|
|
|
+ }
|
|
|
+
|
|
|
}
|