package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
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.Image;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * 镜像数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("imageRepository")
public class ImageRepository extends BasicGenericDao<Image, Long> {

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

	public Image query(Long id) {
		String sql = "select a.*,b.icon,t.name tenant_name,u.name target_name, region.name region_name from image a "
				+ " left join icon b on a.icon_id=b.id left join tenant t on t.id = a.tenant_id "
				+ " left join region region on a.region_id = region.region_id and a.vendor_id = region.vendor_id "
				+ " left join object_relation o on o.res_id = a.id and o.category = 'Image' left join users u on u.id = o.target and o.catalog = 'USER'  where a.is_deleted = 0 and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Image> list = this.list(Image.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public Image queryByImageUuid(String id) {
		String sql = "select * from image a where a.is_deleted = 0 and a.image_uuid =:id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Image> list = this.list(Image.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public Image queryPhysical(Long id) {
		String sql = "select * from image a where a.is_deleted = 0 and a.catalog = 'PM' and a.id =:id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<Image> list = this.list(Image.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<Image> list(int page, int rows, List<Param> params, Map<String, String> sorter) {
		String sql = "select a.*,t.name tenant_name,v.name vendor_name, region.name region_name from image a "
				+ " left join cloud_vendor v on a.vendor_id = v.id "
				+ " left join region region on a.region_id = region.region_id and a.vendor_id = region.vendor_id  "
				+ " left join tenant t on t.id = a.tenant_id where a.is_deleted = 0 and a.status != 'DELETED' and (a.store_id is null or a.store_id = 0)";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(Image.class, sql, paramMap);
	}

	public List<Image> listHighConfig(int page, int rows, List<Param> params, Map<String, String> sorter) {
		String sql = "select a.*,t.name tenant_name,v.name vendor_name, region.name region_name from image a "
				+ " left join cloud_vendor v on a.vendor_id = v.id "
				+ " left join region region on a.region_id = region.region_id and a.vendor_id = region.vendor_id  "
				+ " left join tenant t on t.id = a.tenant_id where a.is_deleted = 0 and a.status != 'DELETED' and (a.store_id is null or a.store_id = 0) and size = '64'";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(Image.class, sql, paramMap);
	}

	public List<Image> list(int page, int rows, List<Param> params, Map<String, String> sorter, List<Long> projectIds,
			List<Long> vendorIds) {
		String sql = "select a.*,t.name tenant_name,v.name vendor_name, region.name region_name from image a "
				+ " left join cloud_vendor v on a.vendor_id = v.id "
				+ " left join region region on a.region_id = region.region_id and a.vendor_id = region.vendor_id  "
				+ " left join tenant t on t.id = a.tenant_id where a.is_deleted = 0 and a.status != 'DELETED' and (a.store_id is null or a.store_id = 0)";
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		if (!ListTool.isEmpty(projectIds)) {
			sql += " and a.project_id in (:projectIds) ";
			paramMap.put("projectIds", projectIds);
		}
		if (!ListTool.isEmpty(vendorIds)) {
			sql += " and a.vendor_id in (:vendorIds) ";
			paramMap.put("vendorIds", vendorIds);
		}
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(Image.class, sql, paramMap);
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) {
		String sql = "select a.id,a.name,a.image_uuid,a.default_user from image a where a.is_deleted = 0 and (a.store_id is null or a.store_id = 0)";
		sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		List<Image> images = this.list(Image.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (Image image : images) {
			SimpleBean simpleBean = new SimpleBean(image.getId(), image.getName(), image.getImageUuid());
			simpleBean.setUuid(image.getDefaultUser());
			beans.add(simpleBean);
		}
		return beans;
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter, List<Long> projectIds,
			List<Long> vendorIds) {
		String sql = "select a.id,a.name,a.image_uuid,a.default_user from image a where a.is_deleted = 0 and (a.store_id is null or a.store_id = 0)";
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		if (!ListTool.isEmpty(projectIds)) {
			sql += " and a.project_id in (:projectIds) ";
			paramMap.put("projectIds", projectIds);
		}
		if (!ListTool.isEmpty(vendorIds)) {
			sql += " and a.vendor_id in (:vendorIds) ";
			paramMap.put("vendorIds", vendorIds);
		}
		sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
		List<Image> images = this.list(Image.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (Image image : images) {
			SimpleBean simpleBean = new SimpleBean(image.getId(), image.getName(), image.getImageUuid());
			simpleBean.setUuid(image.getDefaultUser());
			beans.add(simpleBean);
		}
		return beans;
	}

	public int count(List<Param> params) {
		String sql = "select count(1) from image a where a.is_deleted = 0 and a.status != 'DELETED' and (a.store_id is null or a.store_id = 0)";
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		return this.countQuery(sql, paramMap).intValue();
	}

	public int count(List<Param> params, List<Long> projectIds, List<Long> vendorIds) {
		String sql = "select count(1) from image a where a.is_deleted = 0 and a.status != 'DELETED' and (a.store_id is null or a.store_id = 0)";
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		if (!ListTool.isEmpty(projectIds)) {
			sql += " and a.project_id in (:projectIds) ";
			paramMap.put("projectIds", projectIds);
		}
		if (!ListTool.isEmpty(vendorIds)) {
			sql += " and a.vendor_id in (:vendorIds) ";
			paramMap.put("vendorIds", vendorIds);
		}
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		return this.countQuery(sql, paramMap).intValue();
	}

	public boolean remove(Long id, Long userId) {
		String sql = "update image set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public List<Image> list() {
		String sql = "select a.*,t.name tenant_name from image a  left join tenant t on t.id = a.tenant_id where a.is_deleted = 0 and (a.store_id is null or a.store_id = 0)";
		return this.list(Image.class, sql);
	}

	public Image getByImageUuid(String imageUuid) {
		String sql = "select a.*,t.name tenant_name from image a  left join tenant t on t.id = a.tenant_id where a.is_deleted = 0 and a.image_uuid = :imageUuid";
		Map<String, Object> param = MapTools.simpleMap("imageUuid", imageUuid);
		List<Image> list = this.list(Image.class, sql, param);
		Image image = null;
		if (!list.isEmpty()) {
			image = list.get(0);
		}
		return image;
	}

	public boolean removeByVid(Long vendorId, Long userId) {
		String sql = "update image set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}
	public boolean removeByVidBak(Long vendorId, Long userId) {
		String sql = "update image set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId and os_type != 'cloudServerTemplate'";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public boolean removeByOsType(Long userId, Long vendorId) {
		String sql = "update image set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId " +
				"where is_deleted = 0 and vendor_id = :vendorId and os_type = 'cloudServerTemplate'";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public List<Image> listByByOsType(Long vendorId) {
		String sql = "select a.*,t.name tenant_name from image a  " +
				"left join tenant t on t.id = a.tenant_id where a.is_deleted = 0 and a.os_type= 'cloudServerTemplate' and a.vendor_id = :vendorId and (a.store_id is null or a.store_id = 0)";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(Image.class, sql, params);
	}


	public List<Image> listByVendorId(Long vendorId) {
		String sql = "select a.*,t.name tenant_name from image a  left join tenant t on t.id = a.tenant_id where a.is_deleted = 0 and a.vendor_id = :vendorId and (a.store_id is null or a.store_id = 0)";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(Image.class, sql, params);
	}

	public boolean removeByTemplate(Long templateId, Long userId) {
		String sql = "update image set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and template_id = :templateId";
		Map<String, Object> params = MapTools.simpleMap("templateId", templateId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public boolean removeByStoreId(Long storeId, Long userId) {
		String sql = "update image set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and store_id = :storeId";
		Map<String, Object> params = MapTools.simpleMap("storeId", storeId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public List<Image> listByStoreId(Long storeId) {
		String sql = "select * from image where is_deleted = 0 and store_id = :storeId";
		Map<String, Object> param = MapTools.simpleMap("storeId", storeId);
		return this.list(Image.class, sql, param);
	}

	public List<Image> listByTenant(Long vendorId, Long tenantId, String region) {
		String sql = "select a.* from image a where a.is_deleted = 0 and a.tenant_id in (0," + tenantId
				+ ") and a.vendor_id = " + vendorId
				+ " and a.status = 'ACTIVE' and (a.store_id is null or a.store_id = 0)";
		return this.list(Image.class, sql);
	}

	public List<Image> list(int page, int rows, List<Param> params, Map<String, String> sorter, Long tenantId,
			List<Long> projectIds) throws Exception {
		String sql = "select a.*,v.name vendor_name,t.name tenant_name from image a "
				+ " left join cloud_vendor v on a.vendor_id = v.id "
				+ " left join tenant t on a.tenant_id = t.id where a.is_deleted = 0 ";
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		if (null != tenantId && 0L != tenantId) {
			sql += " and a.tenant_id = :tenantId";
			paramMap.put("tenantId", tenantId);
		}
		if (!ListTool.isEmpty(projectIds)) {
			sql += " and a.project_id in (:projectIds) ";
			paramMap.put("projectIds", projectIds);
		}
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(Image.class, sql, paramMap);
	}

	public int count(List<Param> params, Long tenantId, List<Long> projectIds) {
		String sql = "select count(a.id) from image a left join cloud_vendor v on a.vendor_id = v.id left join tenant t on a.tenant_id = t.id  where a.is_deleted = 0  ";
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		if (null != tenantId && 0L != tenantId) {
			sql += " and a.tenant_id = :tenantId";
			paramMap.put("tenantId", tenantId);
		}
		if (!ListTool.isEmpty(projectIds)) {
			sql += " and a.project_id in (:projectIds) ";
			paramMap.put("projectIds", projectIds);
		}
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		return this.countQuery(sql, paramMap).intValue();
	}

	public boolean tenantCheck(Long tenantId, Long creatorId) {
		String sql = "select count(1) from image a where a.is_deleted = 0 ";
		Map<String, Object> paramMap = Maps.newHashMap();
		if (null != tenantId && 0L != tenantId) {
			sql += " and a.tenant_id = :tenantId";
			paramMap.put("tenantId", tenantId);
		}
		if (null != creatorId && 0L != creatorId) {
			sql += " and a.creator_id = :creatorId";
			paramMap.put("creatorId", creatorId);
		}
		return this.countQuery(sql, paramMap).intValue() > 0;
	}

	public int count(List<Param> params, String type) {
		String sql = "select count(1) from image a where a.is_deleted = 0 and a.status != 'DELETED' and (a.store_id is null or a.store_id = 0)";
		if (!StringUtils.isEmpty(type)) {
			sql += " and a.vendor_type = '" + type + "'";
		}
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	public boolean hasBuilding(Long vendorId) {
		String sql = "select count(1) from image a where a.is_deleted = 0 and a.status = 'BUILDING' and a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.countQuery(sql, paramMap) > 0;
	}

	public List<Image> listByTemplate(Long templateId, Long vendorId) {
		String sql = "select * from image where is_deleted = 0 and template_id = :templateId and vendor_id = :vendorId";
		Map<String, Object> param = MapTools.simpleMap("templateId", templateId);
		param.put(Common.VENDORID, vendorId);
		return this.list(Image.class, sql, param);
	}

	public List<Image> listByReg(String reg, Long vendorId) {
		String sql = "select * from image where is_deleted = 0 and name like '%_" + reg + "' and vendor_id = :vendorId";
		Map<String, Object> param = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(Image.class, sql, param);
	}

	public boolean exists(String type, String version) {
		String sql = "select * from image a where a.is_deleted = 0 and a.catalog = 'PM' and a.os_type = :type and a.os_version = :version";
		Map<String, Object> params = MapTools.simpleMap("type", type);
		params.put("version", version);
		List<Image> list = this.list(Image.class, sql, params);
		return !list.isEmpty();
	}

	public boolean deletePhysicalImage(String type, String version, Long userId) {
		String sql = "update image a set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId"
				+ " where is_deleted = 0 and a.catalog = 'PM' and a.os_type = :type and a.os_version = :version";
		Map<String, Object> params = MapTools.simpleMap("type", type);
		params.put("version", version);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) >= 0;
	}

	public List<String> listFiles() {
		String sql = "select a.file_path from image a where a.is_deleted = 0";
		List<Image> list = this.list(Image.class, sql, null);
		List<String> beans = new ArrayList<String>();
		for (Image image : list) {
			beans.add(image.getFilePath());
		}
		return beans;
	}

	public List<Image> listByVendorId(Long vendorId, String regionId) {
		String sql = "select a.*,t.name tenant_name from image a  left join tenant t on t.id = a.tenant_id where a.is_deleted = 0 and a.vendor_id = :vendorId "
				+ "  and (a.store_id is null or a.store_id = 0) and a.region_id = :regionId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put(Common.REGIONID, regionId);
		return this.list(Image.class, sql, params);
	}

	public boolean removeByVid(Long vendorId, String regionId, Long userId) {
		String sql = "update image set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId and region_id = :regionId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		params.put("regionId", regionId);
		return this.execute(sql, params) > 0;
	}

	public Image getByName(Long vendorId, String name, String regionId) {
		String sql = "SELECT a.* FROM image a  WHERE a.is_deleted = 0 AND a.vendor_id = :vendorId AND a.name = :name AND a.region_id = :regionId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("name", name);
		params.put(Common.REGIONID, regionId);
		List<Image> list = this.list(Image.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<Image> listByCondition(String region, String availablitiyZone) {
		String sql = "select a.* from image a left join cloud_vendor b on b.id = a.vendor_id where b.region = :region and b.availablitiy_zone = :availablitiyZone";
		Map<String, Object> params = MapTools.simpleMap("region", region);
		params.put("availablitiyZone", availablitiyZone);
		return this.list(Image.class, sql, params);
	}

	public List<Image> listPublicImage(Long vendorId) {
		String sql = "select a.* from image a where a.vendor_id = :vendorId and a.is_deleted = 0 and a.image_label is not null";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.list(Image.class, sql, params);
	}

	public Image getByLabel(String imageLabel, Long vendorId) {
		String sql = "select * from image where vendor_id = :vendorId and image_label = :imageLabel";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("imageLabel", imageLabel);
		List<Image> list = this.list(Image.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public boolean tenantCheck(Long tenantId, Long projectId, Long creatorId) throws Exception {
		String sql = "select count(1) from image a where a.is_deleted = 0 ";
		Map<String, Object> paramMap = Maps.newHashMap();
		if (null != tenantId && 0L != tenantId) {
			sql += " and a.tenant_id = :tenantId";
			paramMap.put("tenantId", tenantId);
		}
		if (null != projectId && 0L != projectId) {
			sql += " and a.project_id = :projectId";
			paramMap.put("projectId", projectId);
		}
		if (null != creatorId && 0L != creatorId) {
			sql += " and a.creator_id = :creatorId";
			paramMap.put("creatorId", creatorId);
		}
		return this.countQuery(sql, paramMap).intValue() > 0;
	}

	public boolean removeByGroupId(Long vendorId, Long userId, Long resourceGroupId) {
		String sql = "update image set is_deleted = true,gmt_modify = :gmtModify,mender_id = :menderId where is_deleted = 0 and vendor_id = :vendorId and resource_group_id = :resourceGroupId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		params.put("resourceGroupId", resourceGroupId);
		params.put("menderId", userId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public int count(List<Param> params, List<Long> tenantVendorIds) {
		String sql = "select count(1) from image a where a.is_deleted = 0 and a.status != 'DELETED' and (a.store_id is null or a.store_id = 0)";
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		if (!ListTool.isEmpty(tenantVendorIds)) {
			sql += " and a.vendor_id in (:tenantVendorIds) ";
			paramMap.put("tenantVendorIds", tenantVendorIds);
		}
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		return this.countQuery(sql, paramMap).intValue();
	}

	public List<Image> list(Integer page, Integer rows, List<Param> params, Map<String, String> sorter,
			List<Long> tenantVendorIds) {
		String sql = "select a.*,t.name tenant_name,v.name vendor_name, region.name region_name from image a "
				+ " left join cloud_vendor v on a.vendor_id = v.id "
				+ " left join region region on a.region_id = region.region_id and a.vendor_id = region.vendor_id  "
				+ " left join tenant t on t.id = a.tenant_id where a.is_deleted = 0 and a.status != 'DELETED' and (a.store_id is null or a.store_id = 0)";
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		if (!ListTool.isEmpty(tenantVendorIds)) {
			sql += " and a.vendor_id in (:tenantVendorIds) ";
			paramMap.put("tenantVendorIds", tenantVendorIds);
		}
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(Image.class, sql, paramMap);
	}

	public List<Image> listByServerId(Long serverId) {
		String sql = "select * from image where is_deleted = 0 and id in (select image_id from server_image where server_id = :serverId) ";
		Map<String, Object> params = Collections.singletonMap("serverId", serverId);
		return this.list(Image.class, sql, params);
	}

	public Image getByPath(String filePath, Long vendorId) {
		String sql = "select * from image where is_deleted = 0 and file_path = :filePath and vendor_id = :vendorId ";
		Map<String, Object> params = new HashMap<>();
		params.put("vendorId", vendorId);
		params.put("filePath", filePath);
		return this.list(Image.class, sql, params).stream().findFirst().orElse(null);
	}

	public List<Image> listImageForCloudServerAttach(Long serverId, Pager pager) {
		String sql = "select a.* from image a where a.is_deleted = 0 ";
		Map<String, Object> sqlParams = QueryBuilder.getParam(pager.getParams());
		if (serverId != null) {
			sql += " and a.id not in (select image_id from server_image where server_id = :serverId) ";
			sqlParams.put("serverId", serverId);
		}
		sql = QueryBuilder.build(sql, pager, "a");
		return this.list(Image.class, sql, sqlParams);
	}

	public int countImageForCloudServerAttach(Long serverId, List<Param> params) {
		String sql = "select count(1) from image where is_deleted = 0 ";
		Map<String, Object> sqlParams = QueryBuilder.getParam(params);
		if (serverId != null) {
			sql += " and id not in (select image_id from server_image where server_id = :serverId) ";
			sqlParams.put("serverId", serverId);
		}
		sql = QueryBuilder.build(sql, params, null, "");
		return this.countQuery(sql, sqlParams).intValue();
	}

	public List<Image> listImageOfCloudServerAttached(Long serverId, Pager pager) {
		String sql = "select a.* from image a where a.is_deleted = 0 ";
		Map<String, Object> sqlParams = QueryBuilder.getParam(pager.getParams());
		if (serverId != null) {
			sql += " and a.id in (select image_id from server_image where server_id = :serverId) ";
			sqlParams.put("serverId", serverId);
		}
		sql = QueryBuilder.build(sql, pager, "a");
		return this.list(Image.class, sql, sqlParams);
	}

	public int countImageOfCloudServerAttached(Long serverId, List<Param> params) {
		String sql = "select count(1) from volume where is_deleted = 0 ";
		Map<String, Object> sqlParams = QueryBuilder.getParam(params);
		if (serverId != null) {
			sql += " and id in (select image_id from server_image where server_id = :serverId) ";
			sqlParams.put("serverId", serverId);
		}
		sql = QueryBuilder.build(sql, params, null, "");
		return this.countQuery(sql, sqlParams).intValue();
	}
}
