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.NatGateway;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.*;

@Repository("natGatewayRepository")
public class NatGatewayRepository extends BasicGenericDao<NatGateway, Long> {

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

	public List<NatGateway> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.*, v.name as vpc_name, s.name as subnet_name, s.cidr as subnet_cidr ," +
				"r.name region_name,g.display_name resource_group_name,n.public_ip_address," +
				" n.primary_private_ip_address private_ip_address from nat_gateway a " +
				"left join vpc v on v.id = a.vpc_id left join subnet s on s.id = a.subnet_id " +
				"left join region r on a.region_id = r.region_id and a.vendor_id = r.vendor_id " +
				"left join resource_group g on a.resource_group_uuid=g.resource_group_uuid and a.vendor_id = g.vendor_id and g.is_deleted = 0 " +
				"left join network_interface n on n.id = a.network_interface_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(NatGateway.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 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<NatGateway> groups = this.list(NatGateway.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (NatGateway 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 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<NatGateway> listByVendorId(Long vendorId, String regionId) throws Exception {
		String sql = "select a.*,b.name vendor_name,b.type vendor_type from nat_gateway 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(NatGateway.class, sql, params);
	}


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

	public NatGateway query(Long id) throws Exception {
		String sql = "select a.*, v.name as vpc_name, s.name as subnet_name, s.cidr as subnet_cidr ,g.display_name resource_group_name ,n.public_ip_address from nat_gateway a  " +
				"left join vpc v on v.id = a.vpc_id " +
				"left join subnet s on s.id = a.subnet_id " +
				" left join resource_group g on a.resource_group_uuid=g.resource_group_uuid and a.vendor_id = g.vendor_id " +
				"left join network_interface n on n.id = a.network_interface_id " +
				"where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<NatGateway> list = this.list(NatGateway.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public NatGateway query(String uuid) throws Exception {
		String sql = "select a.* from nat_gateway a  where a.is_deleted = 0 and a.uuid = :uuid";
		Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
		List<NatGateway> list = this.list(NatGateway.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}



	public NatGateway exists(String name, String regionId) throws Exception {
		String sql = "select * from nat_gateway 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<NatGateway> list = this.list(NatGateway.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 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, Long userId) throws Exception {
		String sql = "update nat_gateway 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 batchSave(List<NatGateway> natGateway) {
		return this.batchSave(natGateway, 1000);
	}

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


	public List<NatGateway> listAvailableGateway(Long vendorId, String regionId) {
		String sql = "select a.* from nat_gateway a  "
				+ " where a.is_deleted=0 and a.vendor_id = :vendorId and a.region_id = :regionId  ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("regionId", regionId);
		return this.list(NatGateway.class, sql, params);
	}

	public List<NatGateway> list(Long vendorId, String regionId,Long vpcId) {
		String sql = "select a.* from nat_gateway a   "
				+ " where a.is_deleted=0 and a.vendor_id = :vendorId and a.region_id = :regionId and a.vpc_id = :vpcId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("regionId", regionId);
		params.put("vpcId",vpcId);
		return this.list(NatGateway.class, sql, params);
	}

	public NatGateway queryByUuid(String gatewayUuid, Long vendorId) throws Exception {
		String sql = "select a.* from nat_gateway a where a.is_deleted = 0 and a.uuid = :gatewayUuid and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("gatewayUuid", gatewayUuid);
		params.put(Common.VENDORID, vendorId);
		List<NatGateway> list = this.list(NatGateway.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public NatGateway getByVpcId(String vpcUuid, String subnetUuid, Long vendorId, String regionId) throws Exception {
		String sql = "select a.* from nat_gateway a where a.is_deleted = 0 and a.vpc_uuid = :vpcUuid and a.subnet_uuid = :subnetUuid and a.vendor_id = :vendorId and a.region_id = :regionId";
		Map<String, Object> params = MapTools.simpleMap("vpcUuid", vpcUuid);
		params.put("subnetUuid", subnetUuid);
		params.put(Common.VENDORID, vendorId);
		params.put(Common.REGIONID, regionId);
		List<NatGateway> list = this.list(NatGateway.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<NatGateway> listAvailableForRoute(Long vendorId,String routeTableUuid,String regionId,String vpcUuid) {
		String sql = "select a.* from nat_gateway a  "
				+ " where a.is_deleted=0 and a.vendor_id = :vendorId and a.vpc_uuid = :vpcUuid "
				+ " and  a.region_id = :regionId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("regionId", regionId);
		params.put("vpcUuid",vpcUuid);
		return this.list(NatGateway.class, sql, params);
	}
}
