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

import com.bocloud.common.utils.MapTools;
import com.bocloud.ims.entity.resource.hmc.HmcHeaLogicalPort;
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("hmcHeaLogicalPortRepository")
public class HmcHeaLogicalPortRepository extends EnhancedBaseRepository<HmcHeaLogicalPort, Long> {

	public Boolean deleteByServerId(Long serverId) {
		String sql = "delete from hmc_hea_logical_port where hea_id in (select id from hmc_host_ethernet_adapter where server_id = :serverId)";
		Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
		return this.update(sql, params) > 0;
	}

	public Boolean deleteByHeaId(Long heaId) {
		String sql = "delete from hmc_hea_logical_port where hea_id = :heaId";
		Map<String, Object> params = MapTools.simpleMap("heaId", heaId);
		return this.update(sql, params) > 0;
	}
	
	public List<HmcHeaLogicalPort> queryByHeaId(Long heaId) {
		String sql = "select a.*, b.name as partition_name from hmc_hea_logical_port a " +
				"left join hmc_partition b on a.partition_id = b.id " +
				"where a.hea_id = :heaId";
		Map<String, Object> param = MapTools.simpleMap("heaId", heaId);
		List<Object> list = this.queryForList(sql, param, HmcHeaLogicalPort.class);
		List<HmcHeaLogicalPort> ports = new ArrayList<>();
		for (Object obj : list) {
			ports.add((HmcHeaLogicalPort) obj);
		}
		return ports;
	}
}
