package com.bocloud.ims.repository;

import com.bocloud.common.utils.MapTools;
import com.bocloud.database.utils.PageHelper;
import com.bocloud.ims.entity.resource.NatGatewaySnatRuleRelation;
import com.bocloud.ims.repository.resource.common.EnhancedBaseRepository;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository("natGatewaySnatRuleRelationRepository")
public class NatGatewaySnatRuleRelationRepository extends EnhancedBaseRepository<NatGatewaySnatRuleRelation, Long> {

	public void remove(Long id) {
		String sql = "delete from nat_gateway_snat_rule_relation where id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		this.update(sql, params);
	}

	public void removeBySnatRuleId(Long snatRuleId) {
		String sql = "delete from nat_gateway_snat_rule_relation where snat_rule_id = :snatRuleId";
		Map<String, Object> params = MapTools.simpleMap("snatRuleId", snatRuleId);
		this.update(sql, params);
	}

	public List<NatGatewaySnatRuleRelation> listBySnatRuleId(Long snatRuleId) {
		String sql = "select a.*, b.public_ip as elastic_ip from nat_gateway_snat_rule_relation a "
				+ "left join elastic_ip b on b.id = a.elastic_ip_id " + "where snat_rule_id = :snatRuleId";
		Map<String, Object> params = MapTools.simpleMap("snatRuleId", snatRuleId);
		return this.list(NatGatewaySnatRuleRelation.class, sql, params);
	}

	public NatGatewaySnatRuleRelation getBySnatRuleIdAndElasticIpId(Long snatRuleId, Long elasticIpId) {
		String sql = "select * from nat_gateway_snat_rule_relation where snat_rule_id = :snatRuleId and elastic_ip_id = :elasticIpId";
		Map<String, Object> params = MapTools.simpleMap("snatRuleId", snatRuleId);
		params.put("elasticIpId", elasticIpId);
		List<NatGatewaySnatRuleRelation> list = this.list(NatGatewaySnatRuleRelation.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

}
