package com.ningdatech.pmapi.sys.manage;

import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.sys.convert.RegionConverter;
import com.ningdatech.pmapi.sys.model.dto.RegionDTO;
import com.ningdatech.pmapi.sys.model.vo.RegionTreeVO;
import com.ningdatech.pmapi.sys.service.IRegionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * RegionManage
 * </p>
 *
 * @author ZPF
 * @since 10:39 2023/1/18
 */
@Component
@RequiredArgsConstructor
public class RegionManage {

    private final IRegionService regionService;

    private final static Long ROOT_PARENT_ID = 1L;

    public List<RegionTreeVO> getRegionTree() {
        List<RegionDTO> regions = regionService.queryAll();
        Map<Long, List<RegionDTO>> regionMap = CollUtils.group(regions, RegionDTO::getParentId);
        return RegionConverter.toRegionTree(ROOT_PARENT_ID, regionMap, false);
    }

}
