package com.css.simulation.resource.server.adapter.controller.system_manage.cluster_manage;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.param.system.ClusterParam;
import api.common.pojo.po.system.ClusterPO;
import api.common.pojo.vo.system.ClusterVO;
import api.common.util.ObjectUtil;
import com.css.simulation.resource.server.app.service.ClusterService;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


@RestController
@RequestMapping("/cluster")
@PreAuthorize("@AuthorityCheck.adminAndUser()")
public class ClusterController {

    @Resource
    private ClusterService clusterService;


    /**
     * 查询集群列表
     */
    @RequestMapping("/getClusterList")
    public ResponseBodyVO<PageInfo<ClusterVO>> getClusterList(@RequestBody ClusterParam clusterParam) {
        ResponseBodyVO<PageInfo<ClusterVO>> response = new ResponseBodyVO<PageInfo<ClusterVO>>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(clusterService.getClusterList(clusterParam));
        return response;
    }

    /**
     * 新增、修改
     */
    @RequestMapping("/saveCluster")
    public ResponseBodyVO<ClusterPO> saveCluster(@RequestBody ClusterPO clusterPO) {
        if (ObjectUtil.isNull(clusterPO)) {
            return new ResponseBodyVO(false, 400, "参数必传！", null);
        }
        ResponseBodyVO<ClusterPO> response = new ResponseBodyVO<ClusterPO>(ResponseBodyVO.Response.SUCCESS);
        clusterPO = clusterService.saveCluster(clusterPO);
        response.setInfo(clusterPO);
        return response;
    }

    /**
     * 查询集群修改历史记录
     */
    @RequestMapping("/getClusterHistory")
    public ResponseBodyVO<PageInfo<ClusterVO>> getClusterHistory(@RequestBody ClusterParam clusterParam) {
        ResponseBodyVO<PageInfo<ClusterVO>> response = new ResponseBodyVO<PageInfo<ClusterVO>>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(clusterService.getClusterHistory(clusterParam));
        return response;
    }


    /**
     * 查询已分配未到期节点数量
     */
    @RequestMapping("/getClusterNum")
    public ResponseBodyVO<HashMap<String, Integer>> getClusterNum() {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, clusterService.getClusterNum());
    }

    /**
     * 查询仿真软件license数量
     */
    @RequestMapping("/getLicenseNum")
    public ResponseBodyVO<Integer> getLicenseNum() {
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, clusterService.getLicenseNum());
    }


    /**
     * 编辑页面查询有效的集群信息
     */
    @RequestMapping("/getValidClusterInfo")
    public ResponseBodyVO<ClusterVO> getValidClusterInfo(@RequestBody ClusterParam clusterParam) {
        ResponseBodyVO<ClusterVO> response = new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
        response.setInfo(clusterService.getValidClusterInfo(clusterParam));
        return response;
    }
}
