package com.ruoyi.base.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.base.vo.BaseCityVO;
import com.ruoyi.common.utils.DateUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.base.mapper.BaseCityMapper;
import com.ruoyi.base.domain.BaseCity;
import com.ruoyi.base.service.IBaseCityService;

/**
 * 城市Service业务层处理
 *
 * @author vinjor-m
 * @date 2025-03-18
 */
@Service
public class BaseCityServiceImpl extends ServiceImpl<BaseCityMapper, BaseCity> implements IBaseCityService {
    @Autowired
    private BaseCityMapper baseCityMapper;

    @Override
    public IPage<BaseCity> queryListPage(BaseCity pageReqVO, Page<BaseCity> page) {
        return baseCityMapper.queryListPage(pageReqVO, page);
    }

    /**
     * 查询树列表
     *
     * @return java.util.List<com.ruoyi.base.vo.BaseCityVO>
     * @author PQZ
     * @date 15:12 2025/4/2
     **/
    @Override
    public List<BaseCityVO> treeCity() {
        List<BaseCity> list = listCity();
        return buildCityTree(list);
    }

    /**
     * 生成树结构
     *
     * @param list List<BaseCity>
     * @return java.util.List<com.ruoyi.base.vo.BaseCityVO>
     * @author PQZ
     * @date 9:44 2025/4/3
     **/
    private List<BaseCityVO> buildCityTree(List<BaseCity> list) {
        List<BaseCityVO> tree = new ArrayList<>();
        //创建一个Map来存储每个城市及子城市
        Map<Long, BaseCityVO> cityMap = new HashMap<>();
        for (BaseCity city : list) {
            cityMap.put(city.getCityId(), new BaseCityVO(city.getCityId(), city.getName()));
        }
        //组装城市VO列表
        for (BaseCity city : list) {
            BaseCityVO cityVO = cityMap.get(city.getCityId());
            if (city.getParentId() == 0) {
                //跟城市添加到列表
                tree.add(cityVO);
            } else {
                //非根城市天际到其父城市的孩子中
                BaseCityVO parentCityVO = cityMap.get(city.getParentId());
                if (parentCityVO != null) {
                    parentCityVO.getChildren().add(cityVO);
                }
            }
        }
        return tree;
    }

    /**
     * 逆递归查询树结构
     *
     * @param cityId 最子集城市id
     * @return java.lang.String
     * @author PQZ
     * @date 10:25 2025/4/3
     **/
    @Override
    public String getCityName(Long cityId) {
        List<BaseCity> list = listCity();
        StringBuilder nameBuilder = new StringBuilder();
        reverseRecursion(cityId, nameBuilder, list);
        return nameBuilder.toString();
    }

    /**
     * 逆递归拼接字符串
     *
     * @param cityId      城市id
     * @param nameBuilder 字符串
     * @param list        城市集合
     * @author PQZ
     * @date 10:45 2025/4/3
     **/
    private void reverseRecursion(Long cityId, StringBuilder nameBuilder, List<BaseCity> list) {
        BaseCity city = findCityById(cityId, list);
        if (city != null) {
            // 添加当前城市名称
            nameBuilder.insert(0, city.getName());
            // 继续向上查找父级城市
            reverseRecursion(city.getParentId(),nameBuilder,list);
        }
    }

    /**
     * 取出城市名称
     *
     * @param cityId 城市id
     * @param list   城市集合
     * @return com.ruoyi.base.domain.BaseCity
     * @author PQZ
     * @date 10:43 2025/4/3
     **/
    private BaseCity findCityById(Long cityId, List<BaseCity> list) {
        for (BaseCity city : list) {
            if (city.getCityId().equals(cityId)) {
                return city;
            }
        }
        return null;
    }



    /**
     * 查询树列表
     *
     * @return java.util.List<com.ruoyi.base.domain.BaseCity>
     * @author PQZ
     * @date 10:39 2025/4/3
     **/
    private List<BaseCity> listCity() {
        LambdaQueryWrapper<BaseCity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BaseCity::getIsShow, 1);
        return list(lambdaQueryWrapper);
    }
}
