package com.ruoyi.platform.service.impl;

import com.ruoyi.platform.domain.*;
import com.ruoyi.platform.mapper.*;
import com.ruoyi.platform.service.ExperimentInsService;
import com.ruoyi.platform.service.ModelsService;
import com.ruoyi.platform.service.WorkflowService;
import com.ruoyi.platform.service.WorkspaceService;
import lombok.val;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("WorkspaceService")
public class WorkspaceServiceImpl implements WorkspaceService {


    @Resource
    private WorkflowService workflowService;
    @Resource
    private WorkflowDao workflowDao;
    @Resource
    private ExperimentDao experimentDao;

    @Resource
    private ExperimentInsDao experimentInsDao;

    @Resource
    private ExperimentInsService experimentInsService;

    @Resource
    private ModelsService modelsService;
    @Resource
    private ModelsDao modelsDao;

    @Resource
    private DatasetDao datasetDao;

    @Resource
    private ImageDao imageDao;

    @Resource
    private ComponentDao componentDao;




    @Override
    public Map<String, Object> getOverview() {
        Map<String, Object> resMap = new HashMap<String, Object>();

        // 获取流水线数量
        Long workflowCount = workflowDao.workflowCount();
        resMap.put("workflowCount", workflowCount);

        // 获取运行中实验实例数量
        List<ExperimentIns> experimentInsList = experimentInsService.queryByExperimentIsNotTerminated();
        if (experimentInsList!=null && !experimentInsList.isEmpty()) {
            Integer experimentInsCount = experimentInsList.size();
            resMap.put("runningExperimentInsCount", experimentInsCount);
        }else{
            resMap.put("runningExperimentInsCount", 0);
        }
        //得到最近的三条实例记录,放进返回map
        List<ExperimentIns> latestExperimentInsList = experimentInsDao.getLatestInsList();
        if (latestExperimentInsList != null && !latestExperimentInsList.isEmpty()) {
            for (ExperimentIns experimentIns : latestExperimentInsList){
                Integer experimentId = experimentIns.getExperimentId();
                Experiment experiment = this.experimentDao.queryById(experimentId);
                //拿到对应的流水线id
                Long correspondingWorkflowId = experiment.getWorkflowId();
                experimentIns.setWorkflowId(correspondingWorkflowId);
            }
            resMap.put("latestExperimentInsList", latestExperimentInsList);
        }
        Map<String, Long> statusMap = experimentInsService.countByStatus();
        resMap.put("experimentInsStatus", statusMap);
        return resMap;
    }

    @Override
    public Map<String, Integer> getAssetCount(Boolean isPublic) {
        Map<String,Integer> assetCountMap = new HashMap<String, Integer>();
        int availableRange = isPublic ? 1 : 0;
        //统计数据集数量
        Dataset dataset = new Dataset();
        dataset.setAvailableRange(availableRange);
        Integer datasetCount = (int) this.datasetDao.count(dataset);
        assetCountMap.put("dataset", datasetCount);
        //统计模型数量
        Models models = new Models();
        models.setAvailableRange(availableRange);
        Integer modelCount = (int) this.modelsDao.count(models);
        assetCountMap.put("model", modelCount);
        //统计镜像数量
        Image image = new Image();
        image.setImageType(availableRange);
        Integer imageCount = (int) this.imageDao.count(image);
        assetCountMap.put("image", imageCount);
        //统计组件数量
        Component component = new Component();
        component.setAvailableRange(availableRange);
        Integer componentCount = (int) this.componentDao.count(component);
        assetCountMap.put("component", componentCount);
        //统计流水线数量
        Workflow workflow = new Workflow();
        Integer workflowCount = (int) this.workflowDao.count(workflow);
        assetCountMap.put("workflow", workflowCount);

        return assetCountMap;
    }


}
