package com.simuwang.daq.components.report.parser.py;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.simuwang.base.common.enums.ReportType;
import com.simuwang.base.common.exception.ReportParseException;
import com.simuwang.base.config.DaqProperties;
import com.simuwang.base.mapper.FundInfoMapper;
import com.simuwang.base.pojo.dos.FundAndCompanyInfoDO;
import com.simuwang.base.pojo.dto.report.ParseResult;
import com.simuwang.base.pojo.dto.report.ReportData;
import com.simuwang.base.pojo.dto.report.ReportParseStatus;
import com.simuwang.base.pojo.dto.report.ReportParserParams;
import com.simuwang.daq.components.PythonReportConverter;
import com.simuwang.daq.components.report.parser.ReportParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;

/**
 * @author wangzaijun
 * @date 2024/9/29 16:46
 * @description python解析报告的抽象类
 */
public abstract class AbstractPyReportParser<T extends ReportData> implements ReportParser<T> {
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final DaqProperties properties;
    private final FundInfoMapper fundInfoMapper;

    public AbstractPyReportParser(DaqProperties properties, FundInfoMapper fundInfoMapper) {
        this.properties = properties;
        this.fundInfoMapper = fundInfoMapper;
    }

    @Override
    public T parse(ReportParserParams params) throws IOException, ReportParseException {
        Boolean enablePyParser = this.properties.getEnablePyParser();
        if (!enablePyParser) {
            this.logger.error("The python report parser is unavailable!");
            return null;
        }
        String pyBaseUrl = this.properties.getPyBaseUrl();
        ReportType reportType = this.getReportType();
        String registerNumber = params.getRegisterNumber();
        String api = "/api/v1/parse/amac_report";
        Map<String, Object> param = MapUtil.newHashMap(16);
        param.put("file_id", params.getFileId());
        param.put("file_path", params.getFilepath());
        param.put("register_number", registerNumber);
        param.put("file_type", reportType.getType());
        param.put("file_name", params.getFilename());
        if (StrUtil.isNotBlank(registerNumber)) {
            FundAndCompanyInfoDO info = this.fundInfoMapper.queryFundAndTrustByRegisterNumber(registerNumber);
            if (info != null) {
                param.put("fund_name", info.getFundName());
                param.put("trust_name", info.getCompanyName());
            }
        }
        String body = HttpUtil.post(pyBaseUrl + api, JSONUtil.toJsonStr(param));
        ParseResult<T> result = PythonReportConverter.convert(JSONUtil.parseObj(body), reportType);
        if (result.getStatus() == null) {
            throw new ReportParseException(ReportParseStatus.PARSE_FAIL, "资源文件不存在");
        }
        if (!Objects.equals(1, result.getStatus())) {
            this.logger.error("报告{} 解析失败：{}", params, result.getMsg());
            throw new ReportParseException(result.getStatus(), result.getMsg());
        }
        return result.getData();
    }

    protected abstract ReportType getReportType();
}
