package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.MapTools;
import com.bocloud.database.utils.PageHelper;
import com.bocloud.ims.entity.resource.StorageFCPort;
import com.bocloud.ims.entity.resource.StorageHostPort;
import com.bocloud.ims.entity.resource.StorageHostPortFcPort;
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;

@Repository("storageHostPortFcPortRepository")
public class StorageHostPortFcPortRepository extends EnhancedBaseRepository<StorageHostPortFcPort, Long> {

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

    public void removeByFcPort(long fcportId) {
        String sql = "delete from storage_host_port_fcport where fcport_id = :fcportId";
        Map<String, Object> params = MapTools.simpleMap("fcportId", fcportId);
        this.update(sql, params);
    }

    public void removeByHostPort(long hostPortId) {
        String sql = "delete from storage_host_port_fcport where host_port_id = :hostPortId";
        Map<String, Object> params = MapTools.simpleMap("hostPortId", hostPortId);
        this.update(sql, params);
    }

    public List<StorageHostPortFcPort> listByFcPort(long fcportId) {
        String sql = "select a.* from storage_host_port_fcport a where a.fcport_id = :fcportId";
        Map<String, Object> params = MapTools.simpleMap("fcportId", fcportId);
        return this.list(StorageHostPortFcPort.class, sql, params);
    }

    public List<StorageHostPortFcPort> listByHostPort(long hostPortId) {
        String sql = "select a.* from storage_host_port_fcport a where a.host_port_id = :hostPortId";
        Map<String, Object> params = MapTools.simpleMap("hostPortId", hostPortId);
        return this.list(StorageHostPortFcPort.class, sql, params);
    }

    public StorageHostPortFcPort getByHostPortAndFcPort(long fcportId, long hostPortId) {
        String sql = "select a.* from storage_host_port_fcport a where a.host_port_id = :hostPortId and a.fcport_id = :fcportId";
        Map<String, Object> params = MapTools.simpleMap("hostPortId", hostPortId);
        params.put("fcportId", fcportId);
        List<StorageHostPortFcPort> list = this.list(StorageHostPortFcPort.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }

    public List<StorageHostPort> listHostPortByFcPort(long fcportId, int page, int rows, Map<String, Object> params, Map<String, String> sorter) {
        String sql = " select a.* from storage_host_port a " +
                " where a.is_deleted = 0 and a.id in (select b.host_port_id from storage_host_port_fcport b where b.fcport_id = :fcportId) ";
        Map<String, Object> sqlParams = MapTools.simpleMap("fcportId", fcportId);
        if (params.containsKey("name")) {
            sql += " and a.name like :name";
            sqlParams.put("name","%" + params.get("name") + "%");
        }
        sql += PageHelper.buildPageSql(page, rows);
        List<Object> objects = this.queryForList(sql, sqlParams, StorageHostPort.class);
        List<StorageHostPort> volumes = new ArrayList<>();
        for (Object object : objects) {
            volumes.add((StorageHostPort) object);
        }
        return volumes;
    }

    public int countHostPortByFcPort(long fcportId, Map<String, Object> params) {
        String sql = " select count(1) from storage_host_port a " +
                "where a.is_deleted = 0 and a.id in (select b.host_port_id from storage_host_port_fcport b where b.fcport_id = :fcportId) ";
        Map<String, Object> sqlParams = MapTools.simpleMap("fcportId", fcportId);
        if (params.containsKey("name")) {
            sql += " and a.name like :name";
            sqlParams.put("name","%" + params.get("name") + "%");
        }
        return this.countQuery(sql, sqlParams).intValue();
    }

    public List<StorageFCPort> listFcPortByHostPort(long hostPortId, int page, int rows, Map<String, Object> params, Map<String, String> sorter) {
        String sql = " select a.* from storage_fc_port a " +
                " where a.is_deleted = 0 and a.id in (select b.fcport_id from storage_host_port_fcport b where b.host_port_id = :hostPortId) ";
        Map<String, Object> sqlParams = MapTools.simpleMap("hostPortId", hostPortId);
        if (params.containsKey("name")) {
            sql += " and a.name like :name";
            sqlParams.put("name","%" + params.get("name") + "%");
        }
        sql += PageHelper.buildPageSql(page, rows);
        List<Object> objects = this.queryForList(sql, sqlParams, StorageFCPort.class);
        List<StorageFCPort> mdisks = new ArrayList<>();
        for (Object object : objects) {
            mdisks.add((StorageFCPort) object);
        }
        return mdisks;
    }

    public int countFcPortByHostPort(long hostPortId, Map<String, Object> params) {
        String sql = " select count(1) from storage_fc_port a " +
                "where a.is_deleted = 0 and a.id in (select b.fcport_id from storage_host_port_fcport b where b.host_port_id = :hostPortId) ";
        Map<String, Object> sqlParams = MapTools.simpleMap("hostPortId", hostPortId);
        if (params.containsKey("name")) {
            sql += " and a.name like :name";
            sqlParams.put("name","%" + params.get("name") + "%");
        }
        return this.countQuery(sql, sqlParams).intValue();
    }
}
