package com.cool.store.mq.consumer;

import com.aliyun.openservices.ons.api.MessageListener;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.PropertyValueConst;
import com.aliyun.openservices.ons.api.bean.ConsumerBean;
import com.aliyun.openservices.ons.api.bean.Subscription;
import com.cool.store.constants.CommonConstants;
import com.cool.store.enums.RocketMqGroupEnum;
import com.cool.store.mq.RocketMqConfig;
import com.cool.store.mq.consumer.listener.XfsgTrainingPersonSyncListener;
import com.google.common.collect.Maps;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Properties;

/**
 * @author zhangchenbiao
 * @FileName: OrderlyConsumerClient
 * @Description: 普通消息消费client
 * @date 2021-12-21 11:35
 */
@Configuration
public class ConsumerClient {

    @Resource
    private RocketMqConfig rocketMqConfig;
//    @Resource
//    private FeiShuEventListener feiShuEventListener;
    @Resource
    private XfsgTrainingPersonSyncListener xfsgTrainingPersonSyncListener;

    /**
     * 获取通用配置
     * @param groupEnum
     * @return
     */
    private Properties getCommonProperties(RocketMqGroupEnum groupEnum) {
        //配置文件
        Properties properties = rocketMqConfig.getMqProperties();
        //消费者需指定groupId  根据实际情况指定groupId
        properties.setProperty(PropertyKeyConst.GROUP_ID, RocketMqGroupEnum.getGroupId(groupEnum));
        //消费模式 集群消费
        properties.setProperty(PropertyKeyConst.MessageModel, PropertyValueConst.CLUSTERING);
        //消息最大重试次数
        properties.setProperty(PropertyKeyConst.MaxReconsumeTimes, CommonConstants.MaxReconsumeTimes);
        //开启最大线程数
        properties.setProperty(PropertyKeyConst.ConsumeThreadNums,  CommonConstants.FIVE_STR);
        return properties;
    }

    /**
     * 获取通用订阅关系
     * @param groupEnum
     * @param listener
     * @return
     */
    public Map<Subscription, MessageListener> getCommonSubscriptionTable(RocketMqGroupEnum groupEnum, MessageListener listener) {
        Map<Subscription, MessageListener> subscriptionTable = Maps.newHashMap();
        Subscription subscription = new Subscription();
        subscription.setTopic(rocketMqConfig.getTopic());
        subscription.setExpression(RocketMqGroupEnum.getTag(groupEnum));
        subscriptionTable.put(subscription, listener);
        return subscriptionTable;
    }

    /**
     * 用户事件监听
     * @return
     */
//    @Bean(initMethod = "start", destroyMethod = "shutdown")
//    public ConsumerBean test() {
//        RocketMqGroupEnum groupEnum = RocketMqGroupEnum.FEI_SHU_EVENT_LISTENER;
//        ConsumerBean consumerBean = new ConsumerBean();
//        //配置文件
//        Properties properties = getCommonProperties(groupEnum);
//        consumerBean.setProperties(properties);
////        Map<Subscription, MessageListener> commonSubscriptionTable = getCommonSubscriptionTable(groupEnum, feiShuEventListener);
//        //订阅多个topic如上面设置
//        consumerBean.setSubscriptionTable(commonSubscriptionTable);
//        return consumerBean;
//    }


}
