package com.sundot.airport.ledger.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sundot.airport.ledger.domain.ScoreIndicator;
import com.sundot.airport.ledger.mapper.ScoreIndicatorMapper;
import com.sundot.airport.ledger.service.IScoreIndicatorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ScoreIndicatorServiceImpl
        extends ServiceImpl<ScoreIndicatorMapper, ScoreIndicator>
        implements IScoreIndicatorService {

    @Autowired
    private ScoreIndicatorMapper mapper;

    @Override
    public List<ScoreIndicator> selectList(ScoreIndicator query) {
        return mapper.selectList(query);
    }

    @Override
    public List<ScoreIndicator> selectTree(Long dimensionId,Integer  org) {
        List<ScoreIndicator> all = mapper.selectByDimensionId(dimensionId,org);
        return buildTree(all, 0L);
    }

    private List<ScoreIndicator> buildTree(List<ScoreIndicator> all, Long parentId) {
        List<ScoreIndicator> result = new ArrayList<>();
        for (ScoreIndicator node : all) {
            if (parentId.equals(node.getParentId())) {
                List<ScoreIndicator> children = buildTree(all, node.getId());
                if (!children.isEmpty()) {
                    node.setChildren(children);
                }
                result.add(node);
            }
        }
        return result;
    }
}
