package com.qmth.themis.task.listener;

import com.qmth.themis.business.constant.SpringContextHolder;
import com.qmth.themis.business.constant.SystemConstant;
import com.qmth.themis.business.dto.MqDto;
import com.qmth.themis.business.util.JacksonUtil;
import com.qmth.themis.business.util.RedisUtil;
import com.qmth.themis.common.contanst.Constants;
import com.qmth.themis.mq.templete.Orderly;
import com.qmth.themis.task.listener.service.MqTaskLogicService;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.common.message.MessageExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Description: mq quartz顺序消费监听
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2020/7/31
 */
@Service
public class QuartzOrderlyImpl implements Orderly {
    private final static Logger log = LoggerFactory.getLogger(QuartzOrderlyImpl.class);

    @Override
    public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext consumeOrderContext) {
        RedisUtil redisUtil = SpringContextHolder.getBean(RedisUtil.class);
        MqTaskLogicService mqTaskLogicService = SpringContextHolder.getBean(MqTaskLogicService.class);
        MqDto mqDto = null;
        try {
            long threadId = Thread.currentThread().getId();
            String threadName = Thread.currentThread().getName();
            for (MessageExt messageExt : msgs) {
                log.info(":{}-:{} quartz Consumer重试次数:{}", threadId, threadName, messageExt.getReconsumeTimes());
                mqDto = JacksonUtil.readJson(new String(messageExt.getBody(), Constants.CHARSET), MqDto.class);
                log.info(":{}-:{} quartz Consumer接收到的消息:{}", threadId, threadName, JacksonUtil.parseJson(mqDto));
                int reconsumeTime = messageExt.getReconsumeTimes();
                if (reconsumeTime >= SystemConstant.MAXRECONSUMETIMES) {
                    mqTaskLogicService.execMqMaxReconsumeTime(mqDto, SystemConstant.MQ_TOPIC_BUFFER_LIST);
                } else {
                    if (Objects.nonNull(mqDto.getAck()) && mqDto.getAck().intValue() != SystemConstant.STANDARD_ACK_TYPE && Objects.nonNull(redisUtil.get(SystemConstant.MQ_TOPIC_BUFFER_LIST, mqDto.getId())) && redisUtil.lock(SystemConstant.REDIS_LOCK_MQ_PREFIX + mqDto.getId(), SystemConstant.REDIS_LOCK_MQ_TIME_OUT)) {
                        try {
                            mqTaskLogicService.execMqQuartzLogic(mqDto, SystemConstant.MQ_TOPIC_BUFFER_LIST);
//                            return ConsumeOrderlyStatus.SUCCESS;
                        } finally {
                            if (Objects.nonNull(mqDto)) {
                                redisUtil.releaseLock(SystemConstant.REDIS_LOCK_MQ_PREFIX + mqDto.getId());
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("mq quartz顺序，消息消费出错", e);
            return ConsumeOrderlyStatus.SUSPEND_CURRENT_QUEUE_A_MOMENT;//重试
        }
        return ConsumeOrderlyStatus.SUCCESS;//成功
    }
}