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.resource.ServerConfig;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * 主机配置数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("serverConfigRepository")
public class ServerConfigRepository extends BasicGenericDao<ServerConfig, Long> {

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

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

	public ServerConfig getBySid(Long serverId) {
		String sql = "select * from server_config where server_id = :serverId";
		Map<String, Object> param = MapTools.simpleMap("serverId", serverId);
		List<ServerConfig> list = this.list(ServerConfig.class, sql, param);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}

	public Boolean removeBySid(Long serverId) {
		String sql = "delete from server_config where server_id = :serverId";
		Map<String, Object> param = MapTools.simpleMap("serverId", serverId);
		return this.execute(sql, param) > 0;
	}

	public ServerConfig getConfig(String ipmiIp) {
		String sql = "select a.* from server_config a left join server b on b.id = a.server_id and b.is_deleted = 0 where a.ipmi_address like '%"
				+ ipmiIp + "%' or a.ipmi_ips like '%" + ipmiIp + "%'";
		List<ServerConfig> list = this.list(ServerConfig.class, sql, null);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}

	public List<ServerConfig> listByHostIds(List<Long> hostIds) throws Exception {
		String sql = "select * from server_config where server_id in (:hostIds)";
		Map<String, Object> param = MapTools.simpleMap("hostIds", hostIds);
		return this.list(ServerConfig.class, sql, param);
	}
}
