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.IpPoolPortGroup;
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;

/**
 * IP池网络标签
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年7月12日
 *
 */
@Repository("ipPoolPortGroupRepository")
public class IpPoolPortGroupRepository extends BasicGenericDao<IpPoolPortGroup, Long> {

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

	public void deleteByVendorId(Long vendorId) throws Exception {
		String sql = "delete from ip_pool_port_group where vendor_id = " + vendorId;
		this.execute(sql);
	}

	public void deleteByGroupId(Long groupId) throws Exception {
		String sql = "delete from ip_pool_port_group where group_id = " + groupId;
		this.execute(sql);
	}

	public void deleteByPoolId(Long poolId) throws Exception {
		String sql = "delete from ip_pool_port_group where pool_id = " + poolId;
		this.execute(sql);
	}

	public List<IpPoolPortGroup> listByVendorId(Long vendorId) throws Exception {
		String sql = "select a.* from ip_pool_port_group a where a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.list(IpPoolPortGroup.class, sql, params);
	}

	public List<IpPoolPortGroup> listByGroupId(Long groupId) throws Exception {
		String sql = "select a.* from ip_pool_port_group a where a.group_id = :groupId";
		Map<String, Object> params = MapTools.simpleMap("groupId", groupId);
		return this.list(IpPoolPortGroup.class, sql, params);
	}

	public List<IpPoolPortGroup> listByPoolId(Long poolId) throws Exception {
		String sql = "select a.* from ip_pool_port_group a where a.pool_id = :poolId";
		Map<String, Object> params = MapTools.simpleMap("poolId", poolId);
		return this.list(IpPoolPortGroup.class, sql, params);
	}

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

	public void delete(Long id) throws Exception {
		String sql = "delete from ip_pool_port_group where id = " + id;
		this.execute(sql);
	}

}
