package com.bocloud.ims.repository.powervc;

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.ims.entity.resource.powervc.HostSeaAdapter;
import com.bocloud.ims.entity.resource.powervc.NetworkSeaAdapter;
import com.bocloud.ims.repository.resource.common.EnhancedBaseRepository;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * powervc network sea dao impl
 *
 * @author luyanchao
 */
@Repository
public class NetworkSeaRepository extends EnhancedBaseRepository<NetworkSeaAdapter, Long> {

    public void deleteByNetworkId(long networkId) throws BoCloudDBException {
        String sql = "delete from network_sea_adapter where network_id = :networkId";
        Map<String, Object> paramMap = new HashMap<>(8);
        paramMap.put("networkId", networkId);
        this.update(sql, paramMap);
    }

    public List<NetworkSeaAdapter> listByNetworkId(long networkId) {
        String sql = "select a.*, b.server_id, b.lpar_id, b.sea_name, c.hostname as server_host, c.name as server_name, d.network_uuid "
                + "from network_sea_adapter a " + "join host_sea_adapter b on a.sea_id = b.id "
                + "join server c on b.server_id = c.id " + "join network d on a.network_id = d.id "
                + "where a.network_id = :networkId";
        Map<String, Object> paramMap = new HashMap<>(8);
        paramMap.put("networkId", networkId);
        return this.list(NetworkSeaAdapter.class, sql, paramMap);
    }

    public List<NetworkSeaAdapter> listByVendorId(long vendorId) {
        String sql = "select a.*, b.server_id, b.lpar_id, b.sea_name, c.hostname as server_host, d.network_uuid "
                + "from network_sea_adapter a " + "join host_sea_adapter b on a.sea_id = b.id "
                + "join server c on b.server_id = c.id " + "join network d on a.network_id = d.id "
                + "where a.network_id in (select id from network where is_deleted = 0 and vendor_id = :vendorId)";
        Map<String, Object> paramMap = new HashMap<>(8);
        paramMap.put("vendorId", vendorId);
        return this.list(NetworkSeaAdapter.class, sql, paramMap);
    }

    public List<HostSeaAdapter> listHostSeaByNetworkId(long networkId) {
        String sql = "select a.*, b.name as server_name, b.hostname as server_host " + "from host_sea_adapter a "
                + "join server b on a.server_id = b.id "
                + "where a.id in (select sea_id from network_sea_adapter where network_id = :networkId)";
        Map<String, Object> paramMap = new HashMap<>(8);
        paramMap.put("networkId", networkId);
        List<Object> list = this.queryForList(sql, paramMap, HostSeaAdapter.class);
        List<HostSeaAdapter> seas = new ArrayList<>();
        for (Object object : list) {
            seas.add((HostSeaAdapter) object);
        }
        return seas;
    }

    public void deleteById(long id) {
        String sql = "delete from network_sea_adapter where id = :id";
        Map<String, Object> paramMap = new HashMap<>(8);
        paramMap.put("id", id);
        this.update(sql, paramMap);
    }

    public void deleteByNetworkIdAndSeaId(long networkId, long seaId) {
        String sql = "delete from network_sea_adapter where network_id = :networkId and sea_id = :seaId";
        Map<String, Object> paramMap = new HashMap<>(8);
        paramMap.put("networkId", networkId);
        paramMap.put("seaId", seaId);
        this.update(sql, paramMap);
    }

    public void deleteByVendorId(long vendorId) throws BoCloudDBException {
        String sql = "delete from network_sea_adapter where network_id in "
                + "(select id from network where vendor_id = :vendorId)";
        Map<String, Object> paramMap = new HashMap<>(8);
        paramMap.put("vendorId", vendorId);
        this.update(sql, paramMap);
    }
}
