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.VolumePool;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * 存储volume pool服务器
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("volumePoolRepository")
public class VolumePoolRepository extends BasicGenericDao<VolumePool, Long> {

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

	public List<VolumePool> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
		String sql = "select a.*, b.name as storage_name, c.name as mdisk_name " + "from volume_pool a "
				+ "left join storage b on a.storage_id = b.id " + "left join mdisk c on a.mdisk_id = c.id "
				+ "where a.is_deleted = 0 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(VolumePool.class, sql);
	}

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

	public boolean remove(long id) {
		String sql = "update volume_pool set is_deleted = 1 where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByVendor(long vendorId) {
		String sql = "update volume_pool set is_deleted = 1 where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByStorage(long storageId) {
		String sql = "update volume_pool set is_deleted = 1 where storage_id = :storageId";
		Map<String, Object> params = MapTools.simpleMap("storageId", storageId);
		return this.execute(sql, params) > 0;
	}

	public List<VolumePool> listByVendor(long vendorId) {
		String sql = "select * from volume_pool where is_deleted = 0 and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.list(VolumePool.class, sql, params);
	}

	public List<VolumePool> listByStorage(long storageId) {
		String sql = "select * from volume_pool where is_deleted = 0 and storage_id = :storageId";
		Map<String, Object> params = MapTools.simpleMap("storageId", storageId);
		return this.list(VolumePool.class, sql, params);
	}

	public VolumePool get(long id) {
		String sql = "select a.*, b.name, a.raid,  b.name as mdisk_name from volume_pool a "
				+ "left join mdisk b on a.mdisk_id = b.id " + "where a.id = :id and a.is_deleted = 0 ";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<VolumePool> list = this.list(VolumePool.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public VolumePool getByUuid(String uuid, long storageId) {
		String sql = "select * from volume_pool where is_deleted = 0 and uuid = :uuid and storage_id = :storageId ";
		Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
		params.put("storageId", storageId);
		List<VolumePool> list = this.list(VolumePool.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public VolumePool getByPoolUuid(String poolUuid, long storageId) {
		String sql = "select * from volume_pool where is_deleted = 0 and pool_uuid = :poolUuid and storage_id = :storageId ";
		Map<String, Object> params = MapTools.simpleMap("poolUuid", poolUuid);
		params.put("storageId", storageId);
		List<VolumePool> list = this.list(VolumePool.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public VolumePool getByUuid(String uuid) {
		String sql = "select * from volume_pool where is_deleted = 0 and pool_uuid = :uuid ";
		Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
		List<VolumePool> list = this.list(VolumePool.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public VolumePool getByName(String name, long storageId) {
		String sql = "select * from volume_pool where is_deleted = 0 and name = :name and storage_id = :storageId ";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		params.put("storageId", storageId);
		List<VolumePool> list = this.list(VolumePool.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public VolumePool getByStorageAndIdentifier(long storageId, String identifier) {
		String sql = "select * from volume_pool where is_deleted = 0 and identifier = :identifier and storage_id = :storageId ";
		Map<String, Object> params = MapTools.simpleMap("identifier", identifier);
		params.put("storageId", storageId);
		List<VolumePool> list = this.list(VolumePool.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public List<VolumePool> listByMdisk(long mdiskId) {
		String sql = "select * from volume_pool where is_deleted = 0 and mdisk_id = :mdiskId";
		Map<String, Object> params = MapTools.simpleMap("mdiskId", mdiskId);
		return this.list(VolumePool.class, sql, params);
	}
}
