# RateShopNotifyRuleServiceImpl.java

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

    import cloud.wconnect.commonlib.util.JsonUtils;
    import cloud.wconnect.rateshop.cons.rateshop.RateTypeEnum;
    import cloud.wconnect.rateshop.entity.rateshop.RateShopNotifyRule;
    import cloud.wconnect.rateshop.entity.rateshop.Setting;
    import cloud.wconnect.rateshop.mapper.rateshop.RateShopNotifyRuleMapper;
    import cloud.wconnect.rateshop.pojo.dto.rateshop.NotifyRuleJSONDTO;
    import cloud.wconnect.rateshop.pojo.dto.rateshop.RateConfigDTO;
    import cloud.wconnect.rateshop.service.rateshop.IRateShopNotifyRuleService;
    import cloud.wconnect.rateshop.service.rateshop.ISettingService;
    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.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

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

    /**
     * <p>
     * rate shop通知规则表 服务实现类
     * </p>
     *
     * @author lichen
     * @since 2024-06-18
     */
    @Service
    @DS("rate_shop")
    public class RateShopNotifyRuleServiceImpl extends ServiceImpl<RateShopNotifyRuleMapper, RateShopNotifyRule> implements IRateShopNotifyRuleService {

        @Autowired
        private ObjectMapper objectMapper;
        @Autowired
        private ISettingService settingService;

        /**
         * 获取资产的通知规则，当资产的通知规则为空时返回默认值
         *
         * @param propertyId 资产ID
         * @param type       通知类型
         * @return cloud.wconnect.rateshop.entity.rateshop.RateShopNotifyRule
         * @author lichen
         * @date 2024/6/18
         */
        @Override
        public RateShopNotifyRule getByPropertyIdAndTypeOrDefault(Integer propertyId, Integer type) {
            //通知规则
            RateShopNotifyRule rateShopNotifyRule = getOne(new LambdaQueryWrapper<RateShopNotifyRule>().eq(RateShopNotifyRule::getPropertyId, propertyId)
                    .eq(RateShopNotifyRule::getType, type));
            if (Objects.nonNull(rateShopNotifyRule)) return rateShopNotifyRule;

            //资产的rateShop配置
            Setting setting = settingService.getByPropertyId(propertyId);

            //资产配置的房价类型
            List<RateConfigDTO> rateConfigDTOS = JsonUtils.convertStringToList(objectMapper, setting.getRateConfig(), RateConfigDTO.class);
            Set<Integer> rateTypeSet = rateConfigDTOS.stream().map(RateConfigDTO::getRateType).collect(Collectors.toSet());

            //BAR房价优先
            int newRateType = rateTypeSet.contains(RateTypeEnum.BEST_AVAILABLE_RATE.getRateType()) ?
                    RateTypeEnum.BEST_AVAILABLE_RATE.getRateType() : RateTypeEnum.EARLY_BIRD_RATE.getRateType();

            //指定rateType对应的数据源和平台
            Map<Integer, List<Integer>> datasourceId2PlatformIds = rateConfigDTOS.stream().filter(rateConfig -> rateConfig.getRateType().equals(newRateType))
                    .collect(Collectors.toMap(RateConfigDTO::getDatasourceId, RateConfigDTO::getPlatformIds));
            ArrayList<Integer> datasourceIds = Lists.newArrayList(datasourceId2PlatformIds.keySet());

            //通知规则
            NotifyRuleJSONDTO notifyRuleJSONDTO = new NotifyRuleJSONDTO();
            notifyRuleJSONDTO.setNumberOfDays(10);
            notifyRuleJSONDTO.setRateType(newRateType);
            if (datasourceIds.size() == 1) {
                //数据源只有一个时，初始化数据源
                Integer newDatasourceId = datasourceIds.get(0);
                notifyRuleJSONDTO.setDatasourceId(newDatasourceId);

                //平台只有一个时，初始化平台
                List<Integer> platformIds = datasourceId2PlatformIds.get(newDatasourceId);
                if (platformIds.size() == 1) {
                    notifyRuleJSONDTO.setPlatformId(platformIds.get(0));
                }
            }

            //默认通知规则
            String rule = JsonUtils.convertObjToString(objectMapper, notifyRuleJSONDTO);

            return new RateShopNotifyRule().setPropertyId(propertyId).setRule(rule)
                    .setType(type);
        }

        /**
         * 获取资产的通知规则
         *
         * @param propertyId 资产ID
         * @param type       通知类型
         * @return cloud.wconnect.rateshop.entity.rateshop.RateShopNotifyRule
         * @author lichen
         * @date 2024/6/18
         */
        @Override
        public RateShopNotifyRule getByPropertyIdAndType(Integer propertyId, Integer type){
            //通知规则
            return getOne(new LambdaQueryWrapper<RateShopNotifyRule>().eq(RateShopNotifyRule::getPropertyId, propertyId)
                    .eq(RateShopNotifyRule::getType, type));
        }
    }