package com.bocloud.ims.repository.resource;

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

/**
 * 告警级别数据访问实现
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("alarmLevelRepository")
public class AlarmLevelRepository extends BasicGenericDao<AlarmLevel, Long> {

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

	public void create(AlarmLevel alarmLevel) throws Exception {
		this.save(alarmLevel);
	}

	public AlarmLevel query(Long id) throws Exception {
		String sql = "select a.* from alarm_level a where a.id = :id";
		Map<String, Object> param = MapTools.simpleMap(Common.ID, id);
		List<AlarmLevel> list = this.list(AlarmLevel.class, sql, param);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}

	public List<AlarmLevel> list(List<Param> params) throws Exception {
		String sql = "select a.* from alarm_level a where 1 = 1";
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(AlarmLevel.class, sql, paramMap);
	}

	public AlarmLevel getAlarmLevel(List<Param> params) throws Exception {
		String sql = "select a.* from alarm_level a where 1 = 1";
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		List<AlarmLevel> list = this.list(AlarmLevel.class, sql, paramMap);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}

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