package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
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.CloudStackEvent;
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;

/**
 * 虚拟机秘钥关系 实现
 * 
 * @author ymh
 * @时间 2018年12月17日
 */
@Repository("cloudStackEventRepository")
public class CloudStackEventRepository extends BasicGenericDao<CloudStackEvent, Long> {

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

	public List<CloudStackEvent> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = " select * from cloud_stack_event a where 1=1 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(CloudStackEvent.class, sql, paramMap);
	}

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

	public List<CloudStackEvent> listByStackId(Long stackId, Long vendorId) throws Exception {
		String sql = "select * from cloud_stack_event a where a.stack_id =:stackId and a.vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap("stackId", stackId);
		params.put(Common.VENDORID, vendorId);
		return this.list(CloudStackEvent.class, sql, params);
	}

	public Boolean batchSave(List<CloudStackEvent> cloudStackEvents) throws Exception {
		return this.batchSave(cloudStackEvents, cloudStackEvents.size());
	}

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

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

	public boolean removeByStackId(Long stackId, Long vendorId) throws Exception {
		String sql = "delete from cloud_stack_event a where a.vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		if (null != stackId && stackId > 0) {
			sql += " and a.stack_id = :stackId ";
			params.put("stackId", stackId);
		}
		return this.execute(sql, params) > 0;
	}

	public Boolean save(List<CloudStackEvent> cloudStackEvents) throws Exception {
		return this.batchSave(cloudStackEvents, cloudStackEvents.size());
	}

}
