package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.middleware.K8sNode;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * 数据库节点Repository接口实现
 *
 * @author weiwei
 */
@Repository("k8sNodeRepository")
public class K8sNodeRepository extends BasicGenericDao<K8sNode, Long> {

    public K8sNodeRepository(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate npJdbcTemplate) {
        super(jdbcTemplate, npJdbcTemplate);
    }

    public List<K8sNode> list(Long clusterId) throws Exception {
        String sql = "select * from k8s_node where cluster_id = :clusterId";
        Map<String, Object> params = MapTools.simpleMap("clusterId", clusterId);
        List<K8sNode> list = this.list(K8sNode.class, sql, params);
        List<K8sNode> nodes = new ArrayList<>();
        for (Object obj : list) {
            nodes.add((K8sNode) obj);
        }
        return nodes;
    }

    public boolean deleteByCluster(Long clusterId) throws Exception {
        String sql = "delete from k8s_node where cluster_id = :clusterId";
        Map<String, Object> params = MapTools.simpleMap("clusterId", clusterId);
        return this.execute(sql, params) > 0;
    }

    public K8sNode query(Long id) throws Exception {
        String sql = "select * from k8s_node where id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<K8sNode> list = this.list(K8sNode.class, sql, params);
        return ListTool.isEmpty(list) ? null : (K8sNode) list.get(0);
    }

    public boolean deleteByInstanceId(String instanceId) throws Exception {
        String sql = "delete from k8s_node where instance_id = :instanceId";
        Map<String, Object> params = MapTools.simpleMap("instanceId", instanceId);
        return this.execute(sql, params) > 0;
    }

}
