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.MetricCriteria;
import org.springframework.stereotype.Repository;

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

/**
 * @Company: 博云 www.bocloud.com
 * @ClassName: MetricCriteriaRepository
 * @Description:
 * @author: zhenpingLi/李振平
 * @date: 2021/1/11 14:25
 */
@Repository("metricCriteriaRepository")
public class MetricCriteriaRepository extends BasicDao {

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

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


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


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

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


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

    }


}