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

@Repository("storageAccessPathRepository")
public class StorageAccessPathRepository extends BasicGenericDao<StorageAccessPath, Long> {

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

	public List<StorageAccessPath> list(int page, int rows, List<Param> params, Map<String, String> sorter)
			throws Exception {
		String sql = "select a.* from " + "(select aa.* from storage_access_path aa "
				+ "left join cloud_vendor cv on cv.id = aa.vendor_id " + "where aa.is_deleted = 0) " + "a where 1=1 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(StorageAccessPath.class, sql);
	}

	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from " + "(select aa.* from storage_access_path aa "
				+ "left join cloud_vendor cv on cv.id = aa.vendor_id " + "where aa.is_deleted = 0) " + "a where 1=1 ";
		sql = QueryBuilder.buildRaw(sql, params, null, "a");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

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

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

	public StorageAccessPath getByUuid(String uuid) throws Exception {
		String sql = "select * from storage_access_path a where a.uuid = :uuid a.is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("uuid", uuid);
		List<StorageAccessPath> list = this.list(StorageAccessPath.class, sql, params);
		return list == null || list.size() == 0 ? null : list.get(0);
	}

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

	public StorageAccessPath getByName(String name, Long vendorId) throws Exception {
		String sql = "select * from storage_access_path a where a.is_deleted = 0 and a.name = :name and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		params.put("vendorId", vendorId);
		List<StorageAccessPath> list = this.list(StorageAccessPath.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_access_path set is_deleted = 1 where id = :id ";
		Map<String, Object> params = new HashMap<>(8);
		params.put("id", id);
		execute(sql, params);
	}
}
