package com.bocloud.ims.repository.resource;

import com.bocloud.common.exception.BoCloudDBException;
import com.bocloud.common.utils.Common;
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.PoolGroupZone;

import org.apache.commons.lang3.StringUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

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

/**
 * 资源池 可用区关联表查询
 * 
 * @ClassName: PoolGroupZoneRepository.java
 * @Description:
 *
 * @author: ymh
 * @date: 2021年4月16日 下午5:27:15
 */
@Component("poolGroupZoneRepository")
public class PoolGroupZoneRepository extends BasicGenericDao<PoolGroupZone, Long> {

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

	public boolean remove(Long poolGroupId) throws BoCloudDBException {
		String sql = "delete from pool_group_zone where pool_group_id = :poolGroupId  ";
		Map<String, Object> params = MapTools.simpleMap("poolGroupId", poolGroupId);
		return this.execute(sql, params) > 0;
	}

	public List<PoolGroupZone> listByGroupId(Long vendorId, Long poolGroupId, String regionId, String zoneId)
			throws Exception {
		String sql = "select a.*, r.`name` region_name, z.`name` zone_name, z.`id` zone_real_id, z.disk_category from pool_group_zone a  "
				+ " left join region r on a.vendor_id = r.vendor_id and a.region_id = r.region_id "
				+ " left join zone z on a.vendor_id = z.vendor_id and a.zone_id = z.zone_id "
				+ " where a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		if (null != poolGroupId) {
			sql += " and a.pool_group_id = :poolGroupId ";
			params.put("poolGroupId", poolGroupId);
		}
		if (!StringUtils.isEmpty(regionId)) {
			sql += " and a.region_id = :regionId ";
			params.put("regionId", regionId);
		}
		if (!StringUtils.isEmpty(zoneId)) {
			sql += " and a.zone_id = :zoneId ";
			params.put("zoneId", zoneId);
		}
		return this.list(PoolGroupZone.class, sql, params);
	}
	
	public PoolGroupZone query(Long id) throws Exception {
        String sql = "select a.* from pool_group_zone a where a.id = :id";
        Map<String, Object> params = MapTools.simpleMap("id", id);
        List<PoolGroupZone> list = this.list(PoolGroupZone.class, sql, params);
        if(ListTool.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

	public boolean updateStatus(Long vendorId, Long poolGroupId, String regionId, String zoneId, Boolean status) {
		String sql = "update pool_group_zone set checked = :status where vendor_id = :vendorId and pool_group_id = :poolGroupId and region_id = :regionId and zone_id = :zoneId ";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("poolGroupId", poolGroupId);
		params.put("regionId", regionId);
		params.put("zoneId", zoneId);
		params.put("status", status);
		return this.execute(sql, params) > 0;
	}

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

}
