package com.bocloud.ims.repository.resource;

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.ServerKeypair;
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("serverKeypairRepository")
public class ServerKeypairRepository extends BasicGenericDao<ServerKeypair, Long> {

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

	public List<ServerKeypair> listByServerId(Long serverId) throws Exception {
		String sql = "select * from server_keypair where server_id =:serverId";
		Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
		return this.list(ServerKeypair.class, sql, params);
	}

	public List<ServerKeypair> listByKeypairId(Long keypairId) throws Exception {
		String sql = "select * from server_keypair where keypair_id =:keypairId";
		Map<String, Object> params = MapTools.simpleMap("keypairId", keypairId);
		return this.list(ServerKeypair.class, sql, params);
	}

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

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

	public boolean modify(ServerKeypair ServerKeypair) throws Exception {
		return this.update(ServerKeypair);
	}

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

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

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

	public boolean remove(Long keypairId, Long serverId) throws Exception {
		String sql = "delete from server_keypair where keypair_id = :keypairId and server_id = :serverId";
		Map<String, Object> params = MapTools.simpleMap("keypairId", keypairId);
		params.put("serverId", serverId);
		return this.execute(sql, params) > 0;
	}

}
