package com.bocloud.ims.repository.powervc;

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.powervc.HostSeaAdapter;
import com.bocloud.ims.repository.resource.common.EnhancedBaseRepository;
import org.springframework.stereotype.Repository;

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

/**
 * powervc host sea dao impl
 *
 * @author lkx007
 */
@Repository
public class HostSeaRepository extends EnhancedBaseRepository<HostSeaAdapter, Long> {
    public List<HostSeaAdapter> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
        String sql = "select a.*, b.name as server_name " +
                " from host_sea_adapter a inner join server b on b.id = a.server_id  " +
                " where 1 = 1 ";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.list(HostSeaAdapter.class, sql, paramMap);
    }

    public int count(List<Param> params) {
        String sql = "select count(1) from host_sea_adapter a where 1 = 1  ";
        sql = QueryBuilder.buildRaw(sql, params, null, "a");
        Map<String, Object> paramMap = QueryBuilder.getParam(params);
        return this.countQuery(sql, paramMap).intValue();
    }

    public HostSeaAdapter getByName(String seaName, int lparId, String serverHostName, long vendorId) {
        String sql = "select a.*, b.name as server_name from host_sea_adapter a " +
                "join server b on a.server_id = b.id " +
                "where a.sea_name = :seaName and a.lpar_id = :lparId and b.hostname = :serverHostName and a.vendor_id = :vendorId ";
        Map<String, Object> paramMap = new HashMap<>(8);
        paramMap.put("seaName", seaName);
        paramMap.put("lparId", lparId);
        paramMap.put("serverHostName", serverHostName);
        paramMap.put("vendorId", vendorId);
        List<HostSeaAdapter> adapters = this.list(HostSeaAdapter.class, sql, paramMap);
        return adapters == null || adapters.size() == 0 ? null : adapters.get(0);
    }

    public void removeByVendor(long vendorId) throws BoCloudDBException {
        String sql = "delete from host_sea_adapter where vendor_id = :vendorId ";
        Map<String, Object> paramMap = new HashMap<>(8);
        paramMap.put("vendorId", vendorId);
        this.update(sql, paramMap);
    }

    public List<HostSeaAdapter> listByVendor(long vendorId) {
        String sql = "select a.*, b.name as server_name from host_sea_adapter a " +
                "join server b on a.server_id = b.id " +
                "where a.vendor_id = :vendorId ";
        Map<String, Object> paramMap = new HashMap<>(8);
        paramMap.put("vendorId", vendorId);
        return this.list(HostSeaAdapter.class, sql, paramMap);
    }

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

    public List<HostSeaAdapter> listServerAndSeaByVendor(long vendorId) {
        String sql = "SELECT c.*, a.name as server_name, a.hostname as server_host FROM server a " +
                "       LEFT JOIN (SELECT server_id, max(id) AS id FROM host_sea_adapter " +
                "                  WHERE state = 'available' GROUP BY server_id) b " +
                "         ON a.id = b.server_id " +
                "       LEFT JOIN host_sea_adapter c ON b.id = c.id " +
                "WHERE a.is_deleted = 0 and a.vendor_id = :vendorId ";
        Map<String, Object> paramMap = new HashMap<>(8);
        paramMap.put("vendorId", vendorId);
        return list(HostSeaAdapter.class, sql, paramMap);
    }

    public List<HostSeaAdapter> listByServerId(long serverId) {
        String sql = "select a.*, b.name as server_name, b.hostname as server_host " +
                "from host_sea_adapter a " +
                "left join server b on a.server_id = b.id " +
                "where a.state != 'Not-Applicable' and a.server_id = :serverId ";
        Map<String, Object> paramMap = new HashMap<>(8);
        paramMap.put("serverId", serverId);
        return list(HostSeaAdapter.class, sql, paramMap);
    }
}
