package com.ruoyi.system.service.impl;

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

import com.ruoyi.system.domain.vo.AdministrativeDivisionsVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TAdministrativeDivisionsMapper;
import com.ruoyi.system.domain.TAdministrativeDivisions;
import com.ruoyi.system.service.ITAdministrativeDivisionsService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-09-20
 */
@Service
public class TAdministrativeDivisionsServiceImpl implements ITAdministrativeDivisionsService {
    @Autowired
    private TAdministrativeDivisionsMapper tAdministrativeDivisionsMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TAdministrativeDivisions selectTAdministrativeDivisionsById(Long id) {
        return tAdministrativeDivisionsMapper.selectTAdministrativeDivisionsById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param tAdministrativeDivisions 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TAdministrativeDivisions> selectTAdministrativeDivisionsList(TAdministrativeDivisions tAdministrativeDivisions) {
        //查询
        return tAdministrativeDivisionsMapper.selectTAdministrativeDivisionsList(tAdministrativeDivisions);
    }

    @Override
    public List<AdministrativeDivisionsVo> selectTAdministrativeDivisionsVoList(TAdministrativeDivisions tAdministrativeDivisions) {
        List<AdministrativeDivisionsVo> divisionsVos = new ArrayList<>();
        //查询所有省份List
        List<TAdministrativeDivisions> provinceList = tAdministrativeDivisionsMapper.selectTAdministrativeDivisionsProvinceAllList();
        if (provinceList != null && !provinceList.isEmpty()) {
            divisionsVos = provinceList.subList(0, 2).stream()
                    .map(provinceEntity -> {
                        AdministrativeDivisionsVo provinceVo = new AdministrativeDivisionsVo();
                        BeanUtils.copyProperties(provinceEntity, provinceVo);
                        List<AdministrativeDivisionsVo> cityVos = new ArrayList<>();
                        if (provinceVo.getCode() != null) {
                            //查询所有市List
                            List<TAdministrativeDivisions> cityList = tAdministrativeDivisionsMapper.selectTAdministrativeDivisionsListByProvinceCode(provinceVo.getCode());
                            if (cityList != null && !cityList.isEmpty()) {
                                cityVos = cityList.subList(0, 1).stream()
                                        .map(cityEntity -> {
                                            AdministrativeDivisionsVo cityVo = new AdministrativeDivisionsVo();
                                            BeanUtils.copyProperties(cityEntity, cityVo);
                                            List<AdministrativeDivisionsVo> areaVos = new ArrayList<>();
                                            if (cityVo.getCode() != null) {
                                                //查询所有县级区List
                                                List<TAdministrativeDivisions> areaList = tAdministrativeDivisionsMapper.selectTAdministrativeDivisionsListByCityCode(cityVo.getCode());
                                                if (areaList != null && !areaList.isEmpty()) {
                                                    areaVos = areaList.subList(0, 3).stream()
                                                            .map(areaEntity -> {
                                                                AdministrativeDivisionsVo areaVo = new AdministrativeDivisionsVo();
                                                                BeanUtils.copyProperties(areaEntity, areaVo);
                                                                List<AdministrativeDivisionsVo> streetVos = new ArrayList<>();
                                                                if (areaVo.getCode() != null) {
                                                                    //查询所有乡级镇List
                                                                    List<TAdministrativeDivisions> streetList = tAdministrativeDivisionsMapper.selectTAdministrativeDivisionsListByAreaCode(areaVo.getCode());
                                                                    if (streetList != null && !streetList.isEmpty()) {
                                                                        streetVos = streetList.subList(0, 3).stream()
                                                                                .map(streetEntity -> {
                                                                                    AdministrativeDivisionsVo streetVo = new AdministrativeDivisionsVo();
                                                                                    BeanUtils.copyProperties(streetEntity, streetVo);
                                                                                    List<AdministrativeDivisionsVo> neighborhoodVos = new ArrayList<>();
                                                                                    if (streetVo.getCode() != null) {
                                                                                        //查询所有居委会List
                                                                                        List<TAdministrativeDivisions> neighborhoodList = tAdministrativeDivisionsMapper.selectTAdministrativeDivisionsListByStreetCode(streetVo.getCode());
                                                                                        if (neighborhoodList != null && !neighborhoodList.isEmpty()) {
                                                                                            neighborhoodVos = neighborhoodList.subList(0, 3).stream()
                                                                                                    .map(neighborhoodEntity -> {
                                                                                                        AdministrativeDivisionsVo neighborhoodVo = new AdministrativeDivisionsVo();
                                                                                                        BeanUtils.copyProperties(neighborhoodEntity, neighborhoodVo);
                                                                                                        neighborhoodVo.setParentId(streetVo.getCode());
                                                                                                        return neighborhoodVo;
                                                                                                    }).collect(Collectors.toList());
                                                                                        }
                                                                                    }
                                                                                    streetVo.setChild(neighborhoodVos);
                                                                                    streetVo.setParentId(areaVo.getCode());
                                                                                    return streetVo;
                                                                                }).collect(Collectors.toList());
                                                                    }
                                                                }
                                                                areaVo.setChild(streetVos);
                                                                areaVo.setParentId(cityVo.getCode());
                                                                return areaVo;
                                                            }).collect(Collectors.toList());
                                                }
                                                cityVo.setChild(areaVos);
                                                cityVo.setParentId(cityVo.getCode());
                                            }
                                            cityVo.setChild(areaVos);
                                            cityVo.setParentId(provinceVo.getCode());
                                            return cityVo;
                                        }).collect(Collectors.toList());
                            }
                        }
                        provinceVo.setChild(cityVos);
                        return provinceVo;
                    }).collect(Collectors.toList());
        }
        return divisionsVos;
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param tAdministrativeDivisions 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTAdministrativeDivisions(TAdministrativeDivisions tAdministrativeDivisions) {
        return tAdministrativeDivisionsMapper.insertTAdministrativeDivisions(tAdministrativeDivisions);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param tAdministrativeDivisions 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTAdministrativeDivisions(TAdministrativeDivisions tAdministrativeDivisions) {
        return tAdministrativeDivisionsMapper.updateTAdministrativeDivisions(tAdministrativeDivisions);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTAdministrativeDivisionsByIds(Long[] ids) {
        return tAdministrativeDivisionsMapper.deleteTAdministrativeDivisionsByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTAdministrativeDivisionsById(Long id) {
        return tAdministrativeDivisionsMapper.deleteTAdministrativeDivisionsById(id);
    }
}
