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.TenantProvider;
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 2017年12月8日
 *
 */
@Repository("tenantProviderRepository")
public class TenantProviderRepository extends BasicGenericDao<TenantProvider, Long> {

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

	public List<TenantProvider> listByTenant(Long tenantId) {
		String sql = "select * from tenant_provider where tenant_id = :tenantId";
		Map<String, Object> params = MapTools.simpleMap("tenantId", tenantId);
		return this.list(TenantProvider.class, sql, params);
	}

	public List<TenantProvider> listByProvider(Long vendorId) {
		String sql = "select * from tenant_provider where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.list(TenantProvider.class, sql, params);
	}

	public void deleteByTenant(Long tenantId) {
		String sql = "delete from tenant_provider where tenant_id = " + tenantId;
		this.execute(sql);
	}
	
	public void deleteByProjectName(Long vendorId, String projectName) {
		String sql = "delete from tenant_provider where vendor_id = " + vendorId + " and project_name = '" + projectName +"'";
		this.execute(sql);
	}

	public void deleteByProvider(Long vendorId) {
		String sql = "delete from tenant_provider where vendor_id = " + vendorId;
		this.execute(sql);
	}

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

	public TenantProvider query(Long vendorId, Long tenantId) {
		String sql = "select a.*, o.tenant_uuid tenant_uuid from tenant_provider a "
				+ " left join os_tenant o on a.vendor_id = o.vendor_id and a.project_name = o.`name` "
				+ " where a.vendor_id = :vendorId and a.tenant_id = :tenantId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("tenantId", tenantId);
		List<TenantProvider> list = this.list(TenantProvider.class, sql, params);
		if (ListTool.isEmpty(list)) {
			return null;
		}
		return (TenantProvider) list.get(0);
	}

	public TenantProvider getByProjectName(String projectName, Long vendorId) {
		String sql = "select a.* from tenant_provider a where a.vendor_id = :vendorId and a.project_name = :projectName";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("projectName", projectName);
		List<TenantProvider> list = this.list(TenantProvider.class, sql, params);
		if (ListTool.isEmpty(list)) {
			return null;
		}
		return (TenantProvider) list.get(0);
	}

}
