package cn.iocoder.yudao.module.jx.service.impl;

import cn.iocoder.yudao.module.jx.domain.LJRegion;
import cn.iocoder.yudao.module.jx.dto.LJRegionDto;
import cn.iocoder.yudao.module.jx.dto.RegionDto;
import cn.iocoder.yudao.module.jx.mapper.LJRegionMapper;
import cn.iocoder.yudao.module.jx.service.ILJRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 省市区信息 业务层
 */
@Service
public class LJRegionServiceImpl implements ILJRegionService {
    @Autowired
    private LJRegionMapper regionMapper;

    /**
     * 查询省市区信息
     * @return
     */
    @Override
    public Map<String, Object> selectRegion() {
        Map<String, Object> params = new HashMap<>();
        List<LJRegion> regionList = regionMapper.selectByMap(params);

        List<RegionDto> treeData = new ArrayList<>();
        List<LJRegionDto> list = new ArrayList<>();

        for (LJRegion mtRegion : regionList) {
            if (mtRegion.getLevel().equals(1)) {
                RegionDto dto = new RegionDto();
                LJRegionDto regionDto = new LJRegionDto();

                dto.setId(mtRegion.getId());
                dto.setName(mtRegion.getName());
                dto.setCode(mtRegion.getCode());
                dto.setPid(mtRegion.getPid());
                dto.setLevel(mtRegion.getLevel() + "");
                dto.setCity(new ArrayList<>());

                regionDto.setLabel(mtRegion.getName());
                regionDto.setValue(mtRegion.getName());
                regionDto.setChildren(new ArrayList<>());
                treeData.add(dto);
                list.add(regionDto);
            }
        }

        for (int i = 0; i < treeData.size(); i++) {
            List<RegionDto> cityArr = new ArrayList<>();
            List<LJRegionDto> city = new ArrayList<>();
            for (LJRegion mtRegion : regionList) {
                if (treeData.get(i).getId().equals(mtRegion.getPid())) {
                    RegionDto dto = new RegionDto();
                    LJRegionDto regionDto = new LJRegionDto();

                    dto.setId(mtRegion.getId());
                    dto.setName(mtRegion.getName());
                    dto.setCode(mtRegion.getCode());
                    dto.setPid(mtRegion.getPid());
                    dto.setLevel(mtRegion.getLevel() + "");

                    regionDto.setLabel(mtRegion.getName());
                    regionDto.setValue(mtRegion.getName());

                    List<RegionDto> regionArr = new ArrayList<>();
                    List<LJRegionDto> region = new ArrayList<>();
                    for (LJRegion mtRegion1 : regionList) {
                        if (mtRegion.getId().equals(mtRegion1.getPid())) {
                            RegionDto dto1 = new RegionDto();
                            LJRegionDto regionDto1 = new LJRegionDto();

                            dto1.setId(mtRegion1.getId());
                            dto1.setName(mtRegion1.getName());
                            dto1.setCode(mtRegion1.getCode());
                            dto1.setPid(mtRegion1.getPid());
                            dto1.setLevel(mtRegion1.getLevel() + "");

                            regionDto1.setLabel(mtRegion1.getName());
                            regionDto1.setValue(mtRegion1.getName());

                            regionArr.add(dto1);
                            region.add(regionDto1);
                        }
                    }

                    dto.setRegion(regionArr);
                    regionDto.setChildren(region);
                    cityArr.add(dto);
                    city.add(regionDto);
                }
            }

            treeData.get(i).setCity(cityArr);
            list.get(i).setChildren(city);
        }

        Map<String, Object> result = new HashMap<>();
        result.put("data", treeData);
        result.put("list", list);
        return result;
    }
}
