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.JdbcGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.StorageIScsiPort;
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
public class StorageIScsiPortRepository extends JdbcGenericDao<StorageIScsiPort, Long>  {

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

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

    public int count(List<Param> params) {
        String sql = "select count(1) from storage_iscsi_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_iscsi_port set is_deleted = 1 where id = :id ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("id", id);
        execute(sql, params);
    }

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

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

    public List<StorageIScsiPort> listByVendor(long vendorId) {
        String sql = "select * from storage_iscsi_port where is_deleted = 0 and " +
            "storage_id in (select id from storage where vendor_id = :vendorId)";
        Map<String, Object> params = new HashMap<>(8);
        params.put("vendorId", vendorId);
        return this.list(StorageIScsiPort.class, sql, params);
    }

    public List<StorageIScsiPort> listByStorage(long storageId) {
        String sql = "select * from storage_iscsi_port where is_deleted = 0 and storage_id = :storageId ";
        Map<String, Object> params = new HashMap<>(8);
        params.put("storageId", storageId);
        return this.list(StorageIScsiPort.class, sql, params);
    }

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