package com.mortals.xhx.daemon.applicationservice;

import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.xhx.base.framework.listener.SimpleDynamicListener;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.platform.service.PlatformService;
import com.mortals.xhx.module.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.util.ObjectUtils;


//@Component
@Slf4j
public class RabbitMsgComsumerStartedService implements IApplicationStartedService {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private ProductService productService;
    @Autowired
    private PlatformService platformService;
    @Autowired
    private SimpleDynamicListener simpleDynamicListener;

    @Autowired
    @Qualifier("simpleMessageListenerContainer")
    private SimpleMessageListenerContainer simpleContainer;


    protected Boolean stopped = false;

    @Override
    public void start() {
        log.info("服务端消费消息服务开始..");
        String[] queues = deviceService.find(new DeviceEntity())
                .stream()
                .filter(f -> !ObjectUtils.isEmpty(platformService.get(f.getPlatformId())))
                .filter(f -> !ObjectUtils.isEmpty(productService.get(f.getProductId())))
                .map(item -> Constant.UPLOAD_TOPIC + item.getDeviceCode())
                .distinct()
                .toArray(String[]::new);
        simpleContainer.addQueueNames(queues);
        simpleContainer.start();
        simpleContainer.addQueueNames("123");
        simpleContainer.setMessageListener(simpleDynamicListener);

    }

    @Override
    public void stop() {
        log.info("停止服务..");
    }

    @Override
    public int getOrder() {
        return 10;
    }

}
