package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.StorageHostPort;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

@Repository("storageHostPortRepository")
public class StorageHostPortRepository extends BasicGenericDao<StorageHostPort, Long>  {

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

    
    public List<StorageHostPort> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
        String sql = "select a.* from storage_host_port a where a.is_deleted = 0 ";
        sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
        return this.list(StorageHostPort.class, sql);
    }

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

    
    public void remove(long id) {
        String sql = "update storage_host_port set is_deleted = 1 where id = :id ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("id", id);
        execute(sql, params);
    }

    
    public void removeByHost(long hostId) {
        String sql = "update storage_host_port set is_deleted = 1 where host_id = :hostId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("hostId", hostId);
        execute(sql, params);
    }

    
    public void removeByVendor(long vendorId) {
        String sql = "update storage_host_port set is_deleted = 1 where host_id in " +
                "(select id from storage_host where is_deleted = 0 and storage_id in " +
                "(select id from storage where is_deleted = 0 and vendor_id =: vendorId)) ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("vendorId", vendorId);
        execute(sql, params);
    }

    
    public List<StorageHostPort> listByHost(long hostId) {
        String sql = "select a.* from storage_host_port a where a.is_deleted = 0 and a.host_id = :hostId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("hostId", hostId);
        return this.list(StorageHostPort.class, sql, params);
    }

    
    public List<StorageHostPort> listByVendor(long vendorId) {
        String sql = "select a.*,b.name as host_name from storage_host_port a " +
                "left join storage_host b on a.host_id = b.id " +
                "where a.is_deleted = 0 and a.host_id in " +
                "(select id from storage_host where is_deleted = 0 and storage_id in " +
                "(select id from storage where is_deleted = 0 and vendor_id = :vendorId)) ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("vendorId", vendorId);
        return this.list(StorageHostPort.class, sql, params);
    }

    
    public StorageHostPort get(long id) {
        String sql = "select a.* from storage_host_port a where a.id = :id ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("id", id);
        List<StorageHostPort> list = this.list(StorageHostPort.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }

    
    public StorageHostPort getByIdentifier(String identifier) {
        String sql = "select a.* from storage_host_port a where a.is_deleted = 0 and a.identifier = :identifier ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("identifier", identifier);
        List<StorageHostPort> list = this.list(StorageHostPort.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }

    
    public StorageHostPort getByName(String name, long hostId) {
        String sql = "select a.* from storage_host_port a where a.is_deleted = 0 and a.name = :name and a.host_id = :hostId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("name", name);
        params.put("hostId", hostId);
        List<StorageHostPort> list = this.list(StorageHostPort.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }
}
