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

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

    @Lookup protected abstract HmdServerRepository serverRepository();

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

    @Override
    public void extra(HmdServerSharedProcessorPool pool) {
        Optional.of(pool).map(HmdServerSharedProcessorPool::getServerId).map(this.serverRepository()::getById).ifPresent(pool::setServer);
    }

    public List<HmdServerSharedProcessorPool> listByServerId(long serverId) {
        return this.search(Collections.singletonMap("a.server_id", serverId));
    }

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

    public HmdServerSharedProcessorPool getByUuid(String uuid) {
        return this.search(Collections.singletonMap("a.uuid", uuid)).stream().findFirst().orElse(null);
    }

}
