package com.bocloud.ims.repository.common;

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.nsx.NsxLogicalRouter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 逻辑路由器
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年2月28日
 *
 */
@Repository("nsxRouterRepository")
public class NsxRouterRepository extends BasicGenericDao<NsxLogicalRouter, Long> {

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

	public List<NsxLogicalRouter> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select a.*,t.name tenant_name,v.name vendor_name,c.name cluster_name,o.expired_time from nsx_logical_router a left join tenant t on a.tenant_id = t.id left join cloud_vendor v on v.id = a.vendor_id left join nsx_edge_cluster c on c.id = a.cluster_id left join object_relation o on o.res_id = a.id and o.category = 'Vpc' 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(NsxLogicalRouter.class, sql, paramMap);

	}

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

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

	}

	public NsxLogicalRouter query(Long id) throws Exception {
		String sql = "select a.*,t.name tenant_name,v.name vendor_name,c.name cluster_name,o.expired_time from nsx_logical_router a left join tenant t on a.tenant_id = t.id left join cloud_vendor v on v.id = a.vendor_id left join nsx_edge_cluster c on c.id = a.cluster_id left join object_relation o on o.res_id = a.id and o.category = 'Vpc' where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<NsxLogicalRouter> list = this.list(NsxLogicalRouter.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<NsxLogicalRouter> listByVendorId(Long vendorId) {
		String sql = "select a.*,t.name tenant_name,v.name vendor_name,c.name cluster_name from nsx_logical_router a left join tenant t on a.tenant_id = t.id left join cloud_vendor v on v.id = a.vendor_id left join nsx_edge_cluster c on c.id = a.cluster_id where a.is_deleted = 0 and a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		List<NsxLogicalRouter> list = this.list(NsxLogicalRouter.class, sql, paramMap);
		if (list.isEmpty()) {
			return new ArrayList<>();
		}
		return list;
	}

	public List<NsxLogicalRouter> listByCondition(Long vendorId, String routerType) {
		String sql = "select a.*,t.name tenant_name,v.name vendor_name,c.name cluster_name from nsx_logical_router a left join tenant t on a.tenant_id = t.id left join cloud_vendor v on v.id = a.vendor_id left join nsx_edge_cluster c on c.id = a.cluster_id where a.is_deleted = 0 and a.vendor_id = :vendorId and a.router_type = :routerType";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		paramMap.put("routerType", routerType);
		List<NsxLogicalRouter> list = this.list(NsxLogicalRouter.class, sql, paramMap);
		if (list.isEmpty()) {
			return new ArrayList<>();
		}
		return list;
	}

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

	public List<NsxLogicalRouter> listByClusterId(Long clusterId) {
		String sql = "select a.*,t.name tenant_name,v.name vendor_name,c.name cluster_name from nsx_logical_router a left join tenant t on a.tenant_id = t.id left join cloud_vendor v on v.id = a.vendor_id left join nsx_edge_cluster c on c.id = a.cluster_id where a.cluster_id = :clusterId";
		Map<String, Object> paramMap = MapTools.simpleMap("clusterId", clusterId);
		List<NsxLogicalRouter> list = this.list(NsxLogicalRouter.class, sql, paramMap);
		if (list.isEmpty()) {
			return new ArrayList<>();
		}
		return list;
	}

	public boolean removeByClusterId(Long clusterId, Long menderId) {
		String sql = "update nsx_logical_router set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and cluster_id = :clusterId";
		Map<String, Object> params = MapTools.simpleMap("clusterId", clusterId);
		params.put("menderId", menderId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public boolean remove(Long id, Long menderId) {
		String sql = "update nsx_logical_router set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		params.put("menderId", menderId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public boolean existByCondition(String column, String value, Long vendorId) throws Exception {
		String sql = "select * from nsx_logical_router where " + column
				+ " = :value and vendor_id = :vendorId and is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("value", value);
		params.put(Common.VENDORID, vendorId);
		List<NsxLogicalRouter> list = this.list(NsxLogicalRouter.class, sql, params);
		if (list.isEmpty()) {
			return false;
		}
		return true;
	}

	public NsxLogicalRouter getByRouterId(String routerId, Long vendorId) throws Exception {
		String sql = "select a.*,v.name vendor_name,c.name tenant_name from nsx_logical_router a left join cloud_vendor v on v.id = a.vendor_id left join tenant c on c.id = a.tenant_id where a.is_deleted = 0 and a.router_id = :routerId and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("routerId", routerId);
		params.put(Common.VENDORID, vendorId);
		List<NsxLogicalRouter> list = this.list(NsxLogicalRouter.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<NsxLogicalRouter> listManagementNetwork(Long vcenterVendorId) throws Exception {
		String sql = "select DISTINCT a.* from nsx_logical_router a left join nsx_vcenter_relation b on b.nsx_vendor_id = a.vendor_id left join nsx_router_port c on c.router_id = a.id where a.is_deleted = 0 and b.vcenter_vendor_id = :vcenterVendorId and a.management_network = 1 and a.router_type = 'TIER1' and c.linked_logical_switch_port_id is not null and c.subnets is not null and a.name like '%manage%'";
		Map<String, Object> paramMap = MapTools.simpleMap("vcenterVendorId", vcenterVendorId);
		return this.list(NsxLogicalRouter.class, sql, paramMap);
	}

	public NsxLogicalRouter getByUuid(String uuid) throws Exception {
		String sql = "select a.*,v.name vendor_name from nsx_logical_router a left join cloud_vendor v on v.id = a.vendor_id where a.is_deleted = 0 and a.uuid = :uuid";
		Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
		List<NsxLogicalRouter> list = this.list(NsxLogicalRouter.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public NsxLogicalRouter getRelatedRouter(Long switchId) throws Exception {
		String sql = "select a.*,d.name tenant_name from nsx_logical_router a left join nsx_router_port b on b.router_id = a.id left join nsx_switch_port c on c.id = b.linked_logical_switch_port_id left join tenant d on d.id = a.tenant_id where c.switch_id = :switchId";
		Map<String, Object> params = MapTools.simpleMap("switchId", switchId);
		List<NsxLogicalRouter> list = this.list(NsxLogicalRouter.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}
}
