
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.StackTemplateVersion;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * qos policy
 * 
 * @author ymh
 * @时间 2019年11月13日
 */
@Repository("stackTemplateVersionRepository")
public class StackTemplateVersionRepository extends BasicGenericDao<StackTemplateVersion, Long> {

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

	public List<StackTemplateVersion> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = " select a.* from stack_template_version a where a.is_deleted = 0  ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(StackTemplateVersion.class, sql);
	}

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

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

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

	public StackTemplateVersion getByUuid(String version) throws Exception {
		String sql = "select a.* from stack_template_version a where a.is_deleted = 0 and a.version = :version";
		Map<String, Object> param = MapTools.simpleMap("version", version);
		List<StackTemplateVersion> list = this.list(StackTemplateVersion.class, sql, param);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

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

	public boolean removeByVid(Long vendorId, Long userId) throws Exception {
		String sql = " delete from stack_template_version where vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.execute(sql, params) > 0;
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public StackTemplateVersion queryByUuid(String uuid, Long vendorId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
}
