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.NatGatewaySnatRule;
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("natGatewaySnatRuleRepository")
public class NatGatewaySnatRuleRepository extends BasicGenericDao<NatGatewaySnatRule, Long> {

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

	public List<NatGatewaySnatRule> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select a.* from nat_gateway_snat_rule a  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(NatGatewaySnatRule.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_snat_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<NatGatewaySnatRule> groups = this.list(NatGatewaySnatRule.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (NatGatewaySnatRule 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_snat_rule a where 1 = 1";
		sql = QueryBuilder.buildRaw(sql, params, new HashMap<>(), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	public List<NatGatewaySnatRule> listByVendorId(Long vendorId, String regionId) throws Exception {
		String sql = "select a.*,b.name vendor_name,b.type vendor_type from nat_gateway_snat_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(NatGatewaySnatRule.class, sql, params);
	}

	public List<NatGatewaySnatRule> listByNatGatewayId(Long natGatewayId) throws Exception {
		String sql = "select a.*,b.name vendor_name,b.type vendor_type from nat_gateway_snat_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(NatGatewaySnatRule.class, sql, params);
	}

	public boolean delete(Long id, Long userId) throws Exception {
		String sql = "update nat_gateway_snat_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<NatGatewaySnatRule> listAll() throws Exception {
		String sql = "select a.*,b.name vendor_name,b.type vendor_type from nat_gateway_snat_rule a left join cloud_vendor b on b.id = a.vendor_id where a.is_deleted = 0";
		return this.list(NatGatewaySnatRule.class, sql);
	}

	public NatGatewaySnatRule query(Long id) throws Exception {
		String sql = "select a.*,b.name vendor_name,b.type vendor_type,n.`name` nat_gateway_name ,s.`name` subnet_name from nat_gateway_snat_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 subnet s on a.subnet_id=s.id "
				+ " where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<NatGatewaySnatRule> list = this.list(NatGatewaySnatRule.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public NatGatewaySnatRule exists(String name, String regionId) throws Exception {
		String sql = "select * from nat_gateway_snat_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<NatGatewaySnatRule> list = this.list(NatGatewaySnatRule.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public boolean removeByVid(Long vendorId, Long userId) throws Exception {
		String sql = "update nat_gateway_snat_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 removeByVid(Long vendorId, String regionId, Long userId) throws Exception {
		String sql = "update nat_gateway_snat_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("gmtModify", new Date());
		params.put("menderId", userId);
		params.put("regionId", regionId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByVid(Long vendorId, String regionId, Long userId, Long natGatewayId) throws Exception {
		String sql = "update nat_gateway_snat_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("gmtModify", new Date());
		params.put("menderId", userId);
		params.put("regionId", regionId);
		params.put("natGatewayId", natGatewayId);
		return this.execute(sql, params) > 0;
	}

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

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

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

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

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

}
