package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.resource.RouterSubnet;
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;

/**
 * 路由-子网Repository接口实现
 * 
 * @author weiwei
 *
 */
@Repository("routerSubnetRepository")
public class RouterSubnetRepository extends BasicGenericDao<RouterSubnet, Long> {

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

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

	public boolean deleteByRouter(Long routerId) throws Exception {
		String sql = "delete from router_subnet where router_id = :routerId";
		Map<String, Object> params = MapTools.simpleMap("routerId", routerId);
		return this.execute(sql, params) > 0;
	}

	public boolean deleteBySubnet(Long subnetId) throws Exception {
		String sql = "delete from router_subnet where subnet_id = :subnetId";
		Map<String, Object> params = MapTools.simpleMap("subnetId", subnetId);
		return this.execute(sql, params) > 0;
	}

	public List<RouterSubnet> listByRouter(Long routerId) throws Exception {
		String sql = "select * from router_subnet where router_id = :routerId";
		Map<String, Object> params = MapTools.simpleMap("routerId", routerId);
		return this.list(RouterSubnet.class, sql, params);
	}

	public List<RouterSubnet> listBySubnet(Long subnetId) throws Exception {
		String sql = "select * from router_subnet where subnet_id = :subnetId";
		Map<String, Object> params = MapTools.simpleMap("subnetId", subnetId);
		return this.list(RouterSubnet.class, sql, params);
	}

	public boolean remove(Long routerId, Long subnetId, Long vendorId) throws Exception {
		String sql = "delete from router_subnet where vendor_id = :vendorId and router_id = :routerId and subnet_id = :subnetId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("routerId", routerId);
		params.put("subnetId", subnetId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByVendor(Long vendorId, String regionId) throws Exception {
		String sql = "delete from router_subnet where vendor_id = :vendorId and region_id = :regionId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put(Common.REGIONID, regionId);
		return this.execute(sql, params) > 0;
	}

}
