package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
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.IpPool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * 
 * @description IP池数据访问
 * @version 5.1.0
 * @author wangyu
 * @date: 2020-11-30
 *
 */
@Repository("ipPoolRepository")
public class IpPoolRepository extends BasicGenericDao<IpPool, Long> {

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

	public List<IpPool> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
		String sql = "select a.*,b.name vendor_name,b.type vendor_type from ip_pool a left join cloud_vendor b on a.vendor_id = b.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(IpPool.class, sql, paramMap);
	}

	public int count(List<Param> params) {
		String sql = "select count(1) from ip_pool 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 IpPool query(Long id) {
		String sql = "select a.* from ip_pool a where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<IpPool> list = this.list(IpPool.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public IpPool query(String name) {
		String sql = "select a.* from ip_pool a where a.is_deleted = 0 and a.name = :name";
		Map<String, Object> params = MapTools.simpleMap(Common.NAME, name);
		List<IpPool> list = this.list(IpPool.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<IpPool> pools(List<Long> ids) {
		String sql = "select a.* from ip_pool a where a.is_deleted = 0 and a.id in (:ids) and a.version = 'V4'";
		Map<String, Object> params = MapTools.simpleMap("ids", ids);
		return this.list(IpPool.class, sql, params);
	}

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

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

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

	public IpPool queryBySubnet(Long subnetId, Long vendorId) {
		String sql = "select a.* from ip_pool a where a.is_deleted = 0 and a.subnet_id = :subnetId and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("subnetId", subnetId);
		params.put("vendorId", vendorId);
		List<IpPool> list = this.list(IpPool.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

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

//	public List<IpPool> listByIpVersion(Long vendorId, String version) {
//		String sql = "select a.* from ip_pool a where a.is_deleted = 0 and a.vendor_id = :vendorId";
//		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
//		if (!StringUtils.isEmpty(version)) {
//			sql += " and a.version = :version";
//			params.put("version", version);
//		}
//		return this.list(IpPool.class, sql, params);
//	}

	public List<IpPool> listByGroupId(Long groupId) {
		String sql = "select a.* from ip_pool a left join pool_group_relation b on a.id = b.target and b.category = 'IpPool' where a.is_deleted = 0 and b.pool_group_id = :groupId";
		Map<String, Object> params = MapTools.simpleMap("groupId", groupId);
		return this.list(IpPool.class, sql, params);
	}

	public List<IpPool> listByPortId(Long portId, String version) {
		String sql = "select a.* from ip_pool a where a.is_deleted = 0 and a.router_port_id = :portId";
		Map<String, Object> params = MapTools.simpleMap("portId", portId);
		if (!StringUtils.isEmpty(version)) {
			sql += " and a.version = :version";
			params.put("version", version);
		}
		return this.list(IpPool.class, sql, params);
	}

	public List<IpPool> listBySegmentId(Long segmentId, String version) {
		String sql = "select a.* from ip_pool a where a.is_deleted = 0 and a.segment_id = :segmentId";
		Map<String, Object> params = MapTools.simpleMap("segmentId", segmentId);
		if (!StringUtils.isEmpty(version)) {
			sql += " and a.version = :version";
			params.put("version", version);
		}
		return this.list(IpPool.class, sql, params);
	}

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

	public IpPool getByCidr(String cidr, Long portId) {
		String sql = "select a.* from ip_pool a where a.is_deleted = 0 and a.cidr = :cidr and a.router_port_id = :portId";
		Map<String, Object> params = MapTools.simpleMap("portId", portId);
		params.put("cidr", cidr);
		List<IpPool> list = this.list(IpPool.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<IpPool> listByPortIds(List<Long> portIds, String version) {
		String sql = "select a.* from ip_pool a where a.is_deleted = 0 and a.router_port_id in (:portIds)";
		Map<String, Object> params = MapTools.simpleMap("portIds", portIds);
		if (!StringUtils.isEmpty(version)) {
			sql += " and a.version = :version";
			params.put("version", version);
		}
		return this.list(IpPool.class, sql, params);
	}

	public List<IpPool> listBySegmentIds(List<Long> segmentIds, String version) {
		String sql = "select a.* from ip_pool a where a.is_deleted = 0 and a.segment_id in (:segmentIds)";
		Map<String, Object> params = MapTools.simpleMap("segmentIds", segmentIds);
		if (!StringUtils.isEmpty(version)) {
			sql += " and a.version = :version";
			params.put("version", version);
		}
		return this.list(IpPool.class, sql, params);
	}

	public List<IpPool> listByIpVersion(String version) {
		String sql = "select a.* from ip_pool a where a.is_deleted = 0 ";
		Map<String, Object> params = new HashMap<>(8);
		if (!StringUtils.isEmpty(version)) {
			sql += " and a.version = :version";
			params.put("version", version);
		}
		return this.list(IpPool.class, sql, params);
	}

}
