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

/**
 * vpn
 *
 * @author Administrator
 */
@Repository("vpnRepository")
public class VpnRepository extends BasicGenericDao<Vpn, Long> {

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

	public List<Vpn> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = " select * from vpn v where v.is_deleted = 0 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "v");
		return this.list(Vpn.class, sql);
	}

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

	public List<Vpn> listByVendorId(Long vendorId) throws Exception {
		String sql = "select a.* from vpn a where a.is_deleted = 0 and a.vendor_id =:vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(Vpn.class, sql, params);
	}

	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from vpn v where v.is_deleted = 0 ";
		sql = QueryBuilder.buildRaw(sql, params, null, "v");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

	public int count(Long vendorId, List<String> statusList) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	public boolean remove(Long id, Long userId) throws Exception {
		String sql = "update vpn 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 boolean removeByVid(Long vendorId, Long userId) throws Exception {
		String sql = "update vpn 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 Vpn getByVpnUuid(String vpnUuid) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public Vpn query(Long id) throws Exception {
		String sql = " select v.*, s.cidr, r.name router_name from vpn v left join subnet s on v.subnet_id = s.id  left join router r on v.router_id = r.id where v.is_deleted = 0 and v.id = :id ";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<Vpn> list = this.list(Vpn.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public int count(List<Param> params, Long tenantId) throws Exception {
		String sql = "select count(1) from vpn v where v.is_deleted = 0 ";
		if (null != tenantId) {
			sql += " and v.tenant_id =  " + tenantId;
		}
		sql = QueryBuilder.buildRaw(sql, params, null, "v");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

	public List<Vpn> list(int page, int rows, List<Param> params, Map<String, String> sorter, Long tenantId)
			throws Exception {
		String sql = " select v.*, s.cidr, r.name router_name  from vpn v left join subnet s on v.subnet_id = s.id and s.is_deleted = 0 "
				+ " left join router r on v.router_id = r.id and r.is_deleted = 0 " + " where v.is_deleted = 0 ";
		if (null != tenantId) {
			sql += " and v.tenant_id =  " + tenantId;
		}
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "v");
		return this.list(Vpn.class, sql);
	}

	public List<Vpn> listByTenantId(Long tenantId) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean updateExpriedTime(Long resId, Date expiredTime) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	public List<Vpn> listUsable(Long vendorId, String tenantUuid, Long tenantId) throws Exception {
		String sql = " select * from vpn v where v.vendor_id = :vendorId and v.is_deleted = 0 and v.tenant_uuid = :tenantUuid and v.tenant_id = :tenantId and v.id not in (select c.vpn_id from vpn_conn c where c.is_deleted = 0 and c.vendor_id = :vendorId and c.tenant_id = :tenantId  ) ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("tenantId", tenantId);
		params.put("tenantUuid", tenantUuid);
		return this.list(Vpn.class, sql, params);
	}

	public Vpn getByRouterId(Long routerId, Long vendroId) throws Exception {
		String sql = " select * from vpn v where v.is_deleted = 0 and v.router_id = :routerId and v.vendor_id = :vendroId ";
		Map<String, Object> params = MapTools.simpleMap("routerId", routerId);
		params.put("vendroId", vendroId);
		List<Vpn> list = this.list(Vpn.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public Vpn getBySubnet(Long subnetId, Long vendroId) throws Exception {
		String sql = "  select * from vpn v where v.is_deleted = 0 and v.subnet_id = :subnetId and v.vendor_id = :vendroId ";
		Map<String, Object> params = MapTools.simpleMap("subnetId", subnetId);
		params.put("vendroId", vendroId);
		List<Vpn> list = this.list(Vpn.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

}
