lyh
2025-06-25 e756af0f5bfd1addbd5d5c145441fb34aad91a28
lxzn-module-dnc/src/main/java/org/jeecg/modules/dnc/service/impl/ProductMixServiceImpl.java
@@ -1,17 +1,19 @@
package org.jeecg.modules.dnc.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.dnc.entity.ProductMix;
import org.jeecg.modules.dnc.entity.*;
import org.jeecg.modules.dnc.mapper.ProductMixMapper;
import org.jeecg.modules.dnc.service.IPermissionStreamNewService;
import org.jeecg.modules.dnc.service.IProductMixService;
import org.jeecg.modules.dnc.service.*;
import org.jeecg.modules.dnc.utils.TreeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.util.*;
import java.util.stream.Collectors;
@Service
public class ProductMixServiceImpl extends ServiceImpl<ProductMixMapper, ProductMix> implements IProductMixService {
@@ -19,6 +21,21 @@
    @Autowired
    private IPermissionStreamNewService permissionStreamNewService;
    @Autowired
    @Lazy
    private IDeviceTypeService deviceTypeService;
    @Autowired
    @Lazy
    private IDeviceManagementService deviceManagementService;
    @Autowired
    @Lazy
    private IDocInfoService docInfoService;
    @Autowired
    @Lazy
    private IDocRelativeService docRelativeService;
    /**
     * 默认结构树查询
     * @return
@@ -111,4 +128,72 @@
        return childrenList;
    }
    /**
     * 查询产品结构树(包括设备类与nc文件)
     * @return
     */
    @Override
    public List<ProductMix> getProductMixTree() {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        List<ProductMix> rawData = new ArrayList<>();
        if (loginUser.getUsername().equals("admin")) {
            //不需要权限过滤
            rawData = permissionStreamNewService.loadProductMixAll(loginUser.getId());
        } else {
            //需要权限过滤
            String productIds = loginUser.getProductionIds();
            if (productIds != null && !productIds.isEmpty()) {
                List<String> productIdList = Arrays.asList(productIds.split(","));
                rawData = permissionStreamNewService.loadProductMix(loginUser.getId(), productIdList);
            }
        }
        List<Long> ids = rawData.stream()
                .filter(p -> p.getType() != null)
                .filter(productMix -> productMix.getType() == 5 || productMix.getType() == 6)
                .map(ProductMix::getId)
                .collect(Collectors.toList());
        if (ids.isEmpty()) {
            return rawData;
        }
        //过滤设备类关联信息
        List<DeviceType> deviceTypeList = deviceTypeService.list(new QueryWrapper<DeviceType>()
                .in("attribution_id", ids));
        List<ProductMix> productMixList = new ArrayList<>();
        deviceTypeList.forEach(item->{
            ProductMix productMix = new ProductMix();
            productMix.setId(Long.parseLong(item.getId()));
            productMix.setParentId(Long.parseLong(item.getAttributionId()));
            DeviceManagement deviceManagement=deviceManagementService.getById(item.getDeviceManagementId());
            productMix.setTreeCode(deviceManagement.getDeviceManagementCode());
            productMix.setTreeName(deviceManagement.getDeviceManagementName());
            productMix.setType(7);
            productMixList.add(productMix);
        });
        rawData.addAll(productMixList);
        //过滤文档关联信息
        List<String> deviceTypeIds = deviceTypeList.stream().map(DeviceType::getId).collect(Collectors.toList());
        List<DocRelative> relativeList = docRelativeService.list(new QueryWrapper<DocRelative>()
                .in("attribution_id", deviceTypeIds));
        List<ProductMix> docList = new ArrayList<>();
        relativeList.forEach(item->{
            ProductMix productMix = new ProductMix();
            productMix.setId(Long.parseLong(item.getId()));
            productMix.setParentId(Long.parseLong(item.getAttributionId()));
            DocInfo docInfo = docInfoService.getById(item.getDocId());
            productMix.setTreeCode(docInfo.getDocSuffix());
            productMix.setTreeName(docInfo.getDocName());
            productMix.setType(99);
            docList.add(productMix);
        });
        rawData.addAll(docList);
        TreeBuilder builder = new TreeBuilder();
        TreeBuilder.CleanResult cleanResult = builder.preprocessData(rawData);
        List<ProductMix> sorted = builder.topologicalSort(
                cleanResult.getValidNodes(),
                cleanResult.getNodeMap()
        );
        List<ProductMix> result =builder.assembleTree(sorted, cleanResult.getNodeMap());
        result.sort(Comparator.comparing(ProductMix::getCreateTime, Comparator.nullsLast(Date::compareTo)));
        return result;
    }
}