package com.bocloud.ims.repository.pbm;

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.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.database.utils.QueryBuilder;
import com.bocloud.ims.entity.resource.pbm.PbmProfile;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * vcenter存储策略数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年2月27日
 *
 */
@Repository("pbmProfileRepository")
public class PbmProfileRepository extends BasicGenericDao<PbmProfile, Long> {

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

	public List<PbmProfile> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.* from pbm_profile a where 1 = 1 ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.list(PbmProfile.class, sql, paramMap);
	}

	public List<SimpleBean> list(List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select a.id,a.name from pbm_profile a where 1 = 1 ";
		sql = QueryBuilder.build(sql, new Pager(1, Integer.MAX_VALUE, params, sorter), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		List<PbmProfile> groups = this.list(PbmProfile.class, sql, paramMap);
		List<SimpleBean> beans = new ArrayList<>();
		for (PbmProfile group : groups) {
			beans.add(new SimpleBean(group.getId(), group.getName()));
		}
		return beans;
	}

	public int count(List<Param> params) throws Exception {
		String sql = "select count(a.id) from pbm_profile a where 1 = 1";
		sql = QueryBuilder.buildRaw(sql, params, new HashMap<>(8), "a");
		Map<String, Object> paramMap = QueryBuilder.getParam(params);
		return this.countQuery(sql, paramMap).intValue();
	}

	public PbmProfile query(Long id) throws Exception {
		String sql = "select a.* from pbm_profile a where a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<PbmProfile> list = this.list(PbmProfile.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<PbmProfile> listByVendorId(Long vendorId) {
		String sql = "select a.* from pbm_profile a where a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		List<PbmProfile> list = this.list(PbmProfile.class, sql, paramMap);
		if (list.isEmpty()) {
			return new ArrayList<>();
		}
		return list;
	}

	public boolean removeByVendorId(Long vendorId) {
		String sql = "delete from pbm_profile where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.execute(sql, params) > 0;
	}

	public boolean remove(Long id) {
		String sql = "delete from pbm_profile where id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		return this.execute(sql, params) > 0;
	}

}
