package com.ruoyi.platform.scheduling;

import com.ruoyi.platform.domain.DevEnvironment;
import com.ruoyi.platform.domain.ServiceVersion;
import com.ruoyi.platform.mapper.DevEnvironmentDao;
import com.ruoyi.platform.mapper.ResourceOccupyDao;
import com.ruoyi.platform.mapper.ServiceDao;
import com.ruoyi.platform.service.JupyterService;
import com.ruoyi.platform.service.ResourceOccupyService;
import com.ruoyi.platform.service.ServiceService;
import com.ruoyi.platform.vo.PodStatusVo;
import com.ruoyi.system.api.constant.Constant;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component()
public class ResourceOccupyTask {

    @Resource
    private ResourceOccupyService resourceOccupyService;

    @Resource
    private ResourceOccupyDao resourceOccupyDao;

    @Resource
    private DevEnvironmentDao devEnvironmentDao;

    @Resource
    private ServiceDao serviceDao;

    @Resource
    private ServiceService serviceService;

    @Resource
    private JupyterService jupyterService;

    // 开发环境功能扣除积分
    @Scheduled(cron = "0 0/1 * * * ?") // 每1分钟执行一次
    public void devDeduceCredit() throws Exception {
        List<DevEnvironment> devEnvironments = devEnvironmentDao.getRunning();
        for (DevEnvironment devEnv : devEnvironments) {
            PodStatusVo podStatusVo = this.jupyterService.getJupyterStatus(devEnv);
            if (!devEnv.getStatus().equals(podStatusVo.getStatus())) {
                devEnv.setStatus(podStatusVo.getStatus());
                devEnv.setUrl(podStatusVo.getUrl());
                this.devEnvironmentDao.update(devEnv);
            }
            Long userId = resourceOccupyDao.getResourceOccupyByTask(Constant.TaskType_Dev, Long.valueOf(devEnv.getId()), null, null).get(0).getUserId();
            if (resourceOccupyDao.getUserCredit(userId) <= 0) {
                jupyterService.stopJupyterService(devEnv.getId());
            } else if (Constant.Running.equals(devEnv.getStatus())) {
                resourceOccupyService.deducing(Constant.TaskType_Dev, Long.valueOf(devEnv.getId()), null, null, null);
            }
        }
    }

    // 服务功能扣除积分
    @Scheduled(cron = "0 0/1 * * * ?") // 每1分钟执行一次
    public void serviceDeduceCredit() {
        List<ServiceVersion> serviceVersions = serviceDao.getRunning();
        List<String> deploymentNames = serviceVersions.stream().map(ServiceVersion::getDeploymentName).collect(Collectors.toList());
        Map<String, String> runStates = serviceService.getRunState(deploymentNames);
        serviceService.updateRunState(runStates, serviceVersions);
        for (ServiceVersion serviceVersion : serviceVersions) {
            Long userId = resourceOccupyDao.getResourceOccupyByTask(Constant.TaskType_Service, serviceVersion.getServiceId(), serviceVersion.getId(), null).get(0).getUserId();
            if (resourceOccupyDao.getUserCredit(userId) <= 0) {
                serviceService.stopServiceVersion(serviceVersion.getId());
            } else if (Constant.Running.equals(serviceVersion.getRunState())) {
                resourceOccupyService.deducing(Constant.TaskType_Service, null, serviceVersion.getId(), null, null);
            }
        }
    }
}
