package com.bocloud.ims.booter.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.enums.ResourceCategory;
import com.bocloud.cmp.model.common.ExtensionParam;
import com.bocloud.cmp.model.common.OrderMessage;
import com.bocloud.ims.interfaces.apply.ResourceOperationService;

/**
 * 资源延期监听器
 * 
 * @author wangyu
 * @version 1.0
 * @since 2020年4月15日
 *
 */
@Component
public class ResourceExtensionListener {

	private static Logger logger = LoggerFactory.getLogger(ResourceExtensionListener.class);
	@Autowired
	private ResourceOperationService resourceOperationService;
	private static final String EXTENSION_QUEUE = "iaas.resource.extension.queue";

	@RabbitListener(queues = EXTENSION_QUEUE, admin = "amqpAdmin", autoStartup = "true")
	public void process(ExtensionParam param) throws Exception {
		try {
			if (null == param) {
				return;
			}
			logger.info("接收到资源延期消息：{}", JSONObject.toJSONString(param));
			String category = param.getCategory();
			resourceOperationService.process(ResourceCategory.valueOf(category),
					JSONObject.parseObject(JSONObject.toJSONString(param)), OrderMessage.Operation.Extension);
		} catch (Exception e) {
			logger.error("Get error message:{}", e.getMessage());
		}
	}

}
