package com.css.simulation.resource.server.infra.db.mysql.mapper;

import api.common.pojo.po.home.SystemServerPO;
import api.common.pojo.vo.home.LineChartVO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

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


/**
 * create table if not exists simulation.system_server
 * (
 * id varchar(32) null,
 * server_id varchar(64) null,
 * server_address varchar(32) null,
 * server_type varchar(32) null,
 * cpu_useage varchar(32) null,
 * memory_useage varchar(32) null,
 * memory_available varchar(32) null comment '剩余内存，单位GB',
 * disk_usage varchar(32) null,
 * disk_abailable varchar(32) null comment '剩余磁盘，单位GB',
 * task_number varchar(32) null comment '执行任务数量'
 * )
 * comment '服务器表';
 */
@Mapper
public interface HomePageMapper {
    @Results(id = "server", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.VARCHAR),
            @Result(column = "server_id", property = "serverId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "server_address", property = "serverAddress", jdbcType = JdbcType.VARCHAR),
            @Result(column = "server_type", property = "serverType", jdbcType = JdbcType.VARCHAR),
            @Result(column = "memory_usage", property = "memoryUsage", jdbcType = JdbcType.DOUBLE),
            @Result(column = "memory_available", property = "memoryAvailable", jdbcType = JdbcType.BIGINT),
            @Result(column = "memory_total", property = "memoryTotal", jdbcType = JdbcType.BIGINT),
            @Result(column = "disk_usage", property = "diskUsage", jdbcType = JdbcType.DOUBLE),
            @Result(column = "disk_available", property = "diskAvailable", jdbcType = JdbcType.BIGINT),
            @Result(column = "disk_total", property = "diskTotal", jdbcType = JdbcType.BIGINT),
            @Result(column = "cpu_usage", property = "cpuUsage", jdbcType = JdbcType.DOUBLE),
            @Result(column = "cpu_total", property = "cpuTotal", jdbcType = JdbcType.BIGINT),
            @Result(column = "gpu_usage", property = "gpuUsage", jdbcType = JdbcType.DOUBLE),
            @Result(column = "gpu_total", property = "gpuTotal", jdbcType = JdbcType.BIGINT),
            @Result(column = "task_number", property = "taskNumber", jdbcType = JdbcType.INTEGER)
    })

    @Select("select id,\n" +
            "       server_id,\n" +
            "       server_address,\n" +
            "       server_type,\n" +
            "       memory_usage,\n" +
            "       memory_available,\n" +
            "       memory_total,\n" +
            "       disk_usage,\n" +
            "       disk_available,\n" +
            "       disk_total,\n" +
            "       cpu_usage,\n" +
            "       cpu_total,\n" +
            "       gpu_usage,\n" +
            "       gpu_total,\n" +
            "       task_number\n" +
            "from system_server\n" +
            "where is_deleted = '0'")
    List<SystemServerPO> selectAll();

    @Select("select count(*)\n" +
            "from simulation_manual_project_task smpt\n" +
            "where run_state = 'Running' and is_deleted = '0'")
    Integer selectSimulationLicenseNumber();

    List<LineChartVO> selectRunTaskChart(Map<String, String> param);

    List<LineChartVO> selectRunProjectChart(Map<String, String> param);

    @Select("select count(*)\n" +
            "from simulation_manual_project_task smpt\n" +
            "         left join simulation_manual_project smp on smpt.p_id = smp.id\n" +
            "         left join simulation_automatic_subproject sas on smpt.p_id = sas.id\n" +
            "where smpt.is_deleted = '0'\n" +
            "  and (smp.is_deleted = '0' or sas.is_deleted = '0')\n" +
            "  and (smp.now_run_state = '20' or sas.now_run_state = '20')\n" +
            "  and smpt.run_state in ('Running')\n" +
            "  and smpt.create_user_id = #{currentUserId}")
    int selectRunningTask(@Param("currentUserId") String currentUserId);
}
