package com.docus.server.collect.basic.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.basic.mq.IBasicMqCollectService;
import com.docus.server.collect.web.process.VisitorProcessor;
import com.docus.server.collect.web.service.CollectService;
import com.docus.server.record.common.pojo.dto.TBasicDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author linruifeng
 */
@Service("basicRabbitMqCollectServiceImpl")
@Slf4j
public class BasicRabbitMqCollectServiceImpl implements IBasicMqCollectService {
    @Resource
    private CollectService collectService;

    /**
     * 订阅队列（基础数据）
     *
     * @param message 基础数据
     */
    @Override
    @TrackGroup(
        group = "RABBIT_MQ_PASSIVE_INSERT_BASIC", retryKey = "basic",
        beanNames = {"mqBasicConverter", "mqBasicResultImpl"},
        processor = VisitorProcessor.class)
    public void addTBasic(String message) {
        log.info("注册基础数据 队列接收信息：{}", message);
        collectService.insertOrUpdateBasic(getValue());
    }

    /**
     * 订阅队列（变更基础数据）
     *
     * @param message 基础数据
     */
    @Override
    @TrackGroup(
        group = "RABBIT_MQ_PASSIVE_UPDATE_BASIC", retryKey = "basic",
        beanNames = {"mqBasicConverter", "mqBasicResultImpl"},
        processor = VisitorProcessor.class)
    public void updateTBasic(String message) {
        log.info("变更基础数据 队列接收信息：{}", message);
        collectService.insertOrUpdateBasic(getValue());
    }

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