package com.ruoyi.platform.scheduling;

import com.ruoyi.platform.constant.Constant;
import com.ruoyi.platform.domain.AutoMl;
import com.ruoyi.platform.domain.AutoMlIns;
import com.ruoyi.platform.mapper.AutoMlDao;
import com.ruoyi.platform.mapper.AutoMlInsDao;
import com.ruoyi.platform.service.AutoMlInsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Component()
public class AutoMlInsStatusTask {

    @Resource
    private AutoMlInsService autoMlInsService;

    @Resource
    private AutoMlInsDao autoMlInsDao;

    @Resource
    private AutoMlDao autoMlDao;

    private List<Long> autoMlIds = new ArrayList<>();

    @Scheduled(cron = "0/30 * * * * ?") // 每30S执行一次
    public void executeAutoMlInsStatus() throws Exception {
        // 首先查到所有非终止态的实验实例
        List<AutoMlIns> autoMlInsList = autoMlInsService.queryByAutoMlInsIsNotTerminated();

        // 去argo查询状态
        List<AutoMlIns> updateList = new ArrayList<>();
        if (autoMlInsList != null && autoMlInsList.size() > 0) {
            for (AutoMlIns autoMlIns : autoMlInsList) {
                //当原本状态为null或非终止态时才调用argo接口
                try {
                    autoMlIns = autoMlInsService.queryStatusFromArgo(autoMlIns);
                } catch (Exception e) {
                    autoMlIns.setStatus(Constant.Failed);
                }
                // 线程安全的添加操作
                synchronized (autoMlIds) {
                    autoMlIds.add(autoMlIns.getAutoMlId());
                }
                updateList.add(autoMlIns);
            }
            if (updateList.size() > 0) {
                for (AutoMlIns autoMlIns : updateList) {
                    autoMlInsDao.update(autoMlIns);
                }
            }
        }
    }

    @Scheduled(cron = "0/30 * * * * ?") // / 每30S执行一次
    public void executeAutoMlStatus() throws Exception {
        if (autoMlIds.size() == 0) {
            return;
        }
        // 存储需要更新的实验对象列表
        List<AutoMl> updateAutoMls = new ArrayList<>();
        for (Long autoMlId : autoMlIds) {
            // 获取当前实验的所有实例列表
            List<AutoMlIns> insList = autoMlInsDao.getByAutoMlId(autoMlId);
            List<String> statusList = new ArrayList<>();
            // 更新实验状态列表
            for (int i = 0; i < insList.size(); i++) {
                statusList.add(insList.get(i).getStatus());
            }
            String subStatus = statusList.toString().substring(1, statusList.toString().length() - 1);
            AutoMl autoMl = autoMlDao.getAutoMlById(autoMlId);
            if (!StringUtils.equals(autoMl.getStatusList(), subStatus)) {
                autoMl.setStatusList(subStatus);
                updateAutoMls.add(autoMl);
                autoMlDao.edit(autoMl);
            }
        }

        if (!updateAutoMls.isEmpty()) {
            // 使用Iterator进行安全的删除操作
            Iterator<Long> iterator = autoMlIds.iterator();
            while (iterator.hasNext()) {
                Long autoMlId = iterator.next();
                for (AutoMl autoMl : updateAutoMls) {
                    if (autoMl.getId().equals(autoMlId)) {
                        iterator.remove();
                    }
                }
            }
        }
    }
}
