package cn.reghao.tnb.admin.service;

import cn.reghao.jutil.jdk.converter.DateTimeConverter;
import cn.reghao.jutil.jdk.thread.ThreadPoolWrapper;
import cn.reghao.tnb.admin.model.po.BackendSystem;
import cn.reghao.tnb.admin.model.po.DubboSrv;
import cn.reghao.tnb.admin.model.po.SpringCloudService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author reghao
 * @date 2025-04-17 11:07:52
 */
@Slf4j
@Service
public class TnbService {
    private final ScheduledExecutorService scheduledExecutorService;
    private final TnbZkService tnbZkService;

    public TnbService(TnbZkService tnbZkService) {
        this.scheduledExecutorService = ThreadPoolWrapper.scheduledThreadPool("zkservice-checker", 1);
        this.tnbZkService = tnbZkService;
    }

    public void startServiceChecker() {
        ScheduledFuture<?> future = scheduledExecutorService.scheduleAtFixedRate(new ServiceChecker(), 0, 10, TimeUnit.SECONDS);
    }

    public void add(List<String> appNames) {
        String sysName = "vod";
    }

    public class ServiceChecker implements Runnable {
        @Override
        public void run() {
            String sysName = "vod";
            BackendSystem backendSystem = new BackendSystem(sysName);
            List<String> appNames = backendSystem.getSysServices();
            if (appNames.isEmpty()) {
                return;
            }

            try {
                Map<String, List<SpringCloudService>> springcloudMap = tnbZkService.getSpringCloudServices();
                Map<String, List<DubboSrv>> dubboMap = tnbZkService.getDubboServiceMap();
                log.error("########################################{}########################################", DateTimeConverter.format(System.currentTimeMillis()));
                List<String> springcloudServices = new ArrayList<>();
                List<String> dubboServices = new ArrayList<>();
                for (String appName : appNames) {
                    List<SpringCloudService> springcloudList = springcloudMap.get(appName);
                    if (springcloudList == null || springcloudList.isEmpty()) {
                        springcloudServices.add(appName);
                    }

                    List<DubboSrv> dubboList = dubboMap.get(appName);
                    if (dubboList == null || dubboList.isEmpty()) {
                        dubboServices.add(appName);
                    }
                }

                if (!springcloudServices.isEmpty()) {
                    log.error("{} 没有实例注册到 SpringCloud ZK", springcloudServices);
                }
                if (!dubboServices.isEmpty()) {
                    log.error("{} 没有实例注册到 Dubbo ZK", dubboServices);
                }
                log.error("########################################{}########################################", DateTimeConverter.format(System.currentTimeMillis()));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
