# SettingServiceImpl.java

    package cloud.wconnect.rateshop.service.rateshop.impl;

    import cloud.wconnect.commonlib.component.BeanMapper;
    import cloud.wconnect.commonlib.cons.CurrencyEnum;
    import cloud.wconnect.commonlib.pojo.dto.PropertyDTO;
    import cloud.wconnect.commonlib.service.PropertyInternalService;
    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.rateshop.cons.rateshop.RateTypeEnum;
    import cloud.wconnect.rateshop.entity.rateshop.*;
    import cloud.wconnect.rateshop.mapper.rateshop.SettingMapper;
    import cloud.wconnect.rateshop.pojo.dto.rateshop.hotel.PropertyHotelDTO;
    import cloud.wconnect.rateshop.pojo.vo.rateshop.*;
    import cloud.wconnect.rateshop.service.rateshop.*;
    import com.baomidou.dynamic.datasource.annotation.DS;
    import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.google.common.collect.Lists;
    import org.apache.commons.collections.CollectionUtils;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

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

    import static java.util.stream.Collectors.groupingBy;

    /**
     * <p>
     * rate_shop功能配置 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2023-06-07
     */
    @DS("rate_shop")
    @Service
    public class SettingServiceImpl extends ServiceImpl<SettingMapper, Setting> implements ISettingService {

        @Autowired
        private IPropertyHotelService propertyHotelService;
        @Autowired
        private PropertyInternalService propertyInternalService;
        @Autowired
        private ITrendSourceSettingService trendSourceSettingService;
        @Autowired
        private BeanMapper beanMapper;
        @Autowired
        private ObjectMapper objectMapper;
        @Autowired
        private IEventSourceSettingService eventSourceSettingService;
        @Autowired
        private IRoomTypeService roomTypeService;

        /**
         * 根据hotelId和币种id查询property setting集合
         *
         * @param hotelId    酒店id
         * @param currencyId 币种id
         * @param pos        销售地区
         * @return java.util.List<cloud.wconnect.rateshop.entity.rateshop.Setting>
         * @author lichen
         * @date 2023/6/7
         */
        @Override
        public List<Setting> listByHotelIdAndCurrencyIdAndPos(Integer hotelId, Integer currencyId, String pos) {
            //资产酒店关系 集合
            List<PropertyHotel> propertyHotels = propertyHotelService.list(new LambdaQueryWrapper<PropertyHotel>()
                    .eq(PropertyHotel::getHotelId, hotelId)
                    .eq(PropertyHotel::getCurrencyId, currencyId)
                    .eq(PropertyHotel::getPos, pos));
            if (CollectionUtils.isEmpty(propertyHotels)) return Collections.emptyList();

            //资产id集合
            List<Integer> propertyIds = propertyHotels.stream().map(PropertyHotel::getPropertyId).collect(Collectors.toList());

            //查询资产信息
            List<PropertyDTO> property = propertyInternalService.getProperty(propertyIds);
            //过滤出开启的资产
            List<Integer> openPropertyIds = property.stream().filter(PropertyDTO::getStatus).map(PropertyDTO::getId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(openPropertyIds)) return Collections.emptyList();

            //property setting 集合
            return list(new LambdaQueryWrapper<Setting>().in(Setting::getPropertyId, openPropertyIds));
        }

        /**
         * 根据需求趋势地区ID查询property setting集合
         *
         * @param locationId 需求趋势地区ID
         * @return java.util.List<cloud.wconnect.rateshop.entity.rateshop.Setting>
         * @author lichen
         * @date 2023/6/7
         */
        @Override
        public List<Setting> listByTrendSourceLocationId(Integer locationId) {
            //查询需求趋势关联的资产ID
            List<Integer> propertyIds = trendSourceSettingService.list(new LambdaQueryWrapper<TrendSourceSetting>()
                            .eq(TrendSourceSetting::getLocationId, locationId))
                    .stream().map(TrendSourceSetting::getPropertyId).toList();
            if (CollectionUtils.isEmpty(propertyIds)) return Collections.emptyList();
            //查询资产信息
            List<PropertyDTO> propertyDTOList = propertyInternalService.getProperty(propertyIds);
            //过滤出开启的资产
            List<Integer> openPropertyIds = propertyDTOList.stream().filter(PropertyDTO::getStatus).map(PropertyDTO::getId).collect(Collectors.toList());
            if (CollectionUtils.isEmpty(openPropertyIds)) return Collections.emptyList();

            //查询资产配置的Setting
            return list(new LambdaQueryWrapper<Setting>().in(Setting::getPropertyId, openPropertyIds));
        }

        /**
         * 根据资产id查询rateShop功能设置信息
         *
         * @param propertyId 资产id
         * @return cloud.wconnect.rateshop.entity.rateshop.Setting
         * @author lichen
         * @date 2023/6/17
         */
        @Override
        public Setting getByPropertyId(Integer propertyId) {
            return getOne(new LambdaQueryWrapper<Setting>().eq(Setting::getPropertyId, propertyId));
        }

        /**
         * 根据资产id查询rateShop功能设置信息
         *
         * @param propertyId 资产ID
         * @return cloud.wconnect.rateshop.pojo.vo.rateshop.SettingVO
         * @author lichen
         * @date 2025/5/16
         */
        @Override
        public SettingVO getSettingVO(Integer propertyId) {
            //根据资产id查询rateShop功能设置信息
            Setting setting = getByPropertyId(propertyId);
            if (Objects.isNull(setting)) return null;

            SettingVO settingVO = new SettingVO();
            //设置含税标志
            settingVO.setContainTaxFlag(setting.getContainTaxFlag());
            //设置货币符号
            settingVO.setCurrency(CurrencyEnum.getSymbolById(setting.getCurrencyId()));
            //设置销售地区
            settingVO.setPos(setting.getPos());

            //设置房价配置规则
            List<RateConfigVO> rateConfigVOS = JsonUtils.convertStringToList(objectMapper, setting.getRateConfig(), RateConfigVO.class);
            settingVO.setRateConfigList(rateConfigVOS);

            //设置房价类型，bar房价放到第一个，其他按照顺序排列
            Set<Integer> rateTypeList = rateConfigVOS.stream().map(RateConfigVO::getRateType).collect(Collectors.toSet());
            List<Integer> sortRateTypeList = Lists.newArrayListWithCapacity(rateTypeList.size());
            if (rateTypeList.contains(RateTypeEnum.BEST_AVAILABLE_RATE.getRateType())) {
                sortRateTypeList.add(RateTypeEnum.BEST_AVAILABLE_RATE.getRateType());

                rateTypeList.remove(Integer.valueOf(RateTypeEnum.BEST_AVAILABLE_RATE.getRateType()));
                sortRateTypeList.addAll(rateTypeList.stream().sorted().toList());
            } else {
                sortRateTypeList = rateTypeList.stream().sorted().toList();
            }
            settingVO.setRateTypeList(sortRateTypeList);

            //获取平台id
            Set<Integer> platformIdSet = rateConfigVOS.stream().map(RateConfigVO::getPlatformIds).flatMap(Collection::stream).collect(Collectors.toSet());
            //根据平台信息和销售地区查询某资产配置的类型信息
            List<RoomType> roomTypes = roomTypeService.listByPlatformIdsAndPos(propertyId, platformIdSet, setting.getPos());
            //List<RoomType> -> Map<Integer, List<RoomType>>
            Map<Integer, List<RoomType>> platformId2RoomTypes = roomTypes.stream().collect(groupingBy(RoomType::getPlatformId));

            List<PlatformVO> platformVOS = Lists.newArrayListWithCapacity(platformId2RoomTypes.size());
            platformIdSet.forEach(platformId -> {
                List<RoomType> roomTypesForPlatformId = platformId2RoomTypes.getOrDefault(platformId, Collections.emptyList());
                List<RoomTypeVO> roomTypeVOS = roomTypesForPlatformId.stream()
                        .map(v -> RoomTypeVO.builder()
                                .id(v.getId())
                                .name(v.getTitle())
                                .build())
                        .collect(Collectors.toList());
                platformVOS.add(new PlatformVO(platformId, roomTypeVOS));
            });
            //设置平台信息
            settingVO.setPlatforms(platformVOS);

            //资产配置的数据源ID
            Set<Integer> datasourceIds = rateConfigVOS.stream().map(RateConfigVO::getDatasourceId).collect(Collectors.toSet());
            //查询资产关联的酒店
            List<PropertyHotelDTO> propertyHotelDTOS = propertyHotelService.listSortDTOByPropertyId(propertyId);
            //设置资产关联的酒店
            settingVO.setHotels(beanMapper.mapList(propertyHotelDTOS, HotelVO.class, (dto, vo) -> {
                List<CollectHotelUrlVO> collectHotelUrlVOS = JsonUtils.convertStringToList(objectMapper, dto.getUrls(), CollectHotelUrlVO.class)
                        .stream().filter(url -> datasourceIds.contains(url.getDatasourceId())).toList();
                //设置酒店的采集url
                vo.setUrlList(collectHotelUrlVOS);
            }));

            //查询资产配置的市场需求趋势
            List<TrendSourceSetting> trendSourceSettings = trendSourceSettingService.listByPropertyId(propertyId);
            settingVO.setTrendSourceSettingResult(CollectionUtils.isNotEmpty(trendSourceSettings));

            //查询资产配置的事件源
            List<EventSourceSetting> eventSourceSettings = eventSourceSettingService.listByPropertyId(propertyId);
            settingVO.setEventSourceSettingResult(CollectionUtils.isNotEmpty(eventSourceSettings));
            return settingVO;
        }

    }