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.TenantNetwork;
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 wangyu
 * @version 1.0
 * @since 2018年1月5日
 */
@Repository("tenantNetworkRepository")
public class TenantNetworkRepository extends BasicGenericDao<TenantNetwork, Long> {

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

	public void deleteByTenantId(Long tenantId) throws Exception {
		String sql = "delete from tenant_network where tenant_id = " + tenantId;
		this.execute(sql);
	}

	public void deleteByNetworkId(Long networkId) throws Exception {
		String sql = "delete from tenant_network where network_id = " + networkId;
		this.execute(sql);
	}
	
	public void delete(Long networkId, Long tenantId , Long vendorId) throws Exception {
		String sql = "delete from tenant_network where network_id = " + networkId + " and tenant_id = " + tenantId + " and vendor_id = " + vendorId ;
		this.execute(sql);
	}

	public List<TenantNetwork> listByTenantId(Long tenantId) throws Exception {
		String sql = "select a.*,b.name tenant_name from tenant_network a left join tenant b on a.tenant_id = b.id where b.is_deleted = 0 and a.tenant_id = :tenantId";
		Map<String, Object> params = MapTools.simpleMap("tenantId", tenantId);
		return this.list(TenantNetwork.class, sql, params);
	}

	public List<TenantNetwork> listByNetworkId(Long networkId) throws Exception {
		String sql = "select a.*,b.name tenant_name from tenant_network a left join tenant b on a.tenant_id = b.id where b.is_deleted = 0 and a.network_id = :networkId";
		Map<String, Object> params = MapTools.simpleMap("networkId", networkId);
		return this.list(TenantNetwork.class, sql, params);
	}

	public TenantNetwork query(Long id) throws Exception {
		String sql = "select a.*,b.name tenant_name from tenant_network a left join tenant b on a.tenant_id = b.id  where b.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<TenantNetwork> tenantNetworks = this.list(TenantNetwork.class, sql, params);
		if (ListTool.isEmpty(tenantNetworks)) {
			return null;
		}
		return tenantNetworks.get(0);
	}

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

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

	public boolean tenantCheck(Long tenantId) throws Exception {
		String sql = "select count(a.id) from tenant_network a where a.tenant_id = :tenantId";
		Map<String, Object> paramMap = MapTools.simpleMap("tenantId", tenantId);
		return this.countQuery(sql, paramMap).intValue() > 0;
	}

	/**
	 * 校验是否有租户资源
	 *
	 * @param tenantId
	 * @param vendorId
	 * @return
	 * @throws Exception
	 */

	public boolean tenantCheck(Long tenantId, Long vendorId) throws Exception {
		String sql = "select count(a.id) from tenant_network a where a.tenant_id = :tenantId and a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap("tenantId", tenantId);
		paramMap.put("vendorId", vendorId);
		return this.countQuery(sql, paramMap).intValue() > 0;
	}
}
