package com.css.simulation.resource.server.adapter.controller.home_page;

import api.common.pojo.common.PageVO;
import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.vo.home.HardwareVO;
import api.common.pojo.vo.home.LineChartVO;
import api.common.pojo.vo.home.ServiceVO;
import api.common.pojo.vo.home.SystemServerVO;
import com.css.simulation.resource.server.app.service.AlgorithmService;
import com.css.simulation.resource.server.app.service.HomePageService;
import com.css.simulation.resource.server.app.service.ConfigService;
import com.css.simulation.resource.server.app.service.job_manage.SimulationProjectService;
import com.css.simulation.resource.server.app.service.SceneNaturalService;
import com.css.simulation.resource.server.app.service.ClusterService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

//首页系统概览
@Slf4j
@RestController
@RequestMapping("/homePage")
public class HomePageController {
    @Resource
    private SceneNaturalService sceneNaturalService;
    @Resource
    private AlgorithmService algorithmService;
    @Resource
    private SimulationProjectService simulationProjectService;
    @Resource
    private ConfigService configService;
    @Resource
    private HomePageService homePageService;
    @Resource
    private ClusterService clusterService;


    /**
     * 首页 -- 系统监控 -- 数据概览
     */
    @PostMapping("/queryAllNum")
    public ResponseBodyVO<Map<String, String>> queryAllNum() {
        Map<String, String> map = algorithmService.selectDetailsBySy();     //算法文件数量
        map.put("SySceneNum", sceneNaturalService.querySceneNumBySy() + "");//私有场景数量
        map.put("GySceneNum", sceneNaturalService.querySceneNumByGy() + "");//共有场景数量
        map.put("ConfigTotal", configService.getConfigTotal() + "");             //车辆配置数量
        int licenseNum = clusterService.getLicenseNum();
        if (licenseNum == -1) {
            map.put("maxConcurrency", "无限");          //最大并发数量
        }
        map.put("currentConcurrency", homePageService.getRunningTask() + "");    //当前并发数量
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, map);
    }

    /**
     * 近一周运行运行项目数量
     */
    @RequestMapping("selectRunProjectBySy")
    public ResponseBodyVO<List<LineChartVO>> selectRunProjectBySy() {
        List<LineChartVO> list = homePageService.selectRunProjectChart();
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, list);
    }

    /**
     * 近一周运行任务数量
     */
    @RequestMapping("selectRunTaskBySy")
    public ResponseBodyVO<List<LineChartVO>> selectRunTaskBySy() {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, homePageService.selectRunTaskChart());
    }


    /**
     * 首页 -- 系统监控 -- 状态统计 -- 项目运行状态统计
     */
    @RequestMapping("selectRunProjectByState")
    public ResponseBodyVO<List<Map<String, Object>>> selectRunProjectByState() {
        return simulationProjectService.selectRunProjectByState();
    }

    /**
     * 首页 -- 系统监控 -- 状态统计 -- 任务运行状态统计
     */
    @RequestMapping("selectRunTaskByState")
    public ResponseBodyVO<List<Map<String, Object>>> selectRunTaskByState() {
        return simulationProjectService.selectRunTaskByState();
    }


    /**
     * 评测等级分布-饼图
     */
    @RequestMapping("selectEvaluationLevel")
    public ResponseBodyVO<List<Map<String, Object>>> selectEvaluationLevel() {
        return simulationProjectService.selectEvaluationLevel();
    }


    /**
     * 硬件监控
     */
    @RequestMapping("selectHardware")
    @PreAuthorize("@AuthorityCheck.admin()")
    public ResponseBodyVO<HardwareVO> selectHardware() {
        return homePageService.selectHardware();
    }

    /**
     * 服务监控
     */
    @RequestMapping("selectService")
    @PreAuthorize("@AuthorityCheck.admin()")
    public ResponseBodyVO<ServiceVO> selectService() {
        return homePageService.selectService();
    }

    /**
     * 日访问人数
     */
    @RequestMapping("selectAccess")
    @PreAuthorize("@AuthorityCheck.admin()")
    public ResponseBodyVO<List<LineChartVO>> selectAccess() {
        List<LineChartVO> list = homePageService.selectAccess();
        return new ResponseBodyVO(ResponseBodyVO.Response.SUCCESS, list);
    }


    /**
     * 服务器监控
     */
    @RequestMapping("selectServer")
    @PreAuthorize("@AuthorityCheck.admin()")
    public ResponseBodyVO<PageInfo<SystemServerVO>> selectServer(PageVO pageVO) {
        return homePageService.selectServer(pageVO);
    }
}
