package com.bocloud.ims.repository.resource;

import com.bocloud.common.exception.BoCloudDBException;
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.PoolGroupRelation;

import org.apache.commons.lang3.StringUtils;
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("poolGroupRelationRepository")
public class PoolGroupRelationRepository extends BasicGenericDao<PoolGroupRelation, Long> {

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

    public boolean remove(Long groupId, Long targetId) throws BoCloudDBException {
        String sql = "delete from pool_group_relation  where pool_group_id = :groupId ";
        Map<String, Object> params = MapTools.simpleMap("groupId", groupId);
        if (null != targetId && targetId > 0) {
            sql += " and target = :targetId ";
            params.put("targetId", targetId);
        }
        return this.execute(sql, params) > 0;
    }

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

    /**
     * 查询指定资源池下绑定的对象列表
     *
     * @param poolGroupId
     * @param category
     * @return
     * @throws Exception
     */

	public List<PoolGroupRelation> list(Long poolGroupId, String category) throws Exception {
		String sql = "select * from pool_group_relation where  pool_group_id = :poolGroupId";
		Map<String, Object> params = MapTools.simpleMap("poolGroupId", poolGroupId);
		if (!StringUtils.isEmpty(category)) {
			sql += " and category = :category ";
			params.put("category", category);
		}
		return this.list(PoolGroupRelation.class, sql, params);
	}

    public List<PoolGroupRelation> listByTarget(Long target, String category) throws Exception {
        String sql = "select * from pool_group_relation where category = :category and target = :target";
        Map<String, Object> params = MapTools.simpleMap("target", target);
        params.put("category", category);
        return this.list(PoolGroupRelation.class, sql, params);
    }
    
	public PoolGroupRelation getByTarget(Long target, String category, Long poolGroupId) throws Exception {
		String sql = "select * from pool_group_relation where category = :category and target = :target and pool_group_id = :poolGroupId ";
		Map<String, Object> params = MapTools.simpleMap("target", target);
		params.put("category", category);
		params.put("poolGroupId", poolGroupId);
		List<PoolGroupRelation> list = this.list(PoolGroupRelation.class, sql, params);
		if (ListTool.isEmpty(list)) {
			return null;
		}
		return list.get(0);
	}

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

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

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

}
