package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.resource.AlarmThreshold;
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;

/**
 * 告警阈值数据访问实现(vcenter集群或平台维度)
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("alarmThresholdRepository")
public class AlarmThresholdRepository extends BasicGenericDao<AlarmThreshold, Long> {

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

	public void create(AlarmThreshold alarmThreshold) throws Exception {
		this.save(alarmThreshold);
	}

	public boolean removeByTarget(Long target, String catalog) throws Exception {
		String sql = "delete from alarm_threshold where target = :target and catalog = :catalog";
		Map<String, Object> paramMap = MapTools.simpleMap("target", target);
		paramMap.put("catalog", catalog);
		return this.execute(sql, paramMap) > 0;
	}

	public AlarmThreshold getByTarget(Long target, String catalog) throws Exception {
		String sql = "select * from alarm_threshold where target = :target and catalog = :catalog";
		Map<String, Object> paramMap = MapTools.simpleMap("target", target);
		paramMap.put("catalog", catalog);
		List<AlarmThreshold> list = this.list(AlarmThreshold.class, sql, paramMap);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}

	public List<AlarmThreshold> list() throws Exception {
		String sql = "select * from alarm_threshold";
		return this.list(AlarmThreshold.class, sql);
	}

	public AlarmThreshold getByTarget(Long target, String catalog, String serialNumber) throws Exception {
		String sql = "select * from alarm_threshold where target = :target and catalog = :catalog and serial_number = :serialNumber";
		Map<String, Object> paramMap = MapTools.simpleMap("target", target);
		paramMap.put("catalog", catalog);
		paramMap.put("serialNumber", serialNumber);
		List<AlarmThreshold> list = this.list(AlarmThreshold.class, sql, paramMap);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}
}
