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.KubeDaemonSet;
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;

/**
 * KubeDaemonSet
 * 
 * @author Administrator
 *
 */
@Component("kubeDaemonSetRepository")
public class KubeDaemonSetRepository extends JdbcGenericDao<KubeDaemonSet, Long> {

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

	public List<KubeDaemonSet> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = " select a.* , t.`name` tenant_name , v.`name` vendor_name , v.type vendor_type from kube_daemonset a left join tenant t on a.tenant_id = t.id   left join cloud_vendor v on a.vendor_id = v.id where a.is_deleted = 0 ";
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(KubeDaemonSet.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_daemonset 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<KubeDaemonSet> contracts = this.list(KubeDaemonSet.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<SimpleBean>();
		for (KubeDaemonSet contract : contracts) {
			beans.add(new SimpleBean(contract.getId(), contract.getName()));
		}
		return beans;
	}

	public int count(List<Param> params) throws Exception {
		return this.count(params, null);
	}

	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update kube_daemonset 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 KubeDaemonSet query(Long id) throws Exception {
		String sql = "select a.* from kube_daemonset a where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> param = MapTools.simpleMap(Common.ID, id);
		List<KubeDaemonSet> list = this.list(KubeDaemonSet.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_daemonset 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<KubeDaemonSet> listByVendor(Long vendorId) throws Exception {
		String sql = "select a.* from kube_daemonset a where a.is_deleted = 0 and a.vendor_id = :vendorId";
		Map<String, Object> param = MapTools.simpleMap(Common.VENDORID, vendorId);
		List<KubeDaemonSet> list = this.list(KubeDaemonSet.class, sql, param);
		return list;
	}

	public int count(List<Param> params, List<Long> projectIds) {
		String sql = "select count(1) from kube_daemonset 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 KubeDaemonSet getByName(String name, Long vendorId) {
		String sql = "select a.* from kube_daemonset a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.name = :name ";
		Map<String, Object> param = MapTools.simpleMap(Common.VENDORID, vendorId);
		param.put("name", name);
		List<KubeDaemonSet> list = this.list(KubeDaemonSet.class, sql, param);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<KubeDaemonSet> listByNsId(Long nsId, Long vendorId, Long tenantId) {
		String sql = "select a.* from kube_daemonset a where a.is_deleted = 0 and a.vendor_id = :vendorId and a.app_id = :nsId ";
		Map<String, Object> param = MapTools.simpleMap(Common.VENDORID, vendorId);
		param.put("nsId", nsId);
		if (null != tenantId && 0 != tenantId) {
			sql += " and a.tenant_id = :tenantId ";
			param.put("tenantId", tenantId);
		}
		return this.list(KubeDaemonSet.class, sql, param);
	}

}
