package com.ruoyi.ibs.grid.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.ibs.grid.domain.entity.RegionArea;
import com.ruoyi.ibs.grid.domain.vo.RegionAreaVO;
import com.ruoyi.ibs.grid.mapper.RegionAreaMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 吴凯程
 * @Date 2025/8/1
 **/
@Service
@Slf4j
public class RegionAreaService {

    @Resource
    private RegionAreaMapper regionAreaMapper;

    @Resource
    private RedisCache redisCache;

    private final String REGION_IN_AREA_KEY = "region_in_area_redis_key";

    private final String REGION_OUT_AREA_KEY = "region_out_area_redis_key";

    public void loadRegionAreaToRedis() {
        log.info("开始加载域内行政区划数据到redis");
        LambdaQueryWrapper<RegionArea> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.likeRight(RegionArea::getCode, "33").orderByAsc(RegionArea::getLevel);
        List<RegionArea> regionAreas = regionAreaMapper.selectList(queryWrapper);
        List<RegionAreaVO> inArea = regionAreas.stream().filter(regionArea -> regionArea.getLevel().equals(1))
                .map(regionArea -> dfs(regionArea, regionAreas, 5)).collect(Collectors.toList());
        redisCache.setCacheObject(REGION_IN_AREA_KEY, inArea);

        log.info("开始加载域外行政区划数据到redis");
        List<RegionArea> regionAreas1 = regionAreaMapper.selectRegionOutAreaList();
        List<RegionAreaVO> outArea = regionAreas1.stream().filter(regionArea -> regionArea.getLevel().equals(1))
                .map(regionArea -> dfs(regionArea, regionAreas1, 3)).collect(Collectors.toList());
        redisCache.setCacheObject(REGION_OUT_AREA_KEY, outArea);
    }

    public List<RegionAreaVO> getRegionInAreaList(){
        if(!redisCache.hasKey(REGION_IN_AREA_KEY)){
            loadRegionAreaToRedis();
            throw new ServiceException("redis缓存中不存在域内行政区划数据，开始重新加载");
        }
        return redisCache.getCacheObject(REGION_IN_AREA_KEY);
    }

    public List<RegionAreaVO> getRegionOutAreaList(){
        if(!redisCache.hasKey(REGION_OUT_AREA_KEY)){
            loadRegionAreaToRedis();
            throw new ServiceException("redis缓存中不存在域外行政区划数据，开始重新加载");
        }
        return redisCache.getCacheObject(REGION_OUT_AREA_KEY);
    }

    private RegionAreaVO dfs(RegionArea regionArea, List<RegionArea> regionAreaList, Integer depth){
        RegionAreaVO regionAreaVO = new RegionAreaVO();
        BeanUtils.copyProperties(regionArea, regionAreaVO);
        if (regionArea.getLevel().equals(depth)){
            regionAreaVO.setChildren(new ArrayList<>());
            return regionAreaVO;
        }
        List<RegionAreaVO> children = regionAreaList.stream().filter(regionArea1 -> regionArea1.getParent().equals(regionArea.getCode()))
                .map(regionArea1 -> dfs(regionArea1, regionAreaList, depth)).collect(Collectors.toList());
        regionAreaVO.setChildren(children.size() > 0 ? children : null );
        return regionAreaVO;
    }


}
