package com.bocloud.ims.repository.resource.hmc;

import com.bocloud.common.utils.MapTools;
import com.bocloud.ims.entity.resource.hmc.HmcProfileNetworkAdapter;
import com.bocloud.ims.repository.resource.common.EnhancedBaseRepository;
import org.springframework.stereotype.Repository;

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

@Deprecated
@Repository("HmcProfileNetworkAdapterRepository")
public class HmcProfileNetworkAdapterRepository extends EnhancedBaseRepository<HmcProfileNetworkAdapter, Long> {

	public boolean deleteByPartitionId(Long partitionId) {
		String sql = "delete from hmc_profile_network_adapter where profile_id in (select id from hmc_profile where partition_id = :partitionId)";
		Map<String, Object> params = MapTools.simpleMap("partitionId", partitionId);
		return this.update(sql, params) > 0;
	}

	public boolean deleteByProfileId(Long profileId) {
		String sql = "delete from hmc_profile_network_adapter where profile_id = :profileId";
		Map<String, Object> params = MapTools.simpleMap("profileId", profileId);
		return this.update(sql, params) > 0;
	}

	public List<HmcProfileNetworkAdapter> queryByProfileId(Long profileId) {
		String sql = "select a.* from hmc_profile_network_adapter a where a.profile_id = :profileId";
		Map<String, Object> param = MapTools.simpleMap("profileId", profileId);
		List<Object> list = this.queryForList(sql, param, HmcProfileNetworkAdapter.class);
		List<HmcProfileNetworkAdapter> adapters = new ArrayList<>();
		for (Object obj : list) {
			adapters.add((HmcProfileNetworkAdapter) obj);
		}
		return adapters;
	}

	public List<Map<String, Object>> getVlansBySwitchId(Integer switchId, Long serverId) {
		String sql = "select a.virtual_vlan_id, b.name from hmc_profile_network_adapter a, hmc_partition b, hmc_profile c where "
				+ "(c.partition_id = b.id and c.is_active_profile = true and a.profile_id = c.id and b.status = 'running' and b.is_deleted = false) "
				+ "and a.virtual_switch_id = :switchId and b.server_id =:serverId";
		Map<String, Object> param = MapTools.simpleMap("switchId", switchId);
		param.put("serverId", serverId);
		return this.queryForList(sql, param);
	}

	public HmcProfileNetworkAdapter queryByProfileIdAndAdapterId(Long profileId, int adapterId) {
		String sql = "select a.* from hmc_profile_network_adapter a where a.profile_id = :profileId and a.adapter_id = :adapterId";
		Map<String, Object> param = MapTools.simpleMap("profileId", profileId);
		param.put("adapterId", adapterId);
		List<Object> list = this.queryForList(sql, param, HmcProfileNetworkAdapter.class);
		return list == null || list.size() == 0 ? null : (HmcProfileNetworkAdapter) list.get(0);
	}
}
