
package com.bocloud.ims.repository.resource;

import com.bocloud.common.model.Pager;
import com.bocloud.common.model.Param;
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.AutoScaling;
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.List;
import java.util.Map;

/**
 * 弹性伸缩
 * 
 * @author ymh
 * @时间 2019年11月13日
 */
@Repository("autoScalingRepository")
public class AutoScalingRepository extends BasicGenericDao<AutoScaling, Long> {

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

	public List<AutoScaling> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = " select a.*,n.`name` network_name , c.`name` auto_scaling_config_name from auto_scaling a left join network n on a.vendor_id = n.vendor_id and a.network_uuid = n.network_uuid left join auto_scaling_config c on a.vendor_id = c.vendor_id and a.auto_scaling_config_id = c.id where  a.is_deleted = 0  ";
		sql = QueryBuilder.buildRaw(sql, new Pager(page, rows, params, sorter), "a");
		return this.list(AutoScaling.class, sql);
	}

	public int count(List<Param> params) throws Exception {
		String sql = "select count(1) from auto_scaling v where v.is_deleted = 0";
		sql = QueryBuilder.buildRaw(sql, params, null, "v");
		Map<String, Object> param = QueryBuilder.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

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

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

	public AutoScaling query(Long id) throws Exception {
		String sql = " select a.*,n.`name` network_name , c.`name` auto_scaling_config_name from auto_scaling a left join network n on a.vendor_id = n.vendor_id and a.network_uuid = n.network_uuid left join auto_scaling_config c on a.vendor_id = c.vendor_id and a.auto_scaling_config_id = c.id where  a.is_deleted = 0   and a.id = :id";
		Map<String, Object> params = MapTools.simpleMap(Common.ID, id);
		List<AutoScaling> list = this.list(AutoScaling.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	public List<AutoScaling> listByConfigId(Long configId, Long vendorId) throws Exception {
		String sql = " select a.* from auto_scaling a where  a.is_deleted = 0  and a.vendor_id = :vendorId and a.auto_scaling_config_id = :configId ";
		Map<String, Object> params = MapTools.simpleMap("configId", configId);
		params.put(Common.VENDORID, vendorId);
		List<AutoScaling> list = this.list(AutoScaling.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list;
	}

	public List<AutoScaling> listByStackId(Long stackId, Long vendorId) throws Exception {
		String sql = " select a.* from auto_scaling a where  a.is_deleted = 0  and a.vendor_id = :vendorId and a.stack_id = :stackId ";
		Map<String, Object> params = MapTools.simpleMap("stackId", stackId);
		params.put(Common.VENDORID, vendorId);
		List<AutoScaling> list = this.list(AutoScaling.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list;
	}

	public List<AutoScaling> listByVendorId(Long vendorId) {
		String sql = " select a.* from auto_scaling a  where  a.is_deleted = 0  and a.vendor_id = :vendorId ";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		List<AutoScaling> list = this.list(AutoScaling.class, sql, params);
		if (list.isEmpty()) {
			return null;
		}
		return list;
	}

	public boolean hasBuilding(Long vendorId) {
		String sql = "select count(1) from auto_scaling a where a.is_deleted = 0 and a.status = 'BUILDING' and a.vendor_id = :vendorId";
		Map<String, Object> paramMap = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.countQuery(sql, paramMap) > 0;
	}

}
