package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.resource.PortFixedIp;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

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

@Service("portFixedIpRepository")
public class PortFixedIpRepository extends BasicGenericDao<PortFixedIp, Long> {

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

	public PortFixedIp getByPortIdAndIp(Long portId, String ip) {
		String sql = "select * from port_fixedip where port_id = :portId and ip_address = :ip";
		Map<String, Object> params = MapTools.simpleMap("portId", portId);
		params.put("ip", ip);
		List<PortFixedIp> list = this.list(PortFixedIp.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public List<PortFixedIp> listByPortUuid(String portUuid) {
		if (portUuid == null || "".equals(portUuid)) {
			return new ArrayList<>();
		}
		String sql = "select * from port_fixedip where port_uuid = :portUuid ";
		Map<String, Object> params = MapTools.simpleMap("portUuid", portUuid);
		return this.list(PortFixedIp.class, sql, params);
	}

	public List<PortFixedIp> listByPortId(Long portId) {
		String sql = "select * from port_fixedip where port_id = :portId ";
		Map<String, Object> params = MapTools.simpleMap("portId", portId);
		return this.list(PortFixedIp.class, sql, params);
	}

	public List<PortFixedIp> listByDeviceUuid(String deviceUuid) {
		String sql = "select a.* from port_fixedip a " + "join port b on a.port_id = b.id "
				+ "where b.is_deleted = 0 and b.device_uuid = :deviceUuid ";
		Map<String, Object> params = MapTools.simpleMap("deviceUuid", deviceUuid);
		return this.list(PortFixedIp.class, sql, params);
	}

	public Boolean delete(Long id) {
		String sql = "delete from port_fixedip where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		return this.execute(sql, params) > 0;
	}

	public boolean deleteByPortId(Long portId) {
		String sql = "delete from port_fixedip where port_id = :portId";
		Map<String, Object> params = MapTools.simpleMap("portId", portId);
		return this.execute(sql, params) > 0;
	}

	public boolean deleteByVendorId(Long vendorId) {
		String sql = "delete from port_fixedip where subnet_id in "
				+ "(select id from subnet where vendor_id = :vendorId)";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.execute(sql, params) > 0;
	}

	public boolean delete(Long portId, Long subnetId) {
		String sql = "delete from port_fixedip where port_id = :portId and subnet_id = :subnetId";
		Map<String, Object> params = MapTools.simpleMap("portId", portId);
		params.put("subnetId", subnetId);
		return this.execute(sql, params) > 0;
	}

	public boolean deleteByNetworkId(Long networkId) {
		String sql = "delete from port_fixedip where subnet_id in (select id from subnet where network_id = :networkId) ";
		Map<String, Object> params = MapTools.simpleMap("networkId", networkId);
		return this.execute(sql, params) > 0;
	}

	public PortFixedIp getPortFixedBySubnet(String subnetUuid, String address) {
		String sql = "select * from port_fixedip where subnet_uuid = :subnetUuid and ip_address = :address ";
		Map<String, Object> params = MapTools.simpleMap("subnetUuid", subnetUuid);
		params.put("address", address);
		List<PortFixedIp> list = this.list(PortFixedIp.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}
}
