package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.ListTool;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.resource.StoreHost;
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.List;
import java.util.Map;

/**
 * 存储-主机数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("storeHostRepository")
public class StoreHostRepository extends BasicGenericDao<StoreHost, Long> {

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

	public boolean delete(StoreHost storeHost) throws Exception {
		return this.delete(StoreHost.class, storeHost.getId());
	}

	public List<StoreHost> list(Long vhostId, String category) throws Exception {
		String sql = "select * from store_host where vhost_id = :vhostId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("vhostId", vhostId);
		params.put("category", category);
		return this.list(StoreHost.class, sql, params);
	}
	
	public List<StoreHost> list(List<Long> vhostIds, String category, Long vendorId) throws Exception {
		String sql = "select * from store_host where category = :category and vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("category", category);
		if (!ListTool.isEmpty(vhostIds)) {
			sql += " and vhost_id in (:vhostIds) ";
			params.put("vhostIds", vhostIds);
		}
		return this.list(StoreHost.class, sql, params);
	}

	public boolean deleteByHost(Long vhostId, String category) throws Exception {
		String sql = "delete from store_host where vhost_id = :vhostId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("vhostId", vhostId);
		params.put("category", category);
		return this.execute(sql, params) > 0;
	}

	public boolean deleteByStore(Long storeId) throws Exception {
		String sql = "delete from store_host where store_id = :storeId";
		Map<String, Object> params = MapTools.simpleMap("storeId", storeId);
		return this.execute(sql, params) > 0;
	}

	public boolean removeByVendor(Long vendorId) throws Exception {
		String sql = "delete from store_host where vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.execute(sql, params) > 0;
	}

	public int countByStore(Long storeId, String category) throws Exception {
		String sql = "select count(1) from store_host where store_id = :storeId and category = :category";
		Map<String, Object> params = MapTools.simpleMap("storeId", storeId);
		params.put("category", category);
		return this.countQuery(sql, params).intValue();
	}

	public List<StoreHost> list(Long storeId, Long vhostId, Long vendorId, String category) throws Exception {
		String sql = "select * from store_host where category = :category and vendor_id = :vendorId and store_id = :storeId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("category", category);
		params.put("storeId", storeId);
		if (null != vhostId) {
			sql += " and vhost_id = :vhostId";
		}
		params.put("vhostId", vhostId);
		return this.list(StoreHost.class, sql, params);
	}

	public List<Long> queryVhostIdByStoreId(Long storeId, Long vendorId, String category) throws Exception {
		String sql = "select * from store_host where category = :category and vendor_id = :vendorId and store_id = :storeId";
		Map<String, Object> params = MapTools.simpleMap("storeId", storeId);
		params.put("category", category);
		params.put("vendorId", vendorId);
		List<StoreHost> list = this.list(StoreHost.class, sql, params);
		List<Long> ids = new ArrayList<>();
		for (StoreHost object : list) {
			ids.add(object.getVhostId());
		}
		return ids;
	}

	/**
	 * 批量保存
	 *
	 * @param storeHosts
	 * @return
	 * @throws Exception
	 */

	public boolean batchSave(List<StoreHost> storeHosts) throws Exception {
		return this.batchSave(storeHosts, 1000);
	}
}
