/*
 * 文 件 名:  RegionConfServiceImpl
 * 版    权:
 * 描    述:  <描述>
 * 修 改 人:  learshaw
 * 修改时间:  2022/2/18
 * 跟踪单号:  <跟踪单号>
 * 修改单号:  <修改单号>
 * 修改内容:  <修改内容>
 */
package com.huashe.common.data.service.impl;

import com.huashe.common.data.mapper.DimGbRegionMapper;
import com.huashe.common.data.model.GbRegion;
import com.huashe.common.data.service.RegionService;
import com.huashe.common.domain.model.Region;
import com.huashe.common.utils.ListUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 从数据库提取区域数据
 * <功能详细描述>
 *
 * @author learshaw
 * @version [版本号, 2022/2/18]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Service
public class RegionServiceImpl implements RegionService {
    @Autowired
    private DimGbRegionMapper regionMapper;

    @Override
    public Region getRegionByCode(String adcode) {
        GbRegion gbRegion = regionMapper.getRegionByCode(adcode);

        Region region = new Region();
        BeanUtils.copyProperties(gbRegion, region);

        return region;
    }

    @Override
    public List<Region> getAll() {
        List<GbRegion> regions = regionMapper.getAll();
        List<Region> tmpList = ListUtils.convert(regions, gbRegion -> {
            Region region = new Region();
            BeanUtils.copyProperties(gbRegion, region);
            return region;
        });

        List<Region> rootList = ListUtils.filterList(tmpList, region -> region.getParentArea() == null,
            new ArrayList<>());

        if (CollectionUtils.isNotEmpty(rootList)) {
            List<Region> subList = ListUtils.filterList(tmpList, region -> region.getParentArea() != null,
                new ArrayList<>());
            Map<String, List<Region>> regionMap = subList.stream()
                .collect(Collectors.groupingBy(Region::getParentArea));

            for (Region region : rootList) {
                region.setChildren(buildSub(regionMap, region.getAdcode()));
            }
        }

        return rootList;
    }

    private List<Region> buildSub(Map<String, List<Region>> regionMap, String key) {
        List<Region> tmpList = regionMap.get(key);

        if (CollectionUtils.isNotEmpty(tmpList)) {
            for (Region region : tmpList) {
                region.setChildren(buildSub(regionMap, region.getAdcode()));
            }
        }

        return tmpList;
    }

    @Override
    public List<Region> getRegion(Region param) {
        List<GbRegion> regions = regionMapper.getGbRegion(param);
        List<Region> ret = null;

        if (CollectionUtils.isNotEmpty(regions)) {
            ret = ListUtils.convert(regions, gbRegion -> {
                Region region = new Region();
                BeanUtils.copyProperties(gbRegion, region);
                return region;
            });
        }
        else {
            ret = Lists.newArrayList();
        }

        return ret;
    }

    @Override
    public List<Region> getMunicipality() {
        List<GbRegion> regions = regionMapper.getMunicipality();
        List<Region> ret = null;

        if (CollectionUtils.isNotEmpty(regions)) {
            ret = ListUtils.convert(regions, gbRegion -> {
                Region region = new Region();
                BeanUtils.copyProperties(gbRegion, region);
                return region;
            });
        }
        else {
            ret = Lists.newArrayList();
        }

        return ret;
    }
}