package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.resource.NetworkHost;
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("networkHostRepository")
public class NetworkHostRepository extends BasicGenericDao<NetworkHost, Long> {

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

	public List<NetworkHost> list(Long vhostId, String category) throws Exception {
		String sql = "select * from network_host where vhost_id = :vhostId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("vhostId", vhostId);
		params.put("category", category);
		return this.list(NetworkHost.class, sql, params);
	}

	public boolean deleteByHost(Long vhostId, String category) throws Exception {
		String sql = "delete from network_host where vhost_id = :vhostId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("vhostId", vhostId);
		params.put("category", category);
		return this.execute(sql, params) > 0;
	}

	public boolean deleteByNetwork(Long networkId) throws Exception {
		String sql = "delete from network_host where network_id = :networkId";
		Map<String, Object> params = MapTools.simpleMap("networkId", networkId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByVendor(Long vendorId) throws Exception {
		String sql = "delete from network_host where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.execute(sql, params) > 0;
	}

	public List<NetworkHost> listByNetworkId(Long networkId, String category) throws Exception {
		String sql = "select * from network_host where network_id = :networkId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("networkId", networkId);
		params.put("category", category);
		return this.list(NetworkHost.class, sql, params);
	}

	public List<NetworkHost> listByVhostId(Long vhostId, String category) throws Exception {
		String sql = "select * from network_host where vhost_id = :vhostId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("vhostId", vhostId);
		params.put("category", category);
		return this.list(NetworkHost.class, sql, params);
	}

	public boolean deleteByHostAndNetwork(Long vhostId, Long networkId, String category) throws Exception {
		String sql = "delete from network_host where network_id = :networkId and vhost_id = :vhostId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("networkId", networkId);
		params.put("vhostId", vhostId);
		params.put("category", category);
		return this.execute(sql, params) > 0;
	}

	public List<NetworkHost> listByCategory(Long networkId, String category) throws Exception {
		String sql = "select * from network_host where category = :category and network_id = :networkId";
		Map<String, Object> params = MapTools.simpleMap("category", category);
		params.put("networkId", networkId);
		return this.list(NetworkHost.class, sql, params);
	}
}
