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

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.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<String> messages) {
        log.info("[Biz onMessage][线程编号:{} 消息数量：{}]", Thread.currentThread().getName()+Thread.currentThread().getId(), messages.size());
        List<BizLogEntity> collect = messages.stream().map(str -> {
            BizLogEntity entity = JSON.parseObject(str, BizLogEntity.class);
            entity.setCreateUserId(1L);
            entity.setCreateTime(new Date());
            return entity;
        }).collect(Collectors.toList());
        bizLogService.save(collect);
    }
}
