package com.bocloud.ims.repository.h3c;

import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.ims.entity.resource.Subnet;
import com.bocloud.ims.entity.resource.Switcher;
import com.bocloud.ims.repository.resource.common.EnhancedBaseRepository;
import org.springframework.stereotype.Repository;

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

@Repository("h3cNetworkRepository")
public class H3cNetworkRepository extends EnhancedBaseRepository<Switcher, Long> {

	public List<Map<String, Object>> listH3cSwitcherByHost(Long hostId) {
		String sql = "SELECT s.id, s.name, s.gmt_create as gmtCreate, s.gmt_modify as gmtModify, s.remark, "
				+ " sb.address, sb.cidr, sb.gateway, sb.ip_pools as ipPools FROM switcher s LEFT JOIN network n ON s.id = n.switcher_id "
				+ " LEFT JOIN subnet sb ON n.id = sb.network_id " + " WHERE s.is_deleted = 0 AND s.host_id = :hostId";
		Map<String, Object> params = MapTools.simpleMap("hostId", hostId);
		return queryForList(sql, params);
	}

	public Subnet getSubnetBySwitcher(Long vSwitchId) {
		String sql = "SELECT " + " sb.* " + "FROM " + " switcher s " + "LEFT JOIN network n ON s.id = n.switcher_id "
				+ "LEFT JOIN subnet sb ON n.id = sb.network_id " + "WHERE " + " s.is_deleted = 0 " + "AND s.id=:id";
		Map<String, Object> params = MapTools.simpleMap("id", vSwitchId);
		List<Object> list = this.queryForList(sql, params, Subnet.class);
		Subnet result = null;
		if (!ListTool.isEmpty(list)) {
			result = (Subnet) list.get(0);
		}
		return result;
	}

}
