package org.springblade.etl.controller;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.etl.dto.vo.InstanceCountVO;
import org.springblade.etl.entity.Job;
import org.springblade.etl.enums.InstanceType;
import org.springblade.etl.enums.JobStatus;
import org.springblade.etl.service.JobInstanceService;
import org.springblade.etl.service.JobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/etl/overview")
@Api(value = "etl流程与实例概况", tags = "etl流程与实例概况接口 EtlOverviewController")
public class EtlOverviewController extends BladeController {

    @Autowired
    private JobService jobService;

    @Autowired
    private JobInstanceService jobInstanceService;

    @GetMapping("/overall")
    @ApiOperation(value = "总体概况", notes = "")
    public R overall(){
        JSONObject result =new JSONObject();
        //流程总数
        result.put("jobTotalCount",jobService.count());
        //当前用户流程总数
        result.put("userJobCount",jobService.count(Wrappers.<Job>lambdaQuery().eq(Job::getCreateUserId, AuthUtil.getUserId())));
        //启动调度的流程总数
        result.put("triggerJobCount",jobService.count(Wrappers.<Job>lambdaQuery().eq(Job::getTriggerStatus, 1)));
        //查询实例概况
        InstanceCountVO instanceCountVO = jobInstanceService.getCounts();
        result.put("instanceTotal",instanceCountVO.getTotal());
        result.put("instanceRunningCount",instanceCountVO.getRunningCount());
        result.put("instanceSuccessCount",instanceCountVO.getSuccessCount());
        result.put("instanceFailCount",instanceCountVO.getFailCount());
        result.put("instanceCancelCount",instanceCountVO.getCancelCount());
        return R.data(result);
    }


    /**
     * 30天实例运行折线图
     * @return
     */
    @GetMapping("/instanceLineChart")
    @ApiOperation(value = "30天实例运行折线图", notes = "30天实例运行折线图")
    public R instanceLineChart(){
        return R.data(jobInstanceService.getCountByDate());
    }

    /**
     * 批作业当天调度情况
     * @return
     */
    @GetMapping("/batchInstance")
    @ApiOperation(value = "批作业当天调度情况", notes = "批作业当天调度情况")
    public R batchInstance(){
        return R.data(jobInstanceService.getCountByType(InstanceType.BATCH.getValue()));
    }
    /**
     * 流作业当天调度情况
     * @return
     */
    @GetMapping("/streamInstance")
    @ApiOperation(value = "流作业当天调度情况", notes = "流作业当天调度情况")
    public R streamInstance(){
        return R.data(jobInstanceService.getCountByType(InstanceType.STREAMING.getValue()));
    }


    /**
     * 批作业当天调度情况
     * @return
     */
    @GetMapping("/batchInstanceLineChart")
    @ApiOperation(value = "批作业当天24小时调度折柱图", notes = "批作业当天24小时调度折柱图")
    public R batchInstanceLineChart(){
        return R.data(jobInstanceService.instanceLineChart(InstanceType.BATCH.getValue()));
    }

}
