package com.mortals.xhx.daemon.applicationservice;

import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.xhx.base.framework.listener.DirectDynamicListener;
import com.mortals.xhx.base.system.message.impl.MessageProducer;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.common.key.QueueKey;
import com.mortals.xhx.common.utils.SendTaskThreadPool;
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.DirectMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

/**
 * 初始化设备消费者注册
 *
 * @author: zxfei
 * @date: 2022/9/2 9:17
 */
@Component
@Slf4j
public class DeviceComsumersRegisterService implements IApplicationStartedService {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private ProductService productService;
    @Autowired
    private PlatformService platformService;
    @Autowired
    private DirectMessageListenerContainer container;
    @Autowired
    private MessageProducer messageProducer;
    @Autowired
    private SendTaskThreadPool sendTaskThreadPool;
    @Autowired
    private DirectDynamicListener directDynamicListener;

    @Override
    public void start() {
        log.info("初始化发送线程数量");
        sendTaskThreadPool.init(20);
        log.info("服务端消息队列初始化服务开始..");

        deviceService.find(new DeviceEntity())
                .stream()
                .filter(f -> !ObjectUtils.isEmpty(platformService.getCache(f.getPlatformId()==null?"0":f.getPlatformId().toString())))
                .filter(f -> !ObjectUtils.isEmpty(productService.getCache(f.getProductId()==null?"0":f.getProductId().toString()))).forEach(item -> {
                    String uploadQueue = Constant.UPLOAD_TOPIC + item.getDeviceCode();
                    String downQueue = Constant.DOWN_TOPIC + item.getDeviceCode();
                    messageProducer.queueAddAndBinds(QueueKey.DEFAULT_EXCHANGE, uploadQueue, uploadQueue);
                    messageProducer.queueAddAndBinds(QueueKey.DEFAULT_EXCHANGE, downQueue, downQueue);
                });

        String[] queues = deviceService.find(new DeviceEntity())
                .stream()
                .filter(f -> !ObjectUtils.isEmpty(platformService.getCache(f.getPlatformId().toString())))
                .filter(f -> !ObjectUtils.isEmpty(productService.getCache(f.getProductId().toString())))
                .filter(f->!ObjectUtils.isEmpty(f.getDeviceCode()))
                .map(item -> Constant.UPLOAD_TOPIC + item.getDeviceCode())
                .toArray(String[]::new);

        log.info("服务端消费消息注册服务开始..");
        container.addQueueNames(queues);
        container.start();
        container.setMessageListener(directDynamicListener);
    }

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

    }

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


}
