package com.mortals.xhx.base.framework.listener;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.xhx.common.key.QueueKey;
import com.mortals.xhx.module.access.model.AccessLogEntity;
import com.mortals.xhx.module.access.service.AccessLogService;
import com.mortals.xhx.module.biz.model.BizLogEntity;
import com.mortals.xhx.module.biz.service.BizLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * rabbitmq 业务日志消息消费类
 *
 * @author: zxfei
 * @date: 2022/8/17 20:45
 * @description:
 **/
@Slf4j
@Component
@RabbitListener(queues = QueueKey.BIZ_LOG_QUEUE,
        containerFactory = "consumerBatchContainerFactory")
public class BizMessageConsumerListener {

    @Autowired
    private BizLogService bizLogService;

    @RabbitHandler
    public void onMessage(List<BizLogEntity> messages) {
        log.info("[Biz onMessage][消息数量：{}]",  messages.size());
/*        List<BizLogEntity> collect = messages.stream().map(entity -> {
            try {
           // BizLogEntity entity = JSON.parseObject(new String(str, Charset.defaultCharset()), BizLogEntity.class);
            entity.setLogTime(entity.getLogTime()==null?new Date():entity.getLogTime());
            entity.setId(IdUtil.getSnowflake(0, 1).nextId());
            entity.setCreateUserId(1L);
            entity.setCreateTime(new Date());
            Thread.sleep(10);
            } catch (Exception e) {
                log.info("thread sleep ", e);
            }
            return entity;
        }).filter(f -> f != null).collect(Collectors.toList());
        bizLogService.save(collect);*/
    }

}
