package com.bocloud.ims.repository.resource;

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.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.ClusterNode;
import org.apache.commons.lang3.StringUtils;
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 wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Component("clusterNodeRepository")
public class ClusterNodeRepository extends BasicGenericDao<ClusterNode, Long> {

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

	public List<ClusterNode> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.* from cluster_node a 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(ClusterNode.class, sql, paramMap);
	}

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

	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update cluster_node 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 int count(List<Param> params) throws Exception {
		String sql = "select count(1) from cluster_node 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 removeByRid(String resourceId, Long userId) throws Exception {
		String sql = "update cluster_node set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and resource_id = :resourceId";
		Map<String, Object> params = MapTools.simpleMap("resourceId", resourceId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public ClusterNode queryByRid(String resourceId) throws Exception {
		String sql = "select a.* from cluster_node a where a.is_deleted = 0 and a.resource_id = :resourceId";
		Map<String, Object> params = MapTools.simpleMap("resourceId", resourceId);
		List<ClusterNode> nodes = this.list(ClusterNode.class, sql, params);
		if (nodes.isEmpty()) {
			return null;
		}
		return nodes.get(0);
	}

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

	public ClusterNode query(Long id) throws Exception {
		String sql = "select a.* from cluster_node a where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<ClusterNode> nodes = this.list(ClusterNode.class, sql, params);
		if (nodes.isEmpty()) {
			return null;
		}
		return nodes.get(0);
	}

	public List<ClusterNode> listByCluster(Long clusterId) throws Exception {
		String sql = "select a.* from cluster_node a where a.is_deleted = 0 and a.cluster_id = :clusterId";
		Map<String, Object> params = MapTools.simpleMap("clusterId", clusterId);
		return this.list(ClusterNode.class, sql, params);
	}

	public List<ClusterNode> listByRole(String role, Long vendorId) throws Exception {
		String sql = "select a.* from cluster_node a where a.is_deleted = 0 and a.roles like '%" + role
				+ "%' and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(ClusterNode.class, sql, params);
	}

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

	public boolean removeByVendor(Long vendorId, Long userId) throws Exception {
		String sql = "update cluster_node 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<ClusterNode> listAddHost(Long vendorId) throws Exception {
		String sql = " select a.* from cluster_node a where a.is_deleted = 0 and a.cluster_id is null and a.vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(ClusterNode.class, sql, params);
	}

	public List<ClusterNode> listAddHost(Long vendorId, String region) throws Exception {
		String sql = " select a.* from cluster_node a where a.is_deleted = 0 and a.cluster_id is null and a.vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(ClusterNode.class, sql, params);
	}

	public List<ClusterNode> listRemoveHost(Long clusterId, Long vendorId) {
		String sql = " select a.* from cluster_node a where a.is_deleted = 0 and a.cluster_id = :clusterId and a.vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("clusterId", clusterId);
		return this.list(ClusterNode.class, sql, params);
	}

	public List<ClusterNode> listRemoveHost(Long clusterId, Long vendorId, String region) {
		String sql = " select a.* from cluster_node a where a.is_deleted = 0 and a.cluster_id = :clusterId and a.vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		if(!StringUtils.isEmpty(region)) {
			sql += " and a.zone = :region ";
			params.put("region", region);
		}
		params.put("clusterId", clusterId);
		return this.list(ClusterNode.class, sql, params);
	}

	public ClusterNode getByName(String name, Long vendorId) {
		String sql = "select a.* from cluster_node a where a.is_deleted = 0 and a.name = :name and a.vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		params.put(Common.VENDORID, vendorId);
		List<ClusterNode> nodes = this.list(ClusterNode.class, sql, params);
		if (nodes.isEmpty()) {
			return null;
		}
		return nodes.get(0);
	}

}
