package com.bocloud.ims.repository.topology;

import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.topology.InstanceLocation;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by liuyuanyuan on 2020/6/18 15:20
 */
@Repository("instanceLocationRepository")
public class InstanceLocationRepository extends BasicGenericDao<InstanceLocation, Long> {

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

    public List<InstanceLocation> query(Long instanceId, Long categoryId, Long managerId) throws Exception {
        String sql = "select * from ci_instance_location where instance_id = :instanceId and category_id = :categoryId and manager_id = :managerId ";
        Map<String, Object> params = MapTools.simpleMap("instanceId", instanceId);
        params.put("categoryId", categoryId);
        params.put("managerId", managerId);
        return this.list(InstanceLocation.class, sql, params);
    }

    public boolean delete(Long instanceId, Long categoryId, Long managerId) throws Exception {
        String sql = "delete from ci_instance_location where instance_id = :instanceId and category_id = :categoryId and manager_id = :managerId ";
        Map<String, Object> params = MapTools.simpleMap("instanceId", instanceId);
        params.put("categoryId", categoryId);
        params.put("managerId", managerId);
        return this.execute(sql, params) > 0;
    }

    public List<InstanceLocation> query(Set<Long> nodes, Long categoryId, Long managerId) throws Exception {
        String sql = "select * from ci_instance_location where node_id in (:nodes) and category_id = :categoryId and manager_id = :managerId ";
        Map<String, Object> params = MapTools.simpleMap("nodes", nodes);
        params.put("categoryId", categoryId);
        params.put("managerId", managerId);
        return this.list(InstanceLocation.class, sql, params);
    }
}
