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.ServerLabel;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 主机标签数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("serverLabelRepository")
public class ServerLabelRepository extends BasicGenericDao<ServerLabel, Long> {

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

	public boolean delete(Long id) throws Exception {
		String sql = "delete from server_label where id = :id ";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		return this.execute(sql, params) > 0;
	}

	public boolean deleteByTarget(Long target, String type) {
		String sql = "delete from server_label where target = :target and type = :type ";
		Map<String, Object> params = MapTools.simpleMap("target", target);
		params.put("type", type);
		return this.execute(sql, params) > 0;
	}

	public boolean deleteByVendor(Long vendorId) throws Exception {
		String sql = "delete from server_label where vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.execute(sql, params) > 0;
	}

	public boolean batchSave(List<ServerLabel> serverLabels) throws Exception {
		return this.batchSave(serverLabels, 1000);
	}

	public ServerLabel listByTarget(Long target, String type, Long vendorId) throws Exception {
		String sql = "select a.* from server_label a where a.vendor_id = :vendorId and a.type = :type and a.target = :target";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("type", type);
		params.put("target", target);
		List<ServerLabel> list = this.list(ServerLabel.class, sql, params);
		if (ListTool.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}

	public boolean deleteByLabel(Long labelId) throws Exception {
		String sql = "delete from server_label where label_id = :labelId ";
		Map<String, Object> params = MapTools.simpleMap("labelId", labelId);
		return this.execute(sql, params) > 0;
	}

	public List<ServerLabel> listAll() {
		String sql = "select a.* from server_label a ";
		Map<String, Object> params = new HashMap<>();
		return this.list(ServerLabel.class, sql, params);
	}

}
