package com.bocloud.ims.repository.powervc;

import com.bocloud.common.utils.MapTools;
import com.bocloud.ims.entity.resource.Server;
import com.bocloud.ims.entity.resource.powervc.PowervcHmc;
import com.bocloud.ims.entity.resource.powervc.PowervcHmcServer;
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;

/**
 * @author luyanchao
 * @since 2019/1/23 17:09
 */
@Repository
public class PowervcHmcServerRepository extends EnhancedBaseRepository<PowervcHmcServer, Long> {

    public List<PowervcHmcServer> listByHmcId(long hmcId) {
        String sql = "select a.* from powervc_hmc_server a where a.hmc_id = :hmcId ";
        Map<String, Object> params = MapTools.simpleMap("hmcId", hmcId);
        return list(PowervcHmcServer.class, sql, params);
    }

    public void removeByServer(long serverId) {
        String sql = "delete from powervc_hmc_server where server_id = :serverId ";
        Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
        update(sql, params);
    }

    public void removeByHmc(long hmcId) {
        String sql = "delete from powervc_hmc_server where hmc_id = :hmcId ";
        Map<String, Object> params = MapTools.simpleMap("hmcId", hmcId);
        update(sql, params);
    }

    public List<PowervcHmc> listHmcByServer(long serverId) {
        String sql = "select a.* from powervc_hmc a where a.is_deleted = 0 and a.id in " +
                "(select b.hmc_id from powervc_hmc_server b where b.server_id = :serverId) ";
        Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
        List<Object> objects = queryForList(sql, params, PowervcHmc.class);
        List<PowervcHmc> hmcs = new ArrayList<>(objects.size());
        for (Object object : objects) {
            hmcs.add((PowervcHmc) object);
        }
        return hmcs;
    }

    public List<Server> listServerByHmc(long hmcId) {
        String sql = "select a.* from server a where a.is_deleted = 0 and a.id in " +
                "(select b.server_id from powervc_hmc_server b where b.hmc_id = :hmcId) ";
        Map<String, Object> params = MapTools.simpleMap("hmcId", hmcId);
        List<Object> objects = queryForList(sql, params, Server.class);
        List<Server> hmcs = new ArrayList<>(objects.size());
        for (Object object : objects) {
            hmcs.add((Server) object);
        }
        return hmcs;
    }

    public void remove(long id) {
        String sql = "delete from powervc_hmc_server where id = :id ";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        update(sql, params);
    }
}
