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

/**
 * pod容器
 * 
 * @author Administrator
 *
 */
@Component("kubeContainerRepository")
public class KubeContainerRepository extends JdbcGenericDao<KubeContainer, Long> {

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

	public List<KubeContainer> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		return this.list(page, rows, params, sorter, null);
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from kube_container 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<KubeContainer> contracts = this.list(KubeContainer.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<SimpleBean>();
		for (KubeContainer 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_container 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 boolean batchRemove(List<Long> ids, Long userId) throws Exception {
		String sql = "update kube_container set is_deleted = true , gmt_modify = :gmtModify ,mender_id = :menderId where is_deleted = 0 and id in (:ids) ";
		Map<String, Object> params = MapTools.simpleMap("ids", ids);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

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

	public List<KubeContainer> list(List<Long> ids) throws Exception {
		String sql = "select a.* from kube_container a where a.is_deleted = 0 and a.id in (:ids)";
		Map<String, Object> params = MapTools.simpleMap("ids", ids);
		return this.list(KubeContainer.class, sql, params);
	}

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

	public List<KubeContainer> listByParams(List<Param> params) throws Exception {
		String sql = "select a.id,a.name from kube_container a where a.is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, new Pager(1, Integer.MAX_VALUE, params, null), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(KubeContainer.class, sql, paramMap);
	}

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

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

	public boolean remove(Long vendorId, Long podId, Long userId) {
		String sql = "update kube_container 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);
		if (null != podId && 0 != podId) {
			sql += " and pod_id = :podId ";
			params.put("podId", podId);
		}
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

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

}
