¶Ô±ÈÐÂÎļþ |
| | |
| | | package org.dromara.workflow.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 lombok.RequiredArgsConstructor; |
| | | import org.dromara.common.core.constant.SystemConstants; |
| | | import org.dromara.common.core.exception.ServiceException; |
| | | import org.dromara.common.core.utils.*; |
| | | import org.dromara.common.mybatis.helper.DataBaseHelper; |
| | | import org.dromara.common.satoken.utils.LoginHelper; |
| | | import org.dromara.warm.flow.core.service.DefService; |
| | | import org.dromara.warm.flow.orm.entity.FlowDefinition; |
| | | import org.dromara.workflow.common.ConditionalOnEnable; |
| | | import org.dromara.workflow.common.constant.FlowConstant; |
| | | import org.dromara.workflow.domain.FlowCategory; |
| | | import org.dromara.workflow.domain.bo.FlowCategoryBo; |
| | | import org.dromara.workflow.domain.vo.FlowCategoryVo; |
| | | import org.dromara.workflow.mapper.FlwCategoryMapper; |
| | | import org.dromara.workflow.service.IFlwCategoryService; |
| | | import org.springframework.cache.annotation.CacheEvict; |
| | | import org.springframework.cache.annotation.Cacheable; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * æµç¨åç±»Serviceä¸å¡å±å¤ç |
| | | * |
| | | * @author may |
| | | */ |
| | | @ConditionalOnEnable |
| | | @RequiredArgsConstructor |
| | | @Service |
| | | public class FlwCategoryServiceImpl implements IFlwCategoryService { |
| | | |
| | | private final DefService defService; |
| | | private final FlwCategoryMapper baseMapper; |
| | | |
| | | /** |
| | | * æ¥è¯¢æµç¨åç±» |
| | | * |
| | | * @param categoryId ä¸»é® |
| | | * @return æµç¨åç±» |
| | | */ |
| | | @Override |
| | | public FlowCategoryVo queryById(Long categoryId) { |
| | | FlowCategoryVo category = baseMapper.selectVoById(categoryId); |
| | | if (ObjectUtil.isNull(category)) { |
| | | return null; |
| | | } |
| | | FlowCategoryVo parentCategory = baseMapper.selectVoOne(new LambdaQueryWrapper<FlowCategory>() |
| | | .select(FlowCategory::getCategoryName).eq(FlowCategory::getCategoryId, category.getParentId())); |
| | | category.setParentName(ObjectUtils.notNullGetter(parentCategory, FlowCategoryVo::getCategoryName)); |
| | | return category; |
| | | } |
| | | |
| | | /** |
| | | * æ ¹æ®æµç¨åç±»IDæ¥è¯¢æµç¨åç±»åç§° |
| | | * |
| | | * @param categoryId æµç¨åç±»ID |
| | | * @return æµç¨åç±»åç§° |
| | | */ |
| | | @Cacheable(cacheNames = FlowConstant.FLOW_CATEGORY_NAME, key = "#categoryId") |
| | | @Override |
| | | public String selectCategoryNameById(Long categoryId) { |
| | | if (ObjectUtil.isNull(categoryId)) { |
| | | return null; |
| | | } |
| | | FlowCategory category = baseMapper.selectOne(new LambdaQueryWrapper<FlowCategory>() |
| | | .select(FlowCategory::getCategoryName).eq(FlowCategory::getCategoryId, categoryId)); |
| | | return ObjectUtils.notNullGetter(category, FlowCategory::getCategoryName); |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢ç¬¦åæ¡ä»¶çæµç¨åç±»å表 |
| | | * |
| | | * @param bo æ¥è¯¢æ¡ä»¶ |
| | | * @return æµç¨åç±»å表 |
| | | */ |
| | | @Override |
| | | public List<FlowCategoryVo> queryList(FlowCategoryBo bo) { |
| | | LambdaQueryWrapper<FlowCategory> lqw = buildQueryWrapper(bo); |
| | | return baseMapper.selectVoList(lqw); |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢æµç¨åç±»æ ç»æä¿¡æ¯ |
| | | * |
| | | * @param category æµç¨åç±»ä¿¡æ¯ |
| | | * @return æµç¨åç±»æ ä¿¡æ¯éå |
| | | */ |
| | | @Override |
| | | public List<Tree<String>> selectCategoryTreeList(FlowCategoryBo category) { |
| | | LambdaQueryWrapper<FlowCategory> lqw = buildQueryWrapper(category); |
| | | List<FlowCategoryVo> categorys = baseMapper.selectVoList(lqw); |
| | | if (CollUtil.isEmpty(categorys)) { |
| | | return CollUtil.newArrayList(); |
| | | } |
| | | // è·åå½ååè¡¨ä¸æ¯ä¸ä¸ªèç¹çparentIdï¼ç¶åå¨åè¡¨ä¸æ¥æ¾æ¯å¦æidä¸å
¶parentId对åºï¼è¥æ 对åºï¼åè¡¨ææ¤æ¶èç¹å表ä¸ï¼è¯¥èç¹å¨å½åå表ä¸å±äºé¡¶çº§èç¹ |
| | | List<Tree<String>> treeList = CollUtil.newArrayList(); |
| | | for (FlowCategoryVo d : categorys) { |
| | | String parentId = d.getParentId().toString(); |
| | | FlowCategoryVo categoryVo = StreamUtils.findFirst(categorys, it -> it.getCategoryId().toString().equals(parentId)); |
| | | if (ObjectUtil.isNull(categoryVo)) { |
| | | List<Tree<String>> trees = TreeBuildUtils.build(categorys, parentId, (dept, tree) -> |
| | | tree.setId(dept.getCategoryId().toString()) |
| | | .setParentId(dept.getParentId().toString()) |
| | | .setName(dept.getCategoryName()) |
| | | .setWeight(dept.getOrderNum())); |
| | | Tree<String> tree = StreamUtils.findFirst(trees, it -> it.getId().equals(d.getCategoryId().toString())); |
| | | treeList.add(tree); |
| | | } |
| | | } |
| | | return treeList; |
| | | } |
| | | |
| | | /** |
| | | * æ ¡éªæµç¨åç±»æ¯å¦ææ°æ®æé |
| | | * |
| | | * @param categoryId æµç¨åç±»ID |
| | | */ |
| | | @Override |
| | | public void checkCategoryDataScope(Long categoryId) { |
| | | if (ObjectUtil.isNull(categoryId)) { |
| | | return; |
| | | } |
| | | if (LoginHelper.isSuperAdmin()) { |
| | | return; |
| | | } |
| | | if (baseMapper.countCategoryById(categoryId) == 0) { |
| | | throw new ServiceException("没ææéè®¿é®æµç¨åç±»æ°æ®ï¼"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * æ ¡éªæµç¨åç±»åç§°æ¯å¦å¯ä¸ |
| | | * |
| | | * @param category æµç¨åç±»ä¿¡æ¯ |
| | | * @return ç»æ |
| | | */ |
| | | @Override |
| | | public boolean checkCategoryNameUnique(FlowCategoryBo category) { |
| | | boolean exist = baseMapper.exists(new LambdaQueryWrapper<FlowCategory>() |
| | | .eq(FlowCategory::getCategoryName, category.getCategoryName()) |
| | | .eq(FlowCategory::getParentId, category.getParentId()) |
| | | .ne(ObjectUtil.isNotNull(category.getCategoryId()), FlowCategory::getCategoryId, category.getCategoryId())); |
| | | return !exist; |
| | | } |
| | | |
| | | /** |
| | | * æ¥è¯¢æµç¨åç±»æ¯å¦å卿µç¨å®ä¹ |
| | | * |
| | | * @param categoryId æµç¨åç±»ID |
| | | * @return ç»æ true åå¨ false ä¸åå¨ |
| | | */ |
| | | @Override |
| | | public boolean checkCategoryExistDefinition(Long categoryId) { |
| | | FlowDefinition definition = new FlowDefinition(); |
| | | definition.setCategory(categoryId.toString()); |
| | | return defService.exists(definition); |
| | | } |
| | | |
| | | /** |
| | | * æ¯å¦å卿µç¨åç±»åèç¹ |
| | | * |
| | | * @param categoryId æµç¨åç±»ID |
| | | * @return ç»æ |
| | | */ |
| | | @Override |
| | | public boolean hasChildByCategoryId(Long categoryId) { |
| | | return baseMapper.exists(new LambdaQueryWrapper<FlowCategory>() |
| | | .eq(FlowCategory::getParentId, categoryId)); |
| | | } |
| | | |
| | | private LambdaQueryWrapper<FlowCategory> buildQueryWrapper(FlowCategoryBo bo) { |
| | | LambdaQueryWrapper<FlowCategory> lqw = Wrappers.lambdaQuery(); |
| | | lqw.eq(FlowCategory::getDelFlag, SystemConstants.NORMAL); |
| | | lqw.eq(ObjectUtil.isNotNull(bo.getCategoryId()), FlowCategory::getCategoryId, bo.getCategoryId()); |
| | | lqw.eq(ObjectUtil.isNotNull(bo.getParentId()), FlowCategory::getParentId, bo.getParentId()); |
| | | lqw.like(StringUtils.isNotBlank(bo.getCategoryName()), FlowCategory::getCategoryName, bo.getCategoryName()); |
| | | lqw.orderByAsc(FlowCategory::getAncestors); |
| | | lqw.orderByAsc(FlowCategory::getParentId); |
| | | lqw.orderByAsc(FlowCategory::getOrderNum); |
| | | lqw.orderByAsc(FlowCategory::getCategoryId); |
| | | return lqw; |
| | | } |
| | | |
| | | /** |
| | | * æ°å¢æµç¨åç±» |
| | | * |
| | | * @param bo æµç¨åç±» |
| | | * @return æ¯å¦æ°å¢æå |
| | | */ |
| | | @Override |
| | | public int insertByBo(FlowCategoryBo bo) { |
| | | FlowCategory info = baseMapper.selectById(bo.getParentId()); |
| | | FlowCategory category = MapstructUtils.convert(bo, FlowCategory.class); |
| | | category.setAncestors(info.getAncestors() + StringUtils.SEPARATOR + category.getParentId()); |
| | | return baseMapper.insert(category); |
| | | } |
| | | |
| | | /** |
| | | * ä¿®æ¹æµç¨åç±» |
| | | * |
| | | * @param bo æµç¨åç±» |
| | | * @return æ¯å¦ä¿®æ¹æå |
| | | */ |
| | | @CacheEvict(cacheNames = FlowConstant.FLOW_CATEGORY_NAME, key = "#bo.categoryId") |
| | | @Override |
| | | public int updateByBo(FlowCategoryBo bo) { |
| | | FlowCategory category = MapstructUtils.convert(bo, FlowCategory.class); |
| | | FlowCategory oldCategory = baseMapper.selectById(category.getCategoryId()); |
| | | if (ObjectUtil.isNull(oldCategory)) { |
| | | throw new ServiceException("æµç¨åç±»ä¸åå¨ï¼æ æ³ä¿®æ¹"); |
| | | } |
| | | if (!oldCategory.getParentId().equals(category.getParentId())) { |
| | | // å¦ææ¯æ°ç¶æµç¨åç±» åæ ¡éªæ¯å¦å
·ææ°ç¶æµç¨åç±»æé é¿å
è¶æ |
| | | this.checkCategoryDataScope(category.getParentId()); |
| | | FlowCategory newParentCategory = baseMapper.selectById(category.getParentId()); |
| | | if (ObjectUtil.isNotNull(newParentCategory)) { |
| | | String newAncestors = newParentCategory.getAncestors() + StringUtils.SEPARATOR + newParentCategory.getCategoryId(); |
| | | String oldAncestors = oldCategory.getAncestors(); |
| | | category.setAncestors(newAncestors); |
| | | updateCategoryChildren(category.getCategoryId(), newAncestors, oldAncestors); |
| | | } |
| | | } else { |
| | | category.setAncestors(oldCategory.getAncestors()); |
| | | } |
| | | return baseMapper.updateById(category); |
| | | } |
| | | |
| | | /** |
| | | * ä¿®æ¹åå
ç´ å
³ç³» |
| | | * |
| | | * @param categoryId 被修æ¹çæµç¨åç±»ID |
| | | * @param newAncestors æ°çç¶IDéå |
| | | * @param oldAncestors æ§çç¶IDéå |
| | | */ |
| | | private void updateCategoryChildren(Long categoryId, String newAncestors, String oldAncestors) { |
| | | List<FlowCategory> children = baseMapper.selectList(new LambdaQueryWrapper<FlowCategory>() |
| | | .apply(DataBaseHelper.findInSet(categoryId, "ancestors"))); |
| | | List<FlowCategory> list = new ArrayList<>(); |
| | | for (FlowCategory child : children) { |
| | | FlowCategory category = new FlowCategory(); |
| | | category.setCategoryId(child.getCategoryId()); |
| | | category.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors)); |
| | | list.add(category); |
| | | } |
| | | if (CollUtil.isNotEmpty(list)) { |
| | | baseMapper.updateBatchById(list); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å 餿µç¨åç±»ä¿¡æ¯ |
| | | * |
| | | * @param categoryId ä¸»é® |
| | | * @return æ¯å¦å 餿å |
| | | */ |
| | | @CacheEvict(cacheNames = FlowConstant.FLOW_CATEGORY_NAME, key = "#categoryId") |
| | | @Override |
| | | public int deleteWithValidById(Long categoryId) { |
| | | return baseMapper.deleteById(categoryId); |
| | | } |
| | | } |