package com.mortals.xhx.daemon.applicationservice;

import com.mortals.framework.common.Rest;
import com.mortals.framework.service.ICacheService;
import com.mortals.framework.springcloud.service.IApplicationStartedService;
import com.mortals.xhx.common.pdu.RespData;
import com.mortals.xhx.common.pdu.site.SitePdu;
import com.mortals.xhx.common.utils.SendTaskThreadPool;
import com.mortals.xhx.common.utils.SmsQueueManager;
import com.mortals.xhx.feign.site.ISiteFeign;
import com.mortals.xhx.module.alarm.model.AlarmSmsSendEntity;
import com.mortals.xhx.module.alarm.service.AlarmSmsSendService;
import com.mortals.xhx.module.platform.model.PlatformQuery;
import com.mortals.xhx.module.platform.service.PlatformService;
import com.mortals.xhx.module.product.model.ProductQuery;
import com.mortals.xhx.module.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.List;

import static com.mortals.xhx.common.key.RedisKey.*;

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

    @Autowired
    private SendTaskThreadPool sendTaskThreadPool;

    protected Boolean stopped = false;
    @Autowired
    private ICacheService cacheService;
    @Autowired
    private ISiteFeign siteFeign;
    @Autowired
    private PlatformService platformService;
    @Autowired
    private ProductService productService;
    @Autowired
    private AlarmSmsSendService alarmSmsSendService;

    @Override
    public void start() {
        log.info("初始化发送线程数量");
        sendTaskThreadPool.init(20);
        //启动短信发送响应更新线程
        sendTaskThreadPool.execute(() -> {
            int waitTime = 1000;
            while (!stopped) {
                try {
                    AlarmSmsSendEntity message = SmsQueueManager.pollRespQueue();
                    if (!ObjectUtils.isEmpty(message)) {
                        alarmSmsSendService.update(message, null);
                    }
                    
                } catch (Exception e) {
                    if (!stopped) {
                        try {
                            Thread.sleep(waitTime);
                        } catch (InterruptedException e2) {
                        }
                    }
                }
            }
        });

        //初始化站点缓存
        SitePdu sitePdu = new SitePdu();
        sitePdu.setSize(-1);
        Rest<RespData<List<SitePdu>>> resp = siteFeign.list(sitePdu);
        if (resp.getCode() == 1) {
            List<SitePdu> sitePduList = resp.getData().getData();
            sitePduList.stream().forEach(site -> {
                cacheService.hset(KEY_SITE_CACHE, site.getId().toString(), site.getSiteName());
            });
        }

        productService.find(new ProductQuery()).stream().forEach(item -> {
            cacheService.hset(KEY_PRODUCT_CACHE, item.getProductCode(), item.getProductName());
        });

        platformService.find(new PlatformQuery()).stream().forEach(item -> {
            cacheService.hset(KEY_PLATFORM_CACHE, item.getPlatformSn(), item.getPlatformName());
        });

        log.info("开始服务..[配置已加载完成，但部分框架还未初始化，比如：Kafka]");
    }

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

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

}
