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.CancelationParam;
import com.bocloud.cmp.model.common.OrderMessage;
import com.bocloud.ims.interfaces.apply.ResourceOperationService;

/**
 * 资源退订监听器
 *
 * @author wangyu
 * @version 1.0
 * @since 2018年1月8日
 */
@Component
public class ResourceUnsubscribeListener {

	private static Logger logger = LoggerFactory.getLogger(ResourceUnsubscribeListener.class);
	@Autowired
	private ResourceOperationService resourceOperationService;
	private static final String DELETION_QUEUE = "iaas.resource.deletion.queue";

	@RabbitListener(queues = DELETION_QUEUE, admin = "amqpAdmin", autoStartup = "true")
	public void process(CancelationParam 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.Cancelation);
		} catch (Exception e) {
			logger.error("Get error message:{}", e.getMessage());
		}
	}

}
