package com.ruoyi.platform.scheduling;

import com.ruoyi.platform.constant.Constant;
import com.ruoyi.platform.domain.Ray;
import com.ruoyi.platform.domain.RayIns;
import com.ruoyi.platform.mapper.RayDao;
import com.ruoyi.platform.mapper.RayInsDao;
import com.ruoyi.platform.service.RayInsService;
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 RayInsStatusTask {

    @Resource
    private RayInsService rayInsService;
    @Resource
    private RayInsDao rayInsDao;
    @Resource
    private RayDao rayDao;

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

    @Scheduled(cron = "0/30 * * * * ?") // 每30S执行一次
    public void executeRayInsStatus() {
        List<RayIns> rayInsList = rayInsService.queryByRayInsIsNotTerminated();

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

    @Scheduled(cron = "0/30 * * * * ?") // / 每30S执行一次
    public void executeRayStatus() {
        if (rayIds.size() == 0) {
            return;
        }
        // 存储需要更新的实验对象列表
        List<Ray> updateRays = new ArrayList<>();
        for (Long rayId : rayIds) {
            // 获取当前实验的所有实例列表
            List<RayIns> insList = rayInsDao.getByRayId(rayId);
            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);
            Ray ray = rayDao.getRayById(rayId);
            if (!StringUtils.equals(ray.getStatusList(), subStatus)) {
                ray.setStatusList(subStatus);
                updateRays.add(ray);
                rayDao.edit(ray);
            }
        }

        if (!updateRays.isEmpty()) {
            // 使用Iterator进行安全的删除操作
            Iterator<Long> iterator = rayIds.iterator();
            while (iterator.hasNext()) {
                Long rayId = iterator.next();
                for (Ray ray : updateRays) {
                    if (ray.getId().equals(rayId)) {
                        iterator.remove();
                    }
                }
            }
        }
    }
}
