package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.StorageHost;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Repository("storageHostRepository")
public class StorageHostRepository extends BasicGenericDao<StorageHost, Long> {

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

	public List<StorageHost> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
		String sql = "select a.* from "
				+ " (select aa.id, aa.storage_id, aa.host_uuid, aa.key_path, aa.type, aa.name, aa.status, aa.gmt_create, "
				+ " aa.creator_id, aa.gmt_modify, aa.mender_id, aa.owner_id, aa.props, aa.remark, aa.is_deleted, "
				+ " aa.description, aa.chap, aa.chap_name, aa.chap_secret, aa.iscsi_port_id, "
				+ " bb.name as storage_name, ifnull(aa.vendor_id, bb.vendor_id) as vendor_id, dd.name as iscsi_port_name "
				+ " from storage_host aa "
				+ "  left join storage bb on aa.storage_id = bb.id "
				+ "  left join cloud_vendor cc on cc.id = aa.vendor_id"
				+ "  left join storage_iscsi_port dd on aa.iscsi_port_id = dd.id"
				+ "  where aa.is_deleted = 0) a "
				+ "where 1=1 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(StorageHost.class, sql);
	}

	public int count(List<Param> params) {
		String sql = "select count(1) from "
				+ " (select aa.id, aa.storage_id, aa.host_uuid, aa.key_path, aa.type, aa.name, aa.status, aa.gmt_create, "
				+ " aa.creator_id, aa.gmt_modify, aa.mender_id, aa.owner_id, aa.props, aa.remark, aa.is_deleted, "
				+ " aa.description, aa.chap, aa.chap_name, aa.chap_secret, aa.iscsi_port_id, "
				+ " bb.name as storage_name, ifnull(aa.vendor_id, bb.vendor_id) as vendor_id "
				+ "from storage_host aa "
				+ "  left join storage bb on aa.storage_id = bb.id "
				+ "  left join cloud_vendor cc on cc.id = aa.vendor_id"
				+ "  where aa.is_deleted = 0) a "
				+ "where 1=1 ";
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

	public void remove(long id) {
		String sql = "update storage_host set is_deleted = 1 where id = :id ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("id", id);
		execute(sql, params);
	}

	public void removeByStorage(long storageId) {
		String sql = "update storage_host set is_deleted = 1 where storage_id = :storageId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("storageId", storageId);
		execute(sql, params);
	}

	public void removeByIScsiPort(long portId) {
		String sql = "update storage_host set is_deleted = 1 where iscsi_port_id = :portId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("portId", portId);
		execute(sql, params);
	}

	public List<StorageHost> listByStorage(long storageId) {
		String sql = "select a.* from storage_host a where is_deleted = 0 and storage_id = :storageId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("storageId", storageId);
		return this.list(StorageHost.class, sql, params);
	}

	public List<StorageHost> listByVendor(long vendorId) {
		String sql = "select a.* from storage_host a where is_deleted = 0 and vendor_id = :vendorId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("vendorId", vendorId);
		return this.list(StorageHost.class, sql, params);
	}

	public List<StorageHost> listByIScsiPort(long portId) {
		String sql = "select a.* from storage_host a where is_deleted = 0 and iscsi_port_id = :portId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("portId", portId);
		return this.list(StorageHost.class, sql, params);
	}

	public StorageHost get(long id) {
		String sql = "select a.*, b.name as storage_name, c.name as iscsi_port_name from storage_host a "
				+ "left join storage b on a.storage_id = b.id "
				+ "left join storage_iscsi_port c on a.iscsi_port_id = c.id " + "where a.id = :id ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("id", id);
		List<StorageHost> list = this.list(StorageHost.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public StorageHost getByUuid(String uuid) {
		String sql = "select a.* from storage_host a " + "where a.host_uuid = :uuid ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("uuid", uuid);
		List<StorageHost> list = this.list(StorageHost.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public StorageHost getByIdentifier(String identifier, long storageId) {
		String sql = "select a.* from storage_host a where is_deleted = 0 and identifier = :identifier and storage_id = :storageId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("identifier", identifier);
		params.put("storageId", storageId);
		List<StorageHost> list = this.list(StorageHost.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public StorageHost getByNameAndStorage(String name, long storageId) {
		String sql = "select a.* from storage_host a where is_deleted = 0 and name = :name and storage_id = :storageId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("name", name);
		params.put("storageId", storageId);
		List<StorageHost> list = this.list(StorageHost.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public StorageHost getByNameAndVendorId(String name, long vendorId) {
		String sql = "select a.* from storage_host a where is_deleted = 0 and name = :name and storage_id = :vendorId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("name", name);
		params.put("vendorId", vendorId);
		List<StorageHost> list = this.list(StorageHost.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public List<StorageHost> listHostNotInAccessPath(Long accessPathId, List<Long> storageHostIds) {
		String sql = "select a.* from storage_host a "
				+ "where a.is_deleted = 0 and a.id not in (select storage_host_id from storage_view_group where access_path_id = :accessPathId and is_deleted=0)";
		Map<String, Object> sqlParams = new HashMap<>(8);
		sqlParams.put("accessPathId", accessPathId);
		List<StorageHost> list = this.list(StorageHost.class, sql, sqlParams);
		for (Long storageHostId : storageHostIds) {
			list = list.stream().filter(storageHost -> !Objects.equals(storageHost.getId(), storageHostId))
					.collect(Collectors.toList());
		}
		return list;
	}

	public List<StorageHost> listHostByVolume(int page, int rows, long volumeId, Map<String, Object> params,
			Map<String, String> sorter) {
		String sql = "select a.* from storage_host a "
				+ "where a.id in (select storage_host_id from storage_view_group where volume_id = :volumeId)";
		Map<String, Object> paramsMap = new HashMap<>(8);
		paramsMap.put("volumeId", volumeId);
		return this.list(StorageHost.class, sql, paramsMap);
	}

	public int countHostByVolume(long volumeId, Map<String, Object> params) {
		String sql = "select count(1) from storage_host a "
				+ "where a.id in (select storage_host_id from storage_view_group where volume_id = :volumeId)";
		Map<String, Object> paramsMap = new HashMap<>(8);
		paramsMap.put("volumeId", volumeId);
		return this.countQuery(sql, paramsMap).intValue();
	}
}
