VideoServiceImpl.java 4.45 KB
package com.bsth.service.impl.videoimpl;

import com.bsth.entity.Business;
import com.bsth.entity.Line;
import com.bsth.entity.video.VideoTree;
import com.bsth.service.BusinessService;
import com.bsth.service.LineService;
import com.bsth.service.video.VideoService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liujun
 * @date 2024年07月01日 12:57
 */
@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    private BusinessService businessService;
    @Autowired
    private LineService lineService;

    @Override
    public List<VideoTree> combinationTree() {
        List<VideoTree> returnList = new ArrayList<>();

        List<VideoTree> bussessTree = queryBusiness();
        if (CollectionUtils.isNotEmpty(bussessTree)) {
            returnList.addAll(bussessTree);
        }

//        List<VideoTree> lineTree = queryLine();
//        if (CollectionUtils.isNotEmpty(lineTree)) {
//            returnList.addAll(lineTree);
//        }
        return returnList;

    }

    /***
     *查询公交公司的数据。
     * @author liujun
     * @date 2024/6/27 11:29
     * @return java.util.List<com.bsth.entity.video.VideoTree>
     */
    private List<VideoTree> queryBusiness() {
        Iterable<Business> businessIterable = businessService.list(new HashMap<>());
        if (Objects.isNull(businessIterable)) {
            return Collections.emptyList();
        }

        List<VideoTree> trees = new ArrayList<>();
        Iterator<Business> iterator = businessIterable.iterator();
        while (iterator.hasNext()) {
            Business business = iterator.next();
            VideoTree videoTree = combinationVideoTree(business.getBusinessCode(), business.getBusinessName(), business.getUpCode(), business.getId(), 1, null);
            trees.add(videoTree);
        }
        List<VideoTree> groupTree = new ArrayList<>();
        for (VideoTree tree : trees) {
            if (Objects.equals(tree.getParentId(), "0")) {
                VideoTree target = new VideoTree();
                BeanUtils.copyProperties(tree,target);
                target.setParentId("#");
                groupTree.add(target);
            }
        }

        if (CollectionUtils.isNotEmpty(groupTree)) {
            for (VideoTree tree : groupTree) {
                groupTree(trees, tree);
            }
        }

        return groupTree;
    }

    private List<VideoTree> queryLine() {
        Iterable<Line> lines = lineService.list(new HashMap<>());
        if (Objects.isNull(lines)) {
            return Collections.emptyList();
        }

        List<VideoTree> trees = new ArrayList<>();
        Iterator<Line> iterator = lines.iterator();
        while (iterator.hasNext()) {
            Line line = iterator.next();
            VideoTree videoTree = combinationVideoTree(line.getId(), line.getName(), line.getCompany(), line.getId(), 2, null);
            trees.add(videoTree);
        }


        return trees;
    }

    private List<VideoTree> queryCars() {
        return null;
    }

    /***
     * 组合树
     * @author liujun
     * @date 2024/6/27 13:12
     * @param id id
     * @param text 显示的文本
     * @param sourceId 数据的ID
     * @param parent 数据的父节点
     * @param type 类型
     * @param icon 图标
     * @return com.bsth.entity.video.VideoTree
     */
    private VideoTree combinationVideoTree(Object id, String text, String parent, Integer sourceId, Integer type, String icon) {
        VideoTree videoTree = new VideoTree();
        videoTree.setId(id);
        videoTree.setText(text);
        videoTree.setSourceId(sourceId);
        videoTree.setParentId(parent);
        videoTree.setType(type);
        videoTree.setIcon(icon);
        return videoTree;
    }

    private void groupTree(List<VideoTree> trees, VideoTree tree) {
        if (Objects.isNull(trees) || CollectionUtils.isEmpty(trees)) {
            return;
        }
        for (VideoTree videoTree : trees) {
            if (Objects.equals(videoTree.getParentId(), tree.getId())) {
                if (Objects.isNull(tree.getChildren())) {
                    tree.setChildren(new ArrayList<>());
                }
                tree.getChildren().add(videoTree);
                groupTree(trees, videoTree);
            }
        }
    }

}