“linengliang”
2023-08-29 14b1e801322db00b6b0d8dd059b9720adeb21bd8
lxzn-module-eam/src/main/java/org/jeecg/modules/eam/service/impl/EquipmentCategoryServiceImpl.java
@@ -1,19 +1,31 @@
package org.jeecg.modules.eam.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.checkerframework.checker.units.qual.C;
import org.jeecg.common.api.vo.CommonGenericTree;
import org.jeecg.modules.eam.entity.EquipmentCategory;
import org.jeecg.modules.eam.entity.FaultCause;
import org.jeecg.modules.eam.mapper.EquipmentCategoryMapper;
import org.jeecg.modules.eam.service.IEquipmentCategoryService;
import org.jeecg.modules.system.entity.SysDict;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.mapper.SysDictItemMapper;
import org.jeecg.modules.system.mapper.SysDictMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * @Description: mom_eam_equipment_category
@@ -23,6 +35,11 @@
 */
@Service
public class EquipmentCategoryServiceImpl extends ServiceImpl<EquipmentCategoryMapper, EquipmentCategory> implements IEquipmentCategoryService {
    @Autowired
    private SysDictItemMapper sysDictItemMapper;
    @Autowired
    private SysDictMapper sysDictMapper;
    @Override
    public List<CommonGenericTree> loadTree() {
@@ -30,6 +47,8 @@
        List<CommonGenericTree> commonGenericTrees = loadTree(equipmentCategoryList);
        return commonGenericTrees;
    }
    @SuppressWarnings("unchecked")
    public List<CommonGenericTree> loadTree(List<EquipmentCategory> EquipmentCategory) {
@@ -219,5 +238,98 @@
        }
        return list;
    }
    @Transactional(rollbackFor = {Exception.class})
    public void initRoot(){
        SysDict rootSysDict = sysDictMapper.selectOne(new QueryWrapper<SysDict>().eq("dict_code","equipment_category"));
        if(ObjectUtils.isNotNull(rootSysDict)){
            List<SysDictItem> equipRoots = sysDictItemMapper.selectList(new QueryWrapper<SysDictItem>().eq("dict_id",rootSysDict.getId()));
            List<EquipmentCategory> roots = new ArrayList<>();
            for(SysDictItem rootSysDictItem:equipRoots){
                EquipmentCategory root = new EquipmentCategory();
                root.setId("root-"+rootSysDictItem.getItemValue());
                root.setNum(rootSysDictItem.getItemValue());
                root.setName(rootSysDictItem.getItemText());
                root.setEquipmentCategoryUda1(rootSysDictItem.getItemValue());
                root.setParentId("-1");
                root.setDelFlag(0);
                roots.add(root);
            }
            saveOrUpdateBatch(roots);
        }
    }
    @Override
    public List<CommonGenericTree> loadTreeByRecurrence() {
        CommonGenericTree commonGenericTree = new CommonGenericTree();
        commonGenericTree.setKey("-1");
        commonGenericTree.setTitle("设备分类");
        List<CommonGenericTree> children = getChildren(commonGenericTree);
        if(children.size()==0){
            commonGenericTree.setLeaf(true);
        }
        else {
            commonGenericTree.setLeaf(false);
        }
        commonGenericTree.setChildren(children);
        EquipmentCategory equipmentCategory = new EquipmentCategory();
        equipmentCategory.setId("-1");
        equipmentCategory.setName("设备分类");
        equipmentCategory.setNum("root");
        commonGenericTree.setEntity(equipmentCategory);
        List<CommonGenericTree> list = new ArrayList<>();
        list.add(commonGenericTree);
        return list;
    }
    private List<CommonGenericTree>  getChildren (CommonGenericTree commonGenericTree){
        List<CommonGenericTree> children = baseMapper.getTreeByParentId(commonGenericTree.getKey());
        for(CommonGenericTree child:children){
            child.setEntity(baseMapper.selectById(child.getKey()));
            List<CommonGenericTree> superChild = getChildren(child);
            if(superChild.size()==0){
                child.setLeaf(true);
            }
            else {
                child.setLeaf(false);
            }
            child.setChildren(superChild);
        }
        return children;
    }
    @Override
    public Page<EquipmentCategory> getAllChildren(Page<EquipmentCategory> page,EquipmentCategory equipmentCategory) {
        QueryWrapper<EquipmentCategory>queryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(equipmentCategory.getParentId())){
            List<String> ids = new ArrayList<>();
            ids.add(equipmentCategory.getParentId());
            ids = getChildren(equipmentCategory.getParentId(),ids);
            if(ids.size()>1){
                ids.remove(equipmentCategory.getParentId());
                queryWrapper.in("id",ids);
            }
            else{
                queryWrapper.eq("id","-1");
            }
        }
        return baseMapper.selectPage(page,queryWrapper);
    }
    List<String> getChildren(String parentId,List<String> ids){
        List<String> sons = new ArrayList<>();
        for(String id:ids){
            List<String> childrenIds = baseMapper
                    .selectList(new QueryWrapper<EquipmentCategory>()
                            .eq("parent_id",parentId).eq("del_flag",0)).stream()
                    .map(EquipmentCategory::getId)
                    .collect(Collectors.toList());
            sons.addAll(getChildren(id,childrenIds));
            sons.addAll(childrenIds);
        }
        ids.addAll(sons);
        return ids;
    }
}