package com.leromro.core.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leromro.common.utils.bean.BeanUtils;
import com.leromro.core.domain.Region;
import com.leromro.core.domain.dto.RegionTreeDTO;
import com.leromro.core.mapper.RegionMapper;

import com.leromro.core.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


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

/**
 * 行政地区业务层实现
 *
 * @author Chopper
 * @since 2020/12/2 11:11
 */
@Service
public class RegionServiceImpl extends ServiceImpl<RegionMapper, Region> implements RegionService {

    @Autowired
    private RegionMapper regionMapper;


    public List<RegionTreeDTO> getRegionTree() {
        List<Region> regions = regionMapper.selectList(null);

        // 转换成 DTO 列表
        List<RegionTreeDTO> dtoList = regions.stream()
                .map(region -> {
                    RegionTreeDTO dto = new RegionTreeDTO();
                    com.leromro.common.utils.bean.BeanUtils.copyProperties(region, dto); // 注意顺序
                    return dto;
                }).collect(Collectors.toList());

        // 构建树
        Map<String, RegionTreeDTO> theMap = new HashMap<>();
        dtoList.forEach(dto -> theMap.put(dto.getRegionCode(), dto));

        List<RegionTreeDTO> rootList = new ArrayList<>();

        dtoList.forEach(dto -> {
            if ("0".equals(dto.getParentRegionCode()) || dto.getParentRegionCode() == null) {
                rootList.add(dto);
            } else {
                RegionTreeDTO parent = theMap.get(dto.getParentRegionCode());
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(dto);
                }
            }
        });

        return rootList;
    }

/*
    @Override
    public List<Region> getItem(String id) {
        LambdaQueryWrapper<Region> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Region::getParentId, id);
        List<Region> regions = this.list(lambdaQueryWrapper);
        regions.sort(Comparator.comparing(Region::getOrderNum));
        return regions;
    }

    @Override
    public List<RegionTreeDTO> getRegionTree() {
        // 查询所有地区数据
        List<Region> regions = regionMapper.selectList(null);

        // 转换为 DTO 列表
        List<RegionTreeDTO> dtoList = regions.stream().map(region -> {
            RegionTreeDTO dto = new RegionTreeDTO();
            BeanUtils.copyProperties(region, dto);
            return dto;
        }).collect(Collectors.toList());

        // 构建树
        return buildTree(dtoList);
    }
    private List<RegionTreeDTO> buildTree(List<RegionTreeDTO> nodeList) {
        Map<String, RegionTreeDTO> nodeMap = new HashMap<>();
        List<RegionTreeDTO> rootList = new ArrayList<>();

        // 将每个节点放入 map 中，方便后续查找
        for (RegionTreeDTO node : nodeList) {
            nodeMap.put(node.getId(), node);
        }

        // 设置父子关系
        for (RegionTreeDTO current : nodeList) {
            if ("0".equals(current.getParentId())) { // 根节点判断条件，请根据实际修改
                rootList.add(current);
            } else {
                RegionTreeDTO parent = nodeMap.get(current.getParentId());
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<>());
                    }
                    parent.getChildren().add(current);
                }
            }
        }

        return rootList;
    }*/

}