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.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.middleware.K8sCluster;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * 数据库服务Repository接口实现类
 * 
 * @author wei wei
 */
@Repository("k8sClusterRepository")
public class K8sClusterRepository extends BasicGenericDao<K8sCluster, Long> {

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

	public List<K8sCluster> list(int page, int rows, List<Param> params, Map<String, String> sorter, Long userId,
			Long tenantId) throws Exception {
		String sql = "select a.*,b.expired_time,c.name category_name from k8s_cluster a left join service_category c on a.category_id = c.id,object_relation b where a.is_deleted = 0 and a.id = b.res_id and b.category = 'Kubernetes' ";
		if (null != userId) {
			sql += " and b.target = " + userId;
		}
		if (null != tenantId) {
			sql += " and a.tenant_id = " + tenantId;
		}
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(K8sCluster.class, sql);
	}

	public int count(List<Param> params, Long userId, Long tenantId) throws Exception {
		String sql = "select count(*) from k8s_cluster a,object_relation b where a.is_deleted = 0 and a.id = b.res_id and b.category = 'Kubernets' ";
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		if (null != userId) {
			sql += " and b.target = " + userId;
		}
		if (null != tenantId) {
			sql += " and a.tenant_id = " + tenantId;
		}
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

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

	public K8sCluster query(Long id) throws Exception {
		String sql = "select a.*,b.expired_time,c.name category_name from k8s_cluster a left join service_category c on a.category_id = c.id,object_relation b where a.is_deleted = 0 and a.id = b.res_id and b.category = 'Kubernetes' and a.id = :id";
		Map<String, Object> paramMap = MapTools.simpleMap("id", id);
		List<K8sCluster> list = this.list(K8sCluster.class, sql, paramMap);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

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

	public K8sCluster get(String uuid) throws Exception {
		String sql = "select a.*,b.expired_time,c.name category_name from k8s_cluster a left join service_category c on a.category_id = c.id,object_relation b where a.is_deleted = 0 and a.id = b.res_id and b.category = 'Kubernetes' and a.uuid = :uuid";
		Map<String, Object> params = MapTools.simpleMap(Common.UUID, uuid);
		List<K8sCluster> list = this.list(K8sCluster.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

}
