package com.vctgo.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Joiner;
import com.vctgo.common.core.constant.HttpStatus;
import com.vctgo.common.core.domain.R;
import com.vctgo.common.core.exception.ServiceException;
import com.vctgo.common.core.text.Convert;
import com.vctgo.common.core.utils.ServletUtils;
import com.vctgo.common.core.utils.StrUtils;
import com.vctgo.common.mybatisplus.constant.MybatisPageConstants;
import com.vctgo.common.security.utils.SecurityUtils;
import com.vctgo.system.api.domain.*;
import com.vctgo.system.api.domain.dto.SysLocationReClassDto;
import com.vctgo.system.domain.dto.SysAreaDto;
import com.vctgo.system.domain.dto.SysLocationAttributeDto;
import com.vctgo.system.domain.dto.SysLocationDto;
import com.vctgo.system.domain.vo.SysDownLocation;
import com.vctgo.system.mapper.SysAreaSettingMapper;
import com.vctgo.system.mapper.SysClassSettingMapper;
import com.vctgo.system.mapper.SysLocationSettingMapper;
import com.vctgo.system.mapper.SysReLocationClassMapper;
import com.vctgo.system.service.ISysDictDataService;
import com.vctgo.system.service.ISysLocationSettingService;
import com.vctgo.system.service.ISysReLocationAreaService;
import com.vctgo.system.service.ISysReLocationClassService;
import com.vctgo.system.util.ConstantUtils;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 位置设置Service业务层处理
 *
 * @author caesar
 * @date 2023-05-30
 */
@Slf4j
@Service
public class SysLocationSettingServiceImpl implements ISysLocationSettingService {

    @Autowired
    private SysLocationSettingMapper sysLocationSettingMapper;

    @Autowired
    private ISysReLocationAreaService sysReLocationAreaService;

    @Autowired
    private ISysReLocationClassService sysReLocationClassService;

    @Autowired
    private ISysDictDataService sysDictDataService;


    @Autowired
    private SysAreaSettingMapper sysAreaSettingMapper;

    @Autowired
    private SysClassSettingMapper sysClassSettingMapper;

    @Autowired
    private SysReLocationClassMapper sysReLocationClassMapper;

    /**
     * 查询位置设置
     *
     * @param id 位置设置主键
     * @return 位置设置
     */
    @Transactional(readOnly = true)
    @Override
    public SysLocationSetting selectSysLocationSettingById(Long id) {
        SysLocationSetting sysLocationSetting = sysLocationSettingMapper.selectById(id);

        // 通过位置Id获取关联的区域id
        List<SysReLocationArea> sysReLocationAreaList = sysReLocationAreaService.selectSysReLocationAreaList(SysReLocationArea.builder()
                .locationId(id)
                .isDeleted("0")
                .build());
        if (!sysReLocationAreaList.isEmpty()) {
            List<SysAreaSetting> sysAreaSettingList = new ArrayList<>();
            sysReLocationAreaList.stream().forEach(sysReLocationArea -> {
                SysAreaSetting sysAreaSetting = new SysAreaSetting();
                sysAreaSetting.setAreaId(sysReLocationArea.getAreaId());
                sysAreaSettingList.add(sysAreaSetting);
            });
            sysLocationSetting.setAssiciationPutArea(sysAreaSettingList);
        }

        return sysLocationSetting;
    }

    @Override
    public SysLocationSetting repeatCheck(SysLocationSetting sysLocationSetting) {
        LambdaQueryWrapper<SysLocationSetting> sysLocationSettingWrapper = new LambdaQueryWrapper<>();
        sysLocationSettingWrapper.eq(SysLocationSetting::getIsDeleted, "0");
        sysLocationSettingWrapper.ne(Objects.nonNull(sysLocationSetting.getId()), SysLocationSetting::getId, sysLocationSetting.getId());
        sysLocationSettingWrapper.eq(StrUtils.isNotEmpty(sysLocationSetting.getName()), SysLocationSetting::getName, sysLocationSetting.getName());
        sysLocationSettingWrapper.eq(StrUtils.isNotEmpty(sysLocationSetting.getCode()), SysLocationSetting::getCode, sysLocationSetting.getCode());
        sysLocationSettingWrapper.last("limit 1");
        return sysLocationSettingMapper.selectOne(sysLocationSettingWrapper);
    }

    /**
     * 分页查询位置设置列表--区域组合在一起
     *
     * @param sysLocationSetting 位置设置
     * @return 位置设置
     */
    @Override
    public IPage<SysLocationSetting> selectSysLocationSettingPage(SysLocationSetting sysLocationSetting) {
        Page mpPage = new Page(Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM), 1L)
                , Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE), 10L));
        IPage<SysLocationSetting> sysLocationSettingIPage = sysLocationSettingMapper.selectSysLocationSettingList(mpPage, sysLocationSetting);
        if (!sysLocationSettingIPage.getRecords().isEmpty()) {

            // 获取年级列表
            SysDictData sysDictData = new SysDictData();
            sysDictData.setDictType("toy_release_grade");
            List<SysDictData> gradeDataList = sysDictDataService.selectDictDataList(sysDictData);

            // 获取班级列表
            sysDictData.setDictType("toy_release_class");
            List<SysDictData> classDataList = sysDictDataService.selectDictDataList(sysDictData);

            sysLocationSettingIPage.getRecords().stream().forEach(sysLocationSettingTmp -> {
                List<SysClassSetting> assiciationClassList = sysLocationSettingTmp.getAssiciationClass();

                // 遍历设置班级名称
                assiciationClassList.stream().forEach(sysClassSetting -> {
                    String gradeIn = gradeDataList.stream().filter(gradeData -> gradeData.getDictValue().equals(sysClassSetting.getGradeIn())).findFirst().get().getDictLabel();
                    String classNumber = classDataList.stream().filter(classData -> classData.getDictValue().equals(sysClassSetting.getClassNumber())).findFirst().get().getDictLabel();
                    sysClassSetting.setClassName(gradeIn.replace("班", "") + "(" + classNumber.replace("班", "") + ")" + "班");
                });
            });
        }
        return sysLocationSettingIPage;
    }

    /**
     * 分页查询位置设置列表--区域组合在一起
     *
     * @param sysLocationSetting 位置设置
     * @return 位置设置
     */
    @Override
    public IPage<SysLocationSetting> selectBookSysLocationSettingPage(SysLocationSetting sysLocationSetting) {
        Page mpPage = new Page(Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM), 1L)
                , Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE), 10L));
        IPage<SysLocationSetting> sysLocationSettingIPage = sysLocationSettingMapper.selectBookSysLocationSettingList(mpPage, sysLocationSetting);
        if (!sysLocationSettingIPage.getRecords().isEmpty()) {

            // 获取年级列表
            SysDictData sysDictData = new SysDictData();
            sysDictData.setDictType("toy_release_grade");
            List<SysDictData> gradeDataList = sysDictDataService.selectDictDataList(sysDictData);

            // 获取班级列表
            sysDictData.setDictType("toy_release_class");
            List<SysDictData> classDataList = sysDictDataService.selectDictDataList(sysDictData);

            sysLocationSettingIPage.getRecords().stream().forEach(sysLocationSettingTmp -> {
                List<SysClassSetting> assiciationClassList = sysLocationSettingTmp.getAssiciationClass();

                // 遍历设置班级名称
                assiciationClassList.stream().forEach(sysClassSetting -> {
                    String gradeIn = gradeDataList.stream().filter(gradeData -> gradeData.getDictValue().equals(sysClassSetting.getGradeIn())).findFirst().get().getDictLabel();
                    String classNumber = classDataList.stream().filter(classData -> classData.getDictValue().equals(sysClassSetting.getClassNumber())).findFirst().get().getDictLabel();
                    sysClassSetting.setClassName(gradeIn.replace("班", "") + "(" + classNumber.replace("班", "") + ")" + "班");
                });
            });
        }
        return sysLocationSettingIPage;
    }

    /**
     * 分页查询位置设置列表--区域分开，一条记录，一个区域
     */
    @Override
    public IPage<SysLocationSetting> listDistinguishArea(SysLocationSetting sysLocationSetting) {
        sysLocationSetting.setIsDeleted("0");
        Page mpPage = new Page(Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_NUM), 1L)
                , Convert.toLong(ServletUtils.getParameterToInt(MybatisPageConstants.PAGE_SIZE), 10000L));
        IPage<SysLocationSetting> sysLocationSettingIPage = sysLocationSettingMapper.listDistinguishArea(mpPage, sysLocationSetting);
        if (!sysLocationSettingIPage.getRecords().isEmpty()) {

            // 获取年级列表
            SysDictData sysDictData = new SysDictData();
            sysDictData.setDictType("toy_release_grade");
            List<SysDictData> gradeDataList = sysDictDataService.selectDictDataList(sysDictData);

            // 获取班级列表
            sysDictData.setDictType("toy_release_class");
            List<SysDictData> classDataList = sysDictDataService.selectDictDataList(sysDictData);

            sysLocationSettingIPage.getRecords().stream().forEach(sysLocationSettingTmp -> {
                List<SysClassSetting> assiciationClassList = sysLocationSettingTmp.getAssiciationClass();

                // 遍历设置班级名称
                assiciationClassList.stream().forEach(sysClassSetting -> {
                    Optional<SysDictData> gradeInOptional = gradeDataList.stream().filter(gradeData -> gradeData.getDictValue().equals(sysClassSetting.getGradeIn())).findFirst();
                    if (gradeInOptional.isPresent()) {
                        String gradeIn = gradeInOptional.get().getDictLabel();
                        String classNumber = classDataList.stream().filter(classData -> classData.getDictValue().equals(sysClassSetting.getClassNumber())).findFirst().get().getDictLabel();
                        sysClassSetting.setClassName(gradeIn.replace("班", "") + classNumber.replace("班", "") + "班");
                    }
                });
            });
        }
        return sysLocationSettingIPage;
    }

    @Override
    public List<String> listType() {
        LambdaQueryWrapper<SysLocationSetting> sysLocationSettingWrapper = new LambdaQueryWrapper<>();
        sysLocationSettingWrapper.eq(SysLocationSetting::getIsDeleted, "0");
        sysLocationSettingWrapper.select(SysLocationSetting::getType);
        sysLocationSettingWrapper.groupBy(SysLocationSetting::getType);
        List<SysLocationSetting> sysLocationSettingList = sysLocationSettingMapper.selectList(sysLocationSettingWrapper);
        return sysLocationSettingList.isEmpty() ? new ArrayList<>() : sysLocationSettingList.stream().map(SysLocationSetting::getType).collect(Collectors.toList());
    }

    @Override
    public List<SysLocationSetting> selectLocationListByType(String type) {
        LambdaQueryWrapper<SysLocationSetting> sysLocationSettingWrapper = new LambdaQueryWrapper<>();
        sysLocationSettingWrapper.eq(SysLocationSetting::getIsDeleted, "0");
        sysLocationSettingWrapper.eq(StrUtils.isNotEmpty(type), SysLocationSetting::getType, type);
        return sysLocationSettingMapper.selectList(sysLocationSettingWrapper);
    }

    /**
     * 通过参数获取到所有符合条件的位置id
     *
     * @param sysLocationSetting 位置设置
     * @return
     */
    @Override
    public List<SysLocationSetting> locationIdList(SysLocationSetting sysLocationSetting) {
        return sysLocationSettingMapper.locationIdList(sysLocationSetting);
    }

    @Override
    public List<SysLocationSetting> locationIdList(Long[] locationIdArray) {
        LambdaQueryWrapper<SysLocationSetting> sysLocationSettingWrapper = new LambdaQueryWrapper<>();
        sysLocationSettingWrapper.in(SysLocationSetting::getId, locationIdArray);
        return sysLocationSettingMapper.selectList(sysLocationSettingWrapper);
    }

    /**
     * 查询位置设置列表
     *
     * @param sysLocationSetting 位置设置
     * @return 位置设置
     */
    @Override
    public List<SysLocationSetting> selectSysLocationSettingList(SysLocationSetting sysLocationSetting) {
        return sysLocationSettingMapper.selectSysLocationSettingList(sysLocationSetting);
    }

    @Override
    public List<SysLocationSetting> getSysLocationList(SysLocationSetting sysLocationSetting) {
        return sysLocationSettingMapper.getSysLocationList(sysLocationSetting);
    }

    @Override
    public List<SysLocationReClassDto> getRelatedClassList(SysLocationReClassDto sysLocationReClassDto) {
        return sysLocationSettingMapper.getRelatedClassList(sysLocationReClassDto);
    }

    /**
     * 新增位置设置
     *
     * @param sysLocationSetting 位置设置
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysLocationSetting(SysLocationSetting sysLocationSetting) {

        SysLocationSetting sysLocationSettingTmp = repeatCheck(SysLocationSetting.builder().id(sysLocationSetting.getId()).code(sysLocationSetting.getName()).build());
        if (Objects.nonNull(sysLocationSettingTmp)) {
            throw new ServiceException("位置名称重复");
        }

        if (StrUtils.isNotEmpty(sysLocationSetting.getCode())) {
            sysLocationSettingTmp = repeatCheck(SysLocationSetting.builder().id(sysLocationSetting.getId()).code(sysLocationSetting.getCode()).build());
            if (Objects.nonNull(sysLocationSettingTmp)) {
                throw new ServiceException("编号重复");
            }
        }

        // 插入位置信息
        int result = sysLocationSettingMapper.insert(sysLocationSetting);

        if (!sysLocationSetting.getAssiciationPutArea().isEmpty()) {
            // 批量插入与区域的关系
            ArrayList<SysReLocationArea> sysReLocationAreaList = new ArrayList<>();
            for (SysAreaSetting putArea : sysLocationSetting.getAssiciationPutArea()) {
                SysReLocationArea sysReLocationArea = new SysReLocationArea();
                sysReLocationArea.setAreaId(putArea.getAreaId());
                sysReLocationArea.setLocationId(sysLocationSetting.getId());
                sysReLocationArea.setCreateById(SecurityUtils.getLoginUser().getUserid());
                sysReLocationAreaList.add(sysReLocationArea);
            }
            result = sysReLocationAreaService.saveBatch(sysReLocationAreaList) ? 1 : 0;
        }
        return result;
    }

    /**
     * 修改位置设置
     *
     * @param sysLocationSetting 位置设置
     * @return 结果
     */
    @GlobalTransactional
    @Override
    public int updateSysLocationSetting(SysLocationSetting sysLocationSetting) {
        return 0;
    }

    /**
     * 批量删除位置设置
     *
     * @param ids 需要删除的位置设置主键
     * @return 结果
     */
    @Override
    public int deleteSysLocationSettingByIds(Long[] ids) {
        if (ids.length > 0) {
            List<SysReLocationArea> sysReLocationAreaList = sysReLocationAreaService
                    .selectSysReLocationAreaList(SysReLocationArea
                            .builder()
                            .locationId(ids[0])
                            .isDeleted("0")
                            .build());
            if (!CollectionUtils.isEmpty(sysReLocationAreaList)) {
                throw new ServiceException("该位置已绑定区域，请解绑区域后再试");
            }
        }

        return sysLocationSettingMapper.deleteSysLocationSettingByIds(ids);
    }

    /**
     * 删除位置设置信息
     *
     * @param id 位置设置主键
     * @return 结果
     */
    @Override
    public int deleteSysLocationSettingById(Long id) {
        return sysLocationSettingMapper.deleteById(id);
    }

    @Transactional
    @Override
    public void bindOrUnBindClass(SysLocationSetting sysLocationSetting) {
        String status = sysLocationSetting.getStatus();

        /** 状态 0 解绑 1 绑定*/
        if ("0".equals(status)) {
            List<SysClassSetting> assiciationClassList = sysLocationSetting.getAssiciationClass();
            sysReLocationClassService.updateSysReLocationClassBylocationIdAndAreaIds(sysLocationSetting.getId(),
                    assiciationClassList.stream().map(SysClassSetting::getId).toArray(Long[]::new));
        } else if ("1".equals(status)) {
            ArrayList<SysReLocationClass> sysReLocationClasseList = new ArrayList<>();
            sysLocationSetting.getAssiciationClass().forEach(sysLocationSettingTmp -> sysReLocationClasseList.add(SysReLocationClass.builder()
                    .locationId(sysLocationSetting.getId())
                    .classId(sysLocationSettingTmp.getId())
                    .createById(SecurityUtils.getLoginUser().getUserid())
                    .build()));
            sysReLocationClassService.saveBatch(sysReLocationClasseList);
        }
    }

    @Override
    public List<SysLocationSetting> locationAndAreaList(String tenantId) {
        return sysLocationSettingMapper.locationAndAreaList(tenantId);
    }

    @Override
    public List<SysLocationReClassDto> getRelatedLocationList(SysLocationReClassDto sysLocationReClassDto) {
        return sysLocationSettingMapper.getRelatedLocationList(sysLocationReClassDto);
    }

    @Override
    public List<SysDownLocation> getDownLocationList(SysLocationSetting sysLocationSetting) {
        return sysLocationSettingMapper.getDownLocationList(sysLocationSetting);
    }

    @Override
    public List<SysLocationSetting> getLocationSettingInfo(String attribute) {
        // 判断位置属性是否教室
        if("1".equals(attribute)){
            List<SysLocationSetting> locationSettingList = new ArrayList<>();
            List<SysClassSetting> list = sysClassSettingMapper.getAreaSetting();
            for (SysClassSetting sysClassSetting : list) {
                SysLocationSetting sysLocationSetting = new SysLocationSetting();
                sysLocationSetting.setName(sysClassSetting.getClassName());
                sysLocationSetting.setId(sysClassSetting.getId());
                locationSettingList.add(sysLocationSetting);
            }
            return locationSettingList;
        } else{
            return sysLocationSettingMapper.getLocationSettingInfo(attribute);
        }

    }

    @Override
    public List<SysReLocationClass> getLocationSettingInfoByClassId(String classIds) {
        LambdaQueryWrapper<SysReLocationClass> sysReLocationClassWrapper = new LambdaQueryWrapper<>();
        sysReLocationClassWrapper.eq(SysReLocationClass::getIsDeleted, ConstantUtils.IS_DELETED_0);
        List<String> classId = Arrays.stream(classIds.split(",")).collect(Collectors.toList());
        sysReLocationClassWrapper.in(SysReLocationClass::getClassId,classId);
        sysReLocationClassWrapper.select(SysReLocationClass::getLocationId);
        return sysReLocationClassMapper.selectList(sysReLocationClassWrapper);
    }

    @Override
    public List<SysReLocationClass> getLocationSettingInfoByClassIds(String classIds) {
        LambdaQueryWrapper<SysReLocationClass> sysReLocationClassWrapper = new LambdaQueryWrapper<>();
        sysReLocationClassWrapper.eq(SysReLocationClass::getIsDeleted, ConstantUtils.IS_DELETED_0);
        List<String> classId = Arrays.stream(classIds.split(",")).collect(Collectors.toList());
        sysReLocationClassWrapper.in(SysReLocationClass::getClassId,classId);
        sysReLocationClassWrapper.select(SysReLocationClass::getLocationId);
        return sysReLocationClassMapper.selectList(sysReLocationClassWrapper);
    }

    @Override
    public List<SysClassSetting> selectClassByLocationId(String locationId) {
        LambdaQueryWrapper<SysReLocationClass> sysReLocationClassWrapper = new LambdaQueryWrapper<>();
        sysReLocationClassWrapper.eq(SysReLocationClass::getIsDeleted,ConstantUtils.IS_DELETED_0);
        sysReLocationClassWrapper.eq(SysReLocationClass::getLocationId,locationId);
        sysReLocationClassWrapper.select(SysReLocationClass::getClassId);
        List<SysReLocationClass> sysReLocationClassList = sysReLocationClassMapper.selectList(sysReLocationClassWrapper);
        if(CollectionUtils.isEmpty(sysReLocationClassList)){
            return new ArrayList<>();
        }
        List<String> classIdList = sysReLocationClassList.stream().map(SysReLocationClass::getClassId).map(String::valueOf).collect(Collectors.toList());
        LambdaQueryWrapper<SysClassSetting> sysClassSettingWrapper = new LambdaQueryWrapper<>();
        sysClassSettingWrapper.eq(SysClassSetting::getIsDeleted,ConstantUtils.IS_DELETED_0);
        sysClassSettingWrapper.in(SysClassSetting::getId,classIdList);
        return sysClassSettingMapper.selectList(sysClassSettingWrapper);
    }

    @Override
    public List<SysLocationAttributeDto> getLocationSettingTree() {
        List<SysLocationAttributeDto> list = new ArrayList<>();
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType("attribute");
        List<SysDictData> sysDictDataClassList = sysDictDataService.selectDictDataList(sysDictData);
        if(CollectionUtils.isEmpty(sysDictDataClassList)){
            return list;
        }
        for (SysDictData dictData : sysDictDataClassList) {
            SysLocationAttributeDto sysLocationAttributeDto = new SysLocationAttributeDto();
            // 获取位置信息
            List<SysLocationSetting> locationSettingInfo = getLocationSettingInfo(dictData.getDictValue());
            if("1".equals(dictData.getDictValue())){
                if (!CollectionUtils.isEmpty(locationSettingInfo)) {
                    List<SysLocationDto> sysLocationAttributeDtos = new ArrayList<>();
                    for (SysLocationSetting sysLocationSetting : locationSettingInfo) {
                        SysLocationDto sysLocationDto = new SysLocationDto();
                        List<com.vctgo.system.domain.SysAreaSetting> areaSettingInfo = new ArrayList<>();
                        List<SysReLocationClass> sysReLocationClassList = sysClassSettingMapper.getSysReLocationClassByClassId(String.valueOf(sysLocationSetting.getId()));
                        if(!CollectionUtils.isEmpty(sysReLocationClassList)){
                            sysReLocationClassList = sysReLocationClassList.stream().filter(obj->obj.getLocationId()!=null).collect(Collectors.toList());
                            String locationIdStr =sysReLocationClassList.stream().map(obj->String.valueOf(obj.getLocationId())).collect(Collectors.joining(","));
                            areaSettingInfo = sysAreaSettingMapper.getAreaSettingInfo(locationIdStr);
                            areaSettingInfo = areaSettingInfo.stream()
                                    .collect(Collectors.toMap(
                                            com.vctgo.system.domain.SysAreaSetting::getAreaId,
                                            Function.identity(),
                                            (existing, replacement) -> existing
                                    )).values().stream()
                                    .collect(Collectors.toList());
                        }
                        List<SysAreaDto>  sysAreaDtos = new ArrayList<>();
                        for (com.vctgo.system.domain.SysAreaSetting sysAreaSetting : areaSettingInfo) {
                            SysAreaDto sysAreaDto = new SysAreaDto();
                            sysAreaDto.setId(sysAreaSetting.getAreaId());
                            sysAreaDto.setName(sysAreaSetting.getAreaName());
                            sysAreaDtos.add(sysAreaDto);
                        }
                        // 设置位置信息
                        sysLocationDto.setId(sysLocationSetting.getId());
                        sysLocationDto.setName(sysLocationSetting.getName());
                        sysLocationDto.setChildren(sysAreaDtos);
                        sysLocationAttributeDtos.add(sysLocationDto);
                    }
                    sysLocationAttributeDto.setChildren(sysLocationAttributeDtos);
                }
            } else {
                if (!CollectionUtils.isEmpty(locationSettingInfo)) {
                    List<SysLocationDto> sysLocationAttributeDtos = new ArrayList<>();
                    for (SysLocationSetting sysLocationSetting : locationSettingInfo) {
                        SysLocationDto sysLocationDto = new SysLocationDto();
                        List<com.vctgo.system.domain.SysAreaSetting> areaSettingInfo = new ArrayList<>();
                        areaSettingInfo = sysAreaSettingMapper.getAreaSettingInfo(String.valueOf(sysLocationSetting.getId()));
                        if (!CollectionUtils.isEmpty(areaSettingInfo)){
                            areaSettingInfo = areaSettingInfo.stream()
                                    .collect(Collectors.toMap(
                                            com.vctgo.system.domain.SysAreaSetting::getAreaId,
                                            Function.identity(),
                                            (existing, replacement) -> existing
                                    )).values().stream()
                                    .collect(Collectors.toList());

                        }
                        List<SysAreaDto>  sysAreaDtos = new ArrayList<>();
                        for (com.vctgo.system.domain.SysAreaSetting sysAreaSetting : areaSettingInfo) {
                            SysAreaDto sysAreaDto = new SysAreaDto();
                            sysAreaDto.setId(sysAreaSetting.getAreaId());
                            sysAreaDto.setName(sysAreaSetting.getAreaName());
                            sysAreaDtos.add(sysAreaDto);
                        }
                        // 设置位置信息
                        sysLocationDto.setId(sysLocationSetting.getId());
                        sysLocationDto.setName(sysLocationSetting.getName());
                        sysLocationDto.setChildren(sysAreaDtos);
                        sysLocationAttributeDtos.add(sysLocationDto);
                    }
                    sysLocationAttributeDto.setChildren(sysLocationAttributeDtos);
                }
            }

            // 设置属性信息
            sysLocationAttributeDto.setId(dictData.getDictValue());
            sysLocationAttributeDto.setName(dictData.getDictLabel());
            list.add(sysLocationAttributeDto);
        }
        return list;
    }
}
