package com.cool.store.service.impl;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.cool.store.constants.CommonConstants;
import com.cool.store.context.CurrentUserHolder;
import com.cool.store.dto.StoreNameDTO;
import com.cool.store.dao.store.StoreMasterSignerInfoDAO;
import com.cool.store.dto.store.*;
import com.cool.store.dao.EnterpriseUserDAO;
import com.cool.store.dao.EnterpriseUserRoleDao;
import com.cool.store.dao.StoreDao;
import com.cool.store.dao.SysRoleDao;
import com.cool.store.dto.StoreDTO;
import com.cool.store.dto.UserSimpleDTO;
import com.cool.store.entity.*;
import com.cool.store.entity.store.StoreMasterSignerInfoDO;
import com.cool.store.enums.*;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.*;
import com.cool.store.request.store.StoreListRequest;
import com.cool.store.response.MiniShopsResponse;
import com.cool.store.service.StoreService;
import com.cool.store.service.UserAuthMappingService;
import com.cool.store.utils.BeanUtil;
import com.cool.store.utils.GeoMapUtil;
import com.cool.store.utils.poi.constant.Constants;
import com.cool.store.vo.SysRoleVO;
import com.cool.store.vo.store.StoreListVO;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author suzhuhong
 * @Date 2025/5/13 9:56
 * @Version 1.0
 */
@Service
@Slf4j
public class StoreServiceImpl implements StoreService {

    @Resource
    StoreDao storeDao;
    @Resource
    private EnterpriseUserDAO enterpriseUserDAO;
    @Resource
    private EnterpriseUserRoleDao enterpriseUserRoleDao;
    @Resource
    private SysRoleDao sysRoleDao;
    @Resource
    private UserAuthMappingService userAuthMappingService;
    @Resource
    private RegionMapper regionMapper;
    @Resource
    SysRoleMapper sysRoleMapper;
    @Resource
    StoreMapper storeMapper;
    @Resource
    UserAuthMappingMapper userAuthMappingMapper;
    @Resource
    EnterpriseUserMapper enterpriseUserMapper;
    @Resource
    EnterpriseUserGroupMappingMapper enterpriseUserGroupMappingMapper;
    @Resource
    StoreMasterSignerInfoDAO storeMasterSignerInfoDAO;
    @Resource
    EnterpriseUserRoleMapper enterpriseUserRoleMapper;

    @Override
    public PageInfo<StoreDTO> getStoreExtendFieldInfo(Integer pageSize, Integer pageNum, Boolean queryOperations) {
        if (pageSize>200){
            throw new ServiceException(ErrorCodeEnum.ERROR_MESSAGE,"单次最多获取200条门店数据");
        }
        PageHelper.startPage(pageNum,pageSize);
        List<StoreDO> list = storeDao.list(null);
        PageInfo info = new PageInfo<>(list);
        if (CollectionUtils.isEmpty(list)){
            return  info;
        }
        //使用regionId与branch 合并的集合
        Set<Long> regionIds = list.stream()
                .filter(Objects::nonNull)
                .flatMap(store -> Stream.of(store.getBranch(), store.getRegionId()))
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());

        List<String> userIds = list.stream().filter(Objects::nonNull).map(StoreDO::getJoinSupervision).collect(Collectors.toList());
        Map<String, EnterpriseUserDO> userNameMap = enterpriseUserDAO.getUserMap(userIds);


        List<RegionDO> regionList = regionMapper.getByIds(new ArrayList<>(regionIds));
        Map<Long, String> regionNameMap = regionList.stream().collect(Collectors.toMap(RegionDO::getId, RegionDO::getName));

        // 批量查询门店对应的运营顾问
        Map<String, List<UserSimpleDTO>> operationsConsultantMap = null;
        if (Boolean.TRUE.equals(queryOperations)) {
            operationsConsultantMap = getOperationsConsultantMap(list);
        }

        List<StoreDTO> storeDTOS = processStores(list, regionNameMap, userNameMap, operationsConsultantMap);
        info.setList(storeDTOS);
        return info;
    }

    @Override
    public PageInfo<StoreNameDTO> getIotStoreList(Integer pageNum, Integer pageSize) {
        if (pageSize > 200) {
            throw new ServiceException(ErrorCodeEnum.ERROR_MESSAGE, "单次最多获取200条门店数据");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<StoreDO> list = storeDao.list(1);
        PageInfo info = new PageInfo<>(list);
        if (CollectionUtils.isEmpty(list)){
            return info;
        }
        List<StoreNameDTO> result = list.stream().map(v -> new StoreNameDTO(v.getStoreName(), v.getStoreNum())).collect(Collectors.toList());
        info.setList(result);
        return info;
    }

    @Override
    public PageInfo<MiniShopsResponse> getStoreListByMobile(String mobile,Integer pageNum,Integer pageSize,String storeName,String storeNum) {
        //根据手机号查询 标品userId
        EnterpriseUserDO enterpriseUserDO = enterpriseUserDAO.selectByMobile(mobile);
        List<String> regionIds = new ArrayList<>();
        if (Objects.nonNull(enterpriseUserDO)){
            //获取用户职位
            List<Long> userRoleIds = enterpriseUserRoleDao.getUserRoleIds(enterpriseUserDO.getUserId());
            //查询职位详情，筛选掉店外职位
            List<SysRoleDO> roleIds = sysRoleDao.selectRoleByRoleIds(userRoleIds);
            List<SysRoleDO> sysRoleDOS = roleIds.stream().filter(role -> "store_inside".equals(role.getPositionType())
                    ||UserRoleEnum.SUPERVISION.getCode().equals(role.getId())
                    ||UserRoleEnum.HEAD_OF_DIVISION.getCode().equals(role.getId())
                    ||UserRoleEnum.SERVICE_PACKAGE_DEDICATED.getCode().equals(role.getId())
                    ||UserRoleEnum.REGION_MANAGER.getCode().equals(role.getId())).collect(Collectors.toList());
            if (CollectionUtils.isNotEmpty(sysRoleDOS)){
                //获取用户管辖区域
                List<UserAuthMappingDO> userAuthMapping = userAuthMappingService.listUserAuthMappingByUserId(enterpriseUserDO.getUserId());
                if (CollectionUtils.isNotEmpty(userAuthMapping)){
                    regionIds.addAll(userAuthMapping.stream().map(UserAuthMappingDO::getMappingId).collect(Collectors.toList()));
                }
            }
        }
        PageHelper.startPage(pageNum,pageSize);
        List<MiniShopsResponse> list = storeDao.getSubStoreByRegionIdsAndMobile(regionIds,mobile,storeName,storeNum);
        return  new PageInfo<>(list);
    }

    @Override
    public List<StoreUserPositionDTO> getStoreUser(List<String> storeCodeList) {
        if (CollectionUtils.isNotEmpty(storeCodeList)&&storeCodeList.size()>=100){
            throw new ServiceException(ErrorCodeEnum.ERROR_MESSAGE,"单次最多获取100条门店数据");
        }
        List<StoreDO> list = storeDao.getStoreNumByStoreCodes(storeCodeList);
        if (CollectionUtils.isEmpty(list)){
            return  new ArrayList<>();
        }
        List<String> storeIds = CollStreamUtil.toList(list, StoreDO::getStoreId);
        Map<String, StoreMasterSignerInfoDO> signerMap = storeMasterSignerInfoDAO.getSignerMapByStoreIds(storeIds);


        List<String> userIds = list.stream().filter(Objects::nonNull).map(StoreDO::getJoinSupervision).collect(Collectors.toList());
        Map<String, EnterpriseUserDO> userNameMap = enterpriseUserDAO.getUserMap(userIds);

        List<StoreUserPositionDTO> result = new ArrayList<>();
        list.forEach(x->{
            StoreUserPositionDTO storeUserPositionDTO = new StoreUserPositionDTO();
            List<StoreUserDTO> storeUserPosition = this.getStoreUserPositionDTO(x.getStoreId());
            Map<String, List<StoreUserDTO>> storeUserDTOMap = ListUtils.emptyIfNull(storeUserPosition).stream().collect(Collectors.groupingBy(k -> k.getUserId()));
            storeUserPositionDTO.setStoreId(x.getStoreId());
            storeUserPositionDTO.setStoreName(x.getStoreName());
            storeUserPositionDTO.setShopCode(x.getStoreNum());
            storeUserPositionDTO.setStoreCode(x.getStoreNum());
            List<StoreUserDTO> userList = Lists.newArrayList();
            for (String userId : storeUserDTOMap.keySet()) {
                List<StoreUserDTO> singleUserDTOList = storeUserDTOMap.get(userId);
                if(CollectionUtils.isEmpty(singleUserDTOList)){
                    continue;
                }
                StoreUserDTO storeUserDTO = singleUserDTOList.get(0);
                List<String> positionNameList = ListUtils.emptyIfNull(singleUserDTOList)
                        .stream().map(StoreUserDTO::getPositionName).collect(Collectors.toList());
                storeUserDTO.setPositionName(String.join(Constants.COMMA, positionNameList));
                userList.add(storeUserDTO);
            }
            StoreMasterSignerInfoDO signerInfoDO = signerMap.get(x.getStoreId());
            if (Objects.nonNull(signerInfoDO)) {
                Set<String> mobiles = CollStreamUtil.toSet(userList, StoreUserDTO::getMobile);
                if (StringUtils.isNotBlank(signerInfoDO.getSigner1Mobile()) && mobiles.add(signerInfoDO.getSigner1Mobile())) {
                    userList.add(new StoreUserDTO(signerInfoDO.getSigner1Name(), signerInfoDO.getSigner1Mobile(),"加盟商"));
                }
                if (StringUtils.isNotBlank(signerInfoDO.getSigner2Mobile()) && mobiles.add(signerInfoDO.getSigner2Mobile())) {
                    userList.add(new StoreUserDTO(signerInfoDO.getSigner2Name(), signerInfoDO.getSigner2Mobile(),"加盟商"));
                }
            }
            EnterpriseUserDO enterpriseUserDO = userNameMap.get(x.getJoinSupervision());
            if (Objects.nonNull(enterpriseUserDO)){
                StoreUserDTO storeUserDTO = new StoreUserDTO();
                storeUserDTO.setUserId(enterpriseUserDO.getUserId());
                storeUserDTO.setUserName(enterpriseUserDO.getName());
                storeUserDTO.setMobile(enterpriseUserDO.getMobile());
                storeUserDTO.setPositionName("招商经理");
                userList.add(storeUserDTO);
            }
            storeUserPositionDTO.setUserList(userList);
            result.add(storeUserPositionDTO);
        });
        return result;
    }



    private List<StoreUserDTO> getStoreUserPositionDTO(String storeId){
        if (StrUtil.isBlank(storeId)) {
            throw new ServiceException(ErrorCodeEnum.PARAMS_VALIDATE_ERROR.getCode(), "请选择门店后查询");
        }
        List<AuthStoreUserDTO> authStoreUserDTOList = this.authStoreUser(
                Collections.singletonList(storeId), "store_inside");

        if (CollectionUtils.isEmpty(authStoreUserDTOList)) {
            return new ArrayList<>();
        }
        List<String> userIdList = ListUtils.emptyIfNull(authStoreUserDTOList)
                .stream()
                .map(AuthStoreUserDTO::getUserIdList)
                .flatMap(Collection::stream)
                .distinct()
                .collect(Collectors.toList());
        if (CollUtil.isEmpty(userIdList)) {
            return new ArrayList<>();
        }
        List<StoreUserDTO> result = sysRoleMapper.userAndPositionList( userIdList, null, "store_inside");
        return result;
    }



    @Override
    public List<AuthStoreUserDTO> getStorePositionUserList( List<String> storeIds,
                                                           List<String> positionIds,
                                                           List<String> nodePersonList,
                                                           List<String> groupIdList,
                                                           List<String> regionIdList) {
        log.info("StoreServiceImpl getStorePositionUserList param storeIds:{},positionIds:{},nodePersonList:{},groupIdList:{},regionIdList:{}",
                JSONObject.toJSONString(storeIds),
                JSONObject.toJSONString(positionIds), JSONObject.toJSONString(nodePersonList),
                JSONObject.toJSONString(groupIdList), JSONObject.toJSONString(regionIdList));
        List<AuthStoreUserDTO> authStoreUsers = this.authStoreUser(storeIds, null);
        if (CollUtil.isEmpty(authStoreUsers)) {
            return new ArrayList<>();
        }
        List<String> allUserIds = Lists.newArrayList();
        if (CollectionUtils.isNotEmpty(positionIds)) {
            List<String> userIds = sysRoleMapper.getPositionUserIds(positionIds);
            allUserIds.addAll(userIds);
        }
        if (CollectionUtils.isNotEmpty(groupIdList)) {
            List<String> groupUserIdList = enterpriseUserGroupMappingMapper.getUserIdsByGroupIdList(groupIdList);
            if (CollectionUtils.isNotEmpty(groupUserIdList)) {
                allUserIds.addAll(groupUserIdList);
            }
        }
        if (CollectionUtils.isNotEmpty(regionIdList)) {
            List<String> regionUserIdList = enterpriseUserMapper.getUserIdsByRegionIdList(regionIdList);
            if (CollectionUtils.isNotEmpty(regionUserIdList)) {
                allUserIds.addAll(regionUserIdList);
            }
        }
        if (CollectionUtils.isNotEmpty(nodePersonList)) {
            allUserIds.addAll(nodePersonList);
        }
        List<String> finalAllUserIds = allUserIds;
        authStoreUsers.forEach(f -> f.getUserIdList().retainAll(finalAllUserIds));
        if (CollectionUtils.isNotEmpty(authStoreUsers)) {
            authStoreUsers = authStoreUsers.stream().distinct().collect(Collectors.toList());
        }
        return authStoreUsers;
    }

    @Override
    public PageInfo<StoreListVO> getAuthStoreList(StoreListRequest request) {
        String userId = CurrentUserHolder.getUserId();

        List<String> regionIds = null;
        Boolean isAdmin = enterpriseUserRoleDao.checkIsAdmin(userId);
        if (!isAdmin) {
            List<UserAuthMappingDO> userAuthMappings = userAuthMappingService.listUserAuthMappingByUserId(userId);
            if (CollectionUtils.isEmpty(userAuthMappings)) {
                return new PageInfo<>(Collections.emptyList());
            }
            regionIds = CollStreamUtil.toList(userAuthMappings, UserAuthMappingDO::getMappingId);
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<StoreDO> storeList = storeDao.getStoreByRegionIds(regionIds, request.getKeyword());
        List<String> storeIds = CollStreamUtil.toList(storeList, StoreDO::getStoreId);
        Map<String, BigDecimal> scoreMap = storeDao.getScoreByStoreIds(storeIds);
        PageInfo<StoreListVO> page = BeanUtil.toPage(new PageInfo<>(storeList), StoreListVO.class);
        page.getList().forEach(v -> {
            v.setScore(scoreMap.getOrDefault(v.getStoreId(), new BigDecimal(12)));
        });
        return page;
    }

    @Override
    public Boolean handleStoreLogLai(Integer flag,String specialStoreCode) {
        boolean hasNext = true;
        int pageNum = 1;
        int pageSize = 200;
        while (hasNext) {
            PageHelper.startPage(pageNum, pageSize);
            List<StoreAddressDTO> list = storeDao.getStoreAddress(flag,specialStoreCode);

            //门店列表
            if (CollectionUtils.isEmpty(list)) {
                log.info("------门店列表为空------");
                return Boolean.TRUE;
            }
            List<StoreDO> storeList = new ArrayList<>();
            for (StoreAddressDTO x : list) {
                GeoMapUtil.AddressInfo addressInfo = GeoMapUtil.reverseGeoCoding(x.getLatitude(), x.getLongitude());
                if (Objects.isNull(addressInfo)){
                    continue;
                }
                StoreDO storeDO = new StoreDO();
                storeDO.setStoreId(x.getStoreId());
                storeDO.setProvince(addressInfo.getProvince());
                storeDO.setCity(addressInfo.getCity());
                storeDO.setCounty(addressInfo.getDistrict());
                storeList.add(storeDO);
            }
            storeDao.batchUpdateAddress(storeList);

            hasNext = list.size() >= pageSize;
            pageNum++;
        }

        return Boolean.TRUE;
    }


    @Override
    public List<AuthStoreUserDTO> authStoreUser(List<String> storeIdList, String positionType) {
        List<AuthStoreUserDTO> result = new ArrayList<>();
        if (CollectionUtils.isEmpty(storeIdList)) {
            return result;
        }
        //将拥有管理员角色、角色属性为全企业数据的人查询出来
        List<SysRoleVO> roleUserByRoleId = sysRoleMapper.getRoleUserByRoleEnum(Role.MASTER.getRoleEnum(),positionType);
        List<SysRoleVO> roleUserByRoleAuth = sysRoleMapper.getRoleUserByRoleAuth(AuthRoleEnum.ALL.getCode(),positionType);
        //组合出拥有所有门店信息的人
        List<String>  allStoreUserIdList = getAllStoreUserIdList(roleUserByRoleId, roleUserByRoleAuth);
        //查询出有门店权限配置的的人员
        //   1.将门店区域切分出门店所属于的区域ID
        //   2.将配置了区域的人 查询出来
        //   3.将配置了门店的人 查询出来
        List<StoreAreaDTO> storeAreaList = storeMapper.getStoreAreaList(storeIdList);
        if (CollectionUtils.isEmpty(storeAreaList)) {
            return Collections.emptyList();
        }

        List<String> fullAreaIdList = ListUtils.emptyIfNull(storeAreaList)
                .stream()
                .map(StoreAreaDTO::getAreaIdList)
                .flatMap(Collection::stream)
                .distinct()
                .collect(Collectors.toList());


        List<String> lastAreaIdList = ListUtils.emptyIfNull(storeAreaList)
                .stream()
                .map(StoreAreaDTO::getAreaId)
                .distinct()
                .collect(Collectors.toList());

        //除不包含子区域的可视化范围的区域配置。
        List<UserAuthMappingDO> regionUserAuthMappingList = userAuthMappingMapper.listUserAuthMappingByAuth(
                UserAuthMappingTypeEnum.REGION.getCode(), fullAreaIdList, positionType, AuthRoleEnum.NOT_INCLUDE_SUBORDINATE.getCode());
        //不包含子区域的的直属连接门店的区域下的配置(会重复一些选择了上面数据)
        List<UserAuthMappingDO> notIncludeRegionUserAuthMappingList = userAuthMappingMapper.listUserAuthMappingByAuth(
                UserAuthMappingTypeEnum.REGION.getCode(), lastAreaIdList, positionType, null);
        //配置了门店的的配置
        List<UserAuthMappingDO> storeUserAuthMappingList = userAuthMappingMapper.listUserAuthMappingByMappingList(
                storeIdList, UserAuthMappingTypeEnum.STORE.getCode());
        return ListUtils.emptyIfNull(storeAreaList)
                .stream()
                .map(data -> mapAuthStoreUserDTO(regionUserAuthMappingList, allStoreUserIdList, storeUserAuthMappingList, notIncludeRegionUserAuthMappingList, data))
                .collect(Collectors.toList());
    }

    private List<String> getAllStoreUserIdList(List<SysRoleVO> roleUserByRoleId, List<SysRoleVO> roleUserByRoleAuth) {
        List<String> allUserIdList= new ArrayList<>();
        List<String> masterUserList = ListUtils.emptyIfNull(roleUserByRoleId).stream()
                .map(SysRoleVO::getEnterpriseDOs)
                .flatMap(Collection::stream)
                .map(EnterpriseUserDO::getUserId)
                .collect(Collectors.toList());
        List<String> roleAllUserList = ListUtils.emptyIfNull(roleUserByRoleAuth).stream()
                .map(SysRoleVO::getEnterpriseDOs)
                .flatMap(Collection::stream)
                .map(EnterpriseUserDO::getUserId)
                .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(masterUserList)) {
            allUserIdList.addAll(masterUserList);
        }
        if (CollectionUtils.isNotEmpty(roleAllUserList)) {
            allUserIdList.addAll(roleAllUserList);
        }
        return ListUtils.emptyIfNull(allUserIdList)
                .stream()
                .distinct()
                .collect(Collectors.toList());

    }

    private AuthStoreUserDTO mapAuthStoreUserDTO(
            List<UserAuthMappingDO> regionUserAuthMappingList,
            List<String> allStoreUserIdList,
            List<UserAuthMappingDO> storeUserAuthMappingList,
            List<UserAuthMappingDO> notIncludeRegionUserAuthMappingList,
            StoreAreaDTO data) {

        //组装全部的人员信息
        List<String> authStoreIdList = new ArrayList<>();
        AuthStoreUserDTO authStoreUserDTO = new AuthStoreUserDTO();
        authStoreUserDTO.setStoreId(data.getStoreId());
        authStoreUserDTO.setStoreName(data.getStoreName());
        //组装拥有所有门店信息的人
        if (CollectionUtils.isNotEmpty(allStoreUserIdList)) {
            authStoreIdList.addAll(allStoreUserIdList);
        }
        //组装配置了区域权限的人(除了不包含子区域可视化范围)
        List<String> storeAreaIdList = data.getAreaIdList();
        List<String> regionUserIdList = ListUtils.emptyIfNull(regionUserAuthMappingList).stream()
                .filter(area -> storeAreaIdList.contains(area.getMappingId()))
                .map(UserAuthMappingDO::getUserId)
                .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(regionUserIdList)) {
            authStoreIdList.addAll(regionUserIdList);
        }
        //组装配置了区域权限的人(不包含子区域可视化范围)
        List<String> notIncludeRegionUserIdList = ListUtils.emptyIfNull(notIncludeRegionUserAuthMappingList).stream()
                .filter(area -> StringUtils.equals(data.getAreaId(), area.getMappingId()))
                .map(UserAuthMappingDO::getUserId)
                .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(notIncludeRegionUserIdList)) {
            authStoreIdList.addAll(notIncludeRegionUserIdList);
        }
        //组装配置了门店权限的人
        List<String> storeUserIdList = ListUtils.emptyIfNull(storeUserAuthMappingList).stream()
                .filter(store -> StringUtils.equals(store.getMappingId(), data.getStoreId()))
                .map(UserAuthMappingDO::getUserId)
                .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(storeUserIdList)) {
            authStoreIdList.addAll(storeUserIdList);
        }

        //去除重复数据
        List<String> distinctUserIdList = ListUtils.emptyIfNull(authStoreIdList).stream()
                .distinct()
                .collect(Collectors.toList());
        authStoreUserDTO.setUserIdList(distinctUserIdList);
        return authStoreUserDTO;
    }

    /**
     * 批量查询门店对应的运营顾问
     * 优化：拆分复杂SQL为多个简单查询，在内存中组装
     *
     * @param storeList 门店列表
     * @return 门店ID -> 运营顾问列表
     */
    private Map<String, List<UserSimpleDTO>> getOperationsConsultantMap(List<StoreDO> storeList) {
        if (CollectionUtils.isEmpty(storeList)) {
            return Collections.emptyMap();
        }

        // 查询运营顾问角色的用户ID列表
        Long roleId = UserRoleEnum.SUPERVISION.getCode();
        List<String> consultantUserIds = enterpriseUserRoleMapper.selectUserIdsByRoleId(String.valueOf(roleId));
        if (CollectionUtils.isEmpty(consultantUserIds)) {
            return Collections.emptyMap();
        }

        // 查询这些用户的权限映射（门店/区域）
        List<UserAuthMappingDO> userAuthMappings = userAuthMappingMapper.listUserAuthMappingByUserIdList(consultantUserIds);
        if (CollectionUtils.isEmpty(userAuthMappings)) {
            return Collections.emptyMap();
        }

        // 构建用户权限映射（按用户ID分组）
        Map<String, List<UserAuthMappingDO>> userAuthMap = CollStreamUtil.groupByKey(userAuthMappings, UserAuthMappingDO::getUserId);

        // 构建门店ID集合和门店regionPath映射，用于快速匹配
        Map<String, String> storeRegionPathMap = CollStreamUtil.toMap(storeList, StoreDO::getStoreId, s -> s.getRegionPath() == null ? "" : s.getRegionPath());

        List<UserSimpleDTO> resultUserList = new ArrayList<>();
        for (String userId : consultantUserIds) {
            List<UserAuthMappingDO> authList = userAuthMap.get(userId);
            if (CollectionUtils.isEmpty(authList)) {
                continue;
            }
            // 遍历门店，匹配该用户有权限的门店
            for (Map.Entry<String, String> entry : storeRegionPathMap.entrySet()) {
                String storeId = entry.getKey();
                String regionPath = entry.getValue();
                boolean isMatch = false;
                for (UserAuthMappingDO auth : authList) {
                    if ("store".equals(auth.getType()) && storeId.equals(auth.getMappingId())) {
                        // 方式1: 用户配置了门店权限，直接匹配
                        isMatch = true;
                        break;
                    } else if ("region".equals(auth.getType()) && StringUtils.isNotBlank(regionPath)) {
                        // 方式2: 用户配置了区域权限，匹配 region_path
                        String regionId = auth.getMappingId();
                        if (regionPath.contains("/" + regionId + "/")) {
                            isMatch = true;
                            break;
                        }
                    }
                }
                if (isMatch) {
                    resultUserList.add(new UserSimpleDTO(userId, storeId));
                }
            }
        }
        if (CollectionUtils.isNotEmpty(resultUserList)) {
            List<String> userIds = resultUserList.stream()
                    .map(UserSimpleDTO::getUserId)
                    .distinct()
                    .collect(Collectors.toList());
            List<EnterpriseUserDO> userInfoList = enterpriseUserMapper.searchUserByUserIdsAndKeyword(userIds, null);
            Map<String, EnterpriseUserDO> userInfoMap = CollStreamUtil.toMap(userInfoList, EnterpriseUserDO::getUserId, u -> u);

            return resultUserList.stream()
                    .filter(v -> userInfoMap.containsKey(v.getUserId()))
                    .peek(v -> {
                        EnterpriseUserDO user = userInfoMap.get(v.getUserId());
                        v.setName(user.getName());
                    }).collect(Collectors.groupingBy(UserSimpleDTO::getStoreId));
        }
        return Collections.emptyMap();
    }


    public static List<StoreDTO> processStores(List<StoreDO> stores, Map<Long, String> regionMap,
                                               Map<String, EnterpriseUserDO> userMap,
                                               Map<String, List<UserSimpleDTO>> operationsConsultantMap) {
        // 处理每个门店
        return stores.stream().map(store -> {
            StoreDTO dto = BeanUtil.toBean(store, StoreDTO.class);
            dto.setStoreCode(store.getStoreNum());
            dto.setStoreAvatar(store.getAvatar());
            dto.setStoreType(StoreTypeEnum.getMessage(store.getStoreType()));
            dto.setJoinMode(JoinModeEnum.getByCode(store.getJoinModel()));
            dto.setBrand(FranchiseBrandEnum.getDescByCode(store.getJoinBrand()));
            dto.setOrderMiniProgramName(store.getMiniProgramOrderStoreName());
            dto.setStatus(StoreStatusEnum.getName(store.getStoreStatus()));
            dto.setCity(store.getCity());
            dto.setProvince(store.getProvince());
            dto.setCity(store.getCity());
            if (store.getRegionId() != null){
                dto.setRegionId(store.getRegionId());
                dto.setManagerSupervisionName(regionMap.get(store.getRegionId()));
            }
            if (store.getBranch()!=null){
                dto.setBranchId(store.getBranch());
                dto.setBranchName(regionMap.get(store.getBranch()));
            }
            if (store.getJoinSupervision()!=null){
                EnterpriseUserDO enterpriseUserDO = userMap.getOrDefault(store.getJoinSupervision(),new EnterpriseUserDO());
                dto.setInvestManager(enterpriseUserDO.getUserId());
                dto.setInvestManagerMobile(enterpriseUserDO.getMobile());
            }
            // 设置运营顾问
            if (operationsConsultantMap != null) {
                dto.setOperationsConsultant(operationsConsultantMap.getOrDefault(store.getStoreId(), Collections.emptyList()));
            }
            return dto;
        }).collect(Collectors.toList());
    }

}
