package com.bocloud.ims.repository.resource;

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.ListTool;
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.ServerVolume;
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;

/**
 * 云主机硬盘数据关系
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("serverVolumeRepository")
public class ServerVolumeRepository extends BasicGenericDao<ServerVolume, Long> {

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

	public List<ServerVolume> listByServerId(long serverId) {
		String sql = "select a.*, b.name as server_name, c.name as volume_name " + "from server_volume a "
				+ "join cloud_server b on a.server_id = b.id " + "join volume c on a.volume_id = c.id "
				+ "where a.server_id = :serverId";
		Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
		return this.list(ServerVolume.class, sql, params);
	}

	public List<ServerVolume> listByVolumeId(long volumeId) {
		String sql = "select a.*, b.name as server_name, c.name as volume_name " + "from server_volume a "
				+ "join cloud_server b on a.server_id = b.id " + "join volume c on a.volume_id = c.id "
				+ "where a.volume_id = :volumeId";
		Map<String, Object> params = MapTools.simpleMap("volumeId", volumeId);
		return this.list(ServerVolume.class, sql, params);
	}

	public List<ServerVolume> listByStorageId(long storageId) {
		String sql = "select a.*, b.name as server_name, c.name as volume_name " + "from server_volume a "
				+ "join cloud_server b on a.server_id = b.id " + "join volume c on a.volume_id = c.id "
				+ "where c.storage_id = :storageId";
		Map<String, Object> params = MapTools.simpleMap("storageId", storageId);
		return this.list(ServerVolume.class, sql, params);
	}

	public ServerVolume getByServerIdAndVolumeId(long serverId, long volumeId) {
		String sql = "select * from server_volume where server_id = :serverId and volume_id =:volumeId";
		Map<String, Object> params = MapTools.simpleMap("volumeId", volumeId);
		params.put("serverId", serverId);
		List<ServerVolume> list = this.list(ServerVolume.class, sql, params);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}

	public boolean removeByServerIdAndVolumeId(long serverId, long volumeId) {
		String sql = "delete from server_volume where server_id = :serverId and volume_id =:volumeId";
		Map<String, Object> params = MapTools.simpleMap("volumeId", volumeId);
		params.put("serverId", serverId);
		return this.execute(sql, params) > 0;
	}

	public int count(List<Param> params) {
		String sql = "select count(1) from server_volume where 1=1";
		sql = QueryBuilder.build(sql, params, null, "");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	public ServerVolume query(Long id) {
		String sql = "select * from server_volume where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<ServerVolume> list = this.list(ServerVolume.class, sql, params);
		return ListTool.isEmpty(list) ? null : list.get(0);
	}

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

	public boolean removeBySid(Long serverId) {
		String sql = "delete from server_volume where server_id = :serverId";
		Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByVid(Long volumeId) {
		String sql = "delete from server_volume where volume_id = :volumeId";
		Map<String, Object> params = MapTools.simpleMap("volumeId", volumeId);
		return this.execute(sql, params) > 0;
	}

	public void removeByVendorid(long vendorId, long userId) throws BoCloudDBException {
		String sql = "delete from server_volume where server_id in (select id from cloud_server where vendor_id = :vendorId) ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		this.execute(sql, params);
	}

	public boolean remove(Long volumeId, Long serverId) {
		String sql = "delete from server_volume where volume_id = :volumeId and server_id = :serverId";
		Map<String, Object> params = MapTools.simpleMap("volumeId", volumeId);
		params.put("serverId", serverId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByVolumeIdLt0(Long serverId) {
		String sql = "delete from server_volume where volume_id < 0 and server_id = :serverId";
		Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
		return this.execute(sql, params) > 0;
	}

}
