package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.MapTools;
import com.bocloud.database.utils.PageHelper;
import com.bocloud.ims.entity.resource.Server;
import com.bocloud.ims.entity.resource.StorageIScsiPort;
import com.bocloud.ims.entity.resource.StorageIScsiPortGateway;
import com.bocloud.ims.repository.resource.common.EnhancedBaseRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
public class StorageIScsiPortGatewayRepository extends EnhancedBaseRepository<StorageIScsiPortGateway, Long> {

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

    public void removeByIScsiPort(long portId){
        String sql = "delete from storage_iscsi_port_gateway where iscsi_port_id = :portId";
        Map<String, Object> params = MapTools.simpleMap("portId", portId);
        this.update(sql, params);
    }

    public void removeByGateway(long gatewayId){
        String sql = "delete from storage_iscsi_port_gateway where server_id = :gatewayId";
        Map<String, Object> params = MapTools.simpleMap("gatewayId", gatewayId);
        this.update(sql, params);
    }

    public List<StorageIScsiPortGateway> listByIScsiPort(long portId){
        String sql = "select * from storage_iscsi_port_gateway where iscsi_port_id = :portId";
        Map<String, Object> params = MapTools.simpleMap("portId", portId);
        return this.list(StorageIScsiPortGateway.class, sql, params);
    }

    public List<StorageIScsiPortGateway> listByGateway(long gatewayId){
        String sql = "select * from storage_iscsi_port_gateway where server_id = :gatewayId";
        Map<String, Object> params = MapTools.simpleMap("gatewayId", gatewayId);
        return this.list(StorageIScsiPortGateway.class, sql, params);
    }

    public StorageIScsiPortGateway getByIScsiPortAndGateway(long portId, long gatewayId){
        String sql = "select * from storage_iscsi_port_gateway where server_id = :gatewayId and iscsi_port_id = :portId";
        Map<String, Object> params = MapTools.simpleMap("gatewayId", gatewayId);
        params.put("portId", portId);
        List<StorageIScsiPortGateway> list = this.list(StorageIScsiPortGateway.class, sql, params);
        return list == null || list.size() == 0 ? null : list.get(0);
    }

    public List<Server> listGatewayByIScsiPort(long portId, int page, int rows, Map<String, Object> params, Map<String, String> sorter){
        String sql = " select a.* from server a where a.is_deleted = 0 and " +
            "a.id in (select b.server_id from storage_iscsi_port_gateway b where b.iscsi_port_id = :portId) ";
        Map<String, Object> sqlParams = MapTools.simpleMap("portId", portId);
        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, Server.class);
        return objects.stream().map(o -> (Server) o).collect(Collectors.toList());
    }

    public int countGatewayByIScsiPort(long portId, Map<String, Object> params){
        String sql = " select count(1) from server a where a.is_deleted = 0 and " +
            "a.id in (select b.server_id from storage_iscsi_port_gateway b where b.iscsi_port_id = :portId) ";
        Map<String, Object> sqlParams = MapTools.simpleMap("portId", portId);
        if (params.containsKey("name")) {
            sql += " and a.name like :name";
            sqlParams.put("name","%" + params.get("name") + "%");
        }
        return this.countQuery(sql, sqlParams).intValue();
    }

    public List<Server> listGatewayForIScsiPort(long portId, int page, int rows, Map<String, Object> params, Map<String, String> sorter) {
        String sql = " select a.* from server a where a.is_deleted = 0 and role like '%gateway%' and " +
            "a.id not in (select b.server_id from storage_iscsi_port_gateway b where b.iscsi_port_id = :portId) and " +
            "a.vendor_id in (select b.vendor_id from storage_iscsi_port b where b.id = :portId)";
        Map<String, Object> sqlParams = MapTools.simpleMap("portId", portId);
        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, Server.class);
        return objects.stream().map(o -> (Server) o).collect(Collectors.toList());
    }

    public int countGatewayForIScsiPort(long portId, Map<String, Object> params) {
        String sql = " select count(1) from server a where a.is_deleted = 0 and role like '%gateway%' and " +
            "a.id not in (select b.server_id from storage_iscsi_port_gateway b where b.iscsi_port_id = :portId) and " +
            "a.vendor_id in (select b.vendor_id from storage_iscsi_port b where b.id = :portId)";
        Map<String, Object> sqlParams = MapTools.simpleMap("portId", portId);
        if (params.containsKey("name")) {
            sql += " and a.name like :name";
            sqlParams.put("name","%" + params.get("name") + "%");
        }
        return this.countQuery(sql, sqlParams).intValue();
    }

    public List<StorageIScsiPort> listIScsiPortByGateway(long gatewayId, int page, int rows, Map<String, Object> params, Map<String, String> sorter){
        String sql = " select a.* from iscsi_target a where a.is_deleted = 0 and " +
            "a.id in (select b.iscsi_port_id from storage_iscsi_port_gateway b where b.server_id = :gatewayId) ";
        Map<String, Object> sqlParams = MapTools.simpleMap("gatewayId", gatewayId);
        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, StorageIScsiPort.class);
        return objects.stream().map(o -> (StorageIScsiPort) o).collect(Collectors.toList());
    }

    public int countIScsiPortByGateway(long gatewayId, Map<String, Object> params){
        String sql = " select count(1) from iscsi_target a where a.is_deleted = 0 " +
            "and a.id in (select b.iscsi_port_id from storage_iscsi_port_gateway b where b.server_id = :gatewayId) ";
        Map<String, Object> sqlParams = MapTools.simpleMap("gatewayId", gatewayId);
        if (params.containsKey("name")) {
            sql += " and a.name like :name";
            sqlParams.put("name","%" + params.get("name") + "%");
        }
        return this.countQuery(sql, sqlParams).intValue();
    }
}
