package com.docus.server.collect.report.ws.impl;

import com.docus.core.util.json.JSON;
import com.docus.log.annotation.TrackGroup;
import com.docus.log.context.TrackHelper;
import com.docus.server.archivefile.infrastructure.dto.ReportDTO;
import com.docus.server.collect.IConverter;
import com.docus.server.collect.IWsResult;
import com.docus.server.collect.report.service.IReportService;
import com.docus.server.collect.report.ws.IReportServer;
import com.docus.server.collect.web.process.VisitorProcessor;
import com.docus.server.enums.CollectTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author wyb
 * @date 2023年3月3日13:42:36
 * @see CollectTypeEnum 枚举
 * @see IConverter 通用转化器
 * @see IWsResult 通用返回结果
 * @see VisitorProcessor 通用处理器
 */
@Service
@Slf4j
public class ReportServerImpl implements IReportServer {
    private static final String CONVERTER = "reportConverter";
    private static final String WS_RESULT = "wsReportResultImpl";
    @Resource
    private IReportService reportService;

    @Override
    @TrackGroup(
        group = "WEBSERVICE_XML_SA_REPORT", retryKey = "report",
        beanNames = {CONVERTER, WS_RESULT},
        processor = VisitorProcessor.class)
    public String pushSAReport(String message) {
        report();
        return null;
    }

    @Override
    @TrackGroup(
        group = "WEBSERVICE_XML_ICU_REPORT", retryKey = "report",
        beanNames = {CONVERTER, WS_RESULT},
        processor = VisitorProcessor.class)
    public String pushICUReport(String message) {
        log.info("收到重症消息：{}", message);
        report();
        return null;
    }

    @Override
    @TrackGroup(
        group = "WEBSERVICE_XML_EXAMINATION_REPORT", retryKey = "report",
        beanNames = {CONVERTER, WS_RESULT},
        processor = VisitorProcessor.class)
    public String pushExaminationReport(String message) {
        log.info("收到检验报告消息：{}", message);
        report();
        return null;
    }

    @Override
    @TrackGroup(
        group = "WEBSERVICE_XML_INSERT_INSPECTION_REPORT", retryKey = "report",
        beanNames = {CONVERTER, WS_RESULT},
        processor = VisitorProcessor.class)
    public String pushAddInspectionReport(String message) {
        log.info("收到检查报告新增消息：{}", message);
        report();
        return null;
    }

    @Override
    @TrackGroup(
        group = "WEBSERVICE_XML_UPDATE_INSPECTION_REPORT", retryKey = "report",
        beanNames = {CONVERTER, WS_RESULT},
        processor = VisitorProcessor.class)
    public String pushUpdateInspectionReport(String message) {
        log.info("收到检查报告更新消息：{}", message);
        report();
        return null;
    }

    private void report() {
        reportService.report(JSON.fromJSON((String) TrackHelper.getValue("jsonStr"), ReportDTO.class));
    }
}
