package com.dsk.cscec.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsk.common.core.domain.entity.SysDictData;
import com.dsk.cscec.domain.DimArea;
import com.dsk.cscec.domain.vo.RegionVo;
import com.dsk.cscec.domain.vo.RegionWithLevelVo;
import com.dsk.cscec.mapper.DimAreaMapper;
import com.dsk.cscec.service.IDimAreaService;
import com.dsk.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * (DimArea)表服务实现类
 *
 * @author lcl
 * @since 2023-12-13 13:45:22
 */
@Service
public class DimAreaServiceImpl extends ServiceImpl<DimAreaMapper, DimArea> implements IDimAreaService {

    @Autowired
    private ISysDictTypeService sysDictTypeService;

    private static final String AREA_TYPE = "arealist";

//    @Cacheable(cacheNames = "", key = "")
    @Override
    public List<RegionVo>  allArea() {
        List<RegionVo> vos = new ArrayList<>();
        //区域
        List<SysDictData> sysDictData = sysDictTypeService.selectDictDataByType(AREA_TYPE);
        if(CollectionUtils.isNotEmpty(sysDictData)){
            for (SysDictData data : sysDictData) {
                RegionVo regionVo = new RegionVo();
                regionVo.setValue(data.getDictValue());
                List<RegionVo> provinceVos = new ArrayList<>();
                //省
                List<Object> provinces = baseMapper.selectObjs(Wrappers.<DimArea>lambdaQuery()
                        .select(DimArea::getAreaName2)
                        .eq(DimArea::getAreaName1, data.getDictValue())
                        .ne(DimArea::getAreaName2, data.getDictValue())
                        .groupBy(DimArea::getAreaName2));

                if(CollectionUtils.isNotEmpty(provinces)){
                    for (Object province : provinces) {
                        RegionVo provinceVo = new RegionVo();
                        provinceVo.setValue(String.valueOf(province));
                        List<RegionVo> cityVos = new ArrayList<>();
                        //市
                        List<Object> citys = baseMapper.selectObjs(Wrappers.<DimArea>lambdaQuery()
                                .select(DimArea::getAreaName3)
                                .eq(DimArea::getAreaName2, provinceVo.getValue())
                                .ne(DimArea::getAreaName3, provinceVo.getValue())
                                .groupBy(DimArea::getAreaName3));
                        if(CollectionUtils.isNotEmpty(citys)){
                            for (Object city : citys) {
                                RegionVo cityVo = new RegionVo();
                                cityVo.setValue(String.valueOf(city));
                                cityVos.add(cityVo);
                            }
                        }
                        provinceVo.setChildren(cityVos);
                        provinceVos.add(provinceVo);
                    }
                }
                regionVo.setChildren(provinceVos);
                vos.add(regionVo);
            }
        }
        return vos;
    }

    @Override
    public List<RegionWithLevelVo> allAreaWithoutRegion() {
        List<RegionWithLevelVo> vos = new ArrayList<>();
        //区域
        List<SysDictData> sysDictData = sysDictTypeService.selectDictDataByType(AREA_TYPE);
        if(CollectionUtils.isNotEmpty(sysDictData)){
            for (SysDictData data : sysDictData) {
                //省
                List<Object> provinces = baseMapper.selectObjs(Wrappers.<DimArea>lambdaQuery()
                        .select(DimArea::getAreaName2)
                        .eq(DimArea::getAreaName1, data.getDictValue())
                        .ne(DimArea::getAreaName2, data.getDictValue())
                        .groupBy(DimArea::getAreaName2));

                if(CollectionUtils.isNotEmpty(provinces)){
                    for (Object province : provinces) {
                        RegionWithLevelVo provinceVo = new RegionWithLevelVo();
                        provinceVo.setValue(String.valueOf(province));
                        provinceVo.setLevel("1");
                        List<RegionWithLevelVo> cityVos = new ArrayList<>();
                        //市
                        List<Object> citys = baseMapper.selectObjs(Wrappers.<DimArea>lambdaQuery()
                                .select(DimArea::getAreaName3)
                                .eq(DimArea::getAreaName2, provinceVo.getValue())
                                .ne(DimArea::getAreaName3, provinceVo.getValue())
                                .groupBy(DimArea::getAreaName3));
                        if(CollectionUtils.isNotEmpty(citys)){
                            for (Object city : citys) {
                                RegionWithLevelVo cityVo = new RegionWithLevelVo();
                                cityVo.setValue(String.valueOf(city));
                                cityVo.setLevel("2");
                                cityVos.add(cityVo);
                            }
                        }
                        provinceVo.setChildren(cityVos);
                        provinceVo.setChildrenLength(provinceVo.getChildren().size());
                        vos.add(provinceVo);
                    }
                }
            }
        }
        return vos;
    }
}

