package com.bocloud.ims.repository.resource.azure.monitor;

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.BasicDao;
import com.bocloud.ims.entity.resource.azure.monitor.MetricAlertResourceRelation;
import org.springframework.stereotype.Repository;

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

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: MetricAlertResourceRelationRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/3/12 15:27
 */
@Repository
public class MetricAlertResourceRelationRepository extends BasicDao {

    public MetricAlertResourceRelation save(MetricAlertResourceRelation entity) throws BoCloudDBException {
        return (MetricAlertResourceRelation) this.saveEntity(entity);
    }

    public Boolean update(MetricAlertResourceRelation entity) throws BoCloudDBException {
        return this.updateEntity(entity);
    }

    public Boolean remove(long id) throws BoCloudDBException {
        String sql = "delete from metric_alert_resource_relation where id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.update(sql, params) > 0;
    }

    public Boolean remove(MetricAlertResourceRelation entity) throws BoCloudDBException {
        return this.baseDelete(entity);
    }


    public Boolean removeByMetricAlertId(Long id) throws BoCloudDBException {
        String sql = "delete from metric_alert_resource_relation where metric_alert_id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.update(sql, params) > 0;
    }

    public Boolean removeByResourceId(Long id, String resourceType) throws BoCloudDBException {
        String sql = "delete from metric_alert_resource_relation where resource_id = :id and resource_type = :resourceType";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        params.put("resourceType", resourceType);
        return this.update(sql, params) > 0;
    }

    public List<MetricAlertResourceRelation> listByMetricAlertId(long id) throws BoCloudDBException {
        String sql = "select * from metric_alert_resource_relation where metric_alert_id = :id";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        return this.queryForList(sql, params, MetricAlertResourceRelation.class).stream().map(o -> (MetricAlertResourceRelation) o).collect(Collectors.toList());

    }

    public List<MetricAlertResourceRelation> listByResourceId(long id, String resourceType) throws BoCloudDBException {
        String sql = "select * from metric_alert_resource_relation where resource_id = :id and resource_type = :resourceType";
        Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
        params.put("resourceType", resourceType);
        return this.queryForList(sql, params, MetricAlertResourceRelation.class).stream().map(o -> (MetricAlertResourceRelation) o).collect(Collectors.toList());
    }

}