package org.springblade.etl.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.api.R;
import org.springblade.etl.dto.vo.InstanceCountVO;
import org.springblade.etl.entity.DsyJobInstance;
import org.springblade.etl.service.DsyJobInstanceService;
import org.springblade.etl.service.DsyJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hepingguo
 * @version 1.0
 * @description: TODO
 * @date 2023/12/15 15:11
 */
@RestController
@RequestMapping("/data/overview")
@Api(value = "数据同步流程与实例概况", tags = "数据同步流程与实例概况接口 DataOverviewController")
public class DataOverviewController extends BladeController {

    @Autowired
    private DsyJobService dsyJobService;

    @Autowired
    private DsyJobInstanceService dsyJobInstanceService;

    @GetMapping("/overall")
    @ApiOperation(value = "总体概况", notes = "")
    public R overall() {
        JSONObject result = new JSONObject();
        //任务总数
        result.set("sqlJobTotalCount", dsyJobService.count());
        //调度总数
        result.set("sqlJobCount", dsyJobInstanceService.count(Wrappers.<DsyJobInstance>lambdaQuery()
                .eq(DsyJobInstance::getTenantId, getUser().getTenantId())));
        //查询实例概况
        InstanceCountVO instanceCountVO = dsyJobInstanceService.getCounts();
        result.set("instanceTotal", instanceCountVO.getTotal());
        result.set("instanceRunningCount", instanceCountVO.getRunningCount());
        result.set("instanceSuccessCount", instanceCountVO.getSuccessCount());
        result.set("instanceFailCount", instanceCountVO.getFailCount());
        result.set("instanceCancelCount", instanceCountVO.getCancelCount());
        result.set("instanceWaitCount", instanceCountVO.getWaitCount());
        return R.data(result);
    }

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

    /**
     * 7天sql饼图和sql簇状图
     * @return
     */
    @GetMapping("/batchInstance")
    @ApiOperation(value = "7天sql饼图和sql簇状图", notes = "7天sql饼图和sql簇状图")
    public R batchInstance(){
        Map<String,Object> result = new HashMap<>();
        List<InstanceCountVO> instanceCountVOs = dsyJobInstanceService.getCountByDays(7);
        InstanceCountVO instanceCountVO = new InstanceCountVO();
        instanceCountVO.setFailCount(instanceCountVOs.stream().mapToInt(InstanceCountVO::getFailCount).reduce(0,Integer::sum));
        instanceCountVO.setFailCount(instanceCountVOs.stream().mapToInt(InstanceCountVO::getSuccessCount).reduce(0,Integer::sum));
        instanceCountVO.setFailCount(instanceCountVOs.stream().mapToInt(InstanceCountVO::getWaitCount).reduce(0,Integer::sum));
        result.put("example_diagram",instanceCountVO);
        result.put("cluster_graph",instanceCountVOs);
        return R.data(result);
    }
    /**
     * 当日调度饼图，运行饼图
     * @return
     */
    @GetMapping("/streamInstance")
    @ApiOperation(value = "流作业当天调度情况", notes = "流作业当天调度情况")
    public R streamInstance(){
        return R.data(dsyJobInstanceService.getCountToday());
    }


    /**
     * sql当天24小时调度折柱图
     * @return
     */
    @GetMapping("/batchInstanceLineChart")
    @ApiOperation(value = "sql当天24小时调度折柱图", notes = "sql当天24小时调度折柱图")
    public R batchInstanceLineChart(){
        return R.data(dsyJobInstanceService.instanceLineChart());
    }

    /**
     * 最近同步任务实例列表
     */
    @GetMapping("/sync/job-list")
    @ApiOperation(value = "最近同步任务实例列表", notes = "最近同步任务实例列表")
    public R syncJobList(@RequestParam(required = false,defaultValue = "5") int limit) {
        IPage<DsyJobInstance> page = new Page<>(1,limit);
        IPage<DsyJobInstance> list = dsyJobInstanceService.pageDo(page);

        if (CollectionUtil.isNotEmpty(list.getRecords())) {
            return R.data(list.getRecords());
        }
        return null;
    }
}
