package com.bocloud.ims.repository.resource;

import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.BasicGenericDao;
import com.bocloud.ims.entity.resource.VmResourcePool;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

/**
 * 资源池数据访问
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年5月15日
 *
 */
@Repository("resourcePoolRepository")
public class ResourcePoolRepository extends BasicGenericDao<VmResourcePool, Long> {

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

	public List<VmResourcePool> list(Long parentId, Long vendorId) throws Exception {
		Map<String, Object> params = new HashMap<>(8);
		if (null == parentId || parentId == 0L) {
			String sql = "select * from resource_pool where (parent_id = 0 or parent_id is null) and is_deleted = 0 and vendor_id = :vendorId";
			params.put(Common.VENDORID, vendorId);
			return this.list(VmResourcePool.class, sql, params);
		} else {
			String sql = "select * from resource_pool where parent_id = :parentId and is_deleted = 0 and vendor_id = :vendorId";
			params.put(Common.VENDORID, vendorId);
			params.put("parentId", parentId);
			return this.list(VmResourcePool.class, sql, params);
		}
	}

	public boolean remove(Long id, Long menderId) throws Exception {
		String sql = "update resource_pool set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where id = :id";
		Map<String, Object> params = new HashMap<>(8);
		params.put("gmtModify", new Date());
		params.put("menderId", menderId);
		params.put("id", id);
		return this.execute(sql, params) > 0;
	}

	public VmResourcePool query(Long id) throws Exception {
		String sql = "select * from resource_pool where is_deleted = 0 and id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		List<VmResourcePool> list = this.list(VmResourcePool.class, sql, params);
		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}

	public boolean exists(String name, Long parentId) throws Exception {
		String sql = "select * from resource_pool where is_deleted = 0 and name = :name and parent_id = :parentId";
		Map<String, Object> params = MapTools.simpleMap("name", name);
		params.put("parentId", parentId);
		List<VmResourcePool> list = this.list(VmResourcePool.class, sql, params);
		if (list.isEmpty()) {
			return false;
		}
		return true;
	}

	public List<VmResourcePool> listByVdc(Long vdcId) throws Exception {
		String sql = "select * from resource_pool where is_deleted = 0 and vdc_id = :vdcId";
		Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
		return this.list(VmResourcePool.class, sql, params);
	}

	public boolean removeByVdc(Long vdcId, Long userId) throws Exception {
		String sql = "update resource_pool set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where is_deleted = 0 and vdc_id = :vdcId";
		Map<String, Object> params = MapTools.simpleMap("vdcId", vdcId);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	public VmResourcePool queryByPoolVal(String poolVal, Long vdcId) throws Exception {
		String sql = "select * from resource_pool where is_deleted = 0 and pool_val = :poolVal and vdc_id = :vdcId";
		Map<String, Object> params = MapTools.simpleMap("poolVal", poolVal);
		params.put("vdcId", vdcId);
		List<VmResourcePool> list = this.list(VmResourcePool.class, sql, params);
		if (list.isEmpty()) {
			return null;
		} else {
			return list.get(0);
		}
	}

	public boolean removeByVendorId(Long vendorId, Long userId) throws Exception {
		String sql = "update resource_pool set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where vendor_id = :vendorId";
		Map<String, Object> params = new HashMap<>(8);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		params.put(Common.VENDORID, vendorId);
		return this.execute(sql, params) > 0;
	}

	public List<VmResourcePool> listByParentId(Long parentId) throws Exception {
		String sql = "select * from resource_pool where parent_id = :parentId and is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("parentId", parentId);
		return this.list(VmResourcePool.class, sql, params);
	}

	public boolean removeByOwner(Long owner, String ownerCategory, Long userId) throws Exception {
		String sql = "update resource_pool set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where owner = :owner and owner_category = :ownerCategory";
		Map<String, Object> params = new HashMap<>(8);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		params.put("owner", owner);
		params.put("ownerCategory", ownerCategory);
		return this.execute(sql, params) > 0;
	}

	public List<VmResourcePool> listByVendorId(Long vendorId) {
		String sql = "select * from resource_pool where parent_id != 0 and parent_id is not null and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		return this.list(VmResourcePool.class, sql, params);
	}
}
