package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.Common;
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.NatGatewayDnatRule;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

@Repository("natGatewayDnatRuleRepository")
public class NatGatewayDnatRuleRepository extends BasicGenericDao<NatGatewayDnatRule, Long> {

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

	public List<NatGatewayDnatRule> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select a.*, b.public_ip as elastic_ip from nat_gateway_dnat_rule a "
				+ "left join elastic_ip b on b.id = a.elastic_ip_id " + " where a.is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(NatGatewayDnatRule.class, sql, paramMap);
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from nat_gateway_dnat_rule a where 1 = 1 ";
		sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		List<NatGatewayDnatRule> groups = this.list(NatGatewayDnatRule.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (NatGatewayDnatRule group : groups) {
			beans.add(new SimpleBean(group.getId(), group.getName()));
		}
		return beans;
	}

	public int count(List<Param> params) throws Exception {
		String sql = "select count(a.id) from nat_gateway_dnat_rule a where a.is_deleted=0 ";
		sql = QueryBuilder.buildRaw(sql, params, new HashMap<>(), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	public List<NatGatewayDnatRule> listByVendorId(Long vendorId, String regionId) throws Exception {
		String sql = "select a.*,b.name vendor_name,b.type vendor_type from nat_gateway_dnat_rule a "
				+ "left join cloud_vendor b on b.id = a.vendor_id "
				+ "where a.is_deleted = 0 and a.vendor_id = :vendorId and a.region_id = :regionId ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("regionId", regionId);
		return this.list(NatGatewayDnatRule.class, sql, params);
	}

	public List<NatGatewayDnatRule> listByNatGatewayId(Long natGatewayId) throws Exception {
		String sql = "select a.*,b.name vendor_name,b.type vendor_type from nat_gateway_dnat_rule a "
				+ "left join cloud_vendor b on b.id = a.vendor_id "
				+ "where a.is_deleted = 0 and a.nat_gateway_id = :natGatewayId ";
		Map<String, Object> params = MapTools.simpleMap("natGatewayId", natGatewayId);
		return this.list(NatGatewayDnatRule.class, sql, params);
	}

	public boolean delete(Long id, Long userId) throws Exception {
		String sql = "update nat_gateway_dnat_rule set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	public List<NatGatewayDnatRule> listAll() throws Exception {
		String sql = "select a.*,b.name vendor_name,b.type vendor_type from nat_gateway_dnat_rule a left join cloud_vendor b on b.id = a.vendor_id where a.is_deleted = 0";
		return this.list(NatGatewayDnatRule.class, sql);
	}

	public NatGatewayDnatRule query(Long id) throws Exception {
		String sql = "select a.*,b.name vendor_name,b.type vendor_type,n.name nat_gateway_name, c.id as vm_Id  from nat_gateway_dnat_rule a left join cloud_vendor b " +
				"on b.id = a.vendor_id  left join nat_gateway n on a.nat_gateway_id=n.id left join port p on a.port_id = p.id left join cloud_server c " +
				"on p.device_uuid = c.instance_id  where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<NatGatewayDnatRule> list = this.list(NatGatewayDnatRule.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public NatGatewayDnatRule exists(String name, String regionId) throws Exception {
		String sql = "select * from nat_gateway_dnat_rule where is_deleted = 0 and name = :name ";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		if (null != regionId) {
			sql += " and region_id = :regionId ";
			params.put("regionId", regionId);
		}
		List<NatGatewayDnatRule> list = this.list(NatGatewayDnatRule.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public NatGatewayDnatRule getByPortId(Long portId) throws Exception {
		String sql = "select * from nat_gateway_dnat_rule where is_deleted = 0 and port_id = :portId ";
		Map<String, Object> params = MapTools.simpleMap("portId", portId);
		List<NatGatewayDnatRule> list = this.list(NatGatewayDnatRule.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public boolean removeByVid(Long vendorId, String regionId, Long userId) throws Exception {
		String sql = "update nat_gateway_dnat_rule set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId "
				+ "where vendor_id = :vendorId and region_id = :regionId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("regionId", regionId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByVid(Long vendorId, String regionId, Long userId, Long natGatewayId) throws Exception {
		String sql = "update nat_gateway_dnat_rule set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId "
				+ "where vendor_id = :vendorId and region_id = :regionId and nat_gateway_id = :natGatewayId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("regionId", regionId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		params.put("natGatewayId", natGatewayId);
		return this.execute(sql, params) > 0;
	}

	public boolean remove(Long vendorId, String regionId, Long userId, List<String> forwardTableIds) throws Exception {
		String sql = "update nat_gateway_dnat_rule set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId "
				+ "where vendor_id = :vendorId and region_id = :regionId  and forward_table_id in (:forwardTableIds)  ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("regionId", regionId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		params.put("forwardTableIds", forwardTableIds);
		return this.execute(sql, params) > 0;
	}

	public Boolean batchSave(List<NatGatewayDnatRule> natGateway) {
		return this.batchSave(natGateway, 1000);
	}

	public Boolean batchUpdate(List<NatGatewayDnatRule> natGateway) {
		return this.batchUpdate(natGateway, natGateway.size());
	}

	public List<String> listIpsByNatGatewayId(Long natGatewayId) throws Exception {
		List<String> list = new ArrayList<>();
		String sql = " select a.* from nat_gateway_dnat_rule a   where a.is_deleted = 0 and  a.nat_gateway_id = :natGatewayId ";
		Map<String, Object> params = MapTools.simpleMap("natGatewayId", natGatewayId);
		List<NatGatewayDnatRule> ruleList = this.list(NatGatewayDnatRule.class, sql, params);
		for (NatGatewayDnatRule rule : ruleList) {
			if (StringUtils.isEmpty(rule.getFloatingIpAddress())) {
				continue;
			}
			list.add(rule.getFloatingIpAddress());
		}
		return list;
	}

	public List<String> listBindPrivateIps(Long natGatewayId) {
		String sql = " select private_ip  from nat_gateway_dnat_rule where  "
				+ "  is_deleted = 0 and nat_gateway_id = :natGatewayId ";
		Map<String, Object> params = MapTools.simpleMap("natGatewayId", natGatewayId);
		List<NatGatewayDnatRule> ryleList = this.list(NatGatewayDnatRule.class, sql, params);
		List<String> list = new ArrayList<>();
		for (NatGatewayDnatRule rule : ryleList) {
			if (StringUtils.isEmpty(rule.getPrivateIp())) {
				continue;
			}
			list.add(rule.getPrivateIp());
		}
		return list;
	}

	public boolean removeByVid(Long vendorId, Long userId) throws Exception {
		String sql = "update nat_gateway_dnat_rule set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByNatGatewayId(Long natGatewayId, Long userId) throws Exception {
		String sql = "update nat_gateway_dnat_rule set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId "
				+ "where nat_gateway_id = :natGatewayId  ";
		Map<String, Object> params = MapTools.simpleMap("natGatewayId", natGatewayId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

}
