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

import javax.annotation.Resource;

@Service("deptRabbitMqCollectServiceImpl")
@Slf4j
public class DeptRabbitMqCollectServiceImpl implements IDeptMqCollectService {
    @Resource
    private CollectService collectService;

    /**
     * 订阅队列（注册科室）
     *
     * @param message 科室信息
     */
    @Override
    @TrackGroup(
        group = "RABBIT_MQ_PASSIVE_INSERT_DEPT", retryKey = "dept",
        beanNames = {"mqDeptConverter", "mqDeptResultImpl"},
        processor = VisitorProcessor.class)
    public void addDept(String message) {
        log.info("注册科室 队列接收信息：{}", message);
        collectService.insertOrUpdateDept(getValue());
    }


    /**
     * 订阅队列（变更科室）
     *
     * @param message 科室信息
     */
    @Override
    @TrackGroup(
        group = "RABBIT_MQ_PASSIVE_UPDATE_DEPT", retryKey = "dept",
        beanNames = {"mqDeptConverter", "mqDeptResultImpl"},
        processor = VisitorProcessor.class)
    public void updateDept(String message) {
        log.info("变更科室 队列接收信息：{}", message);
        collectService.insertOrUpdateDept(getValue());
    }

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