
package com.tmerclub.cloud.user.listener;

import com.tmerclub.cloud.api.auth.bo.UserRegisterNotifyBO;
import com.tmerclub.cloud.api.auth.dto.UserRegisterDTO;
import com.tmerclub.cloud.api.auth.dto.UserRegisterExtensionDTO;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.user.model.User;
import com.tmerclub.cloud.user.model.UserExtension;
import com.tmerclub.cloud.user.service.UserService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 账户注册成功监听
 * @author cl
 * @date 2021-05-14 14:49:32
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.BATCH_USER_REGISTER_TOPIC, consumerGroup = RocketMqConstant.BATCH_USER_REGISTER_TOPIC)
public class UserRegisterConsumer implements RocketMQListener<UserRegisterNotifyBO> {

    @Autowired
    private UserService userService;


    private static final Logger LOG = LoggerFactory.getLogger(UserRegisterConsumer.class);

    /**
     * 开始批量保存用户信息
     * @param message 批量注册消息
     */
    @Override
    public void onMessage(UserRegisterNotifyBO message) {
        LOG.info("批量注册用户信息开始... message: {}", Json.toJsonString(message));
        List<UserRegisterDTO> userRegisterList = message.getUserRegisterList();
        List<UserRegisterExtensionDTO> registerExtensionDTOList = message.getUserRegisterExtensionDTOList();
        List<User> userList = BeanUtil.mapAsList(userRegisterList, User.class);
        List<UserExtension> userExtensionList = BeanUtil.mapAsList(registerExtensionDTOList, UserExtension.class);
        // 批量保存用户
        userService.batchUser(userList, userExtensionList);
    }
}
