package com.docus.server.collect.user.mq.impl;

import com.docus.core.util.json.JSON;
import com.docus.log.annotation.TrackGroup;
import com.docus.log.context.TrackHelper;
import com.docus.server.collect.user.mq.IUserMqCollectService;
import com.docus.server.collect.web.process.VisitorProcessor;
import com.docus.server.collect.web.service.CollectService;
import com.docus.server.sys.common.pojo.dto.UserDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("userRocketMqCollectServiceImpl")
@Slf4j
public class UserRocketMqCollectServiceImpl implements IUserMqCollectService {
    @Resource
    private CollectService collectService;

    /**
     * 订阅队列（注册人员）
     *
     * @param message 人员信息
     */
    @Override
    @TrackGroup(
        group = "ROCKET_MQ_PASSIVE_INSERT_USER", retryKey = "user",
        beanNames = {"mqUserConverter", "mqUserResultImpl"},
        processor = VisitorProcessor.class)
    public void addUser(String message) {
        log.info("人员注册 队列接收信息：{}", message);
        collectService.insertOrUpdateUser(getValue());
    }

    /**
     * 订阅队列（变更人员）
     *
     * @param message 人员信息
     */
    @Override
    @TrackGroup(
        group = "ROCKET_MQ_PASSIVE_UPDATE_USER", retryKey = "user",
        beanNames = {"mqUserConverter", "mqUserResultImpl"},
        processor = VisitorProcessor.class)
    public void updateUser(String message) {
        log.info("人员变更 队列接收信息：{}", message);
        collectService.insertOrUpdateUser(getValue());
    }

    public UserDTO getValue() {
        return JSON.fromJSON((String) TrackHelper.getValue("jsonStr"), UserDTO.class);
    }
}
