package com.bocloud.ims.repository.kubernetes;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.kubernetes.KubeNamespace;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

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

/**
 * @author weiwei
 */
@Component("kubeNamespaceRepository")
public class KubeNamespaceRepository extends JdbcGenericDao<KubeNamespace, Long> {

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

	public List<KubeNamespace> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = " select a.*,c.`name` vendor_name, t.`name` tenant_name from kube_namespace a left join cloud_vendor c on a.vendor_id = c.id left join tenant t on a.tenant_id = t.id where a.is_deleted = 0 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(KubeNamespace.class, sql, paramMap);
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from kube_namespace a where a.is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		List<KubeNamespace> contracts = this.list(KubeNamespace.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<SimpleBean>();
		for (KubeNamespace contract : contracts) {
			beans.add(new SimpleBean(contract.getId(), contract.getName()));
		}
		return beans;
	}

	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from kube_namespace a where a.is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update kube_namespace set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public KubeNamespace query(Long id) throws Exception {
		String sql = "select a.*,c.`name` vendor_name from kube_namespace a left join cloud_vendor c on a.vendor_id = c.id where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> param = MapTools.simpleMap(Common.ID, id);
		List<KubeNamespace> list = this.list(KubeNamespace.class, sql, param);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public boolean removeByVendor(Long vendorId, Long userId) throws Exception {
		String sql = "update kube_namespace set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public List<KubeNamespace> listByVendor(Long vendorId) throws Exception {
		String sql = "select a.* from kube_namespace a where a.is_deleted = 0 and a.vendor_id = :vendorId";
		Map<String, Object> param = MapTools.simpleMap(Common.VENDORID, vendorId);
		List<KubeNamespace> list = this.list(KubeNamespace.class, sql, param);
		return list;
	}

	public KubeNamespace queryByName(String name, Long vendorId) throws Exception {
		String sql = " select a.* from kube_namespace a where a.is_deleted = 0 and a.`name` = :name and a.vendor_id = :vendorId ";
		Map<String, Object> param = MapTools.simpleMap("name", name);
		param.put(Common.VENDORID, vendorId);
		List<KubeNamespace> list = this.list(KubeNamespace.class, sql, param);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<KubeNamespace> listByTenant(Long tenantId) {
		String sql = "select a.* from kube_namespace a where a.is_deleted = 0 and a.tenant_id = :tenantId";
		Map<String, Object> param = MapTools.simpleMap("tenantId", tenantId);
		List<KubeNamespace> list = this.list(KubeNamespace.class, sql, param);
		return list;
	}

	public int count(List<Param> params, Long tenantId, Long userId, List<Long> poolIds, List<Long> vdcIds) {
		String sql = " SELECT	a.*,	v.NAME vendor_name,	v.type vendor_type,	t.NAME tenant_name FROM	kube_namespace a	LEFT JOIN cloud_vendor v ON a.vendor_id = v.id	LEFT JOIN tenant t ON a.tenant_id = t.id WHERE	a.is_deleted = 0  ";
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		if (null != tenantId && 0L != tenantId) {
			sql += " and a.tenant_id = :tenantId";
			paramMap.put("tenantId", tenantId);
			if (poolIds != null && !poolIds.isEmpty() && vdcIds != null && !vdcIds.isEmpty()) {
				sql += " and a.vdc_id in (:vdcIds)  and a.poolgroup_id in (:poolIds) ";
				paramMap.put("poolIds", poolIds);
				paramMap.put("vdcIds", vdcIds);
			}
		}
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		sql = "select count(1) from (" + sql + ") ct";
		return this.countQuery(sql, paramMap).intValue();
	}

	public List<KubeNamespace> list(int page, int rows, List<Param> params, Map<String, String> sorter, Long tenantId,
			Long userId, List<Long> poolIds, List<Long> vdcIds) {
		String sql = " SELECT	a.*,v.NAME vendor_name,	v.type vendor_type,	t.NAME tenant_name "
				+ " FROM	kube_namespace a	LEFT JOIN cloud_vendor v ON a.vendor_id = v.id	"
				+ " LEFT JOIN tenant t ON a.tenant_id = t.id " + " WHERE	a.is_deleted = 0  ";
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		if (null != tenantId && 0L != tenantId) {
			sql += " and a.tenant_id = :tenantId";
			paramMap.put("tenantId", tenantId);
			if (poolIds != null && !poolIds.isEmpty() && vdcIds != null && !vdcIds.isEmpty()) {
				sql += " and a.vdc_id in (:vdcIds)  and a.poolgroup_id in (:poolIds) ";
				paramMap.put("poolIds", poolIds);
				paramMap.put("vdcIds", vdcIds);
			}
		}
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(KubeNamespace.class, sql, paramMap);
	}

}
