package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.OSTenant;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * openstack底层租户Repository实现
 *
 * @author wangyu
 * @version 4.2.1-SNAPSHOT
 * @since 2019/11/14
 */
@Repository("oSTenantRepository")
public class OSTenantRepository extends BasicGenericDao<OSTenant, Long> {

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

	/**
	 * 查询openstack租户列表
	 *
	 * @param pager
	 * @return
	 * @throws Exception
	 */

	public List<OSTenant> list(Pager pager) {
		String sql = "select a.*, t.`name` tenant_name from os_tenant a left join tenant t on a.tenant_id = t.id where 1 = 1 ";
		sql = QueryBuilder.buildRaw(sql, pager, "a");
		Map<String, Object> param = QueryBuilder.getParam(pager.getParams());
		return this.list(OSTenant.class, sql, param);
	}

	/**
	 * 查询openstack租户列表
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */

	public List<OSTenant> list(List<Param> params) {
		String sql = "select a.*,b.name creator from os_tenant a left join user b on b.id = a.creator_id where 1 = 1 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, new ArrayList<>(), new HashMap<>(8)), "a");
		return this.list(OSTenant.class, sql, new HashMap<>(8));
	}

	/**
	 * 获取openstack租户数量
	 *
	 * @param params
	 * @return
	 * @throws Exception
	 */

	public int count(List<Param> params) {
		String sql = "select count(id) from os_tenant where 1 = 1 ";
		sql = QueryBuilder.build(sql, params, null, "");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

	/**
	 * 删除openstack租户
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */

	public boolean remove(Long id) {
		String sql = "delete from os_tenant where id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		return this.execute(sql, params) > 0;
	}

	/**
	 * 查询openstack租户详情
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */

	public OSTenant query(Long id) {
		String sql = "select a.*, t.name tenant_name from os_tenant a left join tenant t on a.tenant_id = t.id where a.id = :id ";
		List<OSTenant> osTenants = this.list(OSTenant.class, sql, MapTools.simpleMap("id", id));
		if (ListTool.isEmpty(osTenants)) {
			return null;
		}
		return osTenants.get(0);
	}

	/**
	 * 根据云平台id查询
	 *
	 * @param vendorId
	 * @return
	 * @throws Exception
	 */

	public List<OSTenant> listByVid(Long vendorId) {
		String sql = "select a.*,b.name creator from os_tenant a left join user b on b.id = a.creator_id where a.vendor_id = :vendorId ";
		return this.list(OSTenant.class, sql, MapTools.simpleMap(Common.VENDORID, vendorId));
	}

	/**
	 * 根据云平台id删除
	 *
	 * @param vendorId
	 * @return
	 * @throws Exception
	 */

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

	public boolean removeByTenant(long tenantId) {
		String sql = "delete from os_tenant where tenant_id = :tenantId";
		Map<String, Object> params = MapTools.simpleMap("tenantId", tenantId);
		return this.execute(sql, params) > 0;
	}

	/**
	 * 根据名称查询
	 *
	 * @param name
	 * @param vendorId
	 * @return
	 * @throws Exception
	 */

	public OSTenant getByName(String name, Long vendorId) {
		String sql = "select a.*,b.name creator from os_tenant a left join user b on b.id = a.creator_id where a.name = :name and a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.NAME, name);
		paramMap.put(Common.VENDORID, vendorId);
		List<OSTenant> osTenants = this.list(OSTenant.class, sql, paramMap);
		if (ListTool.isEmpty(osTenants)) {
			return null;
		}
		return osTenants.get(0);
	}

	/**
	 * 根据UUID查询
	 *
	 * @param tenantUuid
	 * @param vendorId
	 * @return
	 * @throws Exception
	 */

	public OSTenant getByTenantUuid(String tenantUuid, Long vendorId) {
		String sql = "select a.*,b.name creator from os_tenant a left join user b on b.id = a.creator_id where a.tenant_uuid = :tenantUuid and a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap("tenantUuid", tenantUuid);
		paramMap.put(Common.VENDORID, vendorId);
		List<OSTenant> osTenants = this.list(OSTenant.class, sql, paramMap);
		if (ListTool.isEmpty(osTenants)) {
			return null;
		}
		return osTenants.get(0);
	}

	public OSTenant getByTenantId(Long tenantId, Long vendorId) {
		String sql = "select a.* where a.tenant_id = :tenantId and a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap("tenantId", tenantId);
		paramMap.put(Common.VENDORID, vendorId);
		List<OSTenant> osTenants = this.list(OSTenant.class, sql, paramMap);
		if (ListTool.isEmpty(osTenants)) {
			return null;
		}
		return osTenants.get(0);
	}
}
