package org.jeecg.modules.subwayNetwork.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.dto.PageSearch;
import org.jeecg.modules.subwayNetwork.dto.LightRailQueryDTO;
import org.jeecg.modules.subwayNetwork.entity.LightRail;
import org.jeecg.modules.subwayNetwork.mapper.LightRailMapper;
import org.jeecg.modules.subwayNetwork.mapper.SubwaySectionMapper;
import org.jeecg.modules.subwayNetwork.mapper.TrainStationMapper;
import org.jeecg.modules.subwayNetwork.service.ILightRailService;
import org.jeecg.modules.subwayNetwork.vo.LightRailQueryVO;
import org.jeecg.modules.subwayNetwork.vo.SectionStationMapVO;
import org.jeecg.modules.subwayNetwork.vo.SectionStationNode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 线路车站-线别管理
 */
@Service
public class LightRailServiceImpl extends ServiceImpl<LightRailMapper, LightRail> implements ILightRailService {

    @Resource
    private SubwaySectionMapper subwaySectionMapper;

    @Resource
    private TrainStationMapper trainStationMapper;

    @Override
    public IPage<LightRailQueryVO> queryPageList(PageSearch<LightRailQueryDTO> dto) {
        IPage<LightRailQueryVO> page = new Page<>(dto.getPageNo(), dto.getPageSize());

        page = this.baseMapper.queryPageList(page, dto.getQuery());
        return page;
    }

    @Override
    public List<SectionStationMapVO> getSectionStation() {
        return this.baseMapper.getSectionStation();
    }

    @Override
    public List<SectionStationNode> getTree() {
        // 1. 查询数据
        // 1.1 查询所有线路,按上下行查询
        List<SectionStationNode> lightRailList = this.baseMapper.getTreeLightRailList();

        // 1.2 查询所有区间
        List<SectionStationNode> subwaySectionList = subwaySectionMapper.getTreeSubwaySectionList();

        // 1.3 查询所有车站
        List<SectionStationNode> trainStation = trainStationMapper.getTreeTrainStationList();


        // 2.组装数据
        // 2.1 组装 车站-区间&组装一个区间map
        Map<String, SectionStationNode> subwaySectionMap = new HashMap<>();
        for (SectionStationNode sectionStationNode : subwaySectionList) {

            // 2.1.1 组装区间map
            subwaySectionMap.put(sectionStationNode.getId(), sectionStationNode);


            // 2.1.2 组装车站-区间
            for (SectionStationNode lightRail : lightRailList) {
                if (sectionStationNode.getParent().equals(lightRail.getId())) {
                    List<SectionStationNode> children = lightRail.getChildren();
                    if (children == null) {
                        children = new ArrayList<>();
                        lightRail.setChildren(children);
                        lightRail.setIsLeaf(false);
                    }
                    children.add(sectionStationNode);
                }
            }

        }
        // 2.1 组装区间-车站数据
        for (SectionStationNode node : trainStation) {
            SectionStationNode sectionStationNode = subwaySectionMap.get(node.getParent());
            // 这个车站属于这个区间就添加进去,说明不是叶子节点
            if (sectionStationNode != null) {
                List<SectionStationNode> children = sectionStationNode.getChildren();
                if (children == null) {
                    children = new ArrayList<>();
                    sectionStationNode.setChildren(children);
                    sectionStationNode.setIsLeaf(false);
                }
                children.add(node);
            }
        }


        return lightRailList;
    }
}
