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

/**
 * 云主机与安全组Repository接口实现
 * 
 * @author weiwei
 */
@Repository("serverSgroupRepository")
public class ServerSgroupRepository extends BasicGenericDao<ServerSgroup, Long> {

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

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

	public List<ServerSgroup> listByGroupId(Long groupId) throws Exception {
		String sql = "select * from server_sgroup where group_id =:groupId";
		Map<String, Object> params = MapTools.simpleMap("groupId", groupId);
		return this.list(ServerSgroup.class, sql, params);
	}

	public ServerSgroup getByServerGroup(Long serverId, Long groupId) throws Exception {
		String sql = "select * from server_sgroup where group_id =:groupId and  server_id =:serverId";
		Map<String, Object> params = MapTools.simpleMap("groupId", groupId);
		params.put("serverId", serverId);
		List<ServerSgroup> list = this.list(ServerSgroup.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

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

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

	public boolean delete(Long id) throws Exception {
		String sql = "delete from server_sgroup 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_sgroup where server_id = :serverId";
		Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
		return this.execute(sql, params) > 0;
	}

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

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

	public ServerSgroup listByNicId(Long nicId) {
		String sql = "select * from server_sgroup where nic_id =:nicId";
		Map<String, Object> params = MapTools.simpleMap("nicId", nicId);
		List<ServerSgroup> list =  this.list(ServerSgroup.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public boolean removeByNicId(Long nicId) throws Exception {
		String sql = "delete from server_sgroup where nic_id =:nicId";
		Map<String, Object> params = MapTools.simpleMap("nicId", nicId);
		return this.execute(sql, params) > 0;
	}

}
