package com.ruoyi.xq.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.BeanConvertUtil;
import com.ruoyi.xq.domain.AreaCode;
import com.ruoyi.xq.dto.admin.areacode.AreaCodeAdminTree;
import com.ruoyi.xq.dto.app.areacode.AreaCodeQuery;
import com.ruoyi.xq.dto.app.areacode.AreaCodeTree;
import com.ruoyi.xq.mapper.AreaCodeMapper;
import com.ruoyi.xq.service.AreaCodeService;
import org.springframework.stereotype.Service;

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

/**
 * 行政区划Service业务层处理
 *
 * @author 77
 * @date 2024-03-19
 */
@Service
public class AreaCodeServiceImpl extends ServiceImpl<AreaCodeMapper,AreaCode> implements AreaCodeService {

    @Override
    public List<AreaCode> listAreaCode(AreaCodeQuery query) {
        List<AreaCode> list = this.list(Wrappers.lambdaQuery(AreaCode.class)
                .eq(query.getPcode() != null,AreaCode::getPcode, query.getPcode())
                .eq(StringUtils.isNotBlank(query.getLevel()),AreaCode::getLevel,query.getLevel()));
        return list;
    }

    @Override
    public List<AreaCodeAdminTree> treeAdmin() {
        List<AreaCode> list = this.list();
        List<AreaCodeAdminTree> nodeList = list.stream().map(i -> {
            AreaCodeAdminTree areaCodeAdminTree = new AreaCodeAdminTree();
            areaCodeAdminTree.setId(i.getCode());
            areaCodeAdminTree.setLabel(i.getName());
            areaCodeAdminTree.setParentId(i.getPcode());
            return areaCodeAdminTree;
        }).collect(Collectors.toList());
        Map<Integer, AreaCodeAdminTree> nodeMap = new HashMap<>();
        Set<AreaCodeAdminTree> roots = new HashSet<>();
        // 遍历列表构建节点映射表
        for (AreaCodeAdminTree node : nodeList) {
            nodeMap.put(node.getId(), node);
            if (node.getParentId() == 0 || !nodeMap.containsKey(node.getParentId())) {
                roots.add(node);
            }
        }
        // 建立父子关系
        for (AreaCodeAdminTree node : nodeList) {
            if (nodeMap.containsKey(node.getParentId())) {
                AreaCodeAdminTree parentNode = nodeMap.get(node.getParentId());
                parentNode.addChild(node);
            }
        }
        return new ArrayList<>(roots);
    }

    @Override
    public List<AreaCodeTree> tree() {
        List<AreaCode> list = this.list();
        List<AreaCodeTree> nodeList = BeanConvertUtil.convertListTo(list, AreaCodeTree::new);
        Map<Integer, AreaCodeTree> nodeMap = new HashMap<>();
        Set<AreaCodeTree> roots = new HashSet<>();
        // 遍历列表构建节点映射表
        for (AreaCodeTree node : nodeList) {
            nodeMap.put(node.getCode(), node);
            if (node.getPcode() == 0 || !nodeMap.containsKey(node.getPcode())) {
                roots.add(node);
            }
        }
        // 建立父子关系
        for (AreaCodeTree node : nodeList) {
            if (nodeMap.containsKey(node.getPcode())) {
                AreaCodeTree parentNode = nodeMap.get(node.getPcode());
                parentNode.addChild(node);
            }
        }
        return new ArrayList<>(roots);
    }
}
