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

import com.bocloud.ims.entity.resource.hmc.HmdViosPhysicalFabricAdapter;
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 HmdViosPhysicalFabricAdapterRepository extends HmdGenericDao<HmdViosPhysicalFabricAdapter> {

    @Lookup protected abstract HmdPartitionRepository partitionRepository();

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

    @Override
    public void extra(HmdViosPhysicalFabricAdapter adapter) {
        Optional.of(adapter).map(HmdViosPhysicalFabricAdapter::getViosId).map(this.partitionRepository()::getById).ifPresent(adapter::setVios);
    }

    public List<HmdViosPhysicalFabricAdapter> listByViosId(long viosId) {
        return this.search(Collections.singletonMap("a.vios_id", viosId));
    }

    public HmdViosPhysicalFabricAdapter getByUniqueDeviceId(String uniqueDeviceId) {
        return this.search(Collections.singletonMap("a.unique_device_id", uniqueDeviceId)).stream().findFirst().orElse(null);
    }

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

    @Override
    public void clear(long id) {
        this.delete(id);
    }

}
