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

/**
 * nsx逻辑路由器端口
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年3月5日
 *
 */
@Repository("nsxRouterPortRepository")
public class NsxRouterPortRepository extends BasicGenericDao<NsxRouterPort, Long> {

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

	public List<NsxRouterPort> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select a.*,v.name vendor_name,l.name router_name from nsx_router_port a left join cloud_vendor v on v.id = a.vendor_id left join nsx_logical_router l on l.id = a.router_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(NsxRouterPort.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_router_port 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<NsxRouterPort> ports = this.list(NsxRouterPort.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (NsxRouterPort port : ports) {
			beans.add(new SimpleBean(port.getId(), port.getName()));
		}
		return beans;
	}

	public int count(List<Param> params) throws Exception {
		String sql = "select count(a.id) from nsx_router_port 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 NsxRouterPort query(Long id) throws Exception {
		String sql = "select a.*,v.name vendor_name,l.name router_name from nsx_router_port a left join cloud_vendor v on v.id = a.vendor_id left join nsx_logical_router l on l.id = a.router_id where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<NsxRouterPort> list = this.list(NsxRouterPort.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<NsxRouterPort> listByVendorId(Long vendorId) {
		String sql = "select a.*,v.name vendor_name,l.name router_name from nsx_router_port a left join cloud_vendor v on v.id = a.vendor_id left join nsx_logical_router l on l.id = a.router_id where a.is_deleted = 0 and a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		List<NsxRouterPort> list = this.list(NsxRouterPort.class, sql, paramMap);
		if (list.isEmpty()) {
			return new ArrayList<>();
		}
		return list;
	}

	public boolean removeByVendorId(Long vendorId, Long menderId) {
		String sql = "update nsx_router_port 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<NsxRouterPort> listByRouterId(Long routerId) {
		String sql = "select a.*,v.name vendor_name,l.name router_name from nsx_router_port a left join cloud_vendor v on v.id = a.vendor_id left join nsx_logical_router l on l.id = a.router_id where a.is_deleted = 0 and a.router_id = :routerId";
		Map<String, Object> paramMap = MapTools.simpleMap("routerId", routerId);
		List<NsxRouterPort> list = this.list(NsxRouterPort.class, sql, paramMap);
		if (list.isEmpty()) {
			return new ArrayList<>();
		}
		return list;
	}

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

	public List<NsxRouterPort> listByLinkedSwitchPortId(Long linkedSwitchPortId) {
		String sql = "select a.*,v.name vendor_name,l.name router_name from nsx_router_port a left join cloud_vendor v on v.id = a.vendor_id left join nsx_logical_router l on l.id = a.router_id where a.is_deleted = 0 and a.linked_switch_port_id = :linkedSwitchPortId";
		Map<String, Object> paramMap = MapTools.simpleMap("linkedSwitchPortId", linkedSwitchPortId);
		List<NsxRouterPort> list = this.list(NsxRouterPort.class, sql, paramMap);
		if (list.isEmpty()) {
			return new ArrayList<>();
		}
		return list;
	}

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

	public List<NsxRouterPort> listByLinkedRouterPortId(Long linkedRouterPortId) {
		String sql = "select a.*,v.name vendor_name,l.name router_name from nsx_router_port a left join cloud_vendor v on v.id = a.vendor_id left join nsx_logical_router l on l.id = a.router_id where a.is_deleted = 0 and a.linked_nsx_router_port_id = :linkedRouterPortId";
		Map<String, Object> paramMap = MapTools.simpleMap("linkedRouterPortId", linkedRouterPortId);
		List<NsxRouterPort> list = this.list(NsxRouterPort.class, sql, paramMap);
		if (list.isEmpty()) {
			return new ArrayList<>();
		}
		return list;
	}

	public boolean removeByLinkedRouterPortId(Long linkedRouterPortId, Long menderId) {
		String sql = "update nsx_router_port set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and linked_nsx_router_port_id = :linkedRouterPortId";
		Map<String, Object> params = MapTools.simpleMap("linkedRouterPortId", linkedRouterPortId);
		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_router_port 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_router_port 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<NsxRouterPort> list = this.list(NsxRouterPort.class, sql, params);
		if (list.isEmpty()) {
			return false;
		}
		return true;
	}

	public NsxRouterPort getByPortId(String portId, Long vendorId) throws Exception {
		String sql = "select a.*,v.name vendor_name,l.name router_name from nsx_router_port a left join cloud_vendor v on v.id = a.vendor_id left join nsx_logical_router l on l.id = a.router_id where a.is_deleted = 0 and a.port_id = :portId";
		Map<String, Object> params = MapTools.simpleMap("portId", portId);
		List<NsxRouterPort> list = this.list(NsxRouterPort.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<NsxRouterPort> listByRouterId(Long routerId, String switchName) throws Exception {
		String sql = "select a.* from nsx_router_port a left join nsx_switch_port b on b.id = a.linked_switch_port_id left join nsx_logical_switch c on c.id = b.switch_id where c.name = :switchName";
		Map<String, Object> paramMap = MapTools.simpleMap("switchName", switchName);
		if (null != routerId && 0L != routerId) {
			sql += " and a.router_id = :routerId ";
			paramMap.put("routerId", routerId);
		}
		return this.list(NsxRouterPort.class, sql, paramMap);
	}

}
