package com.bocloud.ims.repository.resource;

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

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;

/**
 * 云主机业务数据
 * 
 * @ClassName: CloudServerBusinessRepository.java
 * @Description:
 *
 * @author: ymh
 * @date: 2021年4月1日 上午10:58:01
 */
@Repository("cloudServerBusinessRepository")
public class CloudServerBusinessRepository extends BasicGenericDao<CloudServerBusiness, Long> {

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

	public void save(CloudServerBusiness serverBusiness) throws BoCloudDBException {
		super.save(serverBusiness);
	}

	public CloudServerBusiness getByVendorId(String instanceId, String sequenceId, Long vendorId) {
		String sql = "select a.* from cloud_server_business a where a.instance_id = :instanceId and a.sequence_id = :sequenceId and a.vendor_id = :vendorId and a.is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("instanceId", instanceId);
		params.put("sequenceId", sequenceId);
		params.put(Common.VENDORID, vendorId);
		List<CloudServerBusiness> list = this.list(CloudServerBusiness.class, sql, params);
		return list.isEmpty() ? null : list.get(0);
	}

	public List<CloudServerBusiness> getSequenceIdByVendorId(Long vendorId) {
		String sql = "select a.sequence_id from cloud_server_business a where a.is_deleted = 0 and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(CloudServerBusiness.class, sql, params);
	}

	public CloudServerBusiness getByServerId(Long serverId) {
		String sql = "select a.* from cloud_server_business a where a.server_id = :serverId and a.is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
		List<CloudServerBusiness> list = this.list(CloudServerBusiness.class, sql, params);
		return list.isEmpty() ? null : list.get(0);
	}

	public boolean removeByServerId(Long serverId) {
		String sql = "update cloud_server_business set is_deleted = true ,gmt_modify = :gmtModify where server_id = :serverId and is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("serverId", serverId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public boolean removeByVendorId(Long vendorId) {
		String sql = "update cloud_server_business set is_deleted = true ,gmt_modify = :gmtModify where vendor_id = :vendorId and is_deleted = 0";
		Map<String, Object> params = MapTools.simpleMap("vendorId", vendorId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public boolean removeByInsAndSeq(String instanceId, String sequenceId, Long vendorId) {
		String sql = "update cloud_server_business set is_deleted = true ,gmt_modify = :gmtModify where instance_id = :instanceId and sequence_id = :sequenceId and vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap("instanceId", instanceId);
		params.put("sequenceId", sequenceId);
		params.put("vendorId", vendorId);
		params.put("gmtModify", new Date());
		return this.execute(sql, params) > 0;
	}

	public List<CloudServerBusiness> list(Long vendorId) {
		String sql = "select a.*,c.gmt_remove ,c.gmt_plan_remove from cloud_server_business a "
				+ " left join resource_recycle c on a.instance_id = c.resource_id and c.resource_type='VM'  "
				+ " and c.is_deleted = 0 where a.is_deleted = 0 and a.vendor_id = :vendorId";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(CloudServerBusiness.class, sql, params);
	}

	public List<CloudServerBusiness> listDeleted(Long vendorId) {
		String sql = "select a.*,c.gmt_remove ,c.gmt_plan_remove from cloud_server_business a,"
				+ " resource_recycle c where a.instance_id = c.resource_id and c.resource_type='VM'"
				+ " and c.gmt_remove is not null and a.vendor_id = :vendorId order by a.id desc";
		Map<String, Object> params = MapTools.simpleMap(Common.VENDORID, vendorId);
		return this.list(CloudServerBusiness.class, sql, params);
	}

	public List<CloudServerBusiness> listByType(String type) {
		String sql = "select a.*,c.gmt_remove ,c.gmt_plan_remove from cloud_server_business a"
				+ " left join resource_recycle c on a.instance_id = c.resource_id and c.resource_type = 'VM'"
				+ " and c.is_deleted = 0,cloud_vendor v where a.is_deleted = 0 and a.vendor_id = v.id and v.type = :type";
		Map<String, Object> params = MapTools.simpleMap("type", type);
		return this.list(CloudServerBusiness.class, sql, params);
	}

	public List<CloudServerBusiness> listDeletedByType(String type) {
		String sql = "select a.*,c.gmt_remove ,c.gmt_plan_remove from cloud_server_business a,"
				+ " resource_recycle c,cloud_vendor v where a.instance_id = c.resource_id and c.resource_type='VM'"
				+ " and c.gmt_remove is not null and a.vendor_id = v.id and v.type = :type order by a.id desc";
		Map<String, Object> params = MapTools.simpleMap("type", type);
		return this.list(CloudServerBusiness.class, sql, params);
	}

}
