
package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Param;
import com.bocloud.common.model.SimpleBean;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.PageHelper;
import com.bocloud.ims.entity.resource.StorageViewGroup;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * xsky mappinGroup数据访问实现
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("storageViewGroupRepository")
public class StorageViewGroupRepository extends BasicGenericDao<StorageViewGroup, Long> {

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

	public List<StorageViewGroup> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		return null;
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		return null;
	}

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

	public List<StorageViewGroup> listByHostId(Long hostId) throws Exception {
		String sql = "select * from storage_view_group a where a.storage_host_id = :hostId and a.is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("hostId", hostId);
		return this.list(StorageViewGroup.class, sql, params);
	}

	public List<StorageViewGroup> listByAccessPathId(int page, int rows, Long accessPathId, Map<String, Object> params,
			Map<String, String> sorter) throws Exception {
		String sql = "select a.*, b.name as volume_name, c.name as host_name from storage_view_group a "
				+ "left join volume b on a.volume_id = b.id " + "left join storage_host c on a.storage_host_id = c.id "
				+ "where a.access_path_id = :accessPathId and a.is_deleted = 0";
		Map<String, Object> sqlParams = new HashMap<>(8);
		sqlParams.put("accessPathId", accessPathId);
		sql += PageHelper.buildPageSql(page, rows);
		return this.list(StorageViewGroup.class, sql, sqlParams);
	}

	public int countByAccessPathId(Long accessPathId, Map<String, Object> params) throws Exception {
		String sql = "select count(1) from storage_view_group a where a.access_path_id = :accessPathId and a.is_deleted = 0";
		Map<String, Object> sqlParams = new HashMap<>(8);
		sqlParams.put("accessPathId", accessPathId);
		return this.countQuery(sql, sqlParams).intValue();
	}

	public List<StorageViewGroup> listByUuid(String uuid) throws Exception {
		String sql = "select a.*,b.name as volume_name, c.name as host_name from storage_view_group a "
				+ "left join volume b on a.volume_id = b.id " + "left join storage_host c on a.storage_host_id = c.id "
				+ "where a.is_deleted = 0 and a.uuid = :uuid ";
		Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
		return this.list(StorageViewGroup.class, sql, params);
	}

	public StorageViewGroup getById(Long id) throws Exception {
		String sql = "select a.* from storage_view_group a where a.is_deleted = 0 and a.id = :id ";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<StorageViewGroup> list = this.list(StorageViewGroup.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public StorageViewGroup getByUuidAndVolumeId(String uuid, Long volumeId) throws Exception {
		String sql = "select a.* from storage_view_group a where a.is_deleted = 0 and a.uuid = :uuid and a.volume_id = :volumeId";
		Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
		params.put("volumeId", volumeId);
		List<StorageViewGroup> list = this.list(StorageViewGroup.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public void remove(Long id, Long userId) throws Exception {
		String sql = "update storage_view_group set is_deleted = 1 where id = :id ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("id", id);
		execute(sql, params);
	}
}
