package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.resource.ObjectStorageContainer;
import com.google.common.collect.Maps;
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;

/**
 * 对象存储容器Repository实现
 *
 * @author hufy
 * @version 1.0
 * @since 2017年8月29日
 */
@Repository("objectStorageContainerRepository")
public class ObjectStorageContainerRepository extends BasicGenericDao<ObjectStorageContainer, Long> {

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

	public List<ObjectStorageContainer> listContainer(Long userId) throws Exception {
		String sql = "select a.*,b.name as vendorName from object_storage_container a left join cloud_vendor b on a.vendor_id=b.id where a.is_deleted = 0 and a.owner_id=:ownerId";
		Map<String, Object> paramMap = Maps.newHashMap();
		paramMap.put("ownerId", userId);
		return this.list(ObjectStorageContainer.class, sql, paramMap);
	}

	public ObjectStorageContainer get(Class<ObjectStorageContainer> objectStorageContainerClass, Long containerId,
			Long userId) throws Exception {
		String sql = "select * from object_storage_container a where a.id=:containerId and a.owner_id=:ownerId";
		Map<String, Object> paramMap = Maps.newHashMap();
		paramMap.put("containerId", containerId);
		paramMap.put("ownerId", userId);
		List<ObjectStorageContainer> list = this.list(ObjectStorageContainer.class, sql, paramMap);
		return list.size() > 0 ? list.get(0) : null;
	}

	public boolean delete(Long id, Long userId) throws Exception {
		String sql = "update object_storage_container set is_deleted = 1,mender_id = :menderId where id = :id and owner_id=:ownerId";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("menderId", userId);
		params.put("ownerId", userId);
		return this.execute(sql, params) > 0;
	}

	public ObjectStorageContainer getByName(String name) throws Exception {
		String sql = "select * from object_storage_container where name = :name and is_deleted = 0";
		Map<String, Object> paramMap = MapTools.simpleMap("name", name);
		List<ObjectStorageContainer> list = this.list(ObjectStorageContainer.class, sql, paramMap);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public boolean removeByVid(Long vendorId, Long userId) throws Exception {
		String sql = "delete from object_storage_container where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.execute(sql, params) > 0;
	}

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

}
