package com.bocloud.ims.repository.kubernetes;

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.kubernetes.KubeNodeLabel;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("kubeNodeLabelRepository")
public class KubeNodeLabelRepository extends BasicGenericDao<KubeNodeLabel, Long> {

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

	public List<KubeNodeLabel> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select a.*, c.`name` vendor_name from kube_node_label a left join cloud_vendor c on a.vendor_id = c.id  where 1 = 1 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> param = QueryBuilder.getParam(params);
		List<KubeNodeLabel> labels = this.list(KubeNodeLabel.class, sql, param);
		List<KubeNodeLabel> list = new ArrayList<KubeNodeLabel>();
		for (Object label : labels) {
			list.add((KubeNodeLabel) label);
		}
		return list;
	}

	public List<KubeNodeLabel> list(List<Param> params) throws Exception {
		String sql = "select a.*, c.`name` vendor_name from kube_node_label a  left join cloud_vendor c on a.vendor_id = c.id where 1 = 1 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, new HashMap<>()), "a");
		Map<String, Object> param = QueryBuilder.getParam(params);
		List<KubeNodeLabel> labels = this.list(KubeNodeLabel.class, sql, param);
		List<KubeNodeLabel> list = new ArrayList<KubeNodeLabel>();
		for (Object label : labels) {
			list.add((KubeNodeLabel) label);
		}
		return list;
	}

	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from kube_node_label where 1 = 1 ";
		sql = QueryBuilder.buildRaw(sql, params, new HashMap<>(), "");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

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

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

	public boolean exists(String name) throws Exception {
		String sql = "select * from kube_node_label where name = :name";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		List<KubeNodeLabel> labels = this.list(KubeNodeLabel.class, sql, paramMap);
		if (ListTool.isEmpty(labels)) {
			return false;
		}
		return true;
	}

	public boolean exists(String name, Long vendorId) throws Exception {
		String sql = "select * from kube_node_label where vendor_id = :vendorId and name = :name";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		paramMap.put("vendorId", vendorId);
		List<KubeNodeLabel> labels = this.list(KubeNodeLabel.class, sql, paramMap);
		if (ListTool.isEmpty(labels)) {
			return false;
		}
		return true;
	}

	public boolean removeByVendor(Long vendorId, Long userId) {
		String sql = " delete from kube_node_label where 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;
	}
}
