package com.ningdatech.pmapi.sys.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ningdatech.basic.util.CollUtils;
import com.ningdatech.pmapi.AppTests;
import com.ningdatech.pmapi.common.helper.RegionCacheHelper;
import com.ningdatech.pmapi.sys.model.entity.Region;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * <p>
 * IRegionServiceTest
 * </p>
 *
 * @author WendyYang
 * @since 16:57 2023/3/1
 */
class IRegionServiceTest extends AppTests {

    @Autowired
    private RegionCacheHelper regionCacheHelper;
    @Autowired
    private IRegionService regionService;

    @Test
    public void test1() {
        AtomicLong idIncr = new AtomicLong(1);
        Map<Integer, List<Region>> map = CollUtils.group(regionService.list(), Region::getRegionLevel);
        regionService.remove(null);
        map.keySet().stream().sorted().forEach(w -> {
            List<Region> list = new ArrayList<>();
            List<Region> regions = map.get(w);
            for (Region region : regions) {
                if (w == 1) {
                    region.setRegionCodePath(region.getParentCode() + "," + region.getRegionCode());
                    region.setParentId(0L);
                } else {
                    Region parent = regionService.getOne(Wrappers.lambdaQuery(Region.class)
                            .eq(Region::getRegionCode, region.getParentCode())
                            .eq(Region::getRegionLevel, region.getRegionLevel() - 1));
                    region.setRegionCodePath(parent.getRegionCodePath() + "," + region.getRegionCode());
                    region.setParentId(parent.getId());
                }
                region.setId(idIncr.getAndIncrement());
                list.add(region);
            }
            regionService.saveBatch(list);
            System.out.println("============================================================================");
        });
    }

    @Test
    public void test2() {
        System.out.println(regionCacheHelper.getDisplayName("330100", 2));
        System.out.println(regionCacheHelper.getFullDisplayName("330102", 3));
    }

}
