package com.bocloud.ims.repository.resource;

import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.resource.DiskPartition;
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;

/**
 * 磁盘分区数据访问实现
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("diskPartitionRepository")
public class DiskPartitionRepository extends BasicGenericDao<DiskPartition, Long> {

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

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

	public void removeByDisk(long diskId) throws Exception {
		String sql = "update disk_partition set is_deleted = 1 where disk_id = :diskId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("diskId", diskId);
		execute(sql, params);
	}

	public List<DiskPartition> listByDisk(long diskId) throws Exception {
		String sql = "select a.* from disk_partition a where a.is_deleted = 0 and a.disk_id = :diskId ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("diskId", diskId);
		return this.list(DiskPartition.class, sql, params);
	}

	public DiskPartition get(long id) throws Exception {
		String sql = "select a.* from disk_partition a where a.id = :id ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("id", id);
		List<DiskPartition> list = this.list(DiskPartition.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

	public DiskPartition getByDiskAndUuid(long diskId, String partitionUuid) throws Exception {
		String sql = "select a.* from disk_partition a where a.is_deleted = 0 and a.disk_id = :diskId and a.partition_uuid = :partitionUuid";
		Map<String, Object> params = new HashMap<>(8);
		params.put("diskId", diskId);
		params.put("partitionUuid", partitionUuid);
		List<DiskPartition> list = this.list(DiskPartition.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}
}
