package com.bocloud.ims.repository.resource;

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.resource.PoolGroupLabel;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

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

/**
 * 资源池标签
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Component("poolGroupLabelRepository")
public class PoolGroupLabelRepository extends BasicGenericDao<PoolGroupLabel, Long> {

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

	public boolean remove(Long poolGroupId, Long labelId) throws BoCloudDBException {
		String sql = "delete from pool_group_label where pool_group_id = :poolGroupId and label_id = :labelId ";
		Map<String, Object> params = MapTools.simpleMap("poolGroupId", poolGroupId);
		params.put("labelId", labelId);
		return this.execute(sql, params) > 0;
	}

	public List<PoolGroupLabel> listByGroupId(Long poolGroupId) throws Exception {
		String sql = "select * from pool_group_label where pool_group_id = :poolGroupId";
		Map<String, Object> params = MapTools.simpleMap("poolGroupId", poolGroupId);
		return this.list(PoolGroupLabel.class, sql, params);
	}

	public List<PoolGroupLabel> listBylabelId(Long labelId) throws Exception {
		String sql = "select * from pool_group_label where label_id = :labelId";
		Map<String, Object> params = MapTools.simpleMap("labelId", labelId);
		return this.list(PoolGroupLabel.class, sql, params);
	}

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

}
