package com.docus.server.report.listener;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.docus.core.util.Func;
import com.docus.infrastructure.web.api.CommonResult;
import com.docus.infrastructure.web.api.ResultCode;
import com.docus.server.report.config.ApplicationBusinessConfig;
import com.docus.server.report.dto.ReportDownDto;
import com.docus.server.report.dto.ReportDownPatientDto;
import com.docus.server.report.dto.ReportDownScanFileDto;
import com.docus.server.report.entity.AfReportRecord;
import com.docus.server.report.event.ReportDownEvent;
import com.docus.server.report.mapper.AfReportRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 上报下载监听
 *
 * @author wyb
 */
@Component
@Slf4j
public class ReportDownListener {
    @Resource
    private ApplicationBusinessConfig applicationBusinessConfig;

    @Resource
    private AfReportRecordMapper afReportRecordMapper;

    @EventListener
    @Async("threadPoolExecutor")
    public void reportDown(ReportDownEvent reportDownEvent) {
        // 根据任务id获取上报记录信息
        AfReportRecord afReportRecord = afReportRecordMapper.getReportRecordInfoByTaskId(reportDownEvent.getTaskId());

        // 组织基础信息数据
        ReportDownPatientDto reportDownPatientDto = new ReportDownPatientDto(afReportRecord);

        // 组织文件数据
        List<ReportDownScanFileDto> reportDownScanFileDtos = new ArrayList<>(5);
        ReportDownScanFileDto reportDownScanFileDto = new ReportDownScanFileDto(afReportRecord);
        reportDownScanFileDtos.add(reportDownScanFileDto);

        // 组织下载数据，基础信息和文件数据
        ReportDownDto reportDownDto = new ReportDownDto();
        reportDownDto.setAssortid(afReportRecord.getZdAssortId());
        reportDownDto.setCollectorid(afReportRecord.getSysflag());
        reportDownDto.setScanusercode("admin");
        reportDownDto.setScanusername("admin");
        reportDownDto.setPatient(reportDownPatientDto);
        reportDownDto.setScanfiles(reportDownScanFileDtos);

        // 下面是调用下载服务

        String requestParam = Func.toJson(reportDownDto);
        try {
            log.info("调用下载服务，地址：{} ，参数：{}",applicationBusinessConfig.getDownUrl(),requestParam);
            HttpRequest post = HttpUtil.createPost(applicationBusinessConfig.getDownUrl());
            post.timeout(5 * 1000);
            post.header("Content-Type", "application/json; charset=utf-8");
            post.body(requestParam);
            HttpResponse response = post.execute();
            String respBody = response.body();
            log.info("调用下载服务成功,响应参数：{}", respBody);
            CommonResult commonResult = Func.readJson(respBody, CommonResult.class);
            if (ResultCode.SUCCESS.getCode().equals(commonResult.getCode())) {
                // 下载返回了成功更新状态
                afReportRecordMapper.updateStateByTaskId(reportDownEvent.getTaskId());
            }
        } catch (Exception e) {
            log.error("调用下载服务失败", e);
        }
    }
}
