package com.xy.config;

import com.dtp.core.support.DynamicTp;
import com.xy.utils.ThreadPoolUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

@Component
public class DeviceThreadPoolConfig {

    public static final String DEVICE_COMMON_POLL = "deviceCommonPoll";

    public static final String DEVICE_NETWORK_POLL = "deviceNetWorkPoll";

    public static final String ALIPAY_DEVICE_DETAIL = "alipayDeviceDetail";

    public static final String ALI_DEVICE_ACTIVE = "aliDeviceActive";

    public static final String DEVICE_STATUS_UP = "deviceStatusUp";

    public static final String TY_DEVICE_STATUS = "tyDeviceStatus";

    /**
     * 公用线程池
     */
    @DynamicTp
    @Bean(DEVICE_COMMON_POLL)
    public ThreadPoolTaskExecutor deviceCommonPoll() {
        int coreSize = 5;
        return ThreadPoolUtils.newPoll()
                .name(DEVICE_COMMON_POLL)
                .coreSize(coreSize)
                .maxSize(coreSize * 2)
                .keepAlive(60)
                .queueSize(coreSize * 10)
                .builder();
    }

    /**
     * 上下线mqtt消费线程池
     */
    @DynamicTp
    @Bean(DEVICE_NETWORK_POLL)
    public ThreadPoolTaskExecutor deviceNetWorkPoll() {
        int coreSize = 5;
        return ThreadPoolUtils.newPoll()
                .name(DEVICE_NETWORK_POLL)
                .coreSize(coreSize)
                .maxSize(coreSize * 10)
                .keepAlive(30)
                .queueSize(coreSize * 10)
                .builder();
    }

    /**
     * 查询支付宝设备详情线程池
     */
    @DynamicTp
    @Bean(ALIPAY_DEVICE_DETAIL)
    public ThreadPoolTaskExecutor alipayDeviceDetail() {
        int coreSize = 5;
        return ThreadPoolUtils.newPoll()
                .name(ALIPAY_DEVICE_DETAIL)
                .coreSize(coreSize)
                .maxSize(coreSize * 10)
                .keepAlive(30)
                .queueSize(coreSize * 10)
                .builder();
    }

    /**
     * 支付宝设备激活线程池
     */
    @DynamicTp
    @Bean(ALI_DEVICE_ACTIVE)
    public ThreadPoolTaskExecutor aliDeviceActive() {
        int coreSize = 1;
        return ThreadPoolUtils.newPoll()
                .name(ALI_DEVICE_ACTIVE)
                .coreSize(coreSize)
                .maxSize(coreSize * 10)
                .keepAlive(30)
                .queueSize(coreSize)
                .builder();
    }

    /**
     * 拓元设备状态
     *
     * @return {@link ThreadPoolTaskExecutor}
     */
    @DynamicTp
    @Bean(TY_DEVICE_STATUS)
    public ThreadPoolTaskExecutor tyDeviceStatus() {
        int coreSize = 1;
        return ThreadPoolUtils.newPoll()
                .name(TY_DEVICE_STATUS)
                .coreSize(coreSize)
                .maxSize(coreSize * 10)
                .keepAlive(30)
                .queueSize(coreSize)
                .builder();
    }

    /**
     * 设备状态上报线程池
     */
    @DynamicTp
    @Bean(DEVICE_STATUS_UP)
    public ThreadPoolTaskExecutor deviceStatusUp() {
        int coreSize = 8;
        return ThreadPoolUtils.newPoll()
                .name(DEVICE_STATUS_UP)
                .coreSize(coreSize)
                .maxSize(coreSize * 2)
                .keepAlive(300)
                .queueSize(coreSize)
                .builder();
    }
}
