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

import com.bocloud.ims.entity.resource.hmc.HmdVirtualFabricAdapterWwpn;
import org.springframework.beans.factory.annotation.Lookup;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Repository
public abstract class HmdVirtualFabricAdapterWwpnRepository extends HmdGenericDao<HmdVirtualFabricAdapterWwpn> {

    @Lookup protected abstract HmdVirtualFabricAdapterRepository virtualFabricAdapterRepository();

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

    @Override
    public void extra(HmdVirtualFabricAdapterWwpn wwpn) {
        Optional.of(wwpn).map(HmdVirtualFabricAdapterWwpn::getAdapterId).map(this.virtualFabricAdapterRepository()::getById).ifPresent(wwpn::setFabricAdapter);
    }

    public List<HmdVirtualFabricAdapterWwpn> listByAdapterId(long adapterId) {
        return this.search(Collections.singletonMap("a.adapter_id", adapterId));
    }

    public HmdVirtualFabricAdapterWwpn getById(long id) {
        return this.search(Collections.singletonMap("a.id", id)).stream().findFirst().orElse(null);
    }

    public HmdVirtualFabricAdapterWwpn getByWwpn(String wwpn) {
        return this.search(Collections.singletonMap("a.wwpn", wwpn)).stream().findFirst().orElse(null);
    }

}
