package com.prolog.upcloud.base.wh.operate.mq;

import java.util.Date;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.core.type.TypeReference;
import com.prolog.cs.data.exchange.client.core.EtlMessage;
import com.prolog.framework.log.model.Log;
import com.prolog.framework.log.service.ILogService;
import com.prolog.framework.utils.JsonUtils;
import com.prolog.upcloud.base.wh.operate.service.AdjustService;
import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;

/**
 * erp损溢审核监听类
 * 
 * @author Xiong
 *
 */
@Component
@Log4j2
public class AdjustAuditListener {

	@Autowired(required = false)
	private ILogService logService;

	@Autowired(required = false)
	private AdjustService adjustService;

	@RabbitListener(queuesToDeclare = @Queue("prolog_adjust_audit_queuen")) // 监听的队列名称 TestDirectQueue
	public void process(String data, Channel channel, Message message) throws Exception {
		log.info("rec message: " + data);
		try {
			sendUserLog(true, data, "接收data-exchange数据", "", "jobMessage");
			EtlMessage etlMessage = JsonUtils.parseJsonString(data, new TypeReference<EtlMessage>() {
//				@Override
//				public Type getType() {
//					return super.getType();
//				}
			});
			String ids = etlMessage.getId();
			adjustService.procesAdjustAudit(ids);
		} catch (Exception e) {
			sendUserLog(false, data, "接收data-exchange数据失败", e.getMessage(), "jobMessage");
			log.error("任务处理失败(data-exchange)->erp下发的盘点审核.", e);
		} finally {
			// 消息手动确认
			channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		}
//		long deliveryTag = message.getMessageProperties().getDeliveryTag();
//		try {
//			String msgbody = new String(message.getBody());
//			// 1.接收转换消息
//			System.out.println("DirectReceiver消费者 1 收到消息  : " + msgbody + " 编号： " + deliveryTag);
//			// 2. 处理业务逻辑
//			System.out.println("处理业务逻辑...");
//			// 模拟出现错误
//			System.out.println(500 / Double.valueOf(msgbody));
//			// 3. 手动签收
//			channel.basicAck(deliveryTag, true);
//		} catch (Exception e) {
//			try {
//				Thread.sleep(3000);
//			} catch (InterruptedException interruptedException) {
//				interruptedException.printStackTrace();
//			}
//			// e.printStackTrace();
//
//			// 4.拒绝签收
//			/*
//			 * 第三个参数：requeue：重回队列。如果设置为true，则消息重新回到queue，broker会重新发送该消息给消费端
//			 */
//			channel.basicNack(deliveryTag, true, true);
//			// channel.basicReject(deliveryTag,true);
//		}
	}

	private void sendUserLog(boolean success, String param, String desc, String error, String method) {
		if (logService == null) {
			return;
		}
		Log log = new Log();
		log.setCode(System.currentTimeMillis() + "");
		log.setUserName("system");
		String level = success ? Log.LEVEL_INFO : Log.LEVEL_ERROR;
		log.setLevel(level);
		log.setDescription(desc);
		log.setErrorMessage(error);
		log.setCreateTime(new Date());
		log.setClassName("MessageListener");
		log.setEnterprise("default");
		log.setMethod(method);
		log.setParams(param);
		log.setSystemId("data-exchange");
		logService.writeLog(log);
	}
}
