package cn.ibizlab.core.extensions.util;

import cn.ibizlab.core.extensions.domain.EngineMQMsg;
import cn.ibizlab.core.extensions.service.DABuildExService;
import cn.ibizlab.core.extensions.service.RuleEngineExService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.consumer.listener.*;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * MQ订阅消息处理
 */
@Slf4j
@Component
public class MQConsumeMsgListenerProcessor implements MessageListenerOrderly {
    public static final String Type_Engine = "Engine";
    public static final String Type_Build = "Build";

    private Object lockObject =new Object();

    @Value("${rocketmq.producer.ruleEngineTopic:DSTMSG}")
    private String ruleEngineTopic;

    @Autowired
    @Lazy
    private RuleEngineExService ruleEngineExService;

    @Autowired
    @Lazy
    private DABuildExService daBuildExService;

    @Override
    public ConsumeOrderlyStatus consumeMessage(List<MessageExt> list, ConsumeOrderlyContext consumeOrderlyContext) {
        if (CollectionUtils.isEmpty(list)) {
            log.info("MQ接收消息为空，直接返回成功");
            return ConsumeOrderlyStatus.SUCCESS;
        }
        //System.out.println(System.currentTimeMillis());
        MessageExt messageExt = list.get(0);
        log.info("MQ接收到的消息为：" + messageExt.toString());
        try {
            String topic = messageExt.getTopic();
            String tags = messageExt.getTags();
            String body = new String(messageExt.getBody(), "utf-8");

            log.info("MQ消息topic={}, tags={}, 消息内容={}", topic, tags, body);

        } catch (Exception e) {
            log.error("获取MQ消息内容异常{}", e);
        }

        // 处理规则引擎构建消息
        if (ruleEngineTopic.equalsIgnoreCase(messageExt.getTopic())) {
            this.processRuleEngineData(messageExt);
        }
        return ConsumeOrderlyStatus.SUCCESS;
    }

    protected void processRuleEngineData(MessageExt messageExt){
        try {
            String body = new String(messageExt.getBody(), "utf-8");
            String tags = messageExt.getTags();

//            System.out.println("start sleep");
//            Thread.sleep(80000);
//            System.out.println("end sleep");
            EngineMQMsg engineMQMsg = JSON.parseObject(body, new TypeReference<EngineMQMsg>() {});
            if(Type_Engine.equalsIgnoreCase(tags)) {
                ruleEngineExService.processData(engineMQMsg);
            }else if(Type_Build.equalsIgnoreCase(tags)){
                daBuildExService.processData(engineMQMsg);
            }
        } catch (Exception e) {
            log.error("获取MQ消息内容异常{}",e);
        }
    }

}
