package com.bocloud.ims.repository.resource;

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.ims.entity.resource.ServerImage;
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("serverImageRepository")
public class ServerImageRepository extends BasicGenericDao<ServerImage, Long> {

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

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

	public List<ServerImage> listByImageId(long imageId) {
		String sql = "select a.*, b.name as server_name, c.name as image_name "
				+ "from server_image a "
				+ "join cloud_server b on a.server_id = b.id "
				+ "join image c on a.image_id = c.id "
				+ "where a.image_id = :imageId";
		Map<String, Object> params = MapTools.simpleMap("imageId", imageId);
		return this.list(ServerImage.class, sql, params);
	}

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

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

	public boolean delete(long id) {
		String sql = "delete from server_image 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_image where server_id = :serverId";
		Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByIid(long imageId) {
		String sql = "delete from server_image where image_id = :imageId";
		Map<String, Object> params = MapTools.simpleMap("imageId", imageId);
		return this.execute(sql, params) > 0;
	}

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

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

}
